接上回, 这次做了一个多元回归
这里贴一下代码
import numpy as npnp.random.seed(1337)from sklearn.model_selection import train_test_splitimport matplotlib.pyplot as pltimport kerasfrom keras.models import Sequentialfrom keras.layers import Activationfrom keras.layers import LSTMfrom keras.layers import Dropoutfrom keras.layers import Denseimport pandas as pd
datan = 1000# 真实参数ori_weights = [5, -4, 3, -2, 1]colsn = len(ori_weights)bias = -1ori = np.zeros((1, colsn))ori[0] = np.asarray(ori_weights)
# 列信息cols_name = [chr(65+i) for i in range(colsn)]X = np.zeros((colsn, datan))for i in range(colsn): X[i] = np.random.normal(1, 0.1, datan)# 真实YY = np.matmul(ori, X) + bias + np.random.normal(-0.1, 0.1, (datan, ))
# 数据预览df = pd.DataFrame(X.T, columns=cols_name)df['Y'] = df.apply(lambda row: np.matmul(ori, [row[k] for k in df.columns] )[0]+bias, axis=1)df['target'] = Y[0]df.head()
X_train, X_test, Y_train, Y_test = train_test_split(X.T, Y.T, test_size=0.33, random_state=42)
neurons = 128 activation_function = 'tanh' loss = 'mse' optimizer="adam" dropout = 0.01 batch_size = 12 epochs = 200
model = Sequential()model.add(LSTM(neurons, return_sequences=True, input_shape=(1, colsn), activation=activation_function))model.add(Dropout(dropout))model.add(LSTM(neurons, return_sequences=True, activation=activation_function))model.add(Dropout(dropout))model.add(LSTM(neurons, activation=activation_function))model.add(Dropout(dropout))model.add(Dense(output_dim=1, input_dim=1))
model.compile(loss=loss, optimizer=optimizer)
epochs = 2001for step in range(epochs): cost = model.train_on_batch(X_train[:, np.newaxis], Y_train) if step % 30 == 0: print(f'{step} train cost: ', cost)
# testprint('Testing ------------')cost = model.evaluate(X_test[:, np.newaxis], Y_test, batch_size=40)print('test cost:', cost)
# plotting the predictionY_pred = model.predict(X_test[:, np.newaxis])#sdf = pd.DataFrame({'test':list(Y_test.T[0]), 'pred':list(Y_pred.T[0])})sdf.sort_values(by='test', inplace=True)#plt.scatter(range(len(Y_test)), list(sdf.test))plt.plot(range(len(Y_test)), list(sdf.pred), 'r--')plt.show()