让scikit学习与 pandas 一起工作 [英] Getting scikit learn to work with pandas

查看:84
本文介绍了让scikit学习与 pandas 一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始学习ML,并且需要一些帮助来帮助sklearn与熊猫一起工作.

Just getting started in ML and was needing some help with getting sklearn to work with pandas.

http://scikit-learn.org/stable/modules/feature_selection.html#feature-selection-as-part-of-a-pipeline

我正在阅读此书,并决定尝试使用我拥有的DataFrame进行尝试.以下是我的工作以及由此引起的错误.我对所有这些都还很陌生,所以如果我忽略了一些愚蠢的东西,请原谅,但是我认为,在这里问而不是试图在没有真正理解的情况下试图找到答案会更好.

I was reading this and decided to try it out with a DataFrame I had. Below is what I did, and the error that came from it. I'm pretty new to all of this so excuse me if I'm overlooking something dumb, but I thought it would be better to ask here versus try to hack away to find an answer without really understanding it.

谢谢大家!

In [518]: cols = ['A','B','C','D','E','F','G','H','I','J','K']

In [519]: x = df['Miss'].values

In [520]: y = df[list(cols)].values

In [532]: y.shape
Out[532]: (11345, 11)

In [533]: x.shape
Out[533]: (11345,)

clf = Pipeline([
  ('feature_selection', LinearSVC(penalty="l1", dual=False)),
  ('classification', RandomForestClassifier())])

In [536]: clf.fit(x,y)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/cschwalbach/as_research_repo/logs/<ipython-input-536-5c1831092d7a> in <module>()
----> 1 clf.fit(x,y)

/usr/lib64/python2.7/site-packages/sklearn/pipeline.pyc in fit(self, X, y, **fit_params)
    124         data, then fit the transformed data using the final estimator.
    125         """
--> 126         Xt, fit_params = self._pre_transform(X, y, **fit_params)
    127         self.steps[-1][-1].fit(Xt, y, **fit_params)
    128         return self

/usr/lib64/python2.7/site-packages/sklearn/pipeline.pyc in _pre_transform(self, X, y, **fit_params)
    114         for name, transform in self.steps[:-1]:
    115             if hasattr(transform, "fit_transform"):
--> 116                 Xt = transform.fit_transform(Xt, y, **fit_params_steps[name])
    117             else:
    118                 Xt = transform.fit(Xt, y, **fit_params_steps[name]) \

/usr/lib64/python2.7/site-packages/sklearn/base.pyc in fit_transform(self, X, y, **fit_params)
    362         else:
    363             # fit method of arity 2 (supervised transformation)

--> 364             return self.fit(X, y, **fit_params).transform(X)
    365
    366

/usr/lib64/python2.7/site-packages/sklearn/svm/base.pyc in fit(self, X, y)
    684             raise ValueError("X and y have incompatible shapes.\n"
    685                              "X has %s samples, but y has %s." %
--> 686                              (X.shape[0], y.shape[0]))
    687
    688         liblinear.set_verbosity_wrap(self.verbose)

ValueError: X and y have incompatible shapes.
X has 1 samples, but y has 124795.

推荐答案

大多数人将X用作要素,将y用作标签.不幸的是,您的情况与此相反.因此,您可能会对文档感到困惑.

Most of people use X as features and y as label. Unluckily, you are in a contrary way. So you might get confused in the documentation.

改为使用关注内容

In [519]: y = df['Miss'].values

In [520]: X = df[list(cols)].values

然后您可以通过clf.fit(X, y)

这篇关于让scikit学习与 pandas 一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆