python - 是否可以在多输入神经网络中使用 sklearn 中的 StratifiedKFold?

标签 python scikit-learn neural-network keras cross-validation

我有一个数据集,可以以 python 字典列表 的形式传递给多输入神经网络:

示例:

#dict
{'input1': X1, 'input2': X2, 'input3': X3}, {'output': Y}
#list
[ X1, X2, X3], y

现在我想使用K 折交叉验证来估计模型的性能。您认为我可以像这个单输入示例一样使用 sklearn 中的 StratifiedKFold 吗?

for train, test in kfold.split(X, Y):
  # create model
    model = Sequential()
    model.add(Dense(12, input_dim=8, activation='relu'))
    model.add(Dense(8, activation='relu'))
    model.add(Dense(1, activation='sigmoid'))
    # Compile model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    # Fit the model
    model.fit(X[train], Y[train], epochs=150, batch_size=10, verbose=0)
    # evaluate the model
    scores = model.evaluate(X[test], Y[test], verbose=0)
    print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
    cvscores.append(scores[1] * 100)

否则,最好的方法是什么?

最佳答案

skf.split() 返回索引,它仅取决于 Y:

for train_index, test_index in skf.split(X, y):
...    print("TRAIN:", train_index, "TEST:", test_index)
...    X_train, X_test = X[train_index], X[test_index]
...    y_train, y_test = y[train_index], y[test_index]

因此,您可以在此处传递任何 X 数组(甚至将第 1 层的 X1 转换为数据帧或合成 X)。然后你就拿走你的

train_index

test_index

并过滤您的所有输入。

同样,skf() 仅取决于您的 Y。所以目标是通过适当的 Y 并获得索引。

另一种方式:将输出中的所有输入合并到一个数据帧中,并保留每一层的列名称。在本例中,您有一个“大”X。首先将其拆分为 train_index 和 test_index,然后使用您在上面保存的列名称拆分为 X1、X2 和 X3。

关于python - 是否可以在多输入神经网络中使用 sklearn 中的 StratifiedKFold?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52978434/

相关文章:

python - 机器学习电子邮件优先级 - Python

python - 神经网络 - 输入标准化

javascript - 如何正确训练我的神经网络

python - CPython - 打印出所有不可变对象(immutable对象)

python - 在 sklearn.cross_validation 中使用 train_test_split 和 cross_val_score 的区别

python - 在python中从日期时间转换为日期字符串

python-3.x - scikit-learn 的 VotingClassifier 中使用的分类器是什么?

machine-learning - 向后传播 : are analytical second derivatives worth calculating?

python - ctypes 类型的 from_buffer() 方法有何不同?

Python 脚本无法访问某些环境变量