首页 > 解决方案 > 我如何在 python 中绘制训练结果

问题描述

我对python相当陌生,我正在尝试在图表中绘制训练集结果和测试集结果

此图显示了比较 y_test 和 y_predicted 的结果。我用下面的代码来绘制这个

fig, ax = plt.subplots(figsize=(10,5))
ax.plot(range(len(y_test)), y_test, '-b',label='Actual')
ax.plot(range(len(y_pred)), y_pred, 'r', label='Predicted')
plt.show()

现在我想为我的训练数据提供完全相同的图表。我如何生成这个?

标签: pythonpandasmatplotlib

解决方案


示例:使用随机森林

clf = RandomForestClassifier(max_depth=5,random_state=0)
clf.fit(train_x,train_y)
pred_random = clf.predict(test_x)
pred_random2 = clf.predict(train_x)

用于绘制测试图

plt.figure(figsize=(6, 10))
ax1 = sns.distplot(test_y, hist=False, color="r", label="Actual Value")
sns.distplot(pred_random, hist=False, color="b", label="Fitted Values" , ax=ax1)
plt.title('DIST PLOT Random Forest')
plt.xlabel('')
plt.ylabel('')
plt.show()
plt.close()

用于绘制火车图

plt.figure(figsize=(6, 10))
ax1 = sns.distplot(train_y, hist=False, color="r", label="Actual Value")
sns.distplot(pred_random2, hist=False, color="b", label="Fitted Values" , ax=ax1)
plt.title('DIST PLOT Random Forest')
plt.xlabel('')
plt.ylabel('')
plt.show()
plt.close()

推荐阅读