【问题标题】:pipeline fit takes in 2 parameters only管道拟合仅接受 2 个参数
【发布时间】:2017-08-18 03:26:06
【问题描述】:

这是我的代码。

import pandas as pd 
import numpy as np
import json
import seaborn as sb 
from sklearn.metrics import log_loss
from sklearn import linear_model 
from sklearn.model_selection import StratifiedKFold
from sklearn.svm import SVC
from scipy.stats import zscore
from Transformers import TextTransformer
from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn.model_selection import GridSearchCV
%matplotlib inline
df = pd.read_json('data/train.json', encoding = 'utf-8', dtype = {'description': str})
from sklearn.pipeline import Pipeline, FeatureUnion
a = TextTransformer('description', max_features=50)
b = TextTransformer('features', max_features=10)
pipeline = Pipeline([
    ('feats', FeatureUnion([
        ('description',a ), # can pass in either a pipeline
        ('features',b ) # or a transformer
    ])),
    ('clf', SVC())  # classifier
])
pipeline.fit(df)

我很好奇的是我试图预测目标变量 df['interest_level']。然而,pipeline.fit 只接受 2 个参数,其中一个是 self。那我如何传入目标变量呢?

需要注意的另一点是我尝试了 pipeline.fit(df, y=df['interest_level']) 并且它也抛出了相同的异常。我正在使用最新版本的 pandas/numpy/sklearn。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-0a34f1c24eca> in <module>()
      7     ('clf', SVC())  # classifier
      8 ])
----> 9 pipeline.fit(df,df['interest_level'])
     10 # pg = {'clf__C': [0.1,1]}
     11 # grid = GridSearchCV(pipeline, param_grid= pg ,cv = 2)

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/pipeline.pyc in fit(self, X, y, **fit_params)
    266             This estimator
    267         """
--> 268         Xt, fit_params = self._fit(X, y, **fit_params)
    269         if self._final_estimator is not None:
    270             self._final_estimator.fit(Xt, y, **fit_params)

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/pipeline.pyc in _fit(self, X, y, **fit_params)
    232                 pass
    233             elif hasattr(transform, "fit_transform"):
--> 234                 Xt = transform.fit_transform(Xt, y, **fit_params_steps[name])
    235             else:
    236                 Xt = transform.fit(Xt, y, **fit_params_steps[name]) \

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/base.pyc in fit_transform(self, X, y, **fit_params)
    495         else:
    496             # fit method of arity 2 (supervised transformation)
--> 497             return self.fit(X, y, **fit_params).transform(X)
    498 
    499 

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

【问题讨论】:

  • 在分类场景中,通常所有的 scikit learn 估计器 fit() 都会接受至少两个参数:X,y。你只通过了一个:df。将其更改为:X=df[独立列的索引,特征],y=df[目标列的索引]
  • 这就是我尝试过的,但它给了我多个参数异常
  • 你能发布完整的堆栈跟踪吗
  • @VivekKumar 我已经这样做了

标签: python pandas numpy scikit-learn


【解决方案1】:

根据文档,您是对的:

pipeline: fit()

你也可以看看这个例子:

pipeline / featureunion example

您遇到的具体错误是什么?我会运行如下内容:

pipeline.fit(df[:, -1], df[:, len(df.columns)]-1)

【讨论】:

  • 该示例 pipeline.fit 接受 2 个参数。这就是我觉得很奇怪的地方。我确实遵循了它
猜你喜欢
  • 1970-01-01
  • 2014-05-30
  • 2013-07-08
  • 2019-11-14
  • 1970-01-01
  • 1970-01-01
  • 2018-09-08
  • 1970-01-01
  • 2017-12-29
相关资源
最近更新 更多