LSTM ValueError:无法重塑测试数据的大小数组

时间:2019-01-14 17:19:01

标签: python numpy sequence lstm prediction

我正在尝试一个简单的LSTM实验。基本上,目标是预测最终阶段作为预测值。 这是我的数据。

 Pressure  Temperature      time (hr)  time (min)  strain value
0          7000              823   0.035442     2.12651             0.000681   
1          7000              823   0.051088     3.06527             0.000786   
2          7000              823   0.079250     4.75503             0.000915   

   final stage  
0            1  
1            1  
2            1 

所以基本上我首先从excel读取数据,然后我对每个变量进行绘图,我需要预测的是由4 different values(1,2,3 or 4).

组成的最后阶段

我尝试了以下代码,但在此我不断出错 错误指向此行X_tr_t = X_train.reshape(30,1, X_train.shape[1])

ValueError: cannot reshape array of size 174 into shape (30,1,6)

我的数据总行数是50,不包括顶部的标题。所以我不知道为什么当数据应为30 * 6 = 180时为什么会继续显示174?

import numpy as np
from pandas import read_excel, DataFrame, concat
from matplotlib import pyplot
from sklearn.preprocessing import MinMaxScaler
from keras import optimizers
from keras.models import Sequential
from keras.layers import LSTM, Dense, Activation
from keras.callbacks import EarlyStopping

# Loading the raw dataset 
dataset = read_excel("set1Excel.xlsx", usecols="A:F")

values = dataset.values

print(dataset.head(3)) # Lets print the first 3 rows of the dataset to take a peek at what we have

Nc = values.shape[1] # number of columns

values = values.astype('float32') # ensuring all the data is float

i = 0
pyplot.figure()
for group in range(0,Nc):
    i += 1
    pyplot.subplot(Nc, 1, i)
    pyplot.plot(values[:, group])
    pyplot.title(dataset.columns[group], y=0.5, loc='right')
pyplot.show()

n_train = int( values.shape[0] * 0.60)
balance = int( values.shape[0] * 0.40)

#print("N train: %d" % X_train.shape[1])
print("n_train: %d" % n_train)
print("balance: %d" % balance)
# inputs
train_X = values[:n_train, :]
test_X  = values[balance:, :]

sc = MinMaxScaler()
train_sc = sc.fit_transform(train_X)
test_sc = sc.transform(test_X)

X_train = train_sc[:-1]
y_train = train_sc[6]

X_test = test_sc[:-1]
y_test = test_sc[6]


#print("X train shape".X_train.shape[0])
X_tr_t = X_train.reshape(30,1, X_train.shape[1])
X_tst_t = X_test.reshape(20, 1, X_test.shape[1])

from keras.layers import LSTM

model_lstm = Sequential()
#model_lstm.add(LSTM(7, input_shape=(1, ), activation='relu', kernel_initializer='lecun_uniform', return_sequences=False))
#model_lstm.add(Dense(1))
#model_lstm.compile(loss='mean_squared_error', optimizer='adam')
#early_stop = EarlyStopping(monitor='loss', patience=5, verbose=1)
#history_model_lstm = model_lstm.fit(X_tr_t, y_train, epochs=200, batch_size=1, verbose=1, shuffle=False, callbacks=[early_stop])

#y_pred_test_lstm = model_lstm.predict(X_tst_t)
#y_train_pred_lstm = model_lstm.predict(X_tr_t)
#print("The R2 score on the Train set is:\t{:0.3f}".format(r2_score(y_train, y_train_pred_lstm)))
#r2_train = r2_score(y_train, y_train_pred_lstm)
#print("The Adjusted R2 score on the Train set is:\t{:0.3f}\n".format(adj_r2_score(r2_train, X_train.shape[0], X_train.shape[1])))
#print("The R2 score on the Test set is:\t{:0.3f}".format(r2_score(y_test, y_pred_test_lstm)))
#r2_test = r2_score(y_test, y_pred_test_lstm)
#print("The Adjusted R2 score on the Test set is:\t{:0.3f}".format(adj_r2_score(r2_test, X_test.shape[0], X_test.shape[1])))

0 个答案:

没有答案