如何把上述代码可视化
时间: 2023-08-03 21:07:28 浏览: 41
上述代码并没有涉及到数据可视化,但是我们可以对其进行一些修改,使其可以可视化训练和预测结果。例如,我们可以使用 matplotlib 库绘制训练集和测试集的散点图,以及绘制预测结果和真实结果的对比图。
具体来说,我们可以在 train 函数中添加以下代码,绘制训练集和测试集的散点图:
```
import matplotlib.pyplot as plt
plt.scatter(x_train, y_train, label='Training set')
plt.scatter(x_test, y_test, label='Testing set')
plt.xlabel('Year')
plt.ylabel('Value')
plt.legend()
plt.show()
```
在 pred 函数中添加以下代码,绘制预测结果和真实结果的对比图:
```
import matplotlib.pyplot as plt
plt.plot(x, y, label='True values')
plt.plot(x, pred, label='Predictions')
plt.xlabel('Year')
plt.ylabel('Value')
plt.legend()
plt.show()
```
在 main 函数中添加以下代码,根据命令行参数调用相应的可视化代码:
```
if method == 'train':
train(x_train, x_test, y_train, y_test)
plt.scatter(x_train, y_train, label='Training set')
plt.scatter(x_test, y_test, label='Testing set')
plt.xlabel('Year')
plt.ylabel('Value')
plt.legend()
plt.show()
elif method == 'test':
predict = pred(pred_year).round(2)
df = pd.DataFrame({
'预测年份': pred_year,
'预测结果(单位:万吨)': predict
})
print(df)
df.to_excel(save_pred_path, index=False)
plt.plot(x, y, label='True values')
plt.plot(pred_year, predict, 'o', label='Predictions')
plt.xlabel('Year')
plt.ylabel('Value')
plt.legend()
plt.show()
else:
print('wrong')
```
这些代码会在训练和预测过程中分别绘制散点图和对比图,以便更好地理解模型的表现和预测结果。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)