[英]Create list of index of array sorted from largest to smallest
I have an array y_probability
which is 2 dimensional. 我有一个二维的y_probability
数组。 It contains the probability that an instance belongs to each class. 它包含一个实例属于每个类的概率。 I would like to create a list of the indexes of the y_probabiltiy
array ordered from largest to smallest on the first column. 我想创建y_probabiltiy
数组的索引列表,该列表在第一列中y_probabiltiy
排列。 How can I do this? 我怎样才能做到这一点?
Sample data: 样本数据:
y_probability = np.array([[0.3,0.7],[0.5,0.5] ,[0.2,0.8], [0.1,0.9]])
Desired output: 所需的输出:
index = [2,1,3,4]
You can use argsort
on the first column (note the index in python is 0 based, so the result will be one less than what you expect), then reverse the result with [::-1]
since argsort
returns index that sorts array in ascending order: 您可以在第一列上使用argsort
(请注意python中的索引是基于0的,因此结果将比您的期望值少一个),然后使用[::-1]
反转结果,因为argsort
返回对数组进行排序的索引升序:
y_probability[:,0].argsort()[::-1]
# array([1, 0, 2, 3])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.