Мои данные содержат всего 30 строк. Я приложил скриншоты моих данных. Мне нужно предсказать 31-е значение с предыдущими 30 значениями в столбце pow в скриншоте данных
Код:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
# from keras.models import Sequential
# from keras.layers import Dense
# from keras.layers import LSTM
# from keras.layers import Dropout
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, LSTM, Dropout
data_training = pd.read_csv('G:\Agrima\Obj_Rec\power.csv')
data_training = data_training.iloc[:, 2:3].values
scaler = MinMaxScaler()
data_training = scaler.fit_transform(data_training)
print(data_training[0:10])
X_train = []
y_train = []
for i in range(10, data_training.shape[0]):
X_train.append(data_training[i-10:i])
y_train.append(data_training[i, 0])
X_train, y_train = np.array(X_train), np.array(y_train)
print(X_train.shape)
print(y_train.shape)
regressior = Sequential()
regressior.add(LSTM(units = 60, activation = 'relu', return_sequences = True, input_shape = (X_train.shape[0], 5)))
regressior.add(Dropout(0.2))
regressior.add(LSTM(units = 60, activation = 'relu', return_sequences = True))
regressior.add(Dropout(0.2))
regressior.add(LSTM(units = 80, activation = 'relu', return_sequences = True))
regressior.add(Dropout(0.2))
regressior.add(LSTM(units = 120, activation = 'relu'))
regressior.add(Dropout(0.2))
regressior.add(Dense(units = 1))
print(regressior.summary())
regressior.compile(optimizer='adam', loss = 'mean_squared_error')
regressior.fit(X_train, y_train, epochs=50, batch_size=32)
regressior.save_weights('my_model_weights.h5') #Dude this is imp.. Dont train each time.. Try googling how to load weight..or call me
#input new values to get new predicitons
new = 62 #this must be input of the last day data da --- like this it shld be : new = data_training[-1]
inputs = scaler.transform(new)
y_pred = regressior.predict(input)
print(y_pred)
# Visualising the results
plt.figure(figsize=(14,5))
plt.plot(X_train, color = 'red', label = 'Real Data')
plt.plot(y_pred, color = 'blue', label = 'Predicted Data')
plt.title('Power')
plt.xlabel('Time')
plt.ylabel('Power')
plt.legend()
plt.show()
Журнал ошибок:
ValueError: Ошибка при проверке ввода: ожидалось, что lstm_input будет иметь форму (20, 5), но получил массив с формой (10, 1)