SKLearn 0.20.2 - Import error with RandomizedPCA?

Aaraeus picture Aaraeus · Feb 2, 2019 · Viewed 15.5k times · Source

I'm trying to do the Udacity mini project and I've got the latest version of the SKLearn library installed (20.2).

When I run:

from sklearn.decomposition import RandomizedPCA

I get the error:

ImportError: cannot import name 'RandomizedPCA' from 'sklearn.decomposition' (/Users/kintesh/Documents/udacity_ml/python3/venv/lib/python3.7/site-packages/sklearn/decomposition/__init__.py)

I actually even upgraded the version using:

pip3 install -U scikit-learn

Which upgraded from 0.20.0 to 0.20.2, which also uninstalled and reinstalled... so I'm not sure why it can't initialise sklearn.decomposition.

Are there any solutions here that might not result in completely uninstalling python3 from my machine?! Would ideally like to avoid that.

Any help would be thoroughly appreciated!

Edit:

I'm doing some digging and trying to fix this, and it appears as though the __init__.py file in the decomposition library on the SKLearn GitHub doesn't reference RandomizedPCA... has it been removed or something?

Link to the GitHub page

Answer

Aaraeus picture Aaraeus · Feb 2, 2019

As it turns out, RandomizePCA() was depreciated in an older version of SKLearn and is simply a parameter in PCA().

You can fix this by changing the import statement to:

from sklearn.decomposition import PCA as RandomizedPCA

... and then your classifier looks like this:

pca = RandomizedPCA(n_components=n_components, svd_solver='randomized', whiten=True).fit(X_train)

However, if you're here because you're doing the Udacity Machine Learning course on Eigenfaces.py, you'll notice that the PIL library is also deprecated.

Unfortunately I don't have a solution for that one, but here's the GitHub issue page, and here's a kind hearted soul that used a Jupyter Notebook to solve their mini-project back when these repositories worked.

I hope this helps, and gives enough information for the next person to get into Machine Learning. If I get some time I might take a crack at recoding eigenfaces.py for SKLearn 0.20.2, but for now I'm just going to crack on with the rest of this course.