-1

一方面,人们说 pandasscikit-learn配合得很好。例如,pandas 系列对象非常适合本视频中的 sklearn 模型。另一方面,sklearn-pandas在 Scikit-Learn 的机器学习方法和 pandas 风格的数据框架之间提供了一座桥梁,这意味着需要这样的库。此外,例如,有些人将 pandas 数据帧转换为 numpy 数组以拟合模型。

我想知道是否可以在没有任何其他方法和库的情况下将pandasscikit-learn结合起来。我的问题是,每当我通过以下方式将我的数据集拟合到 sklearn 模型时:

import numpy as np
import pandas as pd
from sklearn.cross_validation import train_test_split
from sklearn.svm import SVC

d = {'x': np.linspace(1., 100., 20), 'y': np.linspace(1., 10., 20)}
df = pd.DataFrame(d)

train, test = train_test_split(df, test_size = 0.2)

trainX = train['x']
trainY = train['y']

lin_svm = SVC(kernel='linear').fit(trainX, trainY)

我收到一个错误:

ValueError: Unknown label type: 19    10.000000
0      1.000000
17     9.052632
18     9.526316
12     6.684211
11     6.210526
16     8.578947
14     7.631579
10     5.736842
7      4.315789
8      4.789474
2      1.947368
13     7.157895
1      1.473684
6      3.842105
3      2.421053
Name: y, dtype: float64

据我了解,这是因为数据结构。但是,互联网上使用类似代码没有任何问题的示例很少。

4

1 回答 1

1

您可能想要做的是回归而不是分类

想一想,要进行分类,您需要二进制输出或多输出。在您的情况下,您将连续数据提供给您的分类器

如果你追溯你的错误并深入挖掘sklearn's 方法的实现,.fit()你会发现以下函数:

def check_classification_targets(y):
"""Ensure that target y is of a non-regression type.

Only the following target types (as defined in type_of_target) are allowed:
    'binary', 'multiclass', 'multiclass-multioutput', 
    'multilabel-indicator', 'multilabel-sequences'

Parameters
----------
y : array-like
"""
y_type = type_of_target(y)
if y_type not in ['binary', 'multiclass', 'multiclass-multioutput', 
        'multilabel-indicator', 'multilabel-sequences']:
    raise ValueError("Unknown label type: %r" % y)

该函数的文档字符串type_of_target为:

def type_of_target(y):
"""Determine the type of data indicated by target `y`

Parameters
----------
y : array-like

Returns
-------
target_type : string
    One of:
    * 'continuous': `y` is an array-like of floats that are not all
      integers, and is 1d or a column vector.
    * 'continuous-multioutput': `y` is a 2d array of floats that are
      not all integers, and both dimensions are of size > 1.
    * 'binary': `y` contains <= 2 discrete values and is 1d or a column
      vector.
    * 'multiclass': `y` contains more than two discrete values, is not a
      sequence of sequences, and is 1d or a column vector.
    * 'multiclass-multioutput': `y` is a 2d array that contains more
      than two discrete values, is not a sequence of sequences, and both
      dimensions are of size > 1.
    * 'multilabel-indicator': `y` is a label indicator matrix, an array
      of two dimensions with at least two columns, and at most 2 unique
      values.
    * 'unknown': `y` is array-like but none of the above, such as a 3d
      array, sequence of sequences, or an array of non-sequence objects.

在你的情况下type_of_target(trainY)=='continuous' and then it raises aValueError in the functioncheck_classification_targets()`。


结论

  • 如果要执行分类,请更改目标y。(例如,使用二进制向量)
  • 如果你想保持你的连续数据执行回归。使用svm.SVR.
于 2016-10-07T12:51:16.750 回答