导入sklearn错误

时间:2016-12-19 05:55:20

标签: python numpy scipy scikit-learn pycharm

所以我一直在努力为我正在服用的课程安装numpy,scipy和sklearn。在经过多次尝试和多次尝试之后,我安装了pycharm并使用他们内置的包管理器来获得numpy和scipy。我也安装了sklearn,但是当我在我的代码中导入它时,我收到以下错误:

Traceback (most recent call last):
  Python Shell, prompt 1, line 3
  File "C:\Users\Berges\AppData\Local\Programs\Python\Python35\Lib\site-packages\sklearn\__init__.py", line 57, in <module>
    from .base import clone
  File "C:\Users\Berges\AppData\Local\Programs\Python\Python35\Lib\site-packages\sklearn\base.py", line 12, in <module>
    from .utils.fixes import signature
  File "C:\Users\Berges\AppData\Local\Programs\Python\Python35\Lib\site-packages\sklearn\utils\__init__.py", line 11, in <module>
    from .validation import (as_float_array,
  File "C:\Users\Berges\AppData\Local\Programs\Python\Python35\Lib\site-packages\sklearn\utils\validation.py", line 18, in <module>
    from ..utils.fixes import signature
  File "C:\Users\Berges\AppData\Local\Programs\Python\Python35\Lib\site-packages\sklearn\utils\fixes.py", line 406, in <module>
    if np_version < (1, 12, 0):
builtins.TypeError: unorderable types: str() < int()

(我正在使用python 3.5.2,当你在bash上运行python3时,我可以导入sklearn就好了,但似乎是使用python 3.4.3)

更新:

我安装了Anaconda并尝试从Wing IDE和Atom运行以下代码:

import numpy as np
X = np.array([[-1,-1],[-2,-1],[-3.-2],[1,1],[2,1],[3,2]])
Y = np.array([1,1,1,2,2,2])
from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf.fit(X,Y)
print(clf.predict([[-0.8,-1]]))

然后我收到以下错误:

Traceback (most recent call last):
  File "C:\Users\Berges\Downloads\test.py", line 6, in <module>
    clf.fit(X,Y)
  File "C:\Users\Berges\Anaconda3\lib\site-packages\sklearn\naive_bayes.py", line 173, in fit
    X, y = check_X_y(X, y)
  File "C:\Users\Berges\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 510, in check_X_y
    ensure_min_features, warn_on_dtype, estimator)
  File "C:\Users\Berges\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 373, in check_array
    array = np.array(array, dtype=dtype, order=order, copy=copy)
ValueError: setting an array element with a sequence.

2 个答案:

答案 0 :(得分:0)

如果您只是希望获得某些课程并且想要在Windows上运行它。然后我建议你安装anaconda包管理器。它在Windows上轻而易举地工作,并且非常容易安装并包含所有必需的软件包(您不必担心版本不匹配和所有问题)。

安装anaconda管理器后,将pycharm解释器位置更改为anaconda安装的python解释器。

链接:https://www.continuum.io/downloads 可用包列表:https://docs.continuum.io/anaconda/pkg-docs

答案 1 :(得分:0)

您错过了导致此错误的小细节。

在X阵列中,您输入[-3, -2]而不是import numpy as np X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]) Y = np.array([1, 1, 1, 2, 2, 2]) from sklearn.naive_bayes import GaussianNB clf = GaussianNB() clf.fit(X, Y) print(clf.predict([[-0.8, -1]]))

所以你错过了一个逗号。

以下工作正常:

[1]

<强>结果:

()