在 scikit-learn 管道中更改 imputer 策略时遇到问题
Trouble changing imputer strategy in scikit-learn pipeline
我正在尝试使用 GridSearchCV 来 select 最好的输入策略,但我在这样做时遇到了麻烦。
首先,我有一个用于数值列和分类列的数据准备管道-
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.pipeline import Pipeline, make_pipeline
num_pipe = make_pipeline(SimpleImputer(strategy='median'), StandardScaler())
cat_pipe = make_pipeline(SimpleImputer(strategy='constant', fill_value='NA'),
OneHotEncoder(sparse=False, handle_unknown='ignore'))
preprocessing = ColumnTransformer([
("num", num_pipe, num_cols),
("cat", cat_pipe, cat_cols)
])
接下来,我创建了一个管道来训练具有 selection 特征的支持向量机模型。
from sklearn.feature_selection import SelectFromModel
model = Pipeline([
("preprocess", preprocessing),
("feature_select", SelectFromModel(RandomForestRegressor(random_state=42))),
("regressor", SVR(kernel='rbf', C=30000.0, gamma=0.3))
])
现在,我正在尝试查看哪种输入策略最适合使用 GridSearchCV
为数字列输入缺失值
grid = {"model.named_steps.preprocess.transformers[0][1].named_steps['simpleimputer'].strategy":
['mean','median','most_frequent']}
grid_search = GridSearchCV(model, param_grid = grid, cv=5, scoring='neg_mean_squared_error')
grid_search.fit(X_train, y_train)
这是我收到错误的地方。完整的管道看起来像这样 -
Pipeline(steps=[('preprocess',
ColumnTransformer(transformers=[('num',
Pipeline(steps=[('simpleimputer',
SimpleImputer(strategy='median')),
('standardscaler',
StandardScaler())]),
['longitude', 'latitude',
'housing_median_age',
'total_rooms',
'total_bedrooms',
'population', 'households',
'median_income']),
('cat',
Pipeline(steps=[('simpleimputer',
SimpleImputer(fill_value='NA',
strategy='constant')),
('onehotencoder',
OneHotEncoder(handle_unknown='ignore',
sparse=False))]),
['ocean_proximity'])])),
('feature_select',
SelectFromModel(estimator=RandomForestRegressor(random_state=42))),
('regressor', SVR(C=30000.0, gamma=0.3))])
任何人都可以告诉我需要在网格搜索中更改什么才能使其正常工作吗?
您指定参数的方式是通过字典将 estimator/transformer 的名称和您要更改的参数的名称映射到您想要尝试的参数。如果您有一个管道或管道的管道,名称是其所有父级的名称加上双下划线。所以对于你的情况,它看起来像
gird = {
"preprocess__num__simpleimputer__strategy":['median']
}
simpleimputer 只是 make_pipeline 自动分配的名称。
但是,我认为您的代码中还有其他问题,例如 fill_value='NA' 不正确并且实际上不需要,因为它不是要填充的错误,而是填充缺失所需的值值。
我正在尝试使用 GridSearchCV 来 select 最好的输入策略,但我在这样做时遇到了麻烦。
首先,我有一个用于数值列和分类列的数据准备管道-
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.pipeline import Pipeline, make_pipeline
num_pipe = make_pipeline(SimpleImputer(strategy='median'), StandardScaler())
cat_pipe = make_pipeline(SimpleImputer(strategy='constant', fill_value='NA'),
OneHotEncoder(sparse=False, handle_unknown='ignore'))
preprocessing = ColumnTransformer([
("num", num_pipe, num_cols),
("cat", cat_pipe, cat_cols)
])
接下来,我创建了一个管道来训练具有 selection 特征的支持向量机模型。
from sklearn.feature_selection import SelectFromModel
model = Pipeline([
("preprocess", preprocessing),
("feature_select", SelectFromModel(RandomForestRegressor(random_state=42))),
("regressor", SVR(kernel='rbf', C=30000.0, gamma=0.3))
])
现在,我正在尝试查看哪种输入策略最适合使用 GridSearchCV
为数字列输入缺失值grid = {"model.named_steps.preprocess.transformers[0][1].named_steps['simpleimputer'].strategy":
['mean','median','most_frequent']}
grid_search = GridSearchCV(model, param_grid = grid, cv=5, scoring='neg_mean_squared_error')
grid_search.fit(X_train, y_train)
这是我收到错误的地方。完整的管道看起来像这样 -
Pipeline(steps=[('preprocess',
ColumnTransformer(transformers=[('num',
Pipeline(steps=[('simpleimputer',
SimpleImputer(strategy='median')),
('standardscaler',
StandardScaler())]),
['longitude', 'latitude',
'housing_median_age',
'total_rooms',
'total_bedrooms',
'population', 'households',
'median_income']),
('cat',
Pipeline(steps=[('simpleimputer',
SimpleImputer(fill_value='NA',
strategy='constant')),
('onehotencoder',
OneHotEncoder(handle_unknown='ignore',
sparse=False))]),
['ocean_proximity'])])),
('feature_select',
SelectFromModel(estimator=RandomForestRegressor(random_state=42))),
('regressor', SVR(C=30000.0, gamma=0.3))])
任何人都可以告诉我需要在网格搜索中更改什么才能使其正常工作吗?
您指定参数的方式是通过字典将 estimator/transformer 的名称和您要更改的参数的名称映射到您想要尝试的参数。如果您有一个管道或管道的管道,名称是其所有父级的名称加上双下划线。所以对于你的情况,它看起来像
gird = {
"preprocess__num__simpleimputer__strategy":['median']
}
simpleimputer 只是 make_pipeline 自动分配的名称。
但是,我认为您的代码中还有其他问题,例如 fill_value='NA' 不正确并且实际上不需要,因为它不是要填充的错误,而是填充缺失所需的值值。