我正在尝试使用sklearn_pandas模块来扩展我在熊猫中所做的工作,并涉足机器学习,但是我正为一个错误而苦苦挣扎,我不太了解如何解决。

我正在研究Kaggle上的以下数据集。

它本质上是一个带浮点值的无标题表(1000行,40个要素)。

import pandas as pdfrom sklearn import neighbors
from sklearn_pandas import DataFrameMapper, cross_val_score
path_train ="../kaggle/scikitlearn/train.csv"
path_labels ="../kaggle/scikitlearn/trainLabels.csv"
path_test = "../kaggle/scikitlearn/test.csv"

train = pd.read_csv(path_train, header=None)
labels = pd.read_csv(path_labels, header=None)
test = pd.read_csv(path_test, header=None)
mapper_train = DataFrameMapper([(list(train.columns),neighbors.KNeighborsClassifier(n_neighbors=3))])
mapper_train


输出:

DataFrameMapper(features=[([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39], KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
       n_neighbors=3, p=2, weights='uniform'))])


到目前为止,一切都很好。但后来我尝试

mapper_train.fit_transform(train, labels)


输出:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-e3897d6db1b5> in <module>()
----> 1 mapper_train.fit_transform(train, labels)

//anaconda/lib/python2.7/site-packages/sklearn/base.pyc in fit_transform(self, X, y,     **fit_params)
    409         else:
    410             # fit method of arity 2 (supervised transformation)
--> 411             return self.fit(X, y, **fit_params).transform(X)
    412
    413

//anaconda/lib/python2.7/site-packages/sklearn_pandas/__init__.pyc in fit(self, X, y)
    116         for columns, transformer in self.features:
    117             if transformer is not None:
--> 118                 transformer.fit(self._get_col_subset(X, columns))
    119         return self
    120

TypeError: fit() takes exactly 3 arguments (2 given)`


我究竟做错了什么?尽管在这种情况下的数据都是相同的,但我计划为分类,名义和浮点特征的混合物建立工作流,而sklearn_pandas似乎是合乎逻辑的。

最佳答案

由于sklearn_pandas当前不支持估计器接受带有标签的y向量,因此您仅需使用它即可将所有要素转换为Numpy矩阵,然后在单独的步骤中使用KNeighborsClassifier

更新2015-08-10-sklearn_pandas DataFrameMapper不能用作转换+模型拟合的管道,而仅用于选择性地转换列。如果要进行变换然后估计模型,则第一步是将普通的sklearn Pipeline与数据帧映射器一起使用。

08-25 05:06