neonwatty / machine_learning_refined

Notes, examples, and Python demos for the 2nd edition of the textbook "Machine Learning Refined" (published by Cambridge University Press).
Other
1.67k stars 603 forks source link

Which version of matplotlib was used? #23

Closed jflanaga closed 2 years ago

jflanaga commented 2 years ago

Nice book and repo. I was running through some of the examples in the 2_5_Random.ipynb. Some of the plots render OK, but then others don't (e.g., static_plotter.two_input_surface_contour_plot) and for those I get a long series of the following errors/warnings (matplotlib: 3.5.1).

*c* argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with *x* & *y*.  Please use the *color* keyword-argument or provide a 2D array with a single row if you intend to specify the same RGB or RGBA value for all points.

I was wondering whether you had a requirements.txt that specified the version of the libraries used for creating the notebooks.

It might be something else than what the message says. I would have thought that warnings wouldn’t stop the plot from being run. But a requirements.txt might help identify the culprit.

neonwatty commented 2 years ago

Hey! Thanks for the heads up on this issue!

In the past we recommended conda (for notebook, pandas, numpy, matplotlib, scipy) along with a pip install of autograd. But with the custom animation work in the notes being an open vector for failure, I think your issue points to the need for upgraded guidance on installation. I'll provide this tomorrow and get back to you.

As to your issue - I think there could be one of two things going on.

I received this warning with matplotlib 3.5.1 + (plain) jupyter notebook, but visualizations / widgets work. I just pushed a temp warnings quiet fix.

I received this warning with matplotlib 3.5.1 + jupyterlab but visualizations / widgets did NOT work. Working on a patch there - it appears some widgets / viz tools have trouble with jupyterlab.

What setup are you using (distro, jupyter notebook/lab, docker, etc.,)?

jflanaga commented 2 years ago

I generally use conda as well. I got the error with Python 3.9.12 and matplotlib: 3.5.1. But as you said, it looks like a jupyter issue. Here's following selected core Jupyter packages that gave me the problem (and it was a plain jupyter notebook, not jupyterlab)

Selected Jupyter core packages...  
IPython          : 8.2.0  
ipykernel        : 6.9.1  
ipywidgets       : 7.6.5  
jupyter_client   : 6.1.12  
jupyter_core     : 4.9.2  
jupyter_server   : 1.13.5  
jupyterlab       : 3.3.2  
nbclient         : 0.5.13  
nbconvert        : 6.4.4  
nbformat         : 5.3.0  
notebook         : 6.4.8  
qtconsole        : 5.3.0  
traitlets        : 5.1.1  

As I said, even though jupyterlab is installed, I was just using jupyter notebook.

I then created a conda environment with Python 3.8 and just installed jupyter (as well as some other packages), not jupyter lab. Then I got the warnings, but the plots rendered, as they did with you.

3.8.13  
matplotlib: 3.2.2  

Selected Jupyter core packages...  
IPython          : 8.3.0  
ipykernel        : 6.9.1  
ipywidgets       : 7.6.5  
jupyter_client   : 7.2.2  
jupyter_core     : 4.10.0  
jupyter_server   : not installed  
jupyterlab       : not installed  
nbclient         : 0.5.13  
nbconvert        : 6.4.4  
nbformat         : 5.3.0  
notebook         : 6.4.11  
qtconsole        : 5.3.0  
traitlets        : 5.1.1  

I think the main reason for having a requirements.txt or <env_name>.yml is for the conda environments. Conda is a pretty large install. It might be nice just to create separate environments with packages that are just needed for that environment. Ironically, I got the problem because I originally didn't create a separate environment and just used the full conda installation.

jflanaga commented 2 years ago

It's really strange. I'm getting different results depending upon the version of matplotlib. Anyways, I created a conda enviroment with the following:

conda create --name ml_refined python=3.8 jupyter scipy pandas matplotlib 

This reproduced the problem of the plots not rendering (of course, you then need to install autograd with pip)

Then I did did the following:

conda create --name ml_refined2 python=3.8 jupyter scipy pandas  
conda activate ml_refined2  
conda install -c conda-forge matplotlib 
pip install autograd 

Now the plots render (but with the error messages).

The matplotlib versions are different in the two. The one that fails is 3.5.1 (that gets installed the first way). The one that works is 3.2.2. But 3.5 works for you, so I'm a bit mystifed what the issue is.

I'm on a Mac (still on Catalina, 10.15.7), but I didn't think that would make a difference.

Anyways, maybe this would help diagnose the problem.

neonwatty commented 2 years ago

After digging around a bit on the jupyter-side issue: I think the issue is with the conda installs of jupyter and/or matplotlib. Following your install instructions I encountered the same issue (mac Monterey 12.3.1). Looks like conda-forge matplotlib is 3.5.2 - but didn't seem to matter.

What did work for me via conda env - using pip installs (all latest versions) instead of conda's.

I've added instructions (below) for this conda approach and docker for running the notebooks - tested these on a) ubuntu 20.04 b) mac Monterey (12.3.1) and c) mac big sur 11.1.

Both conda and docker methods worked for me - pull the repo for updates and let me know how things go on your end!

Docker method

After installing docker and docker-compose on your machine
traverse to this repo at your terminal and type

docker-compose up -d

When running this command the first time an associated docker image is pulled from DockerHub.

Then in any web browser go to

localhost:8888

to view the repository contents - including jupyter notebooks.

Anaconda method

After installing Anaconda Python 3 distribution on your machine, cd into this repo's directory and follow these steps to create a conda virtual environment to view its contents and notebooks.

First, create the environment

conda create python=3.8 --name mlr2 --file requirements.txt

Then activate it

conda activate mlr2

Run jupyter via the command below

jupyter notebook --port=8888 --ip=0.0.0.0 --allow-root --NotebookApp.token=''

And finally, open any web browser and traverse to

localhost:8888

to view the repository contents - including jupyter notebooks.

jflanaga commented 2 years ago

Thanks for the detailed instructions! Unfortunately, that didn't work for me either. (I tried two methods -- I was a bit confused because I thought you had written that you installed the packages with pip, but wouldn't the method you have above install it with conda? -- It doesn't matter -- I tried pip install -r requirements.txt after activating the environment and that didn't work either.

There must be something on this machine. The version of matplotlib I installed from conda-forge is definitely 3.2.2. I don't know why it's installing that version -- my version of conda is 4.13. But maybe it's the OS. I have a Ubuntu machine at work that I'll have to try tomorrow and see if the method above works for that. But as it's working for you, it seems that it is more something with own configuration. (I should update this machine but I'm waiting to get a new Mac and thought I would wait until then -- supply issues meant that the wait is much longer than I had anticipated).

I do have things working with the older version of matplotlib described with the method above.

Thanks for the help.

On Edit the issue with the install of matplotlib versions seems to be due to the version of Python. On my machine, if the python version is 3.8, then the matplotlib version that gets installed from conda-forge is 3.2.2. If the Python version is 3.9, then the version is 3.5.1 (not 3.5.2, but at least it's in the right ballpark). So that clears up that issue for me.

neonwatty commented 2 years ago

bummer! did the docker version not work? this should install a fresh containerized version of Python + required installs which should be distinct from your machine's Python.

lol! you're right - the requirements.txt is conda installed the way I wrote the instructions.

how about using pip3 instead of pip since you're using Python 3?

pip3 install -r requirements.txt

This works on my end (python 3.8).

jflanaga commented 2 years ago

I'll have to try Docker tomorrow. As far as I can tell, if the matplotlib version is 3.5+, it doesn't work on this particular machine. (I did try pip3, but as it's the Anaconda version of Python that's in my path, I believe there's no difference between the two). As I said it's not very pressing, as it looks like I've got everything working with matplotlib 3.2.2. But again, thanks for all the assistance.

neonwatty commented 2 years ago

gotcha! please re-open if you need any further help!