Google Colaboratory にて、株のS&P500種指数データを予想する。コードを練習中なのですが、エラー内容が24の範囲外なのでエラーです。的なことが出てるのですが、多分以下のコードらへんからおかしいと思われるのですが、どこのコードの数字を変更すれば良いのか。わからないため、ご教授お願いいたします。
for epoch in range(epochs): print() print(f'Epoch: {epoch+1}') run_train() extending_seq = train_seq[-test_size:].tolist() run_test() plt.figure(figsize=(12, 4)) plt.xlim(-20, len(y)+20) plt.grid(True) plt.plot(y.numpy()) plt.plot( range(len(y)-test_size, len(y)), extending_seq[-test_size:] ) plt.show()
↓
plt.plot(train_losses)
↓
plt.plot(test_losses)
↓
predicted_normalized_labels_list = extending_seq[-test_size:]
↓
predicted_normalized_labels_array_1d = np.array(predicted_normalized_labels_list) predicted_normalized_labels_array_1d
↓
predicted_normalized_labels_array_2d = predicted_normalized_labels_array_1d.reshape(-1, 1) predicted_normalized_labels_array_2d
↓
predicted_labels_array_2d = scaler.inverse_transform(predicted_normalized_labels_array_2d) predicted_labels_array_2d
↓
len(predicted_labels_array_2d)
↓
stock_data["Adj Close"][-test_size:]
↓
len(stock_data["Adj Close"][-test_size:])
↓
stock_data.index
↓
x_2018_10_to_2020_09 = np.arange('2018-10', '2020-10', dtype='datetime64[M]') x_2018_10_to_2020_09
↓
len(x_2018_10_to_2020_09)
↓
fig = plt.figure(figsize=(12, 4)) plt.title('S$P500 prediction with test data') plt.ylabel('Price') plt.grid(True) plt.autoscale(axis='x', tight=True) fig.autofmt_xdate() plt.plot(stock_data["Adj Close"]['2016-01':]) plt.plot(x_2018_10_to_2020_09, predicted_labels_array_2d) plt.show()
↓
stock_data["Adj Close"]['2018-10':]
↓
len(stock_data["Adj Close"]['2018-10':])
↓
real_labels_array_1d = stock_data["Adj Close"]['2018-10':].values real_labels_array_1d
↓
predicted_labels_array_2d
↓
predicted_labels_array_1d = predicted_labels_array_2d.flatten() predicted_labels_array_1d
↓
len(predicted_labels_array_1d)
↓
↓
python
1up_and_down_list = [] 2 3for i in range(len(real_labels_array_1d)): 4 difference = real_labels_array_1d[i] - predicted_labels_array_1d[i] 5 up_and_down_list.append(difference) 6 7up_and_down_list
↓
↓
↓
error
1--------------------------------------------------------------------------- 2IndexError Traceback (most recent call last) 3<ipython-input-45-65a12ea1ba52> in <module>() 4 2 5 3 for i in range(len(real_labels_array_1d)): 6----> 4 difference = real_labels_array_1d[i] - predicted_labels_array_1d[i] 7 5 up_and_down_list.append(difference) 8 6 9 10IndexError: index 24 is out of bounds for axis 0 with size 24
回答1件
あなたの回答
tips
プレビュー