GridSearchCV中的自定义分数函数:访问未缩放的功能,而不是在mod中

2024-09-30 16:27:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python中使用sklearn的GridSearchCV包,我想将它与一个定制的评分函数一起使用。自定义评分函数需要访问不在模型中的变量。问题是,我无法从训练集中访问未缩放/未更改的变量,因为它们不包括在使用缩放数据的模型中,而且gridsearch为每个批随机选择行。你知道我该怎么处理吗?你知道吗

我试图创建一个评分函数,将原始(未评分,不变)训练集作为参数。它可以工作,但由于gridsearch只接受训练集的子集,并且行被洗牌,所以我无法将每一行与其在原始训练集中对应的值“连接”。我试图取消包含在训练集中的数据的缩放,但是没有成功。我考虑过将未缩放列添加到缩放训练集中,但如何将其从模型中排除?你知道吗

# building pipelines
from sklearn.preprocessing import OneHotEncoder

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

num_pipeline = Pipeline([
    ('std_scaler', StandardScaler()),
],verbose=True)
cat_pipeline = Pipeline([
    ('one_hot_enc',OneHotEncoder(sparse=False,handle_unknown='ignore')),
],verbose=True)

from sklearn.compose import ColumnTransformer
full_pipeline = ColumnTransformer([
    ("num", num_pipeline, df_num_reg_attributes),
    ("cat", cat_pipeline, df_cat_attributes)
])

# fitting pipelines
X_train_prepared_reg = full_pipeline.fit_transform(X_res_df)
listColPrepared=np.concatenate((df_num_reg_attributes,full_pipeline.named_transformers_['cat'].named_steps['one_hot_enc'].get_feature_names()))
scalerX_train = full_pipeline.named_transformers_['num'].named_steps['std_scaler']
X_test_prepared_reg = full_pipeline.transform(X_test)
y_train = y_balanced

# scorer
def my_scorer(clf, X, y_true):
    DCWorkCost = 5.00
    OPWorkCost = 2.50
    mergedDataset = pd.DataFrame(data=X,index=np.arange(0,len(X)),columns=listColPrepared)
### this is the column I want -- I tried to unscale the data to access the column but it did not work    
    mergedDataset['Margin'] = scalerX_train.inverse_transform(mergedDataset['Margin'])
    mergedDataset['True'] = y_true
    mergedDataset['Pred'] = clf.predict(X)
 # rest of the scorer.........
    return revenue

# grid search
sgd_clf_cv = SGDClassifier(max_iter=5,tol=-np.infty, random_state=42)
parameters = {'class_weight':({0:.1,1:.9},{0:.2,1:.8},{0:.3,1:.7},{0:.25,1:.75},{0:.15,1:.85},{0:.35,1:.65},{0:.4,1:.6})}
grid = GridSearchCV(estimator=sgd_clf_cv, param_grid=parameters, scoring=my_scorer,verbose=10)
grid.fit(X_train_prepared_reg, y_train)
grid.best_estimator_

当尝试取消缩放代码中显示的数据时,我得到一条关于不对应形状的错误消息。你知道吗


Tags: fromimportdfpipelinetrainsklearnreg评分
1条回答
网友
1楼 · 发布于 2024-09-30 16:27:43

需要两个步骤才能拥有自己的自定义评分函数,该函数还可以访问另一个常量对象。你知道吗

  1. 您的自定义分数函数需要传递给^{}。评分函数的格式需要是def f(y_true, y_predicted)
  2. 您的score函数需要第三个命名参数,您可以在其中添加其他对象。你知道吗

在您的情况下,代码应该类似于

def my_scorer(y_true, y_pred, scaler=None):
    DCWorkCost = 5.00
    OPWorkCost = 2.50
    mergedDataset = pd.DataFrame(data=X, index=np.arange(0, len(y_true)), columns=listColPrepared)
    ### this is the column I want   I tried to unscale the data to access the column but it did not work    
    mergedDataset['Margin'] = scaler.inverse_transform(mergedDataset['Margin'])
    mergedDataset['True'] = y_true
    mergedDataset['Pred'] = y_pred
   # rest of the scorer.........

    return revenue

...
scalerX_train = full_pipeline.named_transformers_['num'].named_steps['std_scaler']
...
sgd_clf_cv = SGDClassifier(max_iter=5,tol=-np.infty, random_state=42)
...
custom_score = make_scorer(my_scorer, scaler=scalarX_train)
...
grid = GridSearchCV(estimator=sgd_clf_cv, param_grid=parameters, scoring=custom_score, verbose=10)

相关问题 更多 >