使用np.reshape时元组索引超出范围错误

问题描述 投票:0回答:1

datasettrain都是785 numpy.ndarrays的列表。数据集和训练中的每个数组分别包含大约1300和950值

x_train1, y_train1 = [],[] #*
for n in range(len(df2)): #*
    scaler = MinMaxScaler(feature_range=(0, 1))
    scaled_data = scaler.fit_transform(dataset[n]) #*dataset instead of dataset[n]
    x_train, y_train = [], []
    for i in range(60,len(train[n])):              #*train instead of train[n]
        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)) #here is the error
    x_train1.append(x_train) #*
    y_train1.append(y_train) #*
print(len(x_train1))

这是错误tuple index out of range当数据集和训练是数组而不是数组列表(即数据集而不是数据集[n])时,此代码可以正常运行。在*标记的部分中没有for循环。

*我在此引用LSTM代码,但是此站点仅具有1个符号(公司)的代码,因此我创建了列表和循环。https://www.analyticsvidhya.com/blog/2018/10/predicting-stock-price-machine-learningnd-deep-learning-techniques-python/

python numpy reshape
1个回答
0
投票

明白了。发生这种情况是因为数据集[n]中有一个特定的数据集,其大小小于60。

© www.soinside.com 2019 - 2024. All rights reserved.