码迷,mamicode.com
首页 > 其他好文 > 详细

numpy 使用详解

时间:2017-12-22 20:52:13      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:div   star   returns   bin   replace   post   sub   ice   sci   

numpy.arange([start, ]stop, [step, ]dtype=None)

  • 返回数值均匀分布的数组
>>> np.arange(3)
array([0, 1, 2])
>>> np.arange(3,7)
array([3, 4, 5, 6])
>>> np.arange(3,7,2)
array([3, 5]

numpy.reshape(a, newshape, order=‘C‘)

ndarray.reshape(shape, order=‘C‘)

  • 返回形状调整后的数组,原数组不变
  • newshape 中可以有一个维度为-1,表明这个维度的大小会根据数组长度和其他维度大小进行推断

 

>>> a = np.array([[1,2,3], [4,5,6]])
>>> np.reshape(a, 6)
array([1, 2, 3, 4, 5, 6])>>> np.reshape(a, (3,-1))       # the unspecified value is inferred to be 2
array([[1, 2],
       [3, 4],
       [5, 6]])

 

numpy.transpose(a, axes=None)

ndarray.transpose(*axes)

numpy.ndarray.T

  • 返回转置后的数组,原数组不变
>>> a = np.array([[1, 2], [3, 4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> a.transpose()
array([[1, 3],
       [2, 4]])
>>> x = np.arange(24).reshape((2,3,4))
>>> x.shape
(2,3,4)
>>> x.transpose(1,0,2).shape
(3,2,4)

 

 ndarray.astype(dtype, order=‘K‘, casting=‘unsafe‘, subok=True, copy=True)

  • 更改数组的数据类型
>>> x = np.array([1, 2, 2.5])
>>> x
array([ 1. ,  2. ,  2.5])
>>> x.astype(int)
array([1, 2, 2])

 

 numpy.concatenate((a1, a2, ...), axis=0)

  • 拼接数组
>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
>>> np.concatenate((a, b), axis=0)
array([[1, 2],
       [3, 4],
       [5, 6]])
>>> np.concatenate((a, b.T), axis=1)
array([[1, 2, 5],
       [3, 4, 6]])
...
>>> x = [np.arange(5) for i in range(5)]
>>> x
[array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4])]
>>> np.concatenate(x)
array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4])

numpy.flatnonzero(a)

  •  Return indices that are non-zero in the flattened version of a
>>> x = np.arange(-2, 3)
>>> x
array([-2, -1,  0,  1,  2])
>>> np.flatnonzero(x)
array([0, 1, 3, 4])

 

 


numpy.random.choice(a, size=None, replace=True, p=None)

https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.choice.html#numpy.random.choice

  •  返回随机数
>>> np.random.choice(5, 3)
array([0, 3, 4])
>>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
array([3, 3, 0])
>>> np.random.choice(5, 3, replace=False)
array([3,1,0])
...
>>> a = np.arange(5)
>>> np.random.choice(a,10)
array([3, 4, 2, 0, 3, 2, 4, 2, 0, 2])

 numpy.argsort(a, axis=-1, kind=‘quicksort‘, order=None)

https://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html#numpy.argsort

  • Perform an indirect sort along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as a that index data along the given axis in sorted order.
>>> x = np.array([3, 1, 2])
>>> np.argsort(x)
array([1, 2, 0])

>>> x = np.array([[0, 3], [2, 2]])
>>> x
array([[0, 3],
       [2, 2]])
>>> np.argsort(x, axis=0)
array([[0, 1],
       [1, 0]])
>>> np.argsort(x, axis=1)
array([[0, 1],
       [0, 1]])

numpy.argmax(a, axis=None, out=None)

https://docs.scipy.org/doc/numpy/reference/generated/numpy.argmax.html#numpy.argmax

  • Returns the indices of the maximum values along an axis.
>>> a = np.arange(6).reshape(2,3)
>>> a
array([[0, 1, 2],
       [3, 4, 5]])
>>> np.argmax(a)
5
>>> np.argmax(a, axis=0)
array([1, 1, 1])
>>> np.argmax(a, axis=1)
array([2, 2])

numpy.bincount(x, weights=None, minlength=0)

https://docs.scipy.org/doc/numpy/reference/generated/numpy.bincount.html#numpy.bincount

  • Count number of occurrences of each value in array of non-negative ints.
>>> np.bincount(np.arange(5))
array([1, 1, 1, 1, 1])
>>> np.bincount(np.array([0, 1, 1, 3, 2, 1, 7]))
array([1, 3, 1, 1, 0, 0, 0, 1])

numpy.sum(a, axis=None, dtype=None, out=None, keepdims=<class numpy._globals._NoValue>)

https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html#numpy.sum


 

numpy 使用详解

标签:div   star   returns   bin   replace   post   sub   ice   sci   

原文地址:http://www.cnblogs.com/irran/p/7794396.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!