NumPy duplicate data and deduplication

In mathematical statistical analysis, it is necessary to remove duplicate data in advance. In NumPy, the unique function in the array can be used to find the unique value and return the sorted results.

Data in the array is deduplicated. (unique(a))

a = np.array(['red','blue','yellow','red','red','white'])
print("原数组:",a)
print("去重后的数组:",np.uni
#Output
#原数组: ['red' 'blue' 'yellow' 'red' 'red' 'white']
#去重后的数组: ['blue' 'red' 'white' 'yellow']

In statistical analysis, sometimes it is necessary to repeat a piece of data several times, and this function can be realized by using the tile and repeat functions.

  • The format of the tile function: np.tile(A, reps)

Among them, the parameter A represents the array to be repeated, and reps represents the number of repetitions.

  • The format of the repeat function: np.repeat(A, reps, axis = None)

Among them, "a": is the array element that needs to be repeated, "repeats": is the number of repetitions, "axis": specifies which axis to repeat along, axis = 0 means to repeat elements by row; axis = 1 means to repeat by column Elements are repeated.

#使用tile 函数实现数据重复。
kk = np.array([1,2,3])
print(kk)
ll = np.tile(kk,3) #将kk数组重复三次
print(ll)
#Output
#[1 2 3]
#[1 2 3 1 2 3 1 2 3]
#使用repeat 函数实现数据重复。
kk = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(kk)
print('------------')
# ll = np.tile(kk,3) #将kk数组重复三次
# print(ll)
ll = np.repeat(kk,2,axis=1)
print(ll)
print('------------')
ss = np.repeat(kk,2,axis=0)
print(ss)
#Output
# [[1 2 3]
#  [4 5 6]
#  [7 8 9]]
# ------------
# [[1 1 2 2 3 3]  #按列进行重复
#  [4 4 5 5 6 6]
#  [7 7 8 8 9 9]]
# ------------
# [[1 2 3]        #按行进行重复
#  [1 2 3]
#  [4 5 6]
#  [4 5 6]
#  [7 8 9]
#  [7 8 9]]

Guess you like

Origin blog.csdn.net/chenjh027/article/details/127953378