lan*_*tar 7 python pipeline post-processing scikit-learn
我正在使用Pipelinescikit学习将一些预处理与a OneClassSVM作为最终分类器组合在一起.为了计算合理的度量,我需要一个后处理,将-1的-1输出转换OneClassSVM为0和1.有没有结构化的方法将这样的后处理添加到Pipeline?变形金刚在最终估算之后不能使用.
sklearn.preprocessing.TransformedTargetRegressor您可以使用带有 SVM 分类器的类作为回归器,并使用inverse_func参数在分类后转换标签。
但是,由于TransformedTargetRegressor应该在拟合之前将标签转换到新空间并将预测的标签重新映射到原始空间,因此它期望在拟合之前转换标签数组,并且不接受空或None目标作为输入。因此,您需要为管道提供一个虚拟目标,这可能会使您的代码有点混乱。
例子:
import numpy as np
from sklearn.compose import TransformedTargetRegressor
from sklearn.svm import OneClassSVM
from sklearn.pipeline import Pipeline
X = np.random.random((10, 2))
regressor = OneClassSVM(gamma='auto')
svm = TransformedTargetRegressor(regressor=regressor,
inverse_func=lambda x: (x+1)//2, # Function that remaps your labels
check_inverse=False) # If not set to False, this code will generate an error since the provided inverse_func is not the inverse of the default func argument, which is the identity function
pipeline = Pipeline([
('svm', svm)
])
pipeline.fit(X, np.zeros((1,1))) # An array of fake label is provided to the pipeline
pipeline.predict(X)
Run Code Online (Sandbox Code Playgroud)
输出:
array([[0],
[1],
[1],
[1],
[1],
[0],
[1],
[0],
[0],
[0]])
Run Code Online (Sandbox Code Playgroud)
请注意,如果您需要OneClassSVM通过Pipeline字典将参数传递给分类器,例如在网格搜索中使用GridSearchCV,则需要在和 参数名称regressor__之间添加参数键名称。svm__例如,svm__kernel变为svm__regressor__kernel.
我们开发了 PipeGraph,它是 Scikit-Learn Pipeline 的扩展,它允许您获取中间数据、构建类似工作流程的图形,特别是解决这个问题(请参阅http://mcasl.github.io/PipeGraph中图库中的示例) )
| 归档时间: |
|
| 查看次数: |
1124 次 |
| 最近记录: |