1. 程式人生 > >numpy函式總結

numpy函式總結

1.numpy.bincount:bin的數量比x中的最大值大1,每個bin給出了它的索引值在x中出現的次數。

# 我們可以看到x中最大的數為7,因此bin的數量為8,那麼它的索引值為0->7
x = np.array([0, 1, 1, 3, 2, 1, 7])
# 索引0出現了1次,索引1出現了3次......索引5出現了0次......
np.bincount(x)
#因此,輸出結果為:array([1, 3, 1, 1, 0, 0, 0, 1])

# 我們可以看到x中最大的數為7,因此bin的數量為8,那麼它的索引值為0->7
x = np.array([7, 6, 2, 1, 4])
# 索引0出現了0次,索引1出現了1次......索引5出現了0次......
np.bincount(x)
#輸出結果為:array([0, 1, 1, 0, 1, 0, 1, 1])

http://blog.csdn.net/xlinsist/article/details/51346523

2.numpy.argsort:返回的是陣列值從小到大的索引值

    One dimensional array:一維陣列
    
    >>> x = np.array([3, 1, 2])
    >>> np.argsort(x)
    array([1, 2, 0])
    
    Two-dimensional array:二維陣列
    
    >>> 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]])

http://blog.csdn.net/maoersong/article/details/21875705

3.numpy.zeros:返回來一個給定形狀和型別的用0填充的陣列;

np.zeros(5)
array([ 0.,  0.,  0.,  0.,  0.])


np.zeros((5,), dtype=np.int)
array([0, 0, 0, 0, 0])


np.zeros((2, 1))
array([[ 0.],
       [ 0.]])


s = (2,2)
np.zeros(s)
array([[ 0.,  0.],
       [ 0.,  0.]])
http://blog.csdn.net/qq_26948675/article/details/54318917