numpy 使用详解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了numpy 使用详解相关的知识,希望对你有一定的参考价值。

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 使用详解的主要内容,如果未能解决你的问题,请参考以下文章

Numpy用法详解

线性回归详解(代码实现+理论证明)

线性回归详解(代码实现+理论证明)

14.VisualVM使用详解15.VisualVM堆查看器使用的内存不足19.class文件--文件结构--魔数20.文件结构--常量池21.文件结构访问标志(2个字节)22.类加载机制概(代码片段

Python中verbaim标签使用详解

numpy 使用详解