使用不同的名称在 for 循环中保存模型预测
Save models predictions inside for loop with different names
我在 roder 中使用 scikit 学习 运行 我拥有的 3 个不同数据帧 (aa、bb、cc) 上的随机森林模型。
为此,我使用 for 循环并为每个模型生成混淆矩阵。
问题是我想保存每个模型的预测,以便以后可以将其用于 ROC 分析。
这是循环的原始脚本:
todrop=['name','code','date','nitrogen','Hour','growth_day']
col='test'
dfs=[aa,bb,cc]
for h in dfs:
h.drop(todrop,axis=1,inplace=True)
y=h[col]
X=h.drop(col,axis=1)
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,stratify=y,random_state=42)
rfc = RandomForestClassifier()
rfc.fit(X_train,y_train)
predictions = rfc.predict(X_test)
conf_mat = confusion_matrix(y_test, predictions)
df_conf_norm = conf_mat / conf_mat.sum(axis=0)
index = ['healthy','sick']
columns = ['healthy','sick']
cm_df = pd.DataFrame(df_conf_norm,columns,index)
seaborn.heatmap(cm_df,annot=True, annot_kws={"size": 15},linewidths=.5, cmap='YlOrRd')
plt.title('Random Forest', fontsize = 20) # title with fontsize 20
plt.xlabel('True Labels', fontsize = 17) # x-axis label with fontsize 15
plt.ylabel('Prediction', fontsize = 17) # y-axis label with fontsize 15
plt.show()
print('Accuracy'+'{}'.format(accuracy_score(y_test,predictions)))
我试过这样保存模型:
predictions[h] = rfc.predict(X_test)
但是我得到了错误:
IndexError: only integers, slices (:
), ellipsis (...
),
numpy.newaxis (None
) and integer or boolean arrays are valid indices
我也尝试过使用 zip 然后将其另存为名称:
names=['aa','bb','cc']
for h,n in (zip(dfs,names)):
...
predictions[n] = rfc.predict(X_test)
但出现同样的错误。
我的最终目标是保存(每个模型的)这些预测,以便最终创建 ROC 图。
在循环的每次迭代中,您将创建一个名为 predictions
:
的 np.array 变量
predictions = rfc.predict(X_test)
所以如果你想把每个模型保存在一个变量中,比如字典,你需要在循环外用不同的名称声明它:
all_predictions = dict()
然后修改第二个示例中的代码应该可行:
names=['aa','bb','cc']
for h,n in (zip(dfs,names)):
...
all_predictions[n] = rfc.predict(X_test)
我在 roder 中使用 scikit 学习 运行 我拥有的 3 个不同数据帧 (aa、bb、cc) 上的随机森林模型。 为此,我使用 for 循环并为每个模型生成混淆矩阵。 问题是我想保存每个模型的预测,以便以后可以将其用于 ROC 分析。
这是循环的原始脚本:
todrop=['name','code','date','nitrogen','Hour','growth_day']
col='test'
dfs=[aa,bb,cc]
for h in dfs:
h.drop(todrop,axis=1,inplace=True)
y=h[col]
X=h.drop(col,axis=1)
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,stratify=y,random_state=42)
rfc = RandomForestClassifier()
rfc.fit(X_train,y_train)
predictions = rfc.predict(X_test)
conf_mat = confusion_matrix(y_test, predictions)
df_conf_norm = conf_mat / conf_mat.sum(axis=0)
index = ['healthy','sick']
columns = ['healthy','sick']
cm_df = pd.DataFrame(df_conf_norm,columns,index)
seaborn.heatmap(cm_df,annot=True, annot_kws={"size": 15},linewidths=.5, cmap='YlOrRd')
plt.title('Random Forest', fontsize = 20) # title with fontsize 20
plt.xlabel('True Labels', fontsize = 17) # x-axis label with fontsize 15
plt.ylabel('Prediction', fontsize = 17) # y-axis label with fontsize 15
plt.show()
print('Accuracy'+'{}'.format(accuracy_score(y_test,predictions)))
我试过这样保存模型:
predictions[h] = rfc.predict(X_test)
但是我得到了错误:
IndexError: only integers, slices (
:
), ellipsis (...
), numpy.newaxis (None
) and integer or boolean arrays are valid indices
我也尝试过使用 zip 然后将其另存为名称:
names=['aa','bb','cc']
for h,n in (zip(dfs,names)):
...
predictions[n] = rfc.predict(X_test)
但出现同样的错误。
我的最终目标是保存(每个模型的)这些预测,以便最终创建 ROC 图。
在循环的每次迭代中,您将创建一个名为 predictions
:
predictions = rfc.predict(X_test)
所以如果你想把每个模型保存在一个变量中,比如字典,你需要在循环外用不同的名称声明它:
all_predictions = dict()
然后修改第二个示例中的代码应该可行:
names=['aa','bb','cc']
for h,n in (zip(dfs,names)):
...
all_predictions[n] = rfc.predict(X_test)