【问题标题】:How to fix folds in sklearn?如何修复sklearn中的折叠?
【发布时间】:2018-03-10 01:13:39
【问题描述】:

我在几个预测任务中应用 CV,并且希望始终对我的每个参数集使用相同的折叠 - 如果可能的话,也可以在不同的 python 脚本中使用,因为性能确实取决于折叠。 我正在使用 sklearns KFold:

kf = KFold(n_splits=folds, shuffle=False, random_state=1986)

并通过

构建我的折叠
for idx_split, (train_index, test_index) in enumerate(kf.split(X, Y)):
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = Y[train_index], Y[test_index]

然后像这样循环遍历它们

for idx_alpha, alpha in enumerate([0, 0.2, 0.4, 0.6, 0.8, 1]):
    # [...]
    for idx_split, (train_index, test_index) in enumerate(kf.split(X, Y)):
        X_train, X_test = X[train_index], X[test_index]
        y_train, y_test = Y[train_index], Y[test_index]**

虽然我选择了一个 random_state 并设置了一个 numpy 种子,但折叠并不总是相等。我可以做些什么来实现这一点,并可能通过几个 python 脚本共享我的折叠?

【问题讨论】:

    标签: python scikit-learn


    【解决方案1】:

    你似乎在重塑 GridSearchCV ;-)

    试试这个方法:

    from sklearn.model_selection import GridSearchCV
    
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
    
    param_grid = dict(model__alpha=[0, 0.2, 0.4, 0.6, 0.8, 1])
    
    model = Lasso()  # put here algorithm, that you want to use
    
    folds = 3
    # alternatively you can prepare folds yourself
    #folds = KFold(n_splits=folds, shuffle=False, random_state=1986)
    grid_search = GridSearchCV(model, param_grid=param_grid, cv=folds, n_jobs=-1, verbose=2)
    grid_search.fit(X_train, y_train)
    
    y_pred = grid_search.best_estimator_.predict(X_test)
    

    【讨论】:

    • 谢谢! GridSearchCV 是否总是对所有参数组合提供相同的拆分?
    • 你有什么想法在不同的python脚本中提供相同的分割吗?
    • @N8_Coder,你能解释一下吗?为什么要在不同的脚本中使用它?
    • 模型的性能很大程度上取决于我拆分的数据。因此,我想在任何地方都使用相同的拆分。
    • @N8_Coder,好吧,你没有解释为什么你不能在同一个脚本中使用它们(为什么你需要多个脚本)......但是,无论如何,你可能想尝试将拆分保存为列表或字典,并使用 sklearn.externals.joblib... 将其保存到磁盘
    猜你喜欢
    • 2021-02-19
    • 2021-05-09
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 2022-07-05
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多