python - 导入错误: cannot import name 'LatentDirichletAllocation'

标签 python python-3.x scikit-learn sklearn-pandas train-test-split

我正在尝试导入以下内容:

from sklearn.model_selection import train_test_split

并出现以下错误,这是堆栈跟踪:

ImportError                               Traceback (most recent call last)
<ipython-input-1-bdd2a2f20673> in <module>
      2 import pandas as pd
      3 from sklearn.model_selection import train_test_split
----> 4 from sklearn.tree import DecisionTreeClassifier
      5 from sklearn.metrics import accuracy_score
      6 from sklearn import tree

~/.local/lib/python3.6/site-packages/sklearn/tree/__init__.py in <module>
      4 """
      5 
----> 6 from ._classes import BaseDecisionTree
      7 from ._classes import DecisionTreeClassifier
      8 from ._classes import DecisionTreeRegressor

~/.local/lib/python3.6/site-packages/sklearn/tree/_classes.py in <module>
     38 from ..utils.validation import check_is_fitted
     39 
---> 40 from ._criterion import Criterion
     41 from ._splitter import Splitter
     42 from ._tree import DepthFirstTreeBuilder

~/.local/lib/python3.6/site-packages/sklearn/tree/_splitter.pxd in init sklearn.tree._criterion()

~/.local/lib/python3.6/site-packages/sklearn/tree/_tree.pxd in init sklearn.tree._splitter()

~/.local/lib/python3.6/site-packages/sklearn/neighbors/_quad_tree.pxd in init sklearn.tree._tree()

~/.local/lib/python3.6/site-packages/sklearn/neighbors/__init__.py in <module>
     15 from ._kde import KernelDensity
     16 from ._lof import LocalOutlierFactor
---> 17 from ._nca import NeighborhoodComponentsAnalysis
     18 from ._base import VALID_METRICS, VALID_METRICS_SPARSE
     19 

~/.local/lib/python3.6/site-packages/sklearn/neighbors/_nca.py in <module>
     20 from ..base import BaseEstimator, TransformerMixin
     21 from ..preprocessing import LabelEncoder
---> 22 from ..decomposition import PCA
     23 from ..utils.multiclass import check_classification_targets
     24 from ..utils.random import check_random_state

~/.local/lib/python3.6/site-packages/sklearn/decomposition/__init__.py in <module>
     17 from ._factor_analysis import FactorAnalysis
     18 from ..utils.extmath import randomized_svd
---> 19 from ._online_lda import LatentDirichletAllocation
     20 
     21 __all__ = ['DictionaryLearning',

ImportError: cannot import name 'LatentDirichletAllocation'

实际上我正在尝试导入:

from sklearn.cross_validation import train_test_split

这给了我以下错误:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-2-9ebede864c4d> in <module>
      1 import numpy as np
      2 import pandas as pd
----> 3 from sklearn.cross_validation import train_test_split
      4 from sklearn.tree import DecisionTreeClassifier
      5 from sklearn.metrics import accuracy_score

ModuleNotFoundError: No module named 'sklearn.cross_validation'

所以,正如 this 中的回答发布后,我使用了 sklearn.model_selection 而不是 sklearn.cross_validation。但还是报错。

我使用的是 python 3.6.8。

最佳答案

这在 0.22 版本中已被破坏

使用稳定版本:

对于 Python 2

pip uninstall scikit-learn
pip install -U scikit-learn==0.20.4

对于 Python 3

pip3 uninstall scikit-learn
pip3 install -U scikit-learn==0.21.3

关于python - 导入错误: cannot import name 'LatentDirichletAllocation' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59320664/

相关文章:

python - 多边算法

python - 如何获取内置模块上重写内置方法的原始版本?

python - 如何处理 boost::python 中的 Python 切片对象?

tensorflow - 在 python 中保存 Keras/Sklearn 并在 tensorflow.js 中加载保存的模型

android - 将 Python 代码移植到 Android

python - 对于 scikit-learn 中的每个文件,如何从 TF-idf 向量中获取最高频率项?

python - 为什么 python gstreamer 在我的脚本顶部没有 "gobject.threads_init()"时崩溃?

python - 使用 Python 将 PDF 文件读取为文本

Python Pandas - Groupby 和 Mean,但保留列名称

python - 在 python selenium webdriver 中发送 key 而不指定元素