numpy部分用法

1. np.argsort()
>>> import numpy as np
>>> a=np.array([1,-1,4,5,0])
>>> index=np.argsort(a) #元素从小到大排序,只返回索引List(原始数组元素位置不改变)
>>> print(index)
[1 4 0 2 3]
2.np.bincount()
#bin=最大值加1,每个bin给出了它的索引值在x中出现的次数
#eg:
# 我们可以看到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])

*************************
# weight参数
w = np.array([0.3, 0.5, 0.2, 0.7, 1., -0.6])
# 我们可以看到x中最大的数为4,因此bin的数量为5,那么它的索引值为0->4
x = np.array([2, 1, 3, 4, 4, 3])
# 索引0 -> 0  #因为0索引出现次数为0
# 索引1 -> w[1] = 0.5 #1索引出现次数为1,且对应权值为0.5
# 索引2 -> w[0] = 0.3 
# 索引3 -> w[2] + w[5] = 0.2 - 0.6 = -0.4 # 3索引出现次数为2,对应两处权值的和
# 索引4 -> w[3] + w[4] = 0.7 + 1 = 1.7
np.bincount(x,  weights=w)
# 因此,输出结果为:array([ 0. ,  0.5,  0.3, -0.4,  1.7])

ref:https://blog.csdn.net/xlinsist/article/details/51346523

3.np.vstack()
垂直排列数组
"""
 Examples
    --------
    >>> a = np.array([1, 2, 3])
    >>> b = np.array([2, 3, 4])
    >>> np.vstack((a,b))
    array([[1, 2, 3],
           [2, 3, 4]])

    >>> a = np.array([[1], [2], [3]])
    >>> b = np.array([[2], [3], [4]])
    >>> np.vstack((a,b))
    array([[1],
           [2],
           [3],
           [2],
           [3],
           [4]])

    """
4.np.hstack()
水平排列数组
"""
Examples
    --------
    >>> 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]])
"""
5. np.linspace()
'''
返回均匀间隔的数组,[start,stop]
Examples
    --------
    >>> np.linspace(2.0, 3.0, num=5)
    array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ])
    >>> np.linspace(2.0, 3.0, num=5, endpoint=False) #不包括stop
    array([ 2. ,  2.2,  2.4,  2.6,  2.8])
    >>> np.linspace(2.0, 3.0, num=5, retstep=True) #返回步长
    (array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ]), 0.25)
'''
6.np.prop()
# Return the product of array elements over a given axis
'''
Examples
    --------
    By default, calculate the product of all elements:

    >>> np.prod([1.,2.])
    2.0

    Even when the input array is two-dimensional:

    >>> np.prod([[1.,2.],[3.,4.]])
    24.0

    But we can also specify the axis over which to multiply:

    >>> np.prod([[1.,2.],[3.,4.]], axis=1)
    array([  2.,  12.])

'''
7.np.random.binomial(n,p,size)
'''
二项分布,在size次实验的每次实验中,n个硬币中出现正面的硬币个数

>>> np.random.binomial(3,0.5,10)
array([2, 2, 2, 1, 2, 1, 1, 2, 2, 1])

>>> np.sum(np.random.binomial(3,0.5,10)==2)/10 #10次实验中出现2次硬币正面的概率
0.4

dropout中的使用:
>>> np.random.binomial(1,0.5,(10,10)) #X.shape=(10,10),保留50%的结点
array([[1, 1, 0, 1, 0, 1, 1, 0, 0, 1],
       [0, 1, 0, 1, 1, 0, 1, 1, 1, 1],
       [0, 0, 1, 1, 1, 1, 0, 1, 1, 0],
       [1, 0, 1, 0, 0, 0, 1, 0, 1, 0],
       [0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
       [1, 0, 1, 1, 0, 0, 1, 1, 1, 1],
       [1, 1, 0, 0, 0, 1, 1, 0, 1, 1],
       [0, 0, 1, 0, 0, 1, 1, 0, 1, 0],
       [0, 0, 0, 1, 0, 1, 1, 0, 1, 1],
       [1, 1, 1, 1, 0, 1, 0, 0, 0, 0]])
上述数组再乘以X即得到dropout后结果
'''
8.np.pad(array, pad_width, mode, **kwargs)
'''
数组的填充,可用于卷积的图像填充

>>> a=np.array([[1,2],[3,4]])
>>> np.pad(a,((2,3),(3,2)),'constant',constant_values=(8, 9))
# (2,3)表示第一维即按行填充,数组行的最前面填充2行全8,最后面填充3行全9;
# (3,2)表示第二维即按列填充,数组列的最前面填充3列全8,最后面填充2行全9;
# (8,9)表示前和后的填充数量
array([[8, 8, 8, 8, 8, 9, 9],
       [8, 8, 8, 8, 8, 9, 9],
       [8, 8, 8, 1, 2, 9, 9],
       [8, 8, 8, 3, 4, 9, 9],
       [8, 8, 8, 9, 9, 9, 9],
       [8, 8, 8, 9, 9, 9, 9],
       [8, 8, 8, 9, 9, 9, 9]])
'''
9.np.newaxis()创建新轴
'''
Example
>>> a = np.array([3,1,5,9])
>>> print(a,a.shape)
[3 1 5 9] (4,)
>>> b=a[np.newaxis,:]
>>> print(b,b.shape)
[[3 1 5 9]] (1, 4)
>>> c=a[:,np.newaxis]
>>> print(c,c.shape)
[[3]
 [1]
 [5]
 [9]] (4, 1)
'''

 

HOG

def:https://baike.baidu.com/item/HOG/9738560

ref:https://blog.csdn.net/zouxy09/article/details/7929348

paper:http://lear.inrialpes.fr/people/triggs/pubs/Dalal-cvpr05.pdf

code:参考cs231n

def hog_feature(im):
  """Compute Histogram of Gradient (HOG) feature for an image
  
       Modified from skimage.feature.hog
       http://scikit-image.org/docs/dev/api/skimage.feature.html#hog
     
     Reference:
       Histograms of Oriented Gradients for Human Detection
       Navneet Dalal and Bill Triggs, CVPR 2005
     
    Parameters:
      im : an input grayscale or rgb image
      
    Returns:
      feat: Histogram of Gradient (HOG) feature
    
  """
  
  # convert rgb to grayscale if needed
  if im.ndim == 3:
    image = rgb2gray(im)
  else:
    image = np.at_least_2d(im)

  sx, sy = image.shape # image size
  orientations = 9 # number of gradient bins
  cx, cy = (8, 8) # pixels per cell

  gx = np.zeros(image.shape)
  gy = np.zeros(image.shape)
  gx[:, :-1] = np.diff(image, n=1, axis=1) # compute gradient on x-direction
  gy[:-1, :] = np.diff(image, n=1, axis=0) # compute gradient on y-direction
  grad_mag = np.sqrt(gx ** 2 + gy ** 2) # gradient magnitude
  grad_ori = np.arctan2(gy, (gx + 1e-15)) * (180 / np.pi) + 90 # gradient orientation

  n_cellsx = int(np.floor(sx / cx))  # number of cells in x
  n_cellsy = int(np.floor(sy / cy))  # number of cells in y
  # compute orientations integral images
  orientation_histogram = np.zeros((n_cellsx, n_cellsy, orientations))
  for i in range(orientations):
    # create new integral image for this orientation
    # isolate orientations in this range
    temp_ori = np.where(grad_ori < 180 / orientations * (i + 1),
                        grad_ori, 0)
    temp_ori = np.where(grad_ori >= 180 / orientations * i,
                        temp_ori, 0)
    # select magnitudes for those orientations
    cond2 = temp_ori > 0
    temp_mag = np.where(cond2, grad_mag, 0)
    orientation_histogram[:,:,i] = uniform_filter(temp_mag, size=(cx, cy))[int(cx/2)::cx, int(cy/2)::cy].T
  
  return orientation_histogram.ravel()