如何在RandomizedSearchCV中使用样本权重?

时间:2014-07-07 18:18:46

标签: python machine-learning scikit-learn cross-validation

我正在使用scikit learn中的python库,我想在使用RandomizedSearchCV进行交叉验证期间对每个样本进行加权。当我尝试这段代码时:

search = RandomizedSearchCV(estimator, param_distributions,
                                    n_iter=args.iterations,
                                    scoring=mae_scorer, n_jobs=8, refit=True,
                                    cv=KFold(X_train.shape[0], 10, shuffle=True,
                                             random_state=args.seed), verbose=1,
                                    random_state=args.seed,
                                    fit_params={'sample_weight': sample_weight})

发生以下错误:

AttributeError: 'list' object has no attribute 'copy'

值得一提的是,sample_weight是一个包含每个类权重的浮点数列表。有什么想法解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

search = RandomizedSearchCV(estimator, param_distributions,
                                    n_iter=args.iterations,
                                    scoring=mae_scorer, n_jobs=8, refit=True,
                                    cv=KFold(X_train.shape[0], 10, shuffle=True,
                                             random_state=args.seed), verbose=1,
                                    random_state=args.seed
                             )

search.fit(X_train, np.ravel(y_train) 
              ,sample_weight=our_sample_weight
             )

类似的事情适用于我的代码。