【问题标题】:kfold validation error - ValueError: bad input shape (513, 10)kfold 验证错误 - ValueError: bad input shape (513, 10)
【发布时间】:2019-09-22 22:57:29
【问题描述】:

用于准确性、AUC 和召回的 Kfold 命令运行良好,但现在显示错误。

内核多次重启并尝试了其他方法都无济于事,例如“stratifiedkfold”、“in emumerate”和循环。

from sklearn.model_selection import KFold
svc_clf = svm.SVC(C=50, 
                  kernel='rbf', 
                  gamma=0.1,
                  probability=False,
                  class_weight={1: 5}
                 )
svc_clf.fit(X_train_std, y_train)

# K-fold cross-validator
kfold = Kfold(n_splits=10, random_state=140311, shuffle=True)
for train_index, test_index in kfold.split(X):
    X_training, X_testing = X_train_std[train_index], X_train_std[test_index]
    y_training, y_testing = y_train[train_index], y_train[test_index]

df_kfold_acc = cross_val_score(svc_clf, X_train_std, y_train, cv=kfold, scoring='accuracy')
print'10 fold validation accuracy scores: \n', (df_kfold_acc)
print'Kfold mean accuracy score: \n', (df_kfold_acc).mean()

df_kfold_auc = cross_val_score(svc_clf, X_train_std, y_train, cv=kfold, scoring='roc_auc')
print'\n\n 10 fold validation AUC scores:\n ', (df_kfold_auc)
print'Kfold mean AUC score: \n', (df_kfold_auc).mean()

df_kfold_recall = cross_val_score(svc_clf, X_train_std, y_train, cv=kfold, scoring='recall')
print'\n\n 10 fold validation recall scores:\n', (df_kfold_recall)
print'Kfold mean recall score: \n', (df_kfold_recall).mean()

预期(和之前得到)如下:

10 倍验证准确度分数:{0.7982993、0.6793838 等(总共 10 倍)} Kfold 平均准确度得分:0.78679979

实际错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-56-61c6420c7a2f> in <module>()
      6                   class_weight={1: 5}
      7                  )
----> 8 svc_clf.fit(X_train_std, y_train)
      9 
     10 # K-fold cross-validator

/Users/db/anaconda2/lib/python2.7/site-packages/sklearn/svm/base.pyc in fit(self, X, y, sample_weight)
    147         self._sparse = sparse and not callable(self.kernel)
    148 
--> 149         X, y = check_X_y(X, y, dtype=np.float64, order='C', accept_sparse='csr')
    150         y = self._validate_targets(y)
    151 

/Users/db/anaconda2/lib/python2.7/site-packages/sklearn/utils/validation.pyc in check_X_y(X, y, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
    576                         dtype=None)
    577     else:
--> 578         y = column_or_1d(y, warn=True)
    579         _assert_all_finite(y)
    580     if y_numeric and y.dtype.kind == 'O':

/Users/db/anaconda2/lib/python2.7/site-packages/sklearn/utils/validation.pyc in column_or_1d(y, warn)
    612         return np.ravel(y)
    613 
--> 614     raise ValueError("bad input shape {0}".format(shape))
    615 
    616 

ValueError: bad input shape (513, 10)

【问题讨论】:

  • 错误在上面描述中提到的错误和以下错误之间交替出现:NameError: name 'Kfold' is not defined

标签: python pandas validation machine-learning


【解决方案1】:

该错误警告您要传递用于训练的数据的形状不正确。 x_train : 训练向量 {array-like, sparse matrix}, shape (n_samples, n_features) y : 相对于 X 数组的目标向量,形状 (n_samples,)。

由于您没有提供有关您正在使用的数据的信息,因此 X 的 (513,10) 形状是可以的,但您应该检查目标矢量形状。应该是上面提到的形状。

from sklearn.model_selection import KFold,cross_val_score
from sklearn import svm

X_train = np.array([[1,1,1,1],[1,1,1,1],[0,0,0,0],[0,0,0,0]])
y_train = np.array([1,1,0,0])

svc_clf = svm.SVC(C=50, 
                  kernel='rbf', 
                  gamma=0.1,
                  probability=False,
                  class_weight={1: 5}
                 )

# K-fold cross-validator
kfold = KFold(n_splits=4, random_state=140311, shuffle=True)

df_kfold_acc = cross_val_score(svc_clf, X_train, y_train, cv=kfold, scoring='accuracy')
print('4 fold validation accuracy scores: \n', (df_kfold_acc))
print('Kfold mean accuracy score: \n', (df_kfold_acc).mean())

输出:

4 fold validation accuracy scores: 
 [1. 1. 1. 1.]
Kfold mean accuracy score: 
 1.0

【讨论】:

  • 570 为类标签(目标),与分割前的整个特征空间的数字相同。拆分后训练集为 456,测试集为 114。
  • 实际上,当您想要适合您的分类器时它会引发错误,因此您传递的数据必须有一些东西。也许如果你能给它包含的一些信息,那么我可以提供帮助。顺便说一下,您得到 NameError 是因为您将 KFold 实例化为 Kfold,
  • 我将名称 kfold 更改为 k_fold 并以相同的错误折叠。重新编码如下:X = X_train_std and y = y_train,导致错误:TypeError: 'KFold' object is not iterable .
  • 数据本身来自 UCL 机器学习网站的印度肝脏数据集。
  • 顺便说一句,您的代码有点混乱。 for 循环实际上什么都不做,您也不需要适合您的估算器 (SVC)。我认为删除这两个并将您的kfold = Kfold(n_splits=10, random_state=140311, shuffle=True) 更改为kfold = KFold(n_splits=10, random_state=140311, shuffle=True) 可以解决问题。
猜你喜欢
  • 2023-03-19
  • 2016-11-12
  • 2021-04-14
  • 2021-01-22
  • 2020-09-06
  • 2015-09-27
  • 2019-08-07
  • 2017-06-04
  • 2016-12-01
相关资源
最近更新 更多