gop*_*410 4 python machine-learning scikit-learn deep-learning data-science
假设我有一个数据管道,它进行预处理并在最后有一个估算器。现在,如果我只想在管道的最后一步更改估算器/模型,我该如何在不重新预处理相同数据的情况下做到这一点。下面是一个代码示例
pipe = make_pipeline(
ColumnSelector(columns),
CategoricalEncoder(categories),
FunctionTransformer(pd.get_dummies, validate=False),
StandardScaler(scale),
LogisticRegression(),
)
Run Code Online (Sandbox Code Playgroud)
现在我想更改模型以使用 Ridge 或 LogisticRegression 以外的其他模型。如何在不重新进行预处理的情况下执行此操作?
编辑:我可以从以下类型的管道中获取转换后的数据吗
pipe = make_pipeline(
ColumnSelector(columns),
CategoricalEncoder(categories),
FunctionTransformer(pd.get_dummies, validate=False),
StandardScaler(scale)
)
Run Code Online (Sandbox Code Playgroud)
对于具有计算成本高昂的转换器的情况,您可以使用 缓存。由于您没有提供变压器,这里是链接中 sklearn 示例的扩展,其中使用缓存管道对两个模型进行网格搜索:
from tempfile import mkdtemp
from shutil import rmtree
from sklearn.externals.joblib import Memory
from sklearn.pipeline import Pipeline
from sklearn.decomposition import PCA
from sklearn.svm import LinearSVC
from sklearn.linear_model import ElasticNet
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import load_digits
# Create a temporary folder to store the transformers of the pipeline
cachedir = mkdtemp()
memory = Memory(cachedir=cachedir, verbose=10)
# the pipeline
pipe = Pipeline([('reduce_dim', PCA()),
('classify', LinearSVC())],
memory=memory)
# models to try
param_grid = {"classify" : [LinearSVC(), ElasticNet()]}
# do the gridsearch on the models
grid = GridSearchCV(pipe, param_grid=param_grid)
digits = load_digits()
grid.fit(digits.data, digits.target)
# delete the temporary cache before exiting
rmtree(cachedir)
Run Code Online (Sandbox Code Playgroud)
当您关注问题中的模型时,并且这个问题关注参数时,我不会认为它是完全重复的。但是,建议的解决方案与此处设置的 param_grid 相结合也将是一个很好的解决方案,甚至可能是更好的解决方案,具体取决于您的确切问题。
归档时间: |
|
查看次数: |
947 次 |
最近记录: |