Thursday, March 30, 2017

Resolve the issue 'cannot import name multivariate_normal'

The multivariate_normal component in the scipy stats library is a very useful component for many machine learning applications. I faced an issue while trying to import this library in my Jupyter notebook and I wanted to share how I fixed it. I am running python 2.7 on an Ubuntu 14.04 machine.

As I was doing my Machine Learning Specialization at Coursera, in one of the programming assignments, I faced an issue while trying to use the multivariate_normal component of scipy.
I had to import
from scipy.stats import multivariate_normal
which was giving the below error
ImportError: cannot import name multivariate_normal

As mentioned in many online forums, I found out that the issue was, my scipy version is 0.13.3 and the multivariate_normal is supported only in 0.14 and above. Many people suggested to run the command
sudo pip install scipy --upgrade

to upgrade the scipy, but for that was failing with the below error
'ascii' codec can't decode byte 0xe2 in position 66: ordinal not in range(128)
Below are the steps I followed to fix it
Step 1) First need to make sure all scipy dependencies are installed properly
sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose
Step 2) Make sure the numpy, which scipy is dependent on is up-to-date
sudo pip install numpy --upgrade
Step 3) Make sure all the build dependencies of scipy is available
sudo apt-get build-dep python-scipy
Step 4) Rerun the upgrade

sudo pip install scipy --upgrade
Now when I try to import multivariate_normal it works fine
$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from scipy.stats import multivariate_normal
>>>

Blog Archive