重塑nparray返回"IndexError:元组索引超出范围". [英] reshaping of an nparray returned "IndexError: tuple index out of range"
问题描述
重塑nparray返回"IndexError:元组索引超出范围"
reshaping of an nparray returned "IndexError: tuple index out of range"
跟随" https://machinelearningmastery. com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/我已经从csv文件中制作了一个数据框.然后将这些值放入nparray数据集".缩放数据集,然后分为训练集和测试集.用值及其1个滞后值组成两列(trainX,trainY).然后尝试重塑trainX.
Following "https://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/" I have made a dataframe from the csv file. Then taken those values into an nparray "dataset". Scaled the dataset then divided into train and test set. Made two columns (trainX, trainY) with the values and its 1 lagged vales. Then tried to reshape trainX.
dataset = passenger_data.values
dataset = dataset.astype('float32')
scale = MinMaxScaler(feature_range=(0,1))
dataset = scale.fit_transform(dataset)
train, test = dataset[0:train_size, :], dataset[train_size:len(dataset), :]
train_size = int(len(dataset) * 0.70)
train, test = dataset[0:train_size, :], dataset[train_size:len(dataset), :]
def create_coloumns(dataset, lag = 1):
colX, colY = [], []
for i in range(len(dataset) - lag):
a = dataset[i,0]
colX.append(a)
for j in range(lag, len(dataset)):
b = dataset[j,0]
colY.append(b)
return np.array(colX), np.array(colY)
trainX, trainY = create_coloumns(train, 1)
testX, testY = create_coloumns(test, 1)
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-62-96b89321dd69> in <module>
1 # trainX.shape
----> 2 trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
3 # testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
IndexError: tuple index out of range
推荐答案
与Matlab不同,numpy数组可以是一维的,因此shape参数中只有一个值.
Unlike in Matlab, numpy arrays can be one-dimensional, so there is only one value from shape parameter.
a = np.array([1,2,3,4])
a.shape[0] # ok
a.shape[1] # error
这篇关于重塑nparray返回"IndexError:元组索引超出范围".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!