如何在python中的LSTM中使用交叉验证

时间:2019-08-29 05:25:11

标签: python machine-learning keras classification lstm

我大约有1000 nodes个数据集,其中每个节点都有4 time-series。每个时间序列正好是6个长度。标签为01

更准确地说,我的数据集如下所示。

node, time-series1, time_series2, time_series_3, time_series4, Label
n1, [1.2, 2.5, 3.7, 4.2, 5.6, 8.8], [6.2, 5.5, 4.7, 3.2, 2.6, 1.8], …, 1
n2, [5.2, 4.5, 3.7, 2.2, 1.6, 0.8], [8.2, 7.5, 6.7, 5.2, 4.6, 1.8], …, 0
and so on.

我想使用LSTM模型进行预测。我是深度学习的新手。因此,我使用最基本的LSTM模型,如下所示,使用了keras。

model = Sequential()
model.add(LSTM(10, input_shape=(6,4)))
model.add(Dense(32))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

print(data.shape) # (1000, 6, 4)
model.fit(data, target)

但是,由于我的数据集很小,我想对我的LSTM模型尝试10-fold cross validation并获得每个label 1的{​​{1}}的预测概率,并将预测概率按降序排列订购。换句话说,我期望输出如下。

node

我能够使用sorted_probability_of_class_1 = [[n888, 1], [n512, 1], ........, [n99, 0.9], [n112, 0.8], ........, [n882, 0], [n9, 0]] 中的randomforest分类器完成类似的任务。我的代码使用具有150个数据点的sklearn数据集如下。

iris

我的输出如下。

from sklearn import datasets

iris = datasets.load_iris()
iris_names = ['iris_0', 'iris_1', 'iris_2', 'iris_3', 'iris_4', 'iris_5', 'iris_6', 'iris_7', 'iris_8', 'iris_9', 'iris_10', 'iris_11', 'iris_12', 'iris_13', 'iris_14', 'iris_15', 'iris_16', 'iris_17', 'iris_18', 'iris_19', 'iris_20', 'iris_21', 'iris_22', 'iris_23', 'iris_24', 'iris_25', 'iris_26', 'iris_27', 'iris_28', 'iris_29', 'iris_30', 'iris_31', 'iris_32', 'iris_33', 'iris_34', 'iris_35', 'iris_36', 'iris_37', 'iris_38', 'iris_39', 'iris_40', 'iris_41', 'iris_42', 'iris_43', 'iris_44', 'iris_45', 'iris_46', 'iris_47', 'iris_48', 'iris_49', 'iris_50', 'iris_51', 'iris_52', 'iris_53', 'iris_54', 'iris_55', 'iris_56', 'iris_57', 'iris_58', 'iris_59', 'iris_60', 'iris_61', 'iris_62', 'iris_63', 'iris_64', 'iris_65', 'iris_66', 'iris_67', 'iris_68', 'iris_69', 'iris_70', 'iris_71', 'iris_72', 'iris_73', 'iris_74', 'iris_75', 'iris_76', 'iris_77', 'iris_78', 'iris_79', 'iris_80', 'iris_81', 'iris_82', 'iris_83', 'iris_84', 'iris_85', 'iris_86', 'iris_87', 'iris_88', 'iris_89', 'iris_90', 'iris_91', 'iris_92', 'iris_93', 'iris_94', 'iris_95', 'iris_96', 'iris_97', 'iris_98', 'iris_99', 'iris_100', 'iris_101', 'iris_102', 'iris_103', 'iris_104', 'iris_105', 'iris_106', 'iris_107', 'iris_108', 'iris_109', 'iris_110', 'iris_111', 'iris_112', 'iris_113', 'iris_114', 'iris_115', 'iris_116', 'iris_117', 'iris_118', 'iris_119', 'iris_120', 'iris_121', 'iris_122', 'iris_123', 'iris_124', 'iris_125', 'iris_126', 'iris_127', 'iris_128', 'iris_129', 'iris_130', 'iris_131', 'iris_132', 'iris_133', 'iris_134', 'iris_135', 'iris_136', 'iris_137', 'iris_138', 'iris_139', 'iris_140', 'iris_141', 'iris_142', 'iris_143', 'iris_144', 'iris_145', 'iris_146', 'iris_147', 'iris_148', 'iris_149']

X = iris.data
y = iris.target

clf=RandomForestClassifier(n_estimators=10, random_state = 42, class_weight="balanced")

k_fold = StratifiedKFold(n_splits=10, shuffle=True, random_state=0)
proba = cross_val_predict(clf, X, y, cv=k_fold, method='predict_proba')

#print probability of class 1
print(proba[:,1])

这就是我在上面的代码中如何组织1类的排序概率。

[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.1 0.  0.  0.
 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
 0.2 0.  0.  0.  0.  0.1 0.  0.  0.  0.  0.  0.  0.  0.  0.9 1.  0.7 1.
 1.  1.  1.  0.7 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  0.9 0.9 0.1 1.
 0.6 1.  1.  1.  0.9 0.  1.  1.  1.  1.  1.  0.4 0.9 0.9 1.  1.  1.  0.9
 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  0.  0.  0.  0.  0.  0.  0.9 0.
 0.1 0.  0.  0.  0.  0.  0.  0.  0.1 0.  0.  0.8 0.  0.1 0.  0.1 0.  0.1
 0.3 0.2 0.  0.6 0.  0.  0.  0.6 0.4 0.  0.  0.  0.8 0.  0.  0.  0.  0.
 0.  0.  0.  0.  0.  0. ]

但是,我不清楚如何对上述LSTM模型执行相同的过程。

很高兴在需要时提供更多详细信息。

0 个答案:

没有答案