【问题标题】:scikit-learn RFECV array with 0 samples具有 0 个样本的 scikit-learn RFECV 数组
【发布时间】:2016-09-26 09:03:40
【问题描述】:

我试图按照给定的教程here 使用我自己的数据使用 scikit-learn 的递归特征消除和交叉验证 (RFECV) 功能,并不断遇到令人费解的错误:

ValueError: 找到包含 0 个样本 (shape=(0, 9)) 的数组,而至少需要 1 个。

我使用的代码如下:

import pandas as pd
import numpy as np

from sklearn import svm
from sklearn.cross_validation import StratifiedKFold
from sklearn.feature_selection import RFECV

data = pd.read_csv('data.csv', index_col = 0)

training = data.iloc[:50]
# training on the first 50 rows
training_y = np.asarray(training.C1, dtype="|S6")
training_x = training.drop('C1', axis=1)

print training_y.shape
print training_x.shape


# Create the RFE object and compute a cross-validated score.
svc = svm.SVC(kernel="linear")
# The "accuracy" scoring is proportional to the number of correct
# classifications
rfecv = RFECV(estimator = svc, step = 1, cv = StratifiedKFold(training_y, 3),
              scoring = 'accuracy')

rfecv.fit(training_x, training_y)

仅供参考,两条打印语句的输出为:

(50,)

(50, 9)

谢谢!

【问题讨论】:

    标签: python scikit-learn


    【解决方案1】:

    我创建了虚拟数据,它对我有用:

    import pandas as pd
    import numpy as np
    
    from sklearn import svm
    from sklearn.cross_validation import StratifiedKFold
    from sklearn.feature_selection import RFECV
    
    data = np.random.randn(50,9)
    
    # training on the first 50 rows
    training_y = np.random.random(50).round()
    training_x = data
    
    print(training_y.shape)
    print(training_x.shape)
    
    
    # Create the RFE object and compute a cross-validated score.
    svc = svm.SVC(kernel="linear")
    # The "accuracy" scoring is proportional to the number of correct
    # classifications
    rfecv = RFECV(estimator = svc, step = 1, cv = StratifiedKFold(training_y, 3),
                  scoring = 'accuracy')
    
    rfecv.fit(training_x, training_y)
    

    输出是:

    RFECV(cv=sklearn.cross_validation.StratifiedKFold(labels=[ 1.  1.  1.  0.  1.  1.  1.  1.  0.  1.  1.  1.  1.  1.  0.  1.  0.  1.
      1.  0.  1.  0.  1.  1.  1.  0.  0.  0.  0.  1.  0.  1.  1.  0.  1.  0.
      1.  1.  0.  1.  1.  0.  0.  0.  1.  0.  0.  0.  1.  0.], n_folds=3, shuffle=False, random_state=None),
       estimator=SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
      decision_function_shape=None, degree=3, gamma='auto', kernel='linear',
      max_iter=-1, probability=False, random_state=None, shrinking=True,
      tol=0.001, verbose=False),
       estimator_params=None, scoring='accuracy', step=1, verbose=0)
    

    如果您能向我们提供您的数据,那就太好了。

    【讨论】:

    • 感谢您的检查!它也适用于我的虚拟数据。字面上不是训练数据中的单个值为零,并且均未长度为零。这就是为什么我被错误消息非常疑惑。我使用import operator 987654324 @ for row in training_x.values: print reduce(operator.mul, row, 1)不幸的是,我不知道我是否可以共享数据。 span>
    猜你喜欢
    • 2016-04-14
    • 2018-10-27
    • 2020-05-31
    • 2015-06-22
    • 2016-08-31
    • 2017-07-18
    • 2018-11-21
    • 2018-08-12
    • 2017-04-12
    相关资源
    最近更新 更多