【发布时间】:2021-12-29 09:06:57
【问题描述】:
我正在使用多元 LSTM 进行脓毒症预测。目标变量是 SepsisLabel。时间序列数据如下所示,其中每一行代表一个小时,有 5864 名患者(P_ID = 1 表示其 1 名患者数据):
| HR | O2Sat | Temp | SBP | DBP | Resp | Age | Gender | ICULOS | SepsisLabel | P_ID |
|---|---|---|---|---|---|---|---|---|---|---|
| 72.0 | 97.4 | 36.33 | 108.5 | 52.7 | 16.7 | 80 | 0 | 1 | 0 | 1 |
| 78.0 | 97.0 | 36.53 | 169.0 | 82.0 | 14.5 | 80 | 0 | 2 | 0 | 1 |
| 68.0 | 97.0 | 36.20 | 150.0 | 71.0 | 14.0 | 80 | 0 | 3 | 0 | 1 |
| 70.0 | 97.0 | 36.21 | 149.0 | 71.0 | 14.0 | 80 | 0 | 4 | 1 | 1 |
| 67.0 | 98.0 | 36.11 | 157.0 | 73.0 | 14.0 | 80 | 0 | 5 | 1 | 1 |
| 73.0 | 98.0 | 36.18 | 162.0 | 78.0 | 15.0 | 80 | 0 | 6 | 1 | 1 |
| 69.0 | 99.0 | 36.63 | 156.0 | 73.0 | 13.0 | 80 | 0 | 7 | 1 | 1 |
| 70.0 | 100.0 | 36.00 | 167.0 | 79.0 | 12.0 | 80 | 0 | 8 | 1 | 1 |
| 78.0 | 98.0 | 37.13 | 177.0 | 79.0 | 17.0 | 80 | 0 | 9 | 0 | 2 |
| 73.0 | 98.0 | 36.78 | 152.0 | 71.0 | 13.5 | 80 | 0 | 10 | 0 | 2 |
| 79.5 | 96.5 | 37.17 | 185.0 | 94.0 | 23.2 | 80 | 0 | 11 | 0 | 2 |
| 73.0 | 96.0 | 36.72 | 190.0 | 96.0 | 24.0 | 80 | 0 | 12 | 0 | 2 |
| 101.0 | 95.0 | 37.13 | 188.0 | 91.0 | 26.5 | 80 | 0 | 13 | 0 | 2 |
| 88.0 | 95.0 | 37.56 | 145.0 | 68.0 | 24.0 | 80 | 0 | 14 | 1 | 2 |
| 92.0 | 95.0 | 37.23 | 172.0 | 81.0 | 24.0 | 80 | 0 | 15 | 1 | 2 |
代码:
X = df.drop('SepsisLabel',axis=1)
y = df[['SepsisLabel']]
x_train, x_test, y_train, y_test = train_test_split(X,y,test_size=0.2,random_state=42)
# reshape input to be [samples, time steps, features]
x_train = np.array(x_train).reshape(x_train.shape[0], x_train.shape[1], 1)
x_test = np.array(x_test).reshape(x_test.shape[0], x_test.shape[1], 1)
# Model
model = Sequential()
model.add(LSTM(128, activation = 'relu', input_shape = (10,1), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(100, activation = 'relu'))
model.add(Dropout(0.2))
model.add(Dense(50,activation='relu'))
model.add(Dense(1,activation='sigmoid'))
model.summary()
model.compile(optimizer='adam', loss='mse', metrics = ['accuracy'])
loss_plot = PlotLossesKeras()
model.fit(x_train, y_train, epochs=15, batch_size=64, verbose=1, validation_split=0.2, shuffle=True, callbacks = [loss_plot])
accuracy
training (min: 0.899, max: 0.900, cur: 0.900)
validation (min: 0.900, max: 0.900, cur: 0.900)
Loss
training (min: 0.100, max: 0.101, cur: 0.100)
validation (min: 0.100, max: 0.100, cur: 0.100)
我最初运行了 500 个 epoch,但结果是一样的。在这里,我使用了 15 个 epoch。 如何改进模型并获得最佳结果?开放接受批评和建议。谢谢。
【问题讨论】:
-
我发现了几个问题。首先,您的数据形状
(10, 1)似乎是错误的。你有 10 个时间步,但只有一个特征?第二,错误的损失函数。SepsisLabel是一个二进制标签,因此您应该考虑交叉熵、精度或recell 等损失函数,而不是mse(均方误差)。如果它仍然无法正常工作,只需尝试安装一个密集的网络而不是 LSTM 即可开始。此外,较小的学习率可能会有所帮助。 -
具有时间步长和特征的数据应该是什么形状?
-
features应该是 10,因为您有 10 个特征:HR、O2Sat、...、PID。time steps应该是任何大于或等于 1 的数字,你应该确定它。你可能会看到这个使用 LSTM 预测时间序列的例子:tensorflow.org/tutorials/structured_data/…
标签: python pandas tensorflow keras lstm