gpt4 book ai didi

python - Sort_value 错误下降

转载 作者:行者123 更新时间:2023-12-05 09:37:08 27 4
gpt4 key购买 nike

我不知道我的代码有什么问题

import pandas as pd
import numpy as np
woe = [1.1147295474833758,0.364043491078754,-0.05525053172192353,-0.3950007109750665,-0.6784658191115104,-0.9522135140050229,-1.1441658353033486]
iv = [0.29078213954085946,0.29078213954085946,0.29078213954085946,0.29078213954085946,0.29078213954085946,0.29078213954085946,0.29078213954085946]
lis = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
fin = [lis,woe,iv]
fin = np.array(fin).T
df_disc = pd.DataFrame(fin,columns=['Label','WoE','IV'])
print(df_disc)
df_disc = df_disc.sort_values(by=['WoE'])
df_disc = df_disc.reset_index(drop=True)
print(df_disc)

结果

  Label                   WoE                   IV
0 A 1.1147295474833758 0.29078213954085946
1 B 0.364043491078754 0.29078213954085946
2 C -0.05525053172192353 0.29078213954085946
3 D -0.3950007109750665 0.29078213954085946
4 E -0.6784658191115104 0.29078213954085946
5 F -0.9522135140050229 0.29078213954085946
6 G -1.1441658353033486 0.29078213954085946
Label WoE IV
0 C -0.05525053172192353 0.29078213954085946
1 D -0.3950007109750665 0.29078213954085946
2 E -0.6784658191115104 0.29078213954085946
3 F -0.9522135140050229 0.29078213954085946
4 G -1.1441658353033486 0.29078213954085946
5 B 0.364043491078754 0.29078213954085946
6 A 1.1147295474833758 0.29078213954085946

我认为正确的应该是标签 G,F,E,D,C,B,A 但结果似乎是错误的

最佳答案

如前所述,该列包含字符串。为了保持精度,将 Series 转换为 Decimal:

from decimal import Decimal

# ...

df_disc['WoE'] = df_disc['WoE'].apply(Decimal)
df_disc = df_disc.sort_values(by='WoE')
print(df_disc)

打印:

  Label                   WoE                   IV
6 G -1.1441658353033486 0.29078213954085946
5 F -0.9522135140050229 0.29078213954085946
4 E -0.6784658191115104 0.29078213954085946
3 D -0.3950007109750665 0.29078213954085946
2 C -0.05525053172192353 0.29078213954085946
1 B 0.364043491078754 0.29078213954085946
0 A 1.1147295474833758 0.29078213954085946

关于python - Sort_value 错误下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64570173/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com