Python Scikit Learn 错误:ValueError:“找到样本数量不一致的输入变量:[4, 10]”

Posted

技术标签:

【中文标题】Python Scikit Learn 错误:ValueError:“找到样本数量不一致的输入变量:[4, 10]”【英文标题】:Python Scikit Learn error: ValueError: 'Found input variables with inconsistent numbers of samples: [4, 10]' 【发布时间】:2018-04-13 07:37:01 【问题描述】:

我正在尝试运行一个 python 程序,该程序使用 scikit-learn 对曲棍球比赛的“赢”或“输”进行二进制分类,基于 2 个数组:1 个统计数据数组(如 currentInjuries 和 Goals反对)为每支参加曲棍球比赛的球队。我习惯于 java 调试,但在 Python 中,堆栈跟踪比我习惯的要长得多。

我应该查看整个堆栈跟踪,还是只查看最相关的错误并专注于解决它?即)最底部的错误,即:

File "C:\Users\Thomas\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 181, in check_consistent_length
    " samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [4, 10]

这是整个堆栈跟踪:

pydev debugger: process 1084 is connecting

C:\Users\Thomas\Anaconda3\lib\site-packages\sklearn\cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)
C:\Users\Thomas\Anaconda3\lib\site-packages\sklearn\lda.py:6: DeprecationWarning: lda.LDA has been moved to discriminant_analysis.LinearDiscriminantAnalysis in 0.17 and will be removed in 0.19
  "in 0.17 and will be removed in 0.19", DeprecationWarning)
canucks_stats: [3, 11, 2, 2]
opposing_stats: [1, 4, 4, 15]
Backend Qt5Agg is interactive backend. Turning interactive mode on.
Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3.2\helpers\pydev\pydevd.py", line 1596, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3.2\helpers\pydev\pydevd.py", line 974, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Thomas/Desktop/!UFV/CIS480/project/NHL-Predictor.py", line 196, in <module>
    for name, (X, y) in [('moon', toms_make_moons(noise=0.3, random_state=0))#,
  File "C:/Users/Thomas/Desktop/!UFV/CIS480/project/NHL-Predictor.py", line 113, in toms_make_moons
    X, y = util_shuffle(X, y, random_state=generator)
  File "C:\Users\Thomas\Anaconda3\lib\site-packages\sklearn\utils\__init__.py", line 285, in shuffle
    return resample(*arrays, **options)
  File "C:\Users\Thomas\Anaconda3\lib\site-packages\sklearn\utils\__init__.py", line 204, in resample
    check_consistent_length(*arrays)
  File "C:\Users\Thomas\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 181, in check_consistent_length
    " samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [4, 10]

Process finished with exit code 1

【问题讨论】:

最底部的错误可以告诉您错误是什么。整个堆栈跟踪可以帮助您找到错误所在。 【参考方案1】:

最底部的错误可以显示错误是什么。整个堆栈跟踪可帮助您找到错误所在的位置。

当您发布问题时,请始终发布完整的堆栈跟踪(以了解您在哪一行收到错误)。参考how to ask

关于错误ValueError: Found input variables with inconsistent numbers of samples: [4, 10]

x 应该是一个大小为 [number_of_samples, number_of_features] 的二维数组。您可以检查一下。

还有一些参考资料:

ref1:ValueError: Found arrays with inconsistent numbers of samples [ 6 1786]

ref2:Loading jpg of different sizes into numpy.array - ValueError: Found input variables with inconsistent numbers of samples

ref3:Number of features of the model must match the input

ref4:Found input variables with inconsistent numbers of samples

【讨论】:

以上是关于Python Scikit Learn 错误:ValueError:“找到样本数量不一致的输入变量:[4, 10]”的主要内容,如果未能解决你的问题,请参考以下文章

执行 python scikit-learn 网格搜索方法时出现无效参数错误

Python Scikit Learn 错误:ValueError:“找到样本数量不一致的输入变量:[4, 10]”

导入期间 Scikit-learn 中的运行时错误

使用 cross_val_predict 与 cross_val_score 时,scikit-learn 的分数不同

Python 中的 Scikit-learn(svm 函数)

Scikit-Learn:如何处理不可排序的类型错误?