Detailed explanation of the usage of bincount, repeat, newaxis and assert in numpy

1. Detailed explanation of numpy.bincount

Give the explanation on the official website first.

write picture description here

The function of the function: count the number of occurrences of each element in the input array. Of course every element in the input array must be non-negative.

Parameters of the function: X : the form of a one-dimensional array, the elements of the array must be non-negative integers
weights: array, which has the same shape as the input array X, optional parameter
minlength: int, which limits the size of the returned array

The return value of the function: out : A one-dimensional array. If the function does not pass in the minlength parameter, the size of the returned value array is the maximum value of the elements in the input array plus 1, that is, np.amax(x)+1

Exception thrown by function: Numerical exception : Input array is not one-dimensional. The value in the input array has a negative value, or the value of minlength is negative. Type exception : The input array is of float type, complex type

The working principle of numpy.bincount( x , weights=None ,minlength=0):

# example one
>>> import numpy as np
>>> a1=np.arange(5)
>>> a1
array([0, 1, 2, 3, 4])  
# 统计数组中每个位置的元素出现的次数。
# 首先 返回数组的长度为 数组元素中的最大值 + 1  = 4+1=5
# 观察a1数组,可以发现,0 出现的次数为 1 次;1 出现的次数 为 1 次;,,,;4出现的次数为 1 次。
# 在返回数组中的数据相应位置填上它们各自出现的次数。
>>> np.bincount(a1)
array([1, 1, 1, 1, 1])


# example two

# length of output array = 10+1 = 11
# calculate the  frequency of every element in the input array
>>> a2=np.array([3,7,8,5,4,10,3,5,7,1])
>>> np.bincount(a2)
array([0, 1, 0, 2, 1, 2, 0, 2, 1, 0, 1])
>>> len(np.bincount(a2))
11



# 输出数组的每个元素-------每个元素下标在X中出现的次数。
# 如果weight参数值给定,那么当输出数组下标对应的元素在x找到一次时,
# 那么这个下标元素的count=count + weight ,而不是默认的count= count +1 
# Each bin gives the number of occurrences of its index value in x. 
# If weights is specified the input array is weighted by it, 
# i.e. if a value n is found at position i, out[n] += weight[i] instead of out[n] += 1

2. Detailed explanation of numpy.repeat

The function of numpy;repeat( ) function: repeats the elements of the input array.

numpy(a1 , repeats , axis=None): Copy the elements of this axis to the input array a1 along the axis specified by axis repeats times

Since the array cannot be dynamically expanded, new space is reallocated to store the expanded data after the function is called.

The following examples illustrate:

>>> re1=np.arange(16).reshape(2,2,4)
>>> re1
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]]])
>>> np.repeat(re1,3,axis=0)  # axis=0 ,则在 0 轴上的每次元素都各自沿 0 轴 复制 3 次
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]]])
>>> np.repeat(re1,3,axis=1)    # axis=1 ,则在 1 轴上的每次元素都各自沿 1 轴 复制 3 次
array([[[ 0,  1,  2,  3],
        [ 0,  1,  2,  3],
        [ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 4,  5,  6,  7],
        [ 4,  5,  6,  7]],

       [[ 8,  9, 10, 11],
        [ 8,  9, 10, 11],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15],
        [12, 13, 14, 15],
        [12, 13, 14, 15]]])
>>> np.repeat(re1,3,axis=2)    # axis=2 ,则在 2 轴上的每个元素都各自沿 2 轴 复制 3 次
array([[[ 0,  0,  0,  1,  1,  1,  2,  2,  2,  3,  3,  3],
        [ 4,  4,  4,  5,  5,  5,  6,  6,  6,  7,  7,  7]],

       [[ 8,  8,  8,  9,  9,  9, 10, 10, 10, 11, 11, 11],
        [12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15]]])   

# 经过一系列的操作之后,那么最后来看看re1的数组变化了没有

>>> re1
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]]])
# 发现并没有什么变化,这说明numpy.repeat操作是返回的一个新数组,原数组不会发生变化。

3. Detailed explanation of newaxis in numpy

The newaxis included in numpy can add a dimension to the array

The position of the newaxis is different, and the new array generated is also different

# 下面来举例说明:
# example one  
# 对于一维数组
>>> new1=np.arange(4)
>>> new1
array([0, 1, 2, 3])
>>> new1[np.newaxis,:]     # 在行上增加一个维度
array([[0, 1, 2, 3]])
>>> new1[:,np.newaxis]     # 在列上增加一个维度
array([[0],
       [1],
       [2],
       [3]])
# example two       
# 对于二维数组
>>> new2=np.arange(8).reshape(2,4)
>>> new2
array([[0, 1, 2, 3],
       [4, 5, 6, 7]])
>>> new2[np.newaxis,:]  # 在原始数组的第一维之前增加一维
array([[[0, 1, 2, 3],
        [4, 5, 6, 7]]])
>>> new2[:,np.newaxis,:] # 在原始数组的第一维之后,第二维之前
array([[[0, 1, 2, 3]],

       [[4, 5, 6, 7]]])
>>> new2[:,:,np.newaxis]  # 在原始数组的第二维之后增加一维。
array([[[0], 
        [1],
        [2],
        [3]],

       [[4],
        [5],
        [6],
        [7]]])

# 再来看看它们分别增加维度之后的shape

>>> new2[np.newaxis,:].shape
(1, 2, 4)
>>> new2[:,np.newaxis,:].shape
(2, 1, 4)
>>> new2[:,:,np.newaxis].shape
(2, 4, 1)
>>> 
# 总结: 将newaxis之后的每个大元素增加一个[ ]在外面就好了。
# 例如: new2[:,:,np.newaxis] 在原始数组的最后一个维度的每个元素加上[ ],并且用逗号分隔。

Fourth, the detailed explanation of assert in python
Function: Before completing a program, we don't know where it will go wrong. Rather than having a program crash at runtime, it is better to crash when an error condition occurs. This requires the role of assert assertion in python.
The boolean value of the expression declared by python's assert must be true. If the value of the expression is false, an exception will be triggered. You can add exception parameters to the assertion.

The usage is as follows:

>>> x=1
>>> y=1
>>> assert x==y   # 表达式为真,则程序正常执行,没有assert异常抛出
>>> x=1
>>> y=3
>>> assert x==y   # 表达式为假,则程序崩溃,assert抛出异常

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    assert x==y
AssertionError   # 这里表示断言异常
>>> x=1
>>> y=7
>>> assert x==y , "x is not equal to y ,please check the value of x,y"  # 给断言添加了异常参数,若前面的表达式的值为假,则断言的异常参数会随异常一起打印出来

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    assert x==y , "x is not equal to y ,please check the value of x,y"
AssertionError: x is not equal to y ,please check the value of x,y  # 异常参数被打印了出来

Learn here today. See you tomorrow. . . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325987679&siteId=291194637