【发布时间】:2020-07-20 13:14:31
【问题描述】:
我试图预测第二天股价会上涨还是下跌。我正在使用具有 43 列的 pandas DataFrame,其中一列是 y 值,y 值是 0 到 1 之间的浮点数,我的 DataFrame 也有 5016 行以数字为索引。我用一些 LSTM 单元制作了一个模型,其中一些 Dense 单元的损失函数为 biary_crossentropy,但是当我运行模型并尝试打印预测时,所有预测都是相同的:
[[0.56393844]
[0.56393844]
[0.56393844]
...
[0.56393844]
[0.56393844]
[0.56393844]]
y 值并不完全相同。 Y 值:
0
1
0
1
1
损失和准确率也开始相同:
7/10 纪元
4012/4012 [==============================] - 20 秒 5 毫秒/样本 - 损失:0.7052 - 加速度: 0.5015 - val_loss:
0.6884 - val_acc: 0.5488
8/10 纪元
4012/4012 [==============================] - 19 秒 5 毫秒/样本 - 损失:0.7054 - 加速度: 0.4980 - val_loss:
0.6907 - val_acc:0.5488
9/10 纪元
4012/4012 [==============================] - 18 秒 5 毫秒/样本 - 损失:0.7078 - 加速度: 0.4890 - val_loss:
0.6894 - val_acc: 0.5488
我的代码如下所示:
df = pd.read_csv("^AEX.csv")
df.index = pd.to_numeric(df.index, errors = 'coerce')
df = df.drop(['date'], axis = 1)
print(df.shape)
x = df.loc[:, df.columns != 'result']
y = df.loc[:, df.columns == 'result']
y = y.astype(int)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, shuffle = False)
x_train = x_train.values.reshape(x_train.shape[0], 1, x_train.shape[1])
x_test = x_test.values.reshape(x_test.shape[0], 1, x_test.shape[1])
model = Sequential()
model.add(LSTM(42, input_shape = (1, 42), activation = 'relu', return_sequences = True))
model.add(Dropout(0.2))
model.add(LSTM(42, activation = 'relu'))
model.add(Dropout(0.2))
model.add(Dense(32, activation = 'relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation = 'sigmoid'))
opt = tf.keras.optimizers.Adam(lr = 0.1, decay=1e-7)
model.compile(loss = 'binary_crossentropy', optimizer = opt, metrics = ['accuracy'])
model.fit(x_train, y_train, epochs = 10, batch_size = 1, validation_data = (x_test, y_test))
prediction = model.predict(x_test)
print(prediction)
print(x_test)
print(y_test)
我对神经网络了解不多,所以不知道哪些层最有效,也不知道应该做多少层。如果有人知道我该如何解决这个问题,以便我的神经网络真正学习,请告诉我。我知道如何使我的网络更高效或更好,也请告诉我。提前致谢。
【问题讨论】:
标签: python pandas tensorflow keras lstm