为什么会这样?解决方法是什么?我曾尝试过在发布前进行搜索,但是我找不到运气在我的代码中为什么会发生这种情况。如果有人可以看的话,那就太好了。
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dropout
from sklearn import preprocessing
from sklearn.preprocessing import MinMaxScaler
dataset = pd.read_csv("msft.us.txt").fillna(0)
le = preprocessing.LabelEncoder()
dataset['Date'] = le.fit_transform(dataset['Date'])
train = dataset[:6386]
valid = dataset[6386:]
#converting dataset into x_train and y_train
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(dataset)
x_train = []
y_train = []
for i in range(60,len(train)):
x_train.append(scaled_data[i-60:i,0])
y_train.append(scaled_data[i,0])
x_train, y_train = np.array(x_train), np.array(y_train)
x_train = np.reshape(x_train, (x_train.shape[0],x_train.shape[1],1))
model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(x_train.shape[1],1)))
model.add(LSTM(units=50))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(x_train, y_train, epochs=1, batch_size=16)
inputs = dataset[len(dataset) - len(valid) - 60:].values
inputs = inputs.reshape(-1,1)
inputs = scaler.transform(inputs)
X_test = []
for i in range(60,inputs.shape[0]):
X_test.append(inputs[i-60:i,0])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0],X_test.shape[1],1))
closing_price = model.predict(X_test)
closing_price = scaler.inverse_transform(closing_price)
dataset['Date'] = le.inverse_transform(dataset['Date'])
valid['Predictions'] = closing_price
plt.plot(train['Close'])
plt.plot(valid[['Close','Predictions']])
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Anthony/Desktop/Machine Learning/Machine Learning Final Project/RNN for Microsft Stock.py", line 39, in <module>
inputs = scaler.transform(inputs)
File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\preprocessing\data.py", line 389, in transform
X *= self.scale_
ValueError: non-broadcastable output operand with shape (11599,1) doesn't match the broadcast shape (11599,7)
上面的文本显示了我收到的输出错误。我认为这与我重塑数据的方式有关。我正在尝试让LSTM预测数据,但是在重塑数据时遇到了麻烦。
答案 0 :(得分:0)
在这里,当您尝试创建具有60个时间戳和1个输出的数据结构时,在for循环中犯了一个错误。在这里缩放数据集,然后将其命名为scaled_data。所以
x_train = []
y_train = []
for i in range(60,len(scaled_data)):
x_train.append(scaled_data[i-60:i,0])
y_train.append(scaled_data[i,0])
x_train, y_train = np.array(x_train), np.array(y_train)
x_train = np.reshape(x_train, (x_train.shape[0],x_train.shape[1],1))