问题
I'm trying to import the following:
from sklearn.model_selection import train_test_split
and got following error, here's the stack trace :
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'
Actually I was trying to import :
from sklearn.cross_validation import train_test_split
Which gave me following error:
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'
So, as answered in this post, I used sklearn.model_selection
instead of sklearn.cross_validation
. But it's still giving an error.
I'm using python 3.6.8.
回答1:
This is broken in 0.22 version
Use stable versions:
For Python 2
pip uninstall scikit-learn
pip install -U scikit-learn==0.20.4
For Python 3
pip3 uninstall scikit-learn
pip3 install -U scikit-learn==0.21.3
回答2:
This is probably due to an old pip
version. You can refer to https://stackoverflow.com/a/59328446/6513708
But long story short, you need to remove the following files which were let by pip
during the update:
~/.local/lib/python3.6/site-packages/sklearn/decomposition/_online_lda.cpython-36m-x86_64-linux-gnu.so
~/.local/lib/python3.6/site-packages/sklearn/feature_extraction/_hashing.cpython-36m-x86_64-linux-gnu.so
来源:https://stackoverflow.com/questions/59320664/importerror-cannot-import-name-latentdirichletallocation