【问题标题】:Error while implementing cross-validation执行交叉验证时出错
【发布时间】:2020-12-26 21:19:42
【问题描述】:

我正在尝试使用交叉验证来评估模型(MNIST):

from sklearn.model_selection import StratifiedKFold
from sklearn.base import clone
skfolds = StratifiedKFold(n_splits=5, random_state=42)

在运行第 3 行时,我收到以下警告:

C:\Users\nextg\Desktop\sample_project\env\lib\site-packages\sklearn\model_selection_split.py:293: FutureWarning:设置 random_state 无效,因为 shuffle 是 错误的。这将在 0.24 中引发错误。你应该离开 random_state 为其默认值(无),或设置 shuffle=True。警告.warn(

忽略我写这段代码的警告

for train_index, test_index in skfolds.split(X_train, y_test_5):
   clone_clf = clone(sgd_clf)
   X_train_folds = X_train[train_index]
   y_train_folds = y_train[train_index]
   X_test_fold = X_test[test_index]
   y_test_fold = y_test_5[test_index]

   clone_clf.fit(X_train_folds, y_train_folds)
   y_pred = clone_clf.predict(X_test_fold)
   n_correct = sum(y_pred == y_test_fold)
   print(n_correct / len(y_pred))

运行此代码后,错误是

ValueError                                Traceback (most recent call last)
<ipython-input-66-7e786591c439> in <module>
 ----> 1 for train_index, test_index in skfolds.split(X_train, y_test_5):
  2     clone_clf = clone(sgd_clf)
  3     X_train_folds = X_train[train_index]
  4     y_train_folds = y_train[train_index]
  5     X_test_fold = X_test[test_index]

 ~\Desktop\sample_project\env\lib\site- 
 packages\sklearn\model_selection\_split.py in split(self, X, y, groups)
     326             The testing set indices for that split.
     327         """
 --> 328         X, y, groups = indexable(X, y, groups)
     329         n_samples = _num_samples(X)
     330         if self.n_splits > n_samples:

   ~\Desktop\sample_project\env\lib\site-packages\sklearn\utils\validation.py in indexable(*iterables)
    291     """
    292     result = [_make_indexable(X) for X in iterables]
--> 293     check_consistent_length(*result)
    294     return result
    295 

 ~\Desktop\sample_project\env\lib\site-packages\sklearn\utils\validation.py in check_consistent_length(*arrays)
    254     uniques = np.unique(lengths)
    255     if len(uniques) > 1:
--> 256         raise ValueError("Found input variables with inconsistent numbers of"
257                          " samples: %r" % [int(l) for l in lengths])
258 

 ValueError: Found input variables with inconsistent numbers of samples: [60000, 10000]

有人可以帮忙解决这个错误

【问题讨论】:

  • 错误到底在哪里弹出 - 在fitpredict?请使用完整的跟踪更新您的问题。
  • 感谢您的回答。问题出在拟合或预测之前的第三行代码中。模型已经在工作。通过这段代码,我试图评估我的模型。在评估时我收到了未来警告。
  • 请在问题中准确说明。我说的是错误,不是警告(这是不言自明的)。
  • 我已更新完整错误。我是否应该编写整个 MNIST 模型以更好地理解错误。

标签: python machine-learning scikit-learn cross-validation k-fold


【解决方案1】:

它的作品:

from sklearn.model_selection import StratifiedKFold
from sklearn.base import clone

skfolds = StratifiedKFold(n_splits=3, random_state=42, shuffle=True)

for train_index, test_index in skfolds.split(X_train, y_train_5):  
    clone_clf = clone(sgd_clf)
    X_train_folds = X_train.values[train_index]
    y_train_folds = y_train_5[train_index]
    X_test_fold = X_train.values[test_index]
    y_test_fold = y_train_5[test_index]
    
    clone_clf.fit(X_train_folds, y_train_folds)
    y_pred = clone_clf.predict(X_test_fold)
    n_correct = sum(y_pred == y_test_fold)
    print(n_correct / len(y_pred))

【讨论】:

    【解决方案2】:

    应该是skfolds.split(X_train, y_train_5) 而不是skfolds.split(X_train, y_test_5) 在 for 循环的第二行中,它的 y_test_fold = y_train_5[test_index] 不是 y_train_folds = y_train[train_index]

    整个问题都是因为tab键而开始的。

    【讨论】:

      【解决方案3】:

      这个表达式没有意义:skfolds.split(X_train, y_test_5)

      应该是skfolds.split(X, y)X.shape[0] == y.shape[0]

      来自doc

      for train_index, test_index in skf.split(X, y):
          print("TRAIN:", train_index, "TEST:", test_index)
          X_train, X_test = X[train_index], X[test_index]
          y_train, y_test = y[train_index], y[test_index]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-16
        • 2017-12-26
        • 1970-01-01
        • 1970-01-01
        • 2014-08-13
        • 2016-04-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多