Import in jupyter notebook fails and command line succeeds - python

The following code imports absolutely fine at the command line:
# Modelling Algorithms
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
However in Jupyter I get the following error:
ImportError Traceback (most recent call last)
<ipython-input-7-f56620e65a88> in <module>()
8
9 # Modelling Algorithms
---> 10 from sklearn.tree import DecisionTreeClassifier
11 from sklearn.linear_model import LogisticRegression
12 from sklearn.neighbors import KNeighborsClassifier
/home/username/.virtualenvs/jupyter/local/lib/python2.7/site-packages/sklearn/__init__.py in <module>()
54 # process, as it may not be compiled yet
55 else:
---> 56 from . import __check_build
57 from .base import clone
58 __check_build # avoid flakes unused variable error
ImportError: cannot import name __check_build
The output of sys.executable is the same.
I would love to fix this.

Related

AttributeError: module 'sklearn.metrics._dist_metrics' has no attribute 'DistanceMetric32'

I'm new to scikit-learn, and currently I just want to use its train_test_split function. Below is what I typed:
from sklearn.model_selection import train_test_split
And this is the error message:
Output exceeds the size limit. Open the full output data in a text editor
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[18], line 1
----> 1 from sklearn.model_selection import train_test_split
File ~/.conda/envs/tf/lib/python3.10/site-packages/sklearn/model_selection/__init__.py:23
20 from ._split import train_test_split
21 from ._split import check_cv
---> 23 from ._validation import cross_val_score
24 from ._validation import cross_val_predict
25 from ._validation import cross_validate
File ~/.conda/envs/tf/lib/python3.10/site-packages/sklearn/model_selection/_validation.py:32
30 from ..utils.fixes import delayed
31 from ..utils.metaestimators import _safe_split
---> 32 from ..metrics import check_scoring
33 from ..metrics._scorer import _check_multimetric_scoring, _MultimetricScorer
34 from ..exceptions import FitFailedWarning
File ~/.conda/envs/tf/lib/python3.10/site-packages/sklearn/metrics/__init__.py:41
37 from ._classification import multilabel_confusion_matrix
39 from ._dist_metrics import DistanceMetric
---> 41 from . import cluster
42 from .cluster import adjusted_mutual_info_score
43 from .cluster import adjusted_rand_score
...
19 )
File sklearn/metrics/_pairwise_distances_reduction/_base.pyx:1, in init sklearn.metrics._pairwise_distances_reduction._base()
AttributeError: module 'sklearn.metrics._dist_metrics' has no attribute 'DistanceMetric32'
I've tried to search for similar errors, and most of them are version issues. My Python is 3.10.8 and scikit-learn is 1.2.0. I tried to upgrading (1.2.0 is already the latest) and downgrading, but the error remains. In addition, the other errors I found are lack of some other attributes rather than 'DistanceMetric32'. Could anyone provides some ideas?

cannot import name 'asanyarray'

I'm currently having trouble importing some simple packages.
I'm working on pycharm professional in a virtual environment.
just trying to load;
import pandas as pd
import numpy as np
from sklearn.ensemble import IsolationForest
from sklearn.ensemble._iforest import _average_path_length
import shap
importError Traceback (most recent call last)
<ipython-input-1-5b7f1bf909af> in <module>
----> 1 import pandas as pd
2 import numpy as np
3 from sklearn.ensemble import IsolationForest
4 from sklearn.ensemble._iforest import _average_path_length
5 import shap
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pandas\__init__.py in <module>
14
15 if missing_dependencies:
---> 16 raise ImportError(
17 "Unable to import required dependencies:\n" + "\n".join(missing_dependencies)
18 )
ImportError: Unable to import required dependencies:
numpy: cannot import name 'asanyarray' from 'numpy.core.multiarray' (C:\Users\James\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\numpy\core\multiarray.py
I've never come across this error before so don't know how to fix.

import Porter error ModuleNotFoundError: No module named 'sklearn.tree.tree'

I am not able to import Porter; I am facing
ModuleNotFoundError: No module named 'sklearn.tree.tree' error
Code:
from sklearn_porter import Porter
Error message:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-19-574fff36293e> in <module>
----> 1 from sklearn_porter import Porter
2
3 porter = Porter(clf, language='c')
4 output = porter.export()
5
~/.local/lib/python3.8/site-packages/sklearn_porter/__init__.py in <module>
7 from json import load
8
----> 9 from sklearn_porter.Porter import Porter
10
11
~/.local/lib/python3.8/site-packages/sklearn_porter/Porter.py in <module>
8
9 from sklearn.metrics import accuracy_score
---> 10 from sklearn.tree.tree import DecisionTreeClassifier
11 from sklearn.ensemble.weight_boosting import AdaBoostClassifier
12 from sklearn.ensemble.forest import RandomForestClassifier
ModuleNotFoundError: No module named 'sklearn.tree.tree'
I guess your version of sklearn is recent (0.20 or newer). In this case the import fails. You could downgrade sklearn, but this is not suggested. Or you can edit the imports in sklearn_porter/Porter.py by hand. Here are the lines to change.
There is a related pull request on GitHub to close this issue in the future.

AttributeError: type object 'sklearn.tree._criterion.array' has no attribute '__reduce_cython__'

I want to import
from sklearn.tree import DecisionTreeRegressor in jupyter.
but when I run just the line contains above import, I got this error.
<ipython-input-16-28b3e81cd98d> in <module>
----> 1 from sklearn.tree import DecisionTreeRegressor
~/.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/_criterion.cpython-36m-x86_64-linux-gnu.so in init sklearn.tree._criterion()
AttributeError: type object 'sklearn.tree._criterion.array' has no attribute '__reduce_cython__'
I tried to install scipy and update scikit-learn by conda but it didn't solved.
Try to update Cython :
pip install cython

ModuleNotFoundError: No module named 'sklearn.tree._criterion'

I am able to import sklearn on jupyter notebook. however, when I am trying to import "DecisionTreeClassifier", I am facing "ModuleNotFoundError: No module named 'sklearn.tree._criterion'" error
Code:
from sklearn.tree import DecisionTreeClassifier
Error message:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-462-f146622b3284> in <module>()
----> 1 from sklearn.tree import DecisionTreeClassifier
~\Anaconda3\lib\site-packages\sklearn\tree\__init__.py in <module>()
4 """
5
----> 6 from .tree import DecisionTreeClassifier
7 from .tree import DecisionTreeRegressor
8 from .tree import ExtraTreeClassifier
~\Anaconda3\lib\site-packages\sklearn\tree\tree.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
ModuleNotFoundError: No module named 'sklearn.tree._criterion'
Any help greatly appreciated!

Categories