简体   繁体   English

Sklearn:有没有办法为管道定义特定的分数类型?

[英]Sklearn: Is there a way to define a specific score type to pipeline?

I can do this:我可以做这个:

model=linear_model.LogisticRegression(solver='lbfgs',max_iter=10000)
kfold = model_selection.KFold(n_splits=number_splits,shuffle=True, random_state=random_state)
scalar = StandardScaler()
pipeline = Pipeline([('transformer', scalar), ('estimator', model)])
results = model_selection.cross_validate(pipeline, X, y, cv=kfold, scoring=score_list,return_train_score=True)

where score_list can be something like ['accuracy','balanced_accuracy','precision','recall','f1'] .其中 score_list 可以类似于['accuracy','balanced_accuracy','precision','recall','f1']

I also can do this:我也可以这样做:

kfold = model_selection.KFold(n_splits=number_splits,shuffle=True, random_state=random_state)
scalar = StandardScaler()
pipeline = Pipeline([('transformer', scalar), ('estimator', model)])
for i, (train, test) in enumerate(kfold.split(X, y)):
    pipeline.fit(self.X[train], self.y[train])
    pipeline.score(self.X[test], self.y[test])

However, I am not able to change the score type for pipeline in the last line.但是,我无法在最后一行更改管道的分数类型。 How can I do that?我怎样才能做到这一点?

score method is always accuracy for classification and r2 score for regression. score方法始终是分类的accuracy和回归的r2分数。 There is no parameter to change that.没有参数可以改变它。 It comes from the Classifiermixin and RegressorMixin .它来自ClassifiermixinRegressorMixin

Instead, when we need other scoring options, we have to import it from sklearn.metrics like the following.相反,当我们需要其他评分选项时,我们必须从sklearn.metrics中导入它,如下所示。

from sklearn.metrics import balanced_accuracy

y_pred=pipeline.score(self.X[test])
balanced_accuracy(self.y_test, y_pred)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sklearn:在 gridsearchCV/Pipeline 中为 F1 分数提供参数 - sklearn: give param to F1 score in gridsearchCV/Pipeline 在 sklearn Pipeline 中为每一列使用特定的 IterativeImputer - Using a specific IterativeImputer in sklearn Pipeline for each column sklearn 管道中特定于列的处理 - Column-specific processing in an sklearn pipeline 无法在 sklearn 管道中使用灵活类型执行 reduce - Cannot perform reduce with flexible type in sklearn pipeline sklearn Pipeline:“ColumnTransformer”类型的参数不可迭代 - sklearn Pipeline: argument of type 'ColumnTransformer' is not iterable 如何使用固定步骤定义自定义 sklearn 管道? - How can I define a custom sklearn Pipeline with fixed steps? 使用sklearn查找文档中特定单词的tf-idf分数 - Find the tf-idf score of specific words in documents using sklearn sklearn的precision_score函数输入错误 - Type error with sklearn's accuracy_score function 在特定列的管道中使用 sklearn `KBinsDiscretizer` 并返回数据框 - Use sklearn `KBinsDiscretizer` within a pipeline on specific columns and return a data frame 如何从 sklearn 中的 ColumnTransformer/Pipeline 中排除特定列? - How can I exclude specific columns from a ColumnTransformer/Pipeline in sklearn?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM