python代码模型模拟结果评估与展示:除了R2和mse之外,还应给出散点图(x轴是 observation values, y轴是predicted values),并在图中标注1:1线,回归方程拟 合线、MSE、R2和N(散点图中的样本个数)
时间: 2024-02-27 07:57:39 浏览: 99
好的,以下是一个Python代码示例,展示如何利用matplotlib库绘制散点图并标注1:1线、回归方程拟合线、MSE、R2和N。假设模型的预测结果为y_pred,观察值为y_true:
```python
import matplotlib.pyplot as plt
from sklearn.metrics import r2_score, mean_squared_error
# 计算R2和MSE
r2 = r2_score(y_true, y_pred)
mse = mean_squared_error(y_true, y_pred)
# 绘制散点图并标注1:1线
fig, ax = plt.subplots()
ax.scatter(y_true, y_pred)
ax.plot([y_true.min(), y_true.max()], [y_true.min(), y_true.max()], 'k--', lw=4)
ax.set_xlabel('Observation Values')
ax.set_ylabel('Predicted Values')
ax.set_title('Scatter plot of predicted vs. observation values')
# 绘制回归方程拟合线
coef = np.polyfit(y_true, y_pred, 1)
poly1d_fn = np.poly1d(coef)
ax.plot(y_true, poly1d_fn(y_true), 'r-')
ax.text(0.05, 0.9, f'Y = {round(coef[0], 2)} * X + {round(coef[1], 2)}', transform=ax.transAxes)
# 在图中标注MSE、R2和N
ax.text(0.05, 0.8, f'MSE = {round(mse, 2)}', transform=ax.transAxes)
ax.text(0.05, 0.75, f'R2 = {round(r2, 2)}', transform=ax.transAxes)
ax.text(0.05, 0.7, f'N = {len(y_true)}', transform=ax.transAxes)
plt.show()
```
这段代码会绘制一个散点图,并在图中标注1:1线、回归方程拟合线、MSE、R2和N。您可以根据实际情况调整标注的位置和格式。
阅读全文