使用Python numpy 找数组中最大值及次大值、第三大值等最值 |
您所在的位置:网站首页 › 酷安app分享安卓版下载安装不了怎么办 › 使用Python numpy 找数组中最大值及次大值、第三大值等最值 |
使用Python numpy 找数组中最大值及次大值、第三大值… 下面就贴上代码 def find_max(a): b = np.zeros(4) c = np.zeros(4) c[0] = np.max(a) #最大值 b[0] = np.where(a==c[0])[0] #最大``值位置 new_a = np.delete(a,b[0]) c[1] = np.max(new_a) #次大值 b[1] = np.where(new_a==c[1])[0] #找出次大值位置。注:位置为新数组位置,无意义 new_a1 = np.delete(new_a,b[1]) c[2] = np.max(new_a1) #第三大值 b[2] = np.where(new_a1==c[2] )[0] #找出第三大值位置 new_a2 = np.delete(new_a1,b[2]) c[3] = np.max(new_a2) #第四大值 b[3] = np.where(new_a2==c[3])[0] #找出第四大值位置 return b,c函数中a为需要查找的数组,b和c的大小可以根据所需进行更改,因为我要找四个最值,所以就设置为4。 返回值中b并无实际应用,因为里面数组已经进行删除更改,返回值中c为找到的最大值 。若需知道几个值的索引,在加上下面几行代码: fmax_index,fmax = find_max(row_sum) print(fmax) #输出所在列 row_sum_max = np.where(row_sum==np.max(row_sum))[0]row_sum为所需查找数组。 第一次写博客,有什么错误地方还请各位大神指出。 共勉! |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |