The Basics of Numpy
2016-02-02 14:54
555 查看
在python语言中,
比较重要的一些
更多内容请阅读:https://docs.scipy.org/doc/numpy-dev/user/quickstart.html
Tensorflow中的
tensor返回的是
numpy ndarray对象。
Numpy的主要对象是齐次多维数组,即一个元素表(通常是数字),所有的元素具有相同类型,可以通过有序整数列表元组
tuple访问其元素。In
Numpy, dimensions are called axes. The number of axes is rank.
Numpy的数组类为
ndarray,它还有一个名气甚大的别名
array。需要注意的是:
numpy.array与python标准库中的
array.array并不完全相同,后者仅仅处理一维数组而且提供的函数功能较少。
比较重要的一些
ndarray数组的属性:
ndarray.ndim: the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank.
ndarray.shape:the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns, shape will be
(n,m). The length of the shape tuple is therefore the rank, or number of dimensions, ndim.
ndarray.size:the total number of elements of the array. This is equal to the product of the elements of shape.
ndarray.dtype:an object describing the type of the elements in the array. One can create or specify dtype’s using standard Python types. Additionally NumPy provides types of its own.
numpy.int32,
numpy.int16, and
numpy.float64are some examples.
ndarray.itemsize:the size in bytes of each element of the array. For example, an array of elements of type float64 has itemsize 8 (=64/8), while one of type complex32 has itemsize 4 (=32/8). It is equivalent to ndarray.dtype.itemsize.
ndarray.data:the buffer containing the actual elements of the array. Normally, we won’t need to use this attribute because we will access the elements in an array using indexing facilities.
An Example
import numpy as np a = np.arange(15).reshape(3, 5) print a print a.ndim print a.shape print a.size print a.dtype print a.itemsize # print [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] 2 (3, 5) 15 int64 8
Array Creation:
>>> a = np.array(1,2,3,4) # WRONG >>> a = np.array([1,2,3,4]) # RIGHT >>> b = np.array([(1.5,2,3), (4,5,6)]) >>> b array([[ 1.5, 2. , 3. ], [ 4. , 5. , 6. ]]) >>> c = np.array( [ [1,2], [3,4] ], dtype=complex ) >>> c array([[ 1.+0.j, 2.+0.j], [ 3.+0.j, 4.+0.j]]) >>> np.zeros( (3,4) ) array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]]) >>> np.ones( (2,3,4), dtype=np.int16 ) # dtype can also be specified array([[[ 1, 1, 1, 1], [ 1, 1, 1, 1], [ 1, 1, 1, 1]], [[ 1, 1, 1, 1], [ 1, 1, 1, 1], [ 1, 1, 1, 1]]], dtype=int16) >>> np.empty( (2,3) ) # uninitialized, output may vary array([[ 3.73603959e-262, 6.02658058e-154, 6.55490914e-260], [ 5.30498948e-313, 3.14673309e-307, 1.00000000e+000]])
Basic Operations
>>> a = np.array( [20,30,40,50] ) >>> b = np.arange( 4 ) >>> b array([0, 1, 2, 3]) >>> c = a-b >>> c array([20, 29, 38, 47]) >>> b**2 array([0, 1, 4, 9]) >>> 10*np.sin(a) array([ 9.12945251, -9.88031624, 7.4511316 , -2.62374854]) >>> a<35 array([ True, True, False, False], dtype=bool)
>>> A = np.array( [[1,1], ... [0,1]] ) >>> B = np.array( [[2,0], ... [3,4]] ) >>> A*B # elementwise product array([[2, 0], [0, 4]]) >>> A.dot(B) # matrix product array([[5, 4], [3, 4]]) >>> np.dot(A, B) # another matrix product array([[5, 4], [3, 4]])
>>> a = np.ones((2,3), dtype=int) >>> b = np.random.random((2,3)) >>> a *= 3 >>> a array([[3, 3, 3], [3, 3, 3]]) >>> b += a >>> b array([[ 3.417022 , 3.72032449, 3.00011437], [ 3.30233257, 3.14675589, 3.09233859]]) >>> a += b # b is not automatically converted to integer type # Traceback (most recent call last): # ... # TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('int64') with casting rule 'same_kind'
更多内容请阅读:https://docs.scipy.org/doc/numpy-dev/user/quickstart.html
相关文章推荐
- The Basics of Numpy
- python环境
- Python 第十一章 常用第三方模块
- Python docs
- Python docs
- python3 黑板客爬虫闯关游戏(四)
- Python Scrapy抓取数据
- Python 10.9 urllib
- 你所不知道的python特点及语句
- python基础知识学习:列表和元组
- json2xml python script
- python中文编码问题
- Python 出现的UnicodeDecodeError的几种情况记录及解决办法
- Python 反弹shell后门用51CTO Blog绑定
- ex26 learning python the hard way新连接
- Python安装MySQL模块
- Python渗透测试工具合集
- Python 10.8 HTMLParser
- python 4000 中beautifulsoup基础教程
- python------列表的使用