Justin Pearson

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.

Breadth First Search in Haskell

Haskell is a really great programming language. It is elegant, the type system is beautiful, and nowadays the compiler is quite good. I've been using functional languages off and on for more than 30 years. I studied at the University of Kent which is the home Miranda which is a precursor to Haskell. All this is a warning. I don't use Haskell that much. The language has changed a lot since I last used it regularly, and so my code might not be optimal or idiomatic Haskell.

Creating and using Python Virtual Environments

In a lot of my courses I encourage students to use python virtual environments. Virtual environments are a great way of making sure that you have the correct version of packages installed. This is very short cheat sheet on how to set them up. I will assume that we are using Python 3. Luckily Python 3 has virtual environments set up. It is all in the documentation, but then sometimes people are too lazy to google, or do not know what to google for. Of course this assumes that you are a command line person. If you are using some IDE, then you are on your own.

Sonic Writing

Short Review of Sonic Writing by Thor Magnusson.