Setting up Python notebooks locally for Machine Learning

This is mainly for students on my machine learning course. I use a lot of Python notebooks either for examples from lectures or for assignments. While there are many options for hosting python notebooks for free remotely, but I prefer to do things locally. If you wish to do the same then please read on. Note that I did home some problems installing skikit-image on a new Apple with M1 silicon, but after upgrading the latest version of the operating system and doing and updating my homebrew setup everything works. Note that these instructions are for using pip. If you are using an alternative package manager such as Anaconda then you are on your own. I know nothing about setting up Python on a windows machine.

Setting up the Virtual Environment

First it is better set up a virtual environment (see Creating and Using Virtual Environments in Python). In a suitable place on your machine do:

python3 -m venv env
source ./env/bin/activate

Note that you might be prompted to upgrade pip. If you are, then follow the instructions. You are only upgrading the version in your local virtual environment.

Installing the relevant packages

When you prompt changes to (env) you are inside your virtual environment. You can now use pip to install packages. For reasons that I’m not going to explain your life is made a little easier if you install wheel

pip install wheel

You can now install various useful packages for machine learning

 pip install numpy  scipy matplotlib scikit-learn pandas scikit-image
 treelib nltk

This might take a while. The package jupyter drives the notebooks, so install that

pip install jupyter

Emacs key bindings

I have been using the editor Emacs for over 30 years. The key binding are hardwired into my brain. If you are like me then installing the jupyter-emacskeys package will make your life a little less frustrating.

pip install jupyter-emacskeys

Launching notebooks

You talk to Python notebooks using the web browser when you are running them locally you start your own little server

jupyter notebook

If you have thing configured then a web browser might open up. If not then look at the output on the terminal and you will see something like:

[I 09:47:31.185 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 09:47:31.216 NotebookApp] 
    
    To access the notebook, open this file in a browser:
        file:///Users/justin/Library/Jupyter/runtime/nbserver-49899-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=83e4ad6deece18a89f8c244f28bb3268d115a4a047764f36
     or http://127.0.0.1:8888/?token=83e4ad6deece18a89f8c244f28bb3268d115a4a047764f36

So in this case you can point your browser at http://127.0.0.1:8888/?token=83e4ad6deece18a89f8c244f28bb3268d115a4a047764f36 and you are ready to go.

Justin Pearson
Justin Pearson
Docent in Computing Science

Lecturer and researcher at Uppsala University.