我正在尝试预测明天的中午温度。我从Google那里搜集了一个LSTM模型,因为它似乎更准确。这里的要点不是其预测的价值,而是能够重用模型并根据我希望将训练作为一项服务而将预测作为另一项服务进行未来的预测。
以下是训练模型的代码:
rcParams['figure.figsize'] = 20,10
scaler = MinMaxScaler(feature_range=(0, 1))
sql_conn = pyodbc.connect(connStr)
query = 'SELECT Date, Temp FROM temps ORDER BY [Date] ASC'
df = pd.read_sql(query, sql_conn)
print(df.head(3))
totalRows = df["Date"].count()
trainRows = int(totalRows * 0.70)
#creating dataframe
data = df.sort_index(ascending=True, axis=0)
new_data = pd.DataFrame(index=range(0,len(df)),columns=['Date', 'Temp'])
for i in range(0,len(data)):
new_data['Date'][i] = data['Date'][i]
new_data['Temp'][i] = data['Temp'][i]
#setting index
new_data.index = new_data.Date
new_data.drop('Date', axis=1, inplace=True)
#creating train and test sets
dataset = new_data.values
train = dataset[0:trainRows,:]
valid = dataset[trainRows:,:]
#converting dataset into x_train and y_train
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(dataset)
x_train, y_train = [], []
for i in range(60,len(train)):
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))
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(x_train.shape[1],1)))
model.add(LSTM(units=50))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(x_train, y_train, epochs=1, batch_size=1, verbose=2)
# serialize model to JSON
model_json = model.to_json()
with open("temp.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("temp.h5")
print("Saved model to disk")
#predicting 246 values, using past 60 from the train data
inputs = new_data[len(new_data) - len(valid) - 60:].values
inputs = inputs.reshape(-1,1)
inputs = scaler.transform(inputs)
X_test = []
for i in range(60,inputs.shape[0]):
X_test.append(inputs[i-60:i,0])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0],X_test.shape[1],1))
temp = model.predict(X_test)
temp = scaler.inverse_transform(temp)
rms = np.sqrt(np.mean(np.power((valid-temp),2)))
print("RMS = " + str(rms))
train = new_data[:trainRows]
valid = new_data[trainRows:]
valid['Predictions'] = temp
plt.plot(train['Temp'])
plt.plot(valid[['Temp','Predictions']])
plt.savefig('temp.png')
这将生成所需的模型文件和一个不错的png文件,以快速了解培训的进行情况。
预测部分应该非常简单:加载最后60个数据点,加载模型,进行预测。但是如何将已加载的熊猫转换为model.predict?
sql_conn = pyodbc.connect(connStr)
query = 'SELECT TOP(60) Date, Temp FROM Temps ORDER BY [Date] DESC'
df = pd.read_sql(query, sql_conn)
json_file = open("temp.json", "r")
loaded_json = json_file.read()
json_file.close()
model = model_from_json(loaded_json)
model.load_weights("temp.h5")
model.compile(loss='mean_squared_error', optimizer='adam')
# how do I get to temp = model.predict(?)
如果我尝试将Temp值简单地放入数组中并运行预测,则会得到一个数组:
x = []
for d in df['Temp']:
x.append(d)
prediction = model.predict(x)
错误:预计lstm_3_input具有3个维度,但数组的形状为(60,1)
我必须改变自己的价值观吗?