我正在关注Kaggle 上的内核,并发现了以下代码:
n_folds = 5
def rmsle_cv(model):
kf = KFold(n_folds, shuffle=True, random_state=42).get_n_splits(train.values)
rmse= np.sqrt(-cross_val_score(model, train.values, y_train, scoring="neg_mean_squared_error", cv = kf))
return(rmse)
Run Code Online (Sandbox Code Playgroud)
我了解 KFold 的目的和用途以及在 中使用的事实cross_val_score。我不明白的是为什么get_n_split使用。据我所知,它返回用于交叉验证的迭代次数,即在本例中返回值 5。当然对于这一行:
rmse= np.sqrt(-cross_val_score(model, train.values, y_train, scoring="neg_mean_squared_error", cv = kf))
Run Code Online (Sandbox Code Playgroud)
简历=5?这对我来说没有任何意义。get_n_splits如果它返回一个整数,为什么还需要使用?我认为KFold 返回一个类,而get_n_splits返回一个整数。
任何人都可以澄清我的理解吗?
我试图了解如何使用交叉验证功能sklearn.model_selection.KFold。如果我定义(就像在本教程中一样)
from sklearn.model_selection import KFold
kf = KFold(n_splits=5, shuffle=False, random_state=100)
Run Code Online (Sandbox Code Playgroud)
我明白了
ValueError: Setting a random_state has no effect since shuffle is False.
You should leave random_state to its default (None), or set shuffle=True.
Run Code Online (Sandbox Code Playgroud)
这个错误是什么意思以及为什么需要设置random_state=Noneor shuffle=True?
我正在使用 Keras 进行基于云的权重和偏差扫描。因此,首先我在 W&B 项目中创建一个新的 Sweep,其配置如下:
description: LSTM Model
method: random
metric:
goal: maximize
name: val_accuracy
name: LSTM-Sweep
parameters:
batch_size:
distribution: int_uniform
max: 128
min: 32
epochs:
distribution: constant
value: 200
node_size1:
distribution: categorical
values:
- 64
- 128
- 256
node_size2:
distribution: categorical
values:
- 64
- 128
- 256
node_size3:
distribution: categorical
values:
- 64
- 128
- 256
node_size4:
distribution: categorical
values:
- 64
- 128
- 256
node_size5:
distribution: categorical
values:
- 64
- 128
- …Run Code Online (Sandbox Code Playgroud) 正如标题所说,我想知道sklearnGroupKFold和GroupShuffleSplit.
两者都针对具有组 ID 的数据进行训练-测试分割,因此组不会在分割中分离。我检查了每个函数的一个训练/测试集,它们看起来都做了很好的分层,但如果有人可以确认所有分割都这样做,那就太好了。
我对两者进行了 10 次分割测试:
gss = GroupShuffleSplit(n_splits=10, train_size=0.8, random_state=42)
for train_idx, test_idx in gss.split(X,y,groups):
print("train:", train_idx, "test:", test_idx)
train: [ 1 2 3 4 5 11 12 13 14 15 16 17 19 20] test: [ 0 6 7 8 9 10 18]
train: [ 1 2 3 4 5 6 7 8 9 10 12 13 14 18 19 20] test: [ 0 11 15 16 17]
train: [ 0 1 3 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用神经网络中的 sklearn 和 Tensorflow 包来实现 KFold 验证。
我的代码看起来像这样。
def training(self):
n_split = 3
instances = self.instance
labels = self.labels
for train_index, test_index in KFold(n_split).split(instances):
x_train, x_test = instances[train_index], instances[test_index]
y_train, y_test = labels[train_index], labels[test_index]
model = self.mlp_model()
model.fit(x_train, y_train, epochs=20)
print('Model Evaluation', model.evaluate(x_test, y_test))
Run Code Online (Sandbox Code Playgroud)
不幸的是,我收到一个错误
raise KeyError(f"None of [{key}] are in the [{axis_name}]")
KeyError: "None of [Int64Index([160, 161, 162, 163, 164, 165, 166, 167, 168, 169,\n ...\n 468, 469, 470, 471, 472, 473, 474, 475, 476, 477],\n dtype='int64', length=318)] are …Run Code Online (Sandbox Code Playgroud)