import numpy as npy(误)
import numpy as np
菜鸟教程上对于NumPy的教程: NumPy
官方的Reference:NumPy Reference
这里仅整理部分使用到的函数。
Numpy 1 numpy.array(object , dtype = None , copy = True , order = None , subok = False , ndmin = 0 )
numpy.zeros 1 2 3 4 5 6 7 8 9 10 11 12 13 14 numpy.zeros(shape, dtype = float , order = 'C' ) shape 数组形状 dtype 数据类型,可选 order 'C' 用于 C 的行数组,或者 'F' 用于 FORTRAN 的列数组 x = np.zeros(5 ) print (x)y = np.zeros((5 ,), dtype = np.int ) print (y)z = np.zeros((2 ,2 ), dtype = [('x' , 'i4' ), ('y' , 'i4' )]) print (z)[0. 0. 0. 0. 0. ] [0 0 0 0 0 ] [[(0 , 0 ) (0 , 0 )] [(0 , 0 ) (0 , 0 )]]
numpy.zeros_like Return an array of zeros with the same shape and type as a given array.
1 2 3 4 5 6 7 8 numpy.zeros_like(a, dtype=None , order='K' , subok=True , shape=None ) x = np.arange(6 ) x = x.reshape((2 , 3 )) array([[0 , 1 , 2 ], [3 , 4 , 5 ]]) np.zeros_like(x) array([[0 , 0 , 0 ], [0 , 0 , 0 ]])
numpy.arange 1 2 3 4 5 6 7 8 9 np.arange(start, stop, step, dtype) start 起始值,默认为0 stop 终止值(不包含) step 步长,默认为1 dtype 返回ndarray的数据类型,如果没有提供,则会使用输入数据的类型。 x = np.arange(5 ) [0 1 2 3 4 ] x = np.arange(10 ,20 ,2 ) [10 12 14 16 18 ]
numpy.reshape 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 numpy.reshape(arr, newshape, order='C' ) arr:要修改形状的数组 newshape:整数或者整数数组,新的形状应当兼容原有形状 order:'C' -- 按行,'F' -- 按列,'A' -- 原顺序,'k' -- 元素在内存中的出现顺序。 a = np.arange(8 ) b = a.reshape(4 ,2 ) print (a)print (b)[0 1 2 3 4 5 6 7 ] [[0 1 ] [2 3 ] [4 5 ] [6 7 ]]
numpy.append 输入数组的维度必须匹配否则将生成ValueError。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 numpy.append(arr, values, axis=None ) arr:输入数组 values:要向arr添加的值,需要和arr形状相同(除了要添加的轴) axis:默认为 None 。 当axis无定义时,是横向加成,返回总是为一维数组!当axis有定义的时候,分别为0 和1 的时候。 当axis有定义的时候,分别为0 和1 的时候(列数要相同)。 当axis为1 时,数组是加在右边(行数要相同)。 a = np.array([[1 ,2 ,3 ],[4 ,5 ,6 ]]) print (a)print (np.append(a, [7 ,8 ,9 ]))print (np.append(a, [[7 ,8 ,9 ]],axis = 0 ))print (np.append(a, [[5 ,5 ,5 ],[7 ,8 ,9 ]],axis = 1 ))[[1 2 3 ] [4 5 6 ]] [1 2 3 4 5 6 7 8 9 ] [[1 2 3 ] [4 5 6 ] [7 8 9 ]] [[1 2 3 5 5 5 ] [4 5 6 7 8 9 ]]
numpy.power 1 2 3 4 5 6 numpy.power(x1, x2, /, out=None , *, where=True , casting='same_kind' , order='K' , dtype=None , subok=True [, signature, extobj]) = <ufunc 'power' >¶ x1 = np.arange(6 ) x2 = np.array([[1 , 2 , 3 , 3 , 2 , 1 ], [1 , 2 , 3 , 3 , 2 , 1 ]]) np.power(x1, x2) array([[ 0 , 1 , 8 , 27 , 16 , 5 ], [ 0 , 1 , 8 , 27 , 16 , 5 ]])
numpy.sqrt 1 2 3 numpy.sqrt(x, /, out=None , *, where=True , casting='same_kind' , order='K' , dtype=None , subok=True [, signature, extobj]) = <ufunc 'sqrt' > np.sqrt([4 , -1 , np.inf]) array([ 2. , nan, inf])
numpy.dot 两个数组的点积,即元素对应相乘.
对于两个一维的数组,计算的是这两个数组对应下标元素的乘积和(数学上称之为内积);
对于二维数组,计算的是两个数组的矩阵乘积;
对于多维数组,它的通用计算公式如下,即结果数组中的每个元素都是:数组a的最后一维上的所有元素与数组b的倒数第二位上的所有元素的乘积和: dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m]) 。
1 2 3 4 5 6 numpy.dot(a, b, out=None ) a = np.array([[1 ,2 ],[3 ,4 ]]) b = np.array([[11 ,12 ],[13 ,14 ]]) print (np.dot(a,b))[[37 40 ] [85 92 ]]
Functional programming numpy.piecewise 1 2 3 4 5 6 7 8 9 10 numpy.piecewise(x, condlist, funclist, *args, **kw) |-- |funclist[0 ](x[condlist[0 ]]) out = |funclist[1 ](x[condlist[1 ]]) |... |funclist[n2](x[condlist[n2]]) |-- x = np.linspace(-2.5 , 2.5 , 6 ) np.piecewise(x, [x < 0 , x >= 0 ], [-1 , 1 ]) array([-1. , -1. , -1. , 1. , 1. , 1. ])
numpy.vectorize 1 2 3 4 5 6 7 8 9 10 11 12 13 14 numpy.vectorize(pyfunc, otypes=None , doc=None , excluded=None , cache=False , signature=None ) def myfunc (a, b ): "Return a-b if a>b, otherwise return a+b" if a > b: return a - b else : return a + b vfunc = np.vectorize(myfunc) vfunc([1 , 2 , 3 , 4 ], 2 ) array([3 , 4 , 1 , 2 ]) --- ladder_move = np.vectorize(lambda x : env.ladders[x] if x in env.ladders else x) step = np.piecewise(step, [step > 100 , step <= 100 ], [lambda x : 200 - x, lambda x : x]) step = ladder_move(step)
Logical functions numpy.all Test whether all array elements along a given axis evaluate to True(not zero?) .
Not a Number (NaN), positive infinity and negative infinity evaluate to True because these are not equal to zero .
1 2 3 4 5 6 numpy.all (a, axis=None , out=None , keepdims=<no value>, *, where=<no value>) np.all ([[True ,False ],[True ,True ]]) False np.all ([-1 , 4 , 5 ]) True
numpy.equal 1 2 3 4 5 6 7 8 9 10 11 numpy.equal(x1, x2, /, out=None , *, where=True , casting='same_kind' , order='K' , dtype=None , subok=True [, signature, extobj]) = <ufunc 'equal' > Return (x1 == x2) element-wise. np.equal([0 , 1 , 3 ], np.arange(3 )) array([ True , True , False ]) The == operator can be used as a shorthand for np.equal on ndarrays. a = np.array([2 , 4 , 6 ]) b = np.array([2 , 4 , 2 ]) a == b array([ True , True , False ])
Random sampling numpy.random.randint Return random integers from low (inclusive) to high (exclusive).
1 2 3 4 5 random.randint(low, high=None , size=None , dtype=int ) np.random.randint([1 , 3 , 5 , 7 ], [[10 ], [20 ]], dtype=np.uint8) array([[ 8 , 6 , 9 , 7 ], [ 1 , 16 , 9 , 12 ]], dtype=uint8)
numpy.random.shuffle Modify a sequence in-place by shuffling its contents.
Multi-dimensional arrays are only shuffled along the first axis:
1 2 3 4 5 6 7 random.shuffle(x) arr = np.arange(9 ).reshape((3 , 3 )) np.random.shuffle(arr) arr array([[3 , 4 , 5 ], [6 , 7 , 8 ], [0 , 1 , 2 ]])
Sorting, searching, and counting numpy.argmax Returns the indices of the maximum values along an axis.
1 2 3 4 5 6 7 8 9 10 11 12 numpy.argmax(a, axis=None , out=None ) a = np.arange(6 ).reshape(2 ,3 ) + 10 a array([[10 , 11 , 12 ], [13 , 14 , 15 ]]) np.argmax(a) 5 np.argmax(a, axis=0 ) array([1 , 1 , 1 ]) np.argmax(a, axis=1 ) array([2 , 2 ])