gpt4 book ai didi

python - Scikit 管道参数 - fit() 得到一个意外的关键字参数 'gamma'

转载 作者:行者123 更新时间:2023-12-03 21:59:22 32 4
gpt4 key购买 nike

包括最小可行示例;)

我想要的只是简单地使用来自 GridSearchCV 的参数来使用 Pipeline。

#I want to create a SVM using a Pipeline, and validate the model (measure the accuracy)
#import libraries
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import pandas as pd

#load test data
data = load_iris()
X_trainset, X_testset, y_trainset, y_testset = train_test_split(data['data'], data['target'], test_size=0.2)

#And here we prepare the pipeline
pipeline = Pipeline([('scaler', StandardScaler()), ('SVM', SVC())])
grid = GridSearchCV(pipeline, param_grid={'SVM__gamma':[0.1,0.01]}, cv=5)
grid.fit(X_trainset, y_trainset)
# (Done! Now I can print the accuracy and other metrics)

#Now I want to put together training set and validation set, to train the model before deployment
#Of course, I want to use the best parameters found by GridSearchCV
big_x = np.concatenate([X_trainset,X_testset])
big_y = np.concatenate([y_trainset,y_testset])

到这里为止,它没有问题。然后,我写下这一行:
model2 = pipeline.fit(big_x,big_y, grid.best_params_)

错误!
TypeError: fit() takes from 2 to 3 positional arguments but 4 were given

然后我试图更明确:
model2 = pipeline.fit(big_x,big_y,fit_params=grid.best_params_)

又出错了!
ValueError: Pipeline.fit does not accept the fit_params parameter. You can pass parameters to specific steps of your pipeline using the stepname__parameter format, e.g. `Pipeline.fit(X, y, logisticregression__sample_weight=sample_weight)`.

然后我尝试(出于好奇)手动插入参数:
pipeline.fit(big_x,big_y, SVM__gamma= 0.01) #Note: I may need to insert many parameters, not just one

又报错了:(
TypeError: fit() got an unexpected keyword argument 'gamma'

我不明白为什么它找不到 Gamma 。我决定打印 pipeline.get_params() 来有个主意。
In [11]: print(pipeline.get_params())
Out [11]:
{'memory': None,
'steps': [('scaler', StandardScaler(copy=True, with_mean=True, with_std=True)), ('SVM', SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf', max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False))],
'verbose': False,
'scaler': StandardScaler(copy=True, with_mean=True, with_std=True),
'SVM': SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False),
'scaler__copy': True, 'scaler__with_mean': True, 'scaler__with_std': True, 'SVM__C': 1.0, 'SVM__break_ties': False, 'SVM__cache_size': 200, 'SVM__class_weight': None, 'SVM__coef0': 0.0, 'SVM__decision_function_shape': 'ovr', 'SVM__degree': 3, 'SVM__gamma': 'scale', 'SVM__kernel': 'rbf', 'SVM__max_iter': -1, 'SVM__probability': False, 'SVM__random_state': None, 'SVM__shrinking': True, 'SVM__tol': 0.001, 'SVM__verbose': False}

我可以在列表中找到 SVM__gamma!那么为什么会出现错误呢?

Scikit 版本:0.22.1

python版本:3.7.6

最佳答案

.fit() ,如对 .fit() 的调用SVC 类的函数,has no parameter called gamma .当您调用 pipeline.fit(SVM__gamma)它将 gamma 参数传递给 .fit()调用 SVM 步骤,这是行不通的。

您使用 .set_params() 在 scikit-learn 中设置参数职能。在最低级别(即针对 SVC 本身),您可以执行 SVC.set_params(gamma='blah') .在管道中,您将遵循您在参数网格中使用的相同双下划线表示法,所以 pipeline.set_params(SVM__gamma=blah) ,

如果您只针对管道的单个步骤设置单个参数,通常使用 pipeline.named_steps.SVM.set_params(gamma='blah') 直接访问该步骤会很方便。 , 或者使用 pipeline.set_params(**grid.best_params_)使用您的网格搜索的最佳参数。 (** 符号将 {'A':1, 'B':2} 的字典分解为 A=1, B=2 )

这是一个脚本片段,可以执行我认为您正在尝试执行的操作(尽管使用不同的算法):

# Set the classifier as an XGBClassifier

clf_pipeline = Pipeline(
steps=[
('preprocessor', preprocessor),
('classifier', XGBClassifier(n_jobs=6, n_estimators=20))
]
)


# In[41]:

# Cross validation: 60 iterations with 3 fold CV.

n_features_after_transform = clf_pipeline.named_steps.preprocessor.fit_transform(df).shape[1]

param_grid = {
'classifier__max_depth':stats.randint(low=2, high=100),
'classifier__max_features':stats.randint(low=2, high=n_features_after_transform),
'classifier__gamma':stats.uniform.rvs(0, 0.25, size=10000),
'classifier__subsample':stats.uniform.rvs(0.5, 0.5, size=10000),
'classifier__reg_alpha':stats.uniform.rvs(0.5, 1., size=10000),
'classifier__reg_lambda':stats.uniform.rvs(0.5, 1., size=10000)
}

rscv = RandomizedSearchCV(
clf_pipeline,
param_grid,
n_iter=60,
scoring='roc_auc',
cv=StratifiedKFold(n_splits=3, shuffle=True)

)

rscv.fit(df, y)


# In[42]:


# Set the tuned best params and beef up the number of estimators.

clf_pipeline.set_params(**rscv.best_params_)
clf_pipeline.named_steps.classifier.set_params(n_estimators=200)

长话短说,您可以通过访问要在管道的 named_steps 中设置参数的类来设置单个参数。 .要设置网格搜索确定为最佳的参数,请使用 pipeline.set_params(**grid.best_params_)

关于python - Scikit 管道参数 - fit() 得到一个意外的关键字参数 'gamma',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60608474/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com