[英]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
.它来自Classifiermixin
和RegressorMixin
。
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.