Skip to content Skip to sidebar Skip to footer

Importerror: Cannot Import Name Np_utils

I'm trying to run the following example from keras but I get this error: ImportError Traceback (most recent call last) in ()

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_utilsimport 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)

Solution 3:

They moved everything to just utils, so if you are using tf 2.x or higher version of keras it is just tf.keras.utils or keras.utils.

So for example: keras.utils.np_utils.to_categorical -> keras.utils.to_categorical

Solution 4:

I ran into the same issue. You need to do pip install np_utils and then restart your terminal. Make sure everything is up to date.

Solution 5:

I was actually having similar issue while using

from keras.utils import to_categorical

But I managed to solve it with:

from tensorflow.keras.utils import to_categorical

Post a Comment for "Importerror: Cannot Import Name Np_utils"