import numpy as np
def numpyWork1(): # 过滤多维数组,每行有空值的数据
two_data = np.array([['12', '13'],
['12', ''],
['15', '16'],
['', '12'],
['', ''],
['5', '6'],
])
result_two1 = np.where(two_data[:, 0] != '')
result_two2 = np.where(two_data[:, 1] != '')
print('result_two1=', result_two1, 'result_two2=', result_two2)
# result_two1= (array([0, 1, 2, 5]),) result_two2= (array([0, 2, 3, 5]),)
result_data = set(result_two1[0]).intersection(set(result_two2[0]))
result_data = list(result_data)
result_all = two_data[result_data]
print('result_all=', result_all)
return result_all
return
def numpyWork2(): # 数组的纵向合并
array1 = np.array(['1', '2', '3', '4', '5'])
array2 = np.array(['a', 'b', 'c', 'e', 'f'])
print('array1 shape=', array1.shape[0])
array1 = array1.reshape(len(array1), 1)
array2 = array2.reshape(len(array2), 1)
print('array1 shape=', array1.shape, 'array2 shape=', array2.shape)
time_data = np.concatenate((array1, array2), axis=1) # 数组的纵向合并
print('time_data=', time_data)
return
def numpyWork3(): # 数组去重,获取去重后的元素,索引值,以及去重后每个元素在源数据集中的个数
array = np.array(['100', '100', '101', '102', '102', '104', '105', '110', '110', '104', '100'])
keep = np.unique(array, return_counts=True, return_inverse=True)
print('keep=', keep)
array1 = keep[0]
print('array1=', array1)
array2 = keep[1]
print('array2=', array2)
array3 = keep[2]
print('array3=', array3)
return
def numpyWork4(): # 一个数组A中的元素,是否在另一个数组B中,如果存在,则返回对应在B中的索引值
name = np.array(['a', 'b', 'c', 'dd', 'abc', 'cc', 'ac', 'adc'])
user = ['abc', 'dd', 'ab']
ix = np.isin(name, user)
print('ix = ', ix)
index = np.where(ix == True)
index = index[0]
print('index=', index)
users = name[index]
print('users=', users)
return
def numpyWork5(): # 一个等值向量的多行矩阵
one = np.array([[1, 0, 1, 0, 0, 1],
[1, 1, 1, 0, 1, 0],
[1, 0, 0, 1, 1, 0],
[0, 1, 1, 0, 1, 0],
[1, 1, 0, 1, 1, 0],
])
two = np.array([0, 1, 1, 0, 1, 1])
similar_len = len(one)
similar_wlen = len(two)
three = np.full((similar_len, similar_wlen), two)
print('three=', three)
return
def numpyWork6(): # 根据索引删除元素
array = np.array([1, 2, 3, 4, 5, 6, 7, 8])
indexs = np.array([0, 3])
array = np.delete(array, indexs)
print('array=', array)
return
def numpyWork7(): # 根据条件,把符合条件的元素替换为指定元素
arr = np.arange(6).reshape(2, 3)
print('arr=', arr)
np.place(arr, arr > 2, [44, 55])
print('arr=', arr)
return
def numpyWork8(): # 向量的横向与纵向拼接
a = np.array([1, 2, 3, 4, 5, 6, 7, 8])
b = np.array([1, 2, 3, 4, 5, 6, 7, 8])
arr1 = np.vstack((a, b))
print('arr1= ', arr1, 'arr1 shape=', arr1.shape) # 纵向: shape= (2, 8)
arr2 = np.hstack((a, b))
print('arr2= ', arr2, 'arr2 shape=', arr2.shape) # 横向: shape= (16,) 这里其实是 shape= (16, 1),因为它直接拼在了尾部
return
def numpyWork9(): # 矩阵的横向与纵向的合并
a = np.array([[1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16]])
b = np.array([[1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16]])
arr1 = np.vstack((a, b))
print('arr1= ', arr1, 'arr1 shape=', arr1.shape) # 纵向: shape= (4, 8)
'''
打印结果:arr1= [[ 1 2 3 4 5 6 7 8]
[ 9 10 11 12 13 14 15 16]
[ 1 2 3 4 5 6 7 8]
[ 9 10 11 12 13 14 15 16]]
arr1 shape= (4, 8)
'''
arr2 = np.hstack((a, b))
print('arr2= ', arr2, 'arr2 shape=', arr2.shape) # 横向: shape= (2, 16)
'''
arr2= [[ 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8]
[ 9 10 11 12 13 14 15 16 9 10 11 12 13 14 15 16]]
arr2 shape= (2, 16)
'''
return
def numpyWork10(): # 向量one中不在向量two中的元素集合
one = np.array(['a', 'b', 'c', 'd', 'd', 'f', 'a', 'b', 'e', 'd', 'e'])
two = np.array(['d', 'f', 't', 'f', 'f', 'g', 's'])
c = list(set(one).difference(set(two)))
print('c=', c)
return
def numpyWork11(): # 向量元素的排序: 倒序、正序
one = np.array([1, 22, 3, 6, 88, 9, 23, 24, 99, 14, 17, 8, 5, 96])
argsortIndex = np.argsort(one, axis=0) # 得到正序索引值
print('argsortIndex= ', argsortIndex)
s_argsortIndex = argsortIndex[::-1]
print('s_argsortIndex= ', s_argsortIndex)
arr1 = one[argsortIndex]
arr2 = one[s_argsortIndex]
print('arr1= ', arr1)
print('arr2= ', arr2)
return
#numpyWork1() # 过滤多维数组,每行有空值的数据
#numpyWork2() # 数组的纵向合并
#numpyWork3() # 数组去重,获取去重后的元素,索引值,以及去重后每个元素在源数据集中的个数
#numpyWork4() # 一个数组A中的元素,是否在另一个数组B中,如果存在,则返回对应在B中的索引值
#numpyWork5() # 一个等值向量的多行矩阵
#numpyWork6() # 根据索引删除元素
#numpyWork7() # 根据条件,把符合条件的元素替换为指定元素
#numpyWork8() # 向量的横向与纵向拼接
#numpyWork9() # 矩阵的横向与纵向的合并
#numpyWork10() # 向量one中不在向量two中的元素集合
#numpyWork11() # 向量元素的排序