gpt4 book ai didi

python - 寻找最佳模型并将其信息纳入新专栏

转载 作者:太空宇宙 更新时间:2023-11-04 02:11:45 25 4
gpt4 key购买 nike

我有一个 df 如下。

enter image description here

p1_conf,p2_conf,p3_conf分别表示模型p1,的置信区间p2p3

我想知道如何选择每行具有最高置信区间的预测并将其存储在一些新列中。所以结果将是:

enter image description here

您可以使用下面的 df 作为原始 df:

df = pd.DataFrame({"id": [1,2,3,4,5],
"Name": ["Dave","Max","Joe","Rose","Mark"],
"model1":["Irish","German","USA","Japan","China"],
"confidence1": [0.9,.99,.83,.45,.51],
"prediction1": [True,False,True,False,False],
"model2":["Oman","Nigeria","India","Russia","Brazil"],
"confidence2": [0.1,.25,.26,.41,.01],
"prediction2": [False,True,False,False,False],
"model3":["Egypt","Cameron","Netherland","Canada","Mexcio"],
"confidence3": [0.01,.23,.12,.34,.61],
"prediction3": [True,False,True,True,False]})

结果应该是这样的:

df1 = pd.DataFrame({"id": [1,2,3,4,5],
"Name":["Dave","Max","Joe","Rose","Mark"],
"model_name":["1","2","1","3",None],
"predicted_gener":["Irish","Nigeria","USA","Canada",None],
"confidence":[0.9,0.25,.83,0.34,None],
"prediction":[True,True,True,True,None]})

感谢任何帮助。

最佳答案

我更新了我的答案以匹配您提供的新信息。希望这会有所帮助。

import pandas as pd

df=pd.DataFrame({"id": [1,2,3,4,5],
"Name": ["Dave","Max","Joe","Rose","Mark"],
"model1":["Irish","German","USA","Japan","China"],
"confidence1": [0.9,.99,.83,.45,.51],
"prediction1": [True,False,True,False,False],
"model2":["Oman","Nigeria","India","Russia","Brazil"],
"confidence2": [0.1,.25,.26,.41,.01],
"prediction2": [False,True,False,False,False],
"model3":["Egypt","Cameron","Netherland","Canada","Mexcio"],
"confidence3": [0.01,.23,.12,.34,.61],
"prediction3": [True,False,True,True,False]})

tweet_id = []
name = []
Model = []
Breed = []
Confidence = []

for i in range(len(df['id'])):
confidences = [df['confidence{0}'.format(model)][i] for model in range(1,4)]
models = ['p{0}'.format(model) for model in range(1,4)]
breeds = [df['model{0}'.format(model)][i] for model in range(1,4)]
isDog = [df['prediction{0}'.format(model)][i] for model in range(1,4)]

best_one = max(zip(confidences, models, breeds, isDog), key=lambda M: M[0])

model = best_one[1]
breed = best_one[2]
confidence = best_one[0]

if not (True in isDog):
model = breed = confidence = 'NaN'

tweet_id.append(df['id'][i])
name.append(df['Name'][i])
Model.append(model)
Breed.append(breed)
Confidence.append(confidence)

print(pd.DataFrame({
'tweet_id': tweet_id,
'name': name,
'Model': Model,
'Breed': Breed,
'Confidence': Confidence
}))

输出

   tweet_id  name Model   Breed Confidence
0 1 Dave p1 Irish 0.9
1 2 Max p1 German 0.99
2 3 Joe p1 USA 0.83
3 4 Rose p1 Japan 0.45
4 5 Mark NaN NaN NaN

关于python - 寻找最佳模型并将其信息纳入新专栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53603997/

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