How do I solve an import issue with numpy in scikit.decomposition.PCA?

Question:

I was trying to use the scikit.decomposition.PCA package and I couldn’t even import it.

import numpy as np
from sklearn.decomposition import PCA

I’ve upgraded both of np and scikit, but the error seems to be w/in scikit, what should I do?

*Note: Using Python 3.8.5 in an .ipynb file on VSCode.

This is the error that shows up:

AttributeError                            Traceback (most recent call last)
<ipython-input-1-32686ef89fa7> in <module>
      1 import numpy as np
----> 2 from sklearn.decomposition import PCA
      3 
      4 rng = np.random.RandomState(0)
      5 n_samples = 500

/opt/anaconda3/lib/python3.8/site-packages/sklearn/decomposition/__init__.py in <module>
     15 with warnings.catch_warnings():
     16     warnings.simplefilter("ignore", category=FutureWarning)
---> 17     from .dict_learning import dict_learning
     18 
     19 

/opt/anaconda3/lib/python3.8/site-packages/sklearn/decomposition/dict_learning.py in <module>
      3 import sys
      4 # mypy error: Module X has no attribute y (typically for C extensions)
----> 5 from . import _dict_learning  # type: ignore
      6 from ..externals._pep562 import Pep562
      7 from ..utils.deprecation import _raise_dep_warning_if_not_pytest

/opt/anaconda3/lib/python3.8/site-packages/sklearn/decomposition/_dict_learning.py in <module>
     19 from ..utils.extmath import randomized_svd, row_norms
...
--> 284         raise AttributeError("module {!r} has no attribute "
    285                              "{!r}".format(__name__, attr))
    286 

AttributeError: module 'numpy' has no attribute 'float'

I tried to import PCA by using

from scikit.decomposition import PCA

but I keep getting the error that module ‘numpy’ no attribute ‘float’

Note the numpy import works just fine.

Asked By: Vishakh Sandwar

||

Answers:

You probably have an incompatible version of numpy for sklearn. Try to downgrade numpy:

# Replace x by 0, 1, 2, 3, 4 or 5 (or use 'numpy<1.24.0')
[...]$ pip install 'numpy==1.23.x'
Answered By: Corralien