参照numpy Quickstart tutorial
1.numpy的array
1.1 存放在array中必须放在一个大列表中
>>> import numpy as np
>>> x=np.array([[1,2,3],[4,5,6]])
>>> y=np.array([1,2,3])
>>> y=np.array([1,2,3],[4,5,6])#这种情况就不行
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
y=np.array([1,2,3],[4,5,6])
TypeError: data type not understood
1.2
- ndarray.ndim
数组轴的个数,在python的世界中,轴的个数被称作秩,例如一个n行m列的矩阵的维数是2,而不是n。
- ndarray.shape
数组的维度。这是一个指示数组在每个维度上大小的整数元组。例如一个n排m列的矩阵,它的shape属性将是(2,3),这个元组的长度显然是秩,即维度或者ndim属性
>>> y.shape
(3,)
>>> y.ndim
1
>>> x.shape
(2, 3)
>>> x.ndim
2
>>> c=array([[[1,2],[3,4]],[[5,6],[7,8]]])
>>> c.ndim
3
>>> c.shape
(2, 2, 2)
2.在用numpy的array 可以用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]])
3.矩阵乘法
dot()函数
>>> 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]])
4.索引,切片和迭代
索引
4.1 一维array的索引和切片
>>> a = np.arange(10)**3
>>> a
array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729])
>>> a[2]
8
>>> a[2:5]
array([ 8, 27, 64])
>>> a[:6:2] = -1000 # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000
>>> a
array([-1000, 1, -1000, 27, -1000, 125, 216, 343, 512, 729])
>>> a[ : :-1] # reversed a
array([ 729, 512, 343, 216, 125, -1000, 27, -1000, 1, -1000])
>>> for i in a:
... print(i**(1/3.))
...
nan
1.0
nan
3.0
nan
5.0
6.0
7.0
8.0
9.0
4.2 多维的array每个轴有一个索引。每个轴的索引用逗号分开。
>>> def f(x,y):
... return 10*x+y
...
>>> b = fromfunction(f,(5,4),dtype=int)
>>> b
array([[ 0, 1, 2, 3],
[10, 11, 12, 13],
[20, 21, 22, 23],
[30, 31, 32, 33],
[40, 41, 42, 43]])
>>> b[2,3]
23
>>> b[0:5, 1] # each row in the second column of b
array([ 1, 11, 21, 31, 41])
>>> b[ : ,1] # equivalent to the previous example
array([ 1, 11, 21, 31, 41])
>>> b[1:3, : ] # each column in the second and third row of b
array([[10, 11, 12, 13],
[20, 21, 22, 23]])
4.3 当少于轴数的索引被提供时,确失的索引被认为是整个切片:
>>> b[-1] # the last row. Equivalent to b[-1,:]
array([40, 41, 42, 43])
点 (…)代表许多产生一个完整的索引元组必要的分号。如果x是秩为5的数组(即它有5个轴)
>>> c = np.array( [[[ 0, 1, 2], # a 3D array (two stacked 2D arrays)
... [ 10, 12, 13]],
... [[100,101,102],
... [110,112,113]]])
>>> c.shape
(2, 2, 3)
>>> c[1,...] # same as c[1,:,:] or c[1]
array([[100, 101, 102],
[110, 112, 113]])
>>> c[...,2] # same as c[:,:,2]
array([[ 2, 13],
[102, 113]])
4.4 迭代 多维数组是就第一个轴而言的:
>>> for row in b:
... print(row)
...
[0 1 2 3]
[10 11 12 13]
[20 21 22 23]
[30 31 32 33]
[40 41 42 43]
4.5 对每个元素操作用flat
>>> for element in b.flat:
... print(element)
...
4.6高级索引
用索引对来找出元素
>>> d = array([[1, 2], [3, 4], [5, 6]])
>>> d[[0,1,2],[0,1,0]] #[0,1,2]表示行索引,[0,1,0]表示列索引
array([1, 4, 5])
>>> d[[0,0],[1,1]]
array([2, 2])
又如:
>>> x = array([[ 0, 1, 2],
... [ 3, 4, 5],
... [ 6, 7, 8],
... [ 9, 10, 11]])
>>> rows = np.array([[0, 0],
... [3, 3]], dtype=np.intp)
>>> columns = np.array([[0, 2],
... [0, 2]], dtype=np.intp)
>>> x[rows, columns]
array([[ 0, 2],
[ 9, 11]])
然而,由于上面的索引数组只是重复,可以使用broadcast(比较操作,比如行[:np.newaxis]+列)
>>> rows = np.array([0, 3], dtype=np.intp) #表示行数对
>>> columns = np.array([0, 2], dtype=np.intp)#表示列数对
>>> rows[:, np.newaxis]
array([[0],
[3]])
>>> x[rows[:, np.newaxis], columns]
array([[ 0, 2],
[ 9, 11]])
newaxis可以用来增加数组的轴数