pandas求行最大值及其索引的实现

在平时训练完模型后,需要对模型预测的值做进一步的数据操作,例如在对模型得到类别的概率值按行取最大值,并将最大值所在的列单独放一列。

数据格式如下:

1
2
3
4
5
6
7
8
9
10
array
array([[ 0.472887690.239822150.2261405 0.06114962],
       [ 0.679695960.114351760.176473220.02947907],
       [ 0.006213930.016521420.311171650.66609299],
       [ 0.240933660.236367580.301138280.22156043],
       [ 0.440936420.2245989 0.245159670.08930501],
       [ 0.055403390.100139420.303618430.54083872],
       [ 0.112218860.756748080.092371310.03866173],
       [ 0.248853160.282430110.283121650.18559511],
       [ 0.012052110.037406380.271065  0.67947656]], dtype=float32)

想在想实现的功能是在上述DataFrame后面增加两列:一列是最大值,一列是最大值所在的行索引。

首先先来了解一下argmax函数。

1
2
3
4
5
argmax(a, axis=None)
 
# a 表示DataFrame
 
# axis 表示指定的轴,默认是None,表示把array平铺,等于1表示按行,等于0表示按列。

对于DataFrame来说,求解过程如下:

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#导入库
import pandas as pd
import numpy as np
#将array转化为DataFrame
arr=pd.DataFrame(array,columns=["one","two","three","four"])
#分别求行最大值及最大值所在索引
arr['max_value']=arr.max(axis=1)
arr['max_index']=np.argmax(array,axis=1)
#得出如下结果:
arr
Out[28]:
        one       two     three      four  max_index  max_value
0  0.472888  0.239822  0.226140  0.061150          0   0.472888
1  0.679696  0.114352  0.176473  0.029479          0   0.679696
2  0.006214  0.016521  0.311172  0.666093          3   3.000000
3  0.240934  0.236368  0.301138  0.221560          2   2.000000
4  0.440936  0.224599  0.245160  0.089305          0   0.440936
5  0.055403  0.100139  0.303618  0.540839          3   3.000000
6  0.112219  0.756748  0.092371  0.038662          1   1.000000
7  0.248853  0.282430  0.283122  0.185595          2   2.000000
8  0.012052  0.037406  0.271065  0.679477          3   3.000000

假如现在要找出行第二大的值及其索引时,该怎么操作呢:

解决思路:可以将行的最大值置为0,然后在寻找每行的最大值及其索引。

具体代码实现过程如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#将最大值置为0
array[arr.index,np.argmax(array,axis=1)]=0
array
array([[ 0.        0.239822150.2261405 0.06114962],
       [ 0.        0.114351760.176473220.02947907],
       [ 0.006213930.016521420.311171650.        ],
       [ 0.240933660.236367580.        0.22156043],
       [ 0.        0.2245989 0.245159670.08930501],
       [ 0.055403390.100139420.303618430.        ],
       [ 0.112218860.        0.092371310.03866173],
       [ 0.248853160.282430110.        0.18559511],
       [ 0.012052110.037406380.271065  0.        ]], dtype=float32)
#取出第二大值及其索引
arr['second_value']=array.max(axis=1)
arr['second_index']=np.argmax(array,axis=1)
arr
Out[208]:
        one       two     three      four  max_value  max_index  second_value 
0  0.472888  0.239822  0.226140  0.061150   0.472888          0      0.239822  
1  0.679696  0.114352  0.176473  0.029479   0.679696          0      0.176473  
2  0.006214  0.016521  0.311172  0.666093   0.666093          3      0.311172  
3  0.240934  0.236368  0.301138  0.221560   0.301138          2      0.240934  
4  0.440936  0.224599  0.245160  0.089305   0.440936          0      0.245160  
5  0.055403  0.100139  0.303618  0.540839   0.540839          3      0.303618  
6  0.112219  0.756748  0.092371  0.038662   0.756748          1      0.112219  
7  0.248853  0.282430  0.283122  0.185595   0.283122          2      0.282430  
8  0.012052  0.037406  0.271065  0.679477   0.679477          3      0.271065  
 
   second_index 
0             1 
1             2 
2             2 
3             0 
4             2 
5             2 
6             0 
7             1 
8             2

到此这篇关于pandas求行最大值及其索引的实现的文章就介绍到这了,更多相关pandas求行最大值及索引内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/code/python/10393.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部