我目前正在使用LSTM神经网络来分析股票数据的收盘价。我收到一条错误消息:
TypeError: Error converting shape to a TensorShape: only size-1 arrays can be converted to Python scalars.
执行此代码时:
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
#take user input to replace stock to analyze
userInput = input('Stocks: \n')
days = int(input('how many time series to analyze? \n'))
#read data
data = pd.read_csv('Stocks/%s.us.txt' % userInput, usecols=[5], engine = 'python', skipfooter = 3)
#convert it into numpy array
dataSet = data.values
#scale the data into a range of 0, 1
scaling = MinMaxScaler(feature_range = (0,1))
#transform testing data to have 0 mean
dataSet = scaling.fit_transform(dataSet)
#splitting to testing and training data
trainData = int(len(dataSet) * 0.80)
testData = len(dataSet) - trainData
#time series data shaping
features = []
labels = []
for i in range(days, len(dataSet)):
features.append(dataSet[i-days:i, 0])
labels.append(dataSet[i, 0])
#convert features and labels to numpy arrays
features, labels = np.array(features), np.array(labels)
#building LSTM model
model = Sequential()
model.add(LSTM(1, input_shape=(1, dataSet)))
model.add(Dense(1))
model.compile(optimizer = 'adam', loss = 'mse')
model.fit(trainData, testData, batch_size = 32, epochs = 10)
在本文here中,它解释了此问题可能是由于索引不同维度的行而不是长度而引起的,但是我看不到它如何适用于我的代码,因为我从未索引过任何内容分类。我曾尝试过寻找其他职位,但没有一种解决方案适用于我的问题。 同样值得注意的是,错误可以追溯到Anaconda3 lib文件的第143行,就像这样
File "C:\Users\me\Anaconda3\lib\site-packages\tensorflow\python\eager\execute.py", line 143, in make_shape
为什么会出现此错误,我该如何解决?