zoukankan      html  css  js  c++  java
  • 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.array_split

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

    >>> x = np.arange(8.0)
    >>> np.array_split(x, 3)
        [array([ 0.,  1.,  2.]), array([ 3.,  4.,  5.]), array([ 6.,  7.])]
    

    numpy.hstack

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

    Take a sequence of arrays and stack them horizontally to make a single array.

    Equivalent to np.concatenate(tup, axis=1) if tup contains arrays that are at least 2-dimensional.

    >>> a = np.array((1,2,3))
    >>> b = np.array((2,3,4))
    >>> np.hstack((a,b))
    array([1, 2, 3, 2, 3, 4])
    >>> a = np.array([[1],[2],[3]])
    >>> b = np.array([[2],[3],[4]])
    >>> np.hstack((a,b))
    array([[1, 2],
           [2, 3],
           [3, 4]])
    
  • 相关阅读:
    Client-Side Attacks
    Web Penetration Testing w3af fierce
    解决kali linux 升级后安装w3af 问题
    Metasploit (二)
    Metasploit
    Dig skipfish proxystrike
    Web Penetration Testing
    Wireless Penetration Testing(命令总结)
    LabView(控件部分)
    Wireless Penetration Testing(7-11 chapter)
  • 原文地址:https://www.cnblogs.com/irran/p/7794396.html
Copyright © 2011-2022 走看看