将函数结果追加到数据框

我想尝试在Python中根据循环将值分配给数据框。

我有以下初始数据框:

thres = 0.1d = { '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  TP0   0.0     0   0   0   01   0.0     0   0   0   02   0.0     0   0   0   03   0.0     0   0   0   04   0.0     0   0   0   0

我期望的结果是逐步填充数据框,像这样:

    T   TN  FN  FP  TP0   0.1     1   0   0   01   0.2     0   3   0   02   0.3     0   0   2   03   0.4     0   0   0   04   0.5     0   4   0   4 

这里的错误在哪里?有没有避免使用循环的优雅方法?


回答:

像这样更改赋值:

dataframe.loc[0,'T'] = thresdataframe.loc[0,'TN'] = tndataframe.loc[0,'FP'] = fpdataframe.loc[0,'FN'] = fndataframe.loc[0,'TP'] = tp 

当使用dataframe.loc[0]['TP'] = tp时,你是将tp赋值给一个副本,而不是原始数据框。详情请见返回视图与副本

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注