1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(14, 10))
# 実際の価格と予測価格
dates = y_test.index
ax1.plot(dates, y_test.values, label='Actual', color='black', linewidth=2)
ax1.plot(dates, lr_pred, label='Linear Regression', color='blue', alpha=0.7)
ax1.plot(dates, rf_pred, label='Random Forest', color='green', alpha=0.7)
ax1.set_title('Stock Price Prediction')
ax1.set_ylabel('Price (USD)')
ax1.legend()
ax1.grid(True, alpha=0.3)
# 予測誤差
lr_error = y_test.values - lr_pred
rf_error = y_test.values - rf_pred
ax2.plot(dates, lr_error, label='Linear Regression Error', color='blue', alpha=0.5)
ax2.plot(dates, rf_error, label='Random Forest Error', color='green', alpha=0.5)
ax2.axhline(y=0, color='red', linestyle='--', alpha=0.5)
ax2.fill_between(dates, lr_error, alpha=0.2, color='blue')
ax2.set_title('Prediction Error')
ax2.set_ylabel('Error (USD)')
ax2.set_xlabel('Date')
ax2.legend()
ax2.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig('stock_prediction.png', dpi=150)
plt.show()
|