在过去的几天里,我在stackoverflow上查找了正确的教程和问答,但没有找到正确的指南,主要是因为显示LabelBinarizer或onehotcoder的用例的示例没有显示它是如何被集成到管道中的,反之亦然。在
我有一个包含4个变量的数据集:
num1 num2 cate1 cate2
3 4 Cat 1
9 23 Dog 0
10 5 Dog 1
num1和num2是数值变量,cate1和cate2是范畴变量。我知道我需要在拟合ML算法之前对分类变量进行编码,但我不太确定在多次尝试之后如何在管道中进行编码。在
^{pr2}$这给了我一个错误ValueError: could not convert string to float: 'Cat'
将最后第4行替换为
('categorical', make_pipeline(Columns(names=X_cat_cols),OneHotEncoder()))
会给我同样的ValueError: could not convert string to float: 'Cat'
。在
将最后第4行替换为
('categorical', make_pipeline(Columns(names=X_cat_cols),LabelBinarizer(),OneHotEncoder()))
])),
会给我一个不同的错误TypeError: fit_transform() takes 2 positional arguments but 3 were given
。在
将最后第4行替换为
('numeric', make_pipeline(Columns(names=X_num_cols),LabelBinarizer())),
会给我这个错误TypeError: fit_transform() takes 2 positional arguments but 3 were given
。在
接受了马库斯的建议,我试图安装scikit learn dev版本,但却发现了一个类似的东西,叫做category_encoders。在
将代码改为以下工作:
至于我,我更喜欢使用
LabelEncoder
。 只是玩具的例子。在我定义
^{pr2}$class
来选择列现在我定义了用
LabelEncoder
进行预处理的类我使用
for-loop
,因为我们可以将LabelEncoder
应用于单个向量。 管道并检查新数据
类似地,我们可以为任何预处理分类数据的方法做。在
相关问题 更多 >
编程相关推荐