close

[Solved] ImportError: cannot import name np_utils

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error ImportError: cannot import name np_utils in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How ImportError: cannot import name np_utils Error Occurs?

Today I get the following error ImportError: cannot import name np_utils in python.

How To Solve ImportError: cannot import name np_utils Error ?

  1. How To Solve ImportError: cannot import name np_utils Error ?

    To Solve ImportError: cannot import name np_utils Error np_utils is a separate package (and a keras dependency – which doesn't get install with it). Can be installed using pip:

  2. ImportError: cannot import name np_utils

    To Solve ImportError: cannot import name np_utils Error np_utils is a separate package (and a keras dependency – which doesn't get install with it). Can be installed using pip:

Solution 1

np_utils is a separate package (and a keras dependency – which doesn’t get install with it). Can be installed using pip:

pip install np_utils

using – Keras==2.0.6


Suggestion: For some odd (and still unknown) reasons, even after installing the import

from keras.utils.np_utils import to_categorical

didn’t work – I had to restart the notebook (first restart even didn’t work), and once it worked, I got stuck again for same import call (gave exception for no module named tensorflow) – as in utils there’s another import from . import conv_utils, which required the tensorflow.

I did try installing tensorflow using pip install tensorflow gave:

Could not find a version that satisfies the requirement tensorflow (from versions: ) No matching distribution found for tensorflow

even this gist didn’t work for me.


Finally, I installed Anaconda – which have all the scientific packages (numpy, scipy, scikit-learn,..) pre-installed. Installed keras:

conda install keras

Best thing was, it even installed tensorflow as its dependency.

Solution 2

For keras > 2.0, please use from keras.utils import to_categorical instead.

Example of usage will be to_categorical(y, num_classes=None)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read