【发布时间】:2021-07-20 03:04:42
【问题描述】:
我是 LSTM 新手,我正在学习股票价格预测教程。
我正在尝试执行所有操作,但一直收到错误消息:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-21-87c8b43ab572> in <module>
36 #Get the models predicted price values
37 predicted_prices= model.predict(x_test)
---> 38 predicted_prices=scaler.inverse_transform(predicted_prices)
~\anaconda3\lib\site-packages\sklearn\preprocessing\_data.py in inverse_transform(self, X)
430 force_all_finite="allow-nan")
431
--> 432 X -= self.min_
433 X /= self.scale_
434 return X
ValueError: non-broadcastable output operand with shape (401,1) doesn't match the broadcast shape (401,6)
我正在寻找这个错误的解释,这里是代码
company = "AC.TO"
start= dt.datetime(2012,1,1)
end= dt.datetime(2020,1,1)
data = web.DataReader(company, "yahoo", start,end)
data_df= data.filter(['Close'])
dataset= data.values
training_data_len= math.ceil(len(dataset)*.8)
scaler = MinMaxScaler(feature_range=(0,1))
scaled_data = scaler.fit_transform(dataset)
train_data=scaled_data[0:training_data_len, :]
predication_days=60
x_train=[]
y_train=[]
for x in range (predication_days,len(train_data)):
x_train.append(train_data[x-predication_days:x, 0])
y_train.append(train_data[x, 0])
if x<= predication_days+1:
print (x_train)
print (y_train)
print()
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,return_sequences=False))
model.add(Dense(units=25))
model.add(Dense(units=1))
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(x_train, y_train, epochs=1, batch_size=1)
test_data = scaled_data[training_data_len - predication_days:,:]
x_test=[]
y_test=dataset[training_data_len:, :]
for x in range(predication_days,len(test_data)):
x_test.append(test_data[x-predication_days:x,0])
x_test=np.array(x_test)
x_test=np.reshape(x_test,(x_test.shape[0],x_test.shape[1],1))
predicted_prices= model.predict(x_test)
predicted_prices=scaler.inverse_transform(predicted_prices)
我的问题更多是关于这个错误的原因,从中吸取教训以及如何解决它?
【问题讨论】:
标签: python tensorflow neural-network lstm