Unable to import 'MinMaxScaler' from sklearn.preprocessing - python-3.x

I tried all possible ways and checked everywhere, but I couldn't find the ideal solution.
Uninstalled sklearn, scikit-learn
Installed sklearn 0.23
Upgraded sklearn
but I still didn't succeed.
ImportError: cannot import name 'MinMaxScalar' from 'sklearn.preprocessing' (C:\Users\MY PC\anaconda3\lib\site-packages\sklearn\preprocessing\__init__.py)

You should change the letter A to an E, so MinMaxScalar changes to MinMaxScaler.

Change the last letter a to e in class name
from sklearn.preprocessing import MinMaxScaler

Related

ImportError: cannot import name 'PCA' from 'matplotlib.mlab'

According to this task:
Principal Component Analysis (PCA) in Python
I included this line
import from matplotlib.mlab import PCA
but I get the error message:
cannot import name 'PCA' from 'matplotlib.mlab'
I'm using Python3.7 and I have no idea how I can use the PCA function from matlab. Is the new version of matplotlib depricated or is PCA included to another library?
I really don't know if it is too late to reply now. But I will just place it here anyways.
import numpy as np
from sklearn.decomposition import PCA

Not able to find out the version of a module

I have imported norm as:
from scipy.stats import norm
I want to find out the version using:
print(scipy.__version__)
but it is raising an error called:
NameError: name 'scipy' is not defined
if i am using this:
print(norm.__version__)
but it is raising another error called:
AttributeError: 'norm_gen' object has no attribute '__version__'
Please help me to solve this issue.
Thanks
The line from scipy.stats import norm doesn't make the name scipy available in your current namespace. To use scipy.__version__, you must first import scipy.
In [57]: import scipy
In [58]: print(scipy.__version__)
1.4.1

How can I solve cannot import name 'fetch_openml' from 'sklearn.datasets'

I'm learning sklearn, but I can't use fetch_openml(). It says,
ImportError: cannot import name 'fetch_openml' from 'sklearn.datasets'
In the new version of sklearn, it's even easier to fetch open ML Datasets. For example, you can add import and fetch mnist dataset as:
from sklearn.datasets import fetch_openml
X, y = fetch_openml('mnist_784', version=1, return_X_y=True, as_frame=False)
print(X.shape, y.shape)
For more details check official example.
You can use this:
from sklearn.datasets import fetch_openml
Apparently, fetch_mldata has been deprecated in the newer sklearn. Use load_digits to achieve loading the MNIST data.
To solve this problem in jupyter follow these steps:
Download file mnist-original from " https://osf.io/jda6s/"
after download file copy it into C:\Users\YOURUSERNAME\scikit_learn_data\mldata
in notebook jupyter do:
from sklearn.datasets import fetch_mldata
mnist = fetch_mldata('mnist-original')

How to run a specific sklearn version?

On my mac, I installed multiple versions of Sklearn as shown below:
Sklearn 0.19.1
~/anaconda2/pkgs/scikit-learn-0.19.1-py27h9788993_0/lib/python2.7/site-packages/sklearn
Sklearn 0.20.0
~/anaconda2/pkgs/scikit-learn-0.20.0-py27h4f467ca_1/lib/python2.7/site-packages/sklearn
When starting jupyter, it automatically runs the sklearn 0.20.0. I was wondering whether there is a way to run sklearn 0.19.1.
Thanks a lot,
Jeff
This should work, am not saying it is elegant but is what I would personally try first. sys.path is a list of all the places where it goes to import modules, so you first remove any occurrences of the one you don't want and then put in the one you do want.
In a cell before you import from sklearn:
import sys
syspath = sys.path
indexes = [i for i, s in enumerate(syspath) if 'scikit-learn-0.20.0-py27h4f467ca_1' in s]
for index in indexes:
syspath.pop(index)
sys.path.insert(0, '~/anaconda2/pkgs/scikit-learn-0.19.1-py27h9788993_0/lib/python2.7/site-packages/sklearn')
# now if you import from sklearn, should come from 19

Why the import "from tensorflow.train import Feature" doesn't work

That's probably totally noob question which has something to do with python module importing, but I can't understand why the following is valid:
> import tensorflow as tf
> f = tf.train.Feature()
> from tensorflow import train
> f = train.Feature()
But the following statement causes an error:
> from tensorflow.train import Feature
ModuleNotFoundError: No module named 'tensorflow.train'
Can please somebody explain me why it doesn't work this way? My goal is to use more short notation in the code like this:
> example = Example(
features=Features(feature={
'x1': Feature(float_list=FloatList(value=feature_x1.ravel())),
'x2': Feature(float_list=FloatList(value=feature_x2.ravel())),
'y': Feature(int64_list=Int64List(value=label))
})
)
tensorflow version is 1.7.0
Solution
Replace
from tensorflow.train import Feature
with
from tensorflow.core.example.feature_pb2 import Feature
Explanation
Remarks about TensorFlow's Aliases
In general, you have to remember that, for example:
from tensorflow import train
is actually an alias for
from tensorflow.python.training import training
You can easily check the real module name by printing the module. For the current example you will get:
from tensorflow import train
print (train)
<module 'tensorflow.python.training.training' from ....
Your Problem
In Tensorflow 1.7, you can't use from tensorflow.train import Feature, because the from clause needs an actual module name (and not an alias). Given train is an alias, you will get an ImportError.
By doing
from tensorflow import train
print (train.Feature)
<class 'tensorflow.core.example.feature_pb2.Feature'>
you'll get the complete path of train. Now, you can use the import path as shown above in the solution above.
Note
In TensorFlow 1.9.0, from tensorflow.train import Feature will work, because tensorflow.train is an actual package, which you can therefore import. (This is what I see in my installed Tensorflow 1.9.0, as well as in the documentation, but not in the Github repository. It must be generated somewhere.)
Info about the path of the modules
You can find the complete module path in the docs. Every module has a "Defined in" section. See image below (taken from Module: tf.train):
I would advise against importing Feature (or any other object) from the non-public API, which is inconvenient (you have to figure out where Feature is actually defined), verbose, and subject to change in future versions.
I would suggest as an alternative to simply define
import tensorflow as tf
Feature = tf.train.Feature

Resources