gpt4 book ai didi

python - 将函数结果附加到数据框

转载 作者:行者123 更新时间:2023-11-30 09:42:28 25 4
gpt4 key购买 nike

我尝试将取决于循环的值分配给Python中的数据帧。

我有以下开始数据框:

thres = 0.1
d = { 'T': [0.], 'TN': [0], 'FN': [0], 'FP':[0], 'TP':[0]}
dataframef = pd.DataFrame(data=d)

使用我的起始变量thres

现在我正在进入循环:

while thres <= 0.4:
a = { 'T': [0], 'TN': [0], 'FN': [0], 'FP':[0], 'TP':[0]}
dataframe = pd.DataFrame(data=a)
y_pred = predictionthreshold(RandomForestClassifier(random_state=42), thres)
tn, fp, fn, tp = confusion_matrix(y_test.ravel(), y_pred).ravel()
dataframe.loc[0]['T'] = thres
dataframe.loc[0]['TN'] = tn
dataframe.loc[0]['FP'] = fp
dataframe.loc[0]['FN'] = fn
dataframe.loc[0]['TP'] = tp
dataframef = dataframef.append(dataframe, ignore_index=True)
thres=thres+0.1

我的结果是:

    T   TN  FN  FP  TP
0 0.0 0 0 0 0
1 0.0 0 0 0 0
2 0.0 0 0 0 0
3 0.0 0 0 0 0
4 0.0 0 0 0 0

我期望有一些东西可以逐步填充数据框:

    T   TN  FN  FP  TP
0 0.1 1 0 0 0
1 0.2 0 3 0 0
2 0.3 0 0 2 0
3 0.4 0 0 0 0
4 0.5 0 4 0 4

哪里出错了?有没有优雅的方法来避免循环?

最佳答案

像这样更改分配:

dataframe.loc[0,'T'] = thres
dataframe.loc[0,'TN'] = tn
dataframe.loc[0,'FP'] = fp
dataframe.loc[0,'FN'] = fn
dataframe.loc[0,'TP'] = tp

当使用dataframe.loc[0]['TP'] = tp时,您将tp分配给副本,而不是原始数据帧。详情见Returning a view versus a copy .

关于python - 将函数结果附加到数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56964820/

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