The Basics
NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy dimensions are called axes. The number of axes is rank.
Numpy的主要对象是同类的多维数组。Numpy是一个具有相同类型数值的表,表的内容可以通过一个tuple来索引。Numpy中的维度称为称为axes,axes的数量称为rank
Numpy’s array class is called
ndarray
. It is also known by the alias array. Note thatnumpy.array
is not the same as the Standard Python Library classarray.array
, which only handles one-dimensional arrays and offers less functionality. The more important attributes of anndarray
object are:
Numpy的数组类称为ndarray
,也称作别名数组。numpy.array
与Python标准库里的array.array
类并不一样,标准库里的数组只能是一维的而且功能很少。ndarray
对象的重要属性展示如下:
-
ndarray.ndim
返回一个number,维度,axes数,rank值
the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank. -
ndarray.shape
返回一个tuple,表示形状,如(2,3)表示2x3,(3,3,3)表示3x3x3
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
返回一个number,表示ndarray
中所有元素的个数,等价于shape中各个元素的乘积
the total number of elements of the array. This is equal to the product of the elements of shape. -
ndarray.dtype
返回一个dtype对象,表示ndarray中元素的类型
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.float64 are some examples. -
ndarray.itemsize
the size in bytes of each element of the array. For example, an array of elements of type float64 hasitemsize
8 (=64/8), while one of type complex32 has itemsize 4 (=32/8). It is equivalent tondarray.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.
Array Creation
有几种方式可以产生数组,可以使用array
方法通过常规的Python list或者tuple来生成数组
-
ndarray.array(seq[, dtype=''])
参数必须为sequence, 这个seq可以是单一的seq也可以是seq的seq的seq...,对应着产生几维的array。也可以同时指定元素类型
>>> b = np.array([1.2, 3.5, 5.1]) #seq
>>> b.dtypedtype('float64')
>>> b = np.array([(1.5,2,3), (4,5,6)]) #seq of seq
>>> 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]])
- 有时候不知道数组的元素,却知道数组的shape,这时可以通过以下三种方法生产数组
-
zeros(tuple, dtype)
: np.zeros( (3,4) ),3x4全零数组 -
ones(tuple, dtype)
:np.ones( (2,3,4), dtype=np.int16 ),2x3x4全1数组 -
empty(tuple)
:np.empty( (2,3) ),随机数组
-
arange(init,stop,step)
类似Python中标准的range
函数,step为间隔长度 -
linspace(init,stop,segment)
segment为array的长度
Printing Arrays
print()
函数这样展示数组
- 最后一个轴从左往右打印,
- 倒数第二个轴从上往下打印,
- 其余的也是从上往下打印,只是规模变大了。
如果数组太大,打印会省略一部分
>>> print(np.arange(10000))
[ 0 1 2 ..., 9997 9998 9999]
Basic Operations
- 算术操作 (Arithmetic operators on arrays apply elementwise.)
-
+,-,*,/,
四则运算 -
**
乘方运算 -
>,<, ==
逻辑运算,返回布尔数组
这几种操作都是elementwise operation,都是针对数组元素的操作。
- matrix product
A.dot(B)
np.dot(A, B)
- 求和与极值,可以通过axis选择数组的轴
A.sum(axis)
A.min(axis)
A.max(axis)
>>> b = np.arange(12).reshape(3,4)
>>> b
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
>>>
>>> b.sum(axis=0) # sum of each column
array([12, 15, 18, 21])
>>>
>>> b.min(axis=1) # min of each row
array([0, 4, 8])
>>>
>>> b.cumsum(axis=1) # cumulative sum along each row
array([[ 0, 1, 3, 6], [ 4, 9, 15, 22], [ 8, 17, 27, 38]])
Universal Functions¶
NumPy provides familiar mathematical functions such as
sin
,cos
, andexp
. In NumPy, these are called “universal functions” (ufunc). Within NumPy, these functions operate elementwise on an array, producing an array as output.
Numpy提供了像sin,cos,exp这样类似的数学函数。在Numpy,它们被称作全局函数。这些函数操作数组的所有元素,产生一个输出数组。
>>> B = np.arange(3)
>>> Barray([0, 1, 2])
>>> np.exp(B)
array([ 1. , 2.71828183, 7.3890561 ])
>>> np.sqrt(B)
array([ 0. , 1. , 1.41421356])
>>> C = np.array([2., -1., 4.])
>>> np.add(B, C)
array([ 2., 0., 6.])
Indexing, Slicing and Iterating 索引,切片和迭代
一维数组的索引,切片和迭代就像python中常用的sequence一样。
多维数组每个轴有一个索引, 这些下标用逗号分割的tuple表示,表示方法与matlab相似。省略的下标表示全部索引
当数组的维度比较大时,可以使用
...
来省略索引。对于rank=5
的数组:
-
x[1,2,...]
is equivalent tox[1,2,:,:,:]
-
x[...,3]
tox[:,:,:,:,3]
and -
x[4,...,5,:]
tox[4,:,:,5,:]
-
迭代。多维数组的第一个轴作为迭代轴
for row in b: print(row)
打印的是b的第一个轴即行。可以用flat
属性把多维数组的元素全部展开for element in b.flat: print(element)
,这样能打印b的所有元素。
Shape Manipulation
- 改变数组的形状
>>> a = np.floor(10*np.random.random((3,4)))
>>> a
array([[ 2., 8., 0., 6.],
[ 4., 5., 1., 1.],
[ 8., 9., 3., 6.]])
>>> a.shape
(3, 4)
>>> a.ravel() # flatten the array
array([ 2., 8., 0., 6., 4., 5., 1., 1., 8., 9., 3., 6.])
>>> a.shape = (6, 2)
>>> a.T
array([[ 2., 0., 4., 1., 8., 3.],
[ 8., 6., 5., 1., 9., 6.]])
- 把不同的数组堆到一起
Random sampling (numpy.random)
1
1
11
1
1
1
1
1
11
1
1
1
11