主要介绍numpy的 argmax,argsort函数,以及heapq库
# 返回最大值的索引
a = np.array([[6.8284258e-09, 5.7629350e-09, 7.4942248e-09,9.5846470e-11,
5.4229814e-14, 8.6114553e-15],
[1.1742729e-09, 9.2132124e-10, 1.2312825e-09,2.3951413e-04,
5.2452291e-09, 7.0311121e-09]])
d=[c.argmax() for c in a]
print(d)
>>>[2, 3]
# argsort
# np.argsort(a, axis=-1, kind='quicksort', order=None)
Examples
--------
One dimensional array:
>>> x = np.array([3, 1, 2])
>>> np.argsort(x)
array([1, 2, 0])
Two-dimensional array:
>>> x = np.array([[0, 3], [2, 2]])
>>> x
array([[0, 3],
[2, 2]])
>>> np.argsort(x, axis=0) # sorts along first axis (down)
array([[0, 1],
[1, 0]])
>>> np.argsort(x, axis=1) # sorts along last axis (across)
array([[0, 1],
[0, 1]])
Indices of the sorted elements of a N-dimensional array:
>>> ind = np.unravel_index(np.argsort(x, axis=None), x.shape)
>>> ind
(array([0, 1, 1, 0]), array([0, 0, 1, 1]))
>>> x[ind] # same as np.sort(x, axis=None)
array([0, 2, 2, 3])
Sorting with keys:
>>> x = np.array([(1, 0), (0, 1)], dtype=[('x', '>> x
array([(1, 0), (0, 1)],
dtype=[('x', '>> np.argsort(x, order=('x','y'))
array([1, 0])
>>> np.argsort(x, order=('y','x'))
array([0, 1])
# 返回前top3的最大值
import heapq
import numpy as np
import pandas as pd
a = np.array([[6.8284258e-09, 5.7629350e-09, 7.4942248e-09,9.5846470e-11,
5.4229814e-14, 8.6114553e-15],
[1.1742729e-09, 9.2132124e-10, 1.2312825e-09,2.3951413e-04,
5.2452291e-09, 7.0311121e-09]])
c=[]
for i in range(len(a)):
b=heapq.nlargest(3, range(len(a[i])), a[i].take)
c.append(b)
>>>[2, 0, 1], [3, 5, 4]]
# 机器学习的特征重要性的排序
que=[]
a=clf.feature_importances_
w= heapq.nlargest(10, range(len(a)), a.take) #top10最重要的特征
b= df.columns #(训练的全部列)
for i in w:
print(i,b[i],a[i]*100)
que.append(i)