Setting up a machine learning environment
There is a significant segmentation in the Python community due to an issue of back-compatibility between Python 2 and Python 3—many active projects still use Python 2.7 (released in 2010), while many new tools are not backward-compatible with it, because they are based on the Python 3.x. Some tools have both versions. macOS is shipped with legacy Python 2.7.10 (released in 2015) pre-installed, while an up-to-date version at the moment of writing this book is Python 3.6.1. We will use the system's default Python throughout this book, if the opposite is not mentioned explicitly. The primary reason for this is that Core ML tools are compatible only with Python 2.7.x.
First, in the Terminal, go to the user's root:
> cd ~
pip is a Python package manager. Unlike the up-to-date version of Python, the system's one doesn't have it by default. Instead, it should have the old legacy package manager easy_install. Don't use it for anything except for pip installation; it will likely mess up your system. It requires sudo privileges to install things:
> sudo easy_install pip
If you have some version of pip preinstalled, you can upgrade it to the latest one with the following command:
> pip install --upgrade pip
Many third-party programs are using the system's Python version, so to not interfere with them, it's safer to create the separate Python environment and install all dependencies that we need into it. The Virtualenv is a tool for isolated Python environment creation. It is also missing from the macOS Python, while present by default in all recent distributions starting with Python 3.3 and later. After successful installation of pip, we can use it to install virtualenv:
> pip install -U virtualenv
The -U option tells pip to install the package for the current user only.
Never run pip with the sudo. Whenever you need the sudo to run it, you know that you're doing something wrong.
To create a virtual environment for the book, run:
> cd ~ > virtualenv swift-ml-book
This will create the swift-ml-book folder, and a separate copy of Python, pip, and other tools in it. To switch to this environment (activate the environment), run the following command:
> source swift-ml-book/bin/activate
Now swift-ml-book prepends all your commands in the Terminal, so you know on which environment you are now. When you want to deactivate the Python 3 environment, run:
> deactivate
Finally, we can install libraries; be sure that you've activated the environment:
> pip install -U numpy scipy matplotlib ipython jupyter scikit-learn pydotplus coremltools
You should see a long output to the command line; downloading and installing all dependencies may take a while. Eventually, you should see the message Successfully installed ..., and a list of installed packages. It will be much longer than the one that we've provided pip with, because it includes a bunch of transient dependencies.
Most importantly, now you should have two new commands in your Terminal: ipython, and jupyter notebook. The first one runs interactive IPython REPL, and the second one runs a web-based GUI for IPython, where you can create notebooks—interactive documents, similar to Swift playgrounds.
Additionally, we should install Graphviz—an open source tool for graphs visualization. It can be downloaded from the official site, or installed using Homebrew:
> brew install graphviz
If you don't have Homebrew, install it. Installation instructions should look like the following, but you'd better check the official site (https://brew.sh/) for the exact command:
> ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"