亲爱的同事们,我已经创建了一个scikit-learn管道来训练和调整不同的HistBoostRegressors。
然而,我现在想知道是否可以将不同的特征名称传递给step pipeline_hist_boost_mimo_inside["estimator"]。
我注意到在多输出回归器的文档中有一个名为feature_names的参数:
feature_names_in_ndarray of shape (n_features_in_,) Names of features seen during fit. Only defined if the underlying estimators expose such an attribute when fit. New in version 1.0.
我还发现了一些scikit learn列选择器的文档,其中有一个参数: patternstr,default = None:包含此正则表达式模式的列的名称将被包括在内。如果为None,则不会基于模式选择列。
问题是这个模式将取决于我正在拟合的目标。
有没有一种优雅的方法来解决这个问题?
编辑:数据集示例:
多输出回归器将为每个(feat1、feat2、feat3和targetn)对拟合一个直方图回归器。在下表的示例中,我将拥有一个管道,其估计器步骤将包含一个由3个估计器组成的列表,因为我有3个目标。
问题是如何将例如feat1和feat2传递给target1,但将feat1和feat3传递给target2。
from scipy.stats import loguniform
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.feature_selection import VarianceThreshold
from sklearn.multioutput import MultiOutputRegressor
from sklearn.model_selection import RandomizedSearchCV
class loguniform_int:
"""Integer valued version of the log-uniform distribution"""
def __init__(self, a, b):
self._distribution = loguniform(a, b)
def rvs(self, *args, **kwargs):
"""Random variable sample"""
return self._distribution.rvs(*args, **kwargs).astype(int)
data_train, data_test, target_train, target_test = train_test_split(
df.drop(columns=TARGETS),
df[target_dict],
random_state=42)
pipeline_hist_boost_mimo_inside = Pipeline([('scaler', StandardScaler()),
('variance_selector', VarianceThreshold(threshold=0.03)),
('estimator', MultiOutputRegressor(HistGradientBoostingRegressor(loss='poisson')))])
parameters = {
'estimator__estimator__l2_regularization': loguniform(1e-6, 1e3),
'estimator__estimator__learning_rate': loguniform(0.001, 10),
'estimator__estimator__max_leaf_nodes': loguniform_int(2, 256),
'estimator__estimator__max_leaf_nodes': loguniform_int(2, 256),
'estimator__estimator__min_samples_leaf': loguniform_int(1, 100),
'estimator__estimator__max_bins': loguniform_int(2, 255),
}
random_grid_inside = RandomizedSearchCV(estimator=pipeline_hist_boost_mimo_inside, param_distributions=parameters, random_state=0, n_iter=50,
n_jobs=-1, refit=True, cv=3, verbose=True,
pre_dispatch='2*n_jobs',
return_train_score=True)
results_inside_train = random_grid_inside.fit(data_train, target_train)
然而,我现在想知道是否可以将不同的特征名称传递给step pipeline_hist_boost_mimo_inside["estimator"]。
我注意到在多输出回归器的文档中有一个名为feature_names的参数:
feature_names_in_ndarray of shape (n_features_in_,) Names of features seen during fit. Only defined if the underlying estimators expose such an attribute when fit. New in version 1.0.
我还发现了一些scikit learn列选择器的文档,其中有一个参数: patternstr,default = None:包含此正则表达式模式的列的名称将被包括在内。如果为None,则不会基于模式选择列。
问题是这个模式将取决于我正在拟合的目标。
有没有一种优雅的方法来解决这个问题?
编辑:数据集示例:
feat1, feat2, feat3.... target1, target2, target3....
1 47 0.65 0 0.5 0.6
多输出回归器将为每个(feat1、feat2、feat3和targetn)对拟合一个直方图回归器。在下表的示例中,我将拥有一个管道,其估计器步骤将包含一个由3个估计器组成的列表,因为我有3个目标。
问题是如何将例如feat1和feat2传递给target1,但将feat1和feat3传递给target2。
column_selector
或DropFeatures
的转换器,但应用于目标变量?如果您的任务是回归HistGradientBoostingRegressor
,为什么您的目标只有12个不同的值? - Miguel TrejoMultiOutputRegressor
运行模型之前选择其中一些12个特征,例如低维度矩阵(m,7)。您能给出目标变量的示例吗? - Miguel Trejo