简体   繁体   English

ImportError:无法为sklearn导入名称“树”

[英]ImportError: cannot import name 'tree' for sklearn

I've recently installed Scipy, Numpy and Scikit-learn by using pip, but when I run the program below 我最近通过使用pip安装了Scipy,Numpy和Scikit-learn,但是当我运行以下程序时

from sklearn import tree

features = [[140, 1], [130, 1], [150, 1], [170, 1]] #input
labels = [0, 0, 1, 1] #output

clf = tree.DecisionTreeClassifier()  
clf = clf.fit(features, labels) #fit = find patterns in data

print (clf.predict([[160, 0]]))

The shell prints this error 外壳打印此错误

Traceback (most recent call last):
  File "C:/Machine Learning/sklearn.py", line 1, in <module>
    from sklearn import tree
  File "C:/Machine Learning\sklearn.py", line 1, in <module>
    from sklearn import tree
ImportError: cannot import name 'tree'

Does anyone know how to solve this? 有谁知道如何解决这个问题? I've tried uninstalling and reinstalling it, but I get the same error. 我尝试卸载并重新安装它,但是遇到相同的错误。 Many thanks in advance! 提前谢谢了!

The solution is to rename your "sklearn.py" under the "Machine Learning" folder to any other name but not "sklearn.py". 解决方案是将“机器学习”文件夹下的“ sklearn.py”重命名为其他任何名称,但不能重命名为“ sklearn.py”。

Why? 为什么? That's the mechanism of Python modules searching sequence. 这就是Python模块搜索序列的机制。 Try prepend these lines to your "sklearn.py": 尝试将这些行添加到您的“ sklearn.py”中:

import sys
print(sys.path)

You'll find the first element of the output list is always an empty string, which means the current directory has the highest priority on modules searching. 您会发现输出列表的第一个元素始终是一个空字符串,这意味着当前目录在模块搜索中具有最高优先级。 Runs from sklearn import tree at "C:\\Machine Learning" folder will import the local same name "sklearn.py" as "sklearn" module, instead of importing the machine learning module globally. from sklearn import tree “ C:\\ Machine Learning”文件夹中的from sklearn import tree运行from sklearn import tree将导入与“ sklearn”模块相同的本地名称“ sklearn.py”,而不是全局导入机器学习模块。

I had the same issue. 我遇到过同样的问题。 Using sublime text 3 as editor and build. 使用sublime text 3作为编辑器和构建。 Very simple to solve in my case. 在我的情况下,解决起来非常简单。

My working file was also named 'sklearn.py' so when I was attempting to "import sklearn" it was importing its own file found in the current working directory - without error. 我的工作文件也被命名为“ sklearn.py”,因此当我尝试“导入sklearn”时,它正在导入在当前工作目录中找到的自己的文件-没有错误。 But when I attempted to 'from sklearn import tree' it failed. 但是,当我尝试“从sklearn导入树”时,它失败了。

Just changing the name of my working file program to something else solved the problem. 只需将工作文件程序的名称更改为其他名称即可解决该问题。

Thanks. 谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导入错误:无法从“sklearn.tree”导入名称“plot_tree” - ImportError: cannot import name 'plot_tree' from 'sklearn.tree' 如何解决 ImportError: cannot import name 'DesicionTreeClassifier' from 'sklearn.tree' in python? - How to resolve the ImportError: cannot import name 'DesicionTreeClassifier' from 'sklearn.tree' in python? 来自sklearn import svm ImportError:无法导入名称lsqr - from sklearn import svm ImportError: cannot import name lsqr 导入错误:无法从“sklearn.tree”导入名称“tree” - Import Error: cannot import name 'tree' from 'sklearn.tree' 从 sklearn 导入时出现导入错误:无法导入名称 check_build - ImportError in importing from sklearn: cannot import name check_build ImportError:无法从“sklearn.model_selection”导入名称“HalvingGridSearchCV” - ImportError: cannot import name 'HalvingGridSearchCV' from 'sklearn.model_selection' ImportError:无法从“sklearn.base”导入名称“_ClassNamePrefixFeaturesOutMixin” - ImportError: cannot import name '_ClassNamePrefixFeaturesOutMixin' from 'sklearn.base' 导入错误:无法从“sklearn.neighbors._base”导入名称“克隆” - ImportError: cannot import name 'clone' from 'sklearn.neighbors._base ImportError:导入 sklearn.mixture 时无法导入名称选择 - ImportError: cannot import name choice when importing sklearn.mixture ImportError:无法从“ sklearn.base”导入名称“ _UnstableArchMixin” - ImportError: cannot import name '_UnstableArchMixin' from 'sklearn.base'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM