指点成金-最美分享吧

登录

numpy常用函数

佚名 举报

篇首语:本文由小编为大家整理,主要介绍了numpy常用函数相关的知识,希望对你有一定的参考价值。

a = np.arange(9).reshape((3,3))  变换维度

np.max(a)   全局最大,也可以加参数,找某个维度最大的

print(np.max(a,axis=0)) #每列最大

 

print(np.max(a,axis=1)) #每行最大

print(np.where(a==np.max(a))) ----》 (array([2], dtype=int64), array([2], dtype=int64))   用where得到最大值的索引,前面的array对应行数,后者对应列数。 如果array中有相同的最大值,where会将其位置全部给出

print(np.where(a==np.max(a,axis=0))) ---》(array([2, 2, 2], dtype=int64), array([0, 1, 2], dtype=int64))

np.argsort([1,2,3,5,9])   ---> [0,1,4,2,3]  返回排序的下标

np.argmax([1,2,3,5,9])    返回最大值得下标

np.argmin([1,2,3,5,9])    返回最小值得下标

np.bincount([1,2,3,5,9])  返回每个元素出现的次数  [0,1,1,1,0,1,0,0,0,1]

np.argmax(np.bincount(array))  返回出现次数最多的元素

a = np.array([[1, 5, 5, 2], [9, 6, 2, 8], [3, 7, 9, 1]])    print(np.sum(a, axis=0)) ---> [13,18,16,11]

a = np.array([[1, 5, 5, 2], [9, 6, 2, 8], [3, 7, 9, 1]]) print(np.sum(a, axis=1))   ---->  [13,25,20].

 

 np.dot() 点乘

array.T   a.transpose()   a的转置,但是对一维数组不起作用

a.reshape(a.shape[0],1)  一维数组的转置

a.reshape([-1]+a.shape[2:])  高维数组降一维

以上是关于numpy常用函数的主要内容,如果未能解决你的问题,请参考以下文章