CatBoost文档表示,randomized_search方法可以通过cv参数接受训练和测试拆分,而不是定义交叉验证方法。要做到这一点,应规定:
是一种可迭代的生成序列,测试分裂作为索引数组。
我们如何定义这个对象?
作为一个坏例子,假设我的功能数据集有10行。我希望使用前5行进行培训,最后5行用于验证/测试。
我提取索引值。
train_index = X[0:5].index
test_index = X[5:10].index我向randomized_search方法提供索引
a_search = model.randomized_search(param_distributions=params,
X = X,
y = y,
n_iter=5,
cv={train_index,test_index})我在cv={train_index,test_index}中提供的这个集合是不可能的,因为它是不可迭代的,但是我不知道这样的迭代应该如何。我只想定义X和y的哪些行用于培训,哪些行用于测试。目标是通过免除交叉验证和使用专用验证数据集来加快培训。
发布于 2022-06-25 05:40:25
您可以使用一个元组列表:
cv=[(train_index, test_index)]示例:
import pandas as pd
import numpy as np
from catboost import CatBoost
# generate the data
data = np.random.normal(loc=0, scale=1, size=(10, 3))
labels = np.mean(a=data, axis=1)
df = pd.DataFrame(
data=np.hstack([data, np.expand_dims(labels, axis=1)]),
columns=['x1', 'x2', 'x3', 'y']
)
# split the data
train_index = df.index[:5]
test_index = df.index[5:]
# instantiate the model
model = CatBoost()
# tune the model
results = model.randomized_search(
param_distributions={
'depth': [2, 3, 4],
'iterations': [5, 6, 7]
},
X=df[['x1', 'x2', 'x3']],
y=df['y'],
cv=[(train_index, test_index)],
)https://stackoverflow.com/questions/72747256
复制相似问题