Reinstalling the Python machine learning environment in my Mac
That is conda and python 3.7, tensorflow, keras, numpy, pandas, jupyter, sklearn etc.
Something went wrong with my previous installation of Anaconda on an upgrade and I decided to reinstall it. Debugging it, would have probably taken more time (if possible at all), while a fresh start is always welcome as long as your code and data are secure. And the Anaconda installation is totally and easily replaceable. So straight to the steps:
Step 1: I removed my old installation
- The Anaconda folder was in
~/anacondaso I did a
rm -rf anaconda3while in my home folder.
- I also removed an entry in my
Step 2: I installed Anaconda again
- Downloaded and run the installer for Python 3.7
- Followed the standard, as is installation. This would also install Anaconda paths on my zsh profile. And indeed it did (
tail -n 20 ~/.zshrc):
- Anaconda installed at
Step 3: I verified the installation
- From the Applications folder I opened the Anaconda Navigator (didn’t wait until it was indexed by Spotlight).
Step 4: I also opened a terminal and run a few commands
- everything seems to be working
Step 5: I updated
conda to latest version (and not all the packages)
conda update conda(this took
condafrom 4.8.2 => 4.8.3)
- I didn’t update all the packages to their latest version. Basically I followed the advice from here and the section “Why Updating the Anaconda Package is Almost Always a Bad Idea”. It is a fresh install and if I need to update any package down the road I will do so individually and for a reason.
Step 6: I ensured that the basic libraries I want are installed from a script in this Machine Learning Mastery blog post:
print('scipy: %s' % scipy.__version__)
print('numpy: %s' % numpy.__version__)
print('matplotlib: %s' % matplotlib.__version__)
print('pandas: %s' % pandas.__version__)
print('statsmodels: %s' % statsmodels.__version__)
print('sklearn: %s' % sklearn.__version__)
print('tensorflow: %s' % tensorflow.__version__)
print('keras: %s' % tensorflow.keras.__version__)
I have created a gist for that so:
- git clone https://gist.github.com/848a30b3ede1be71dfcbd032d7c6cd3e.git
- cd 848a30b3ede1be71dfcbd032d7c6cd3e
- python confirm-ml.py
The standard machine learning libraries I wanted are installed in the base environment so what’s left is to also add TensorFlow to complete the installation. But before that I’ll check also that Jupyter notebooks are running.
Step 6: Check Jupyter notebooks
From the command line with
jupyter notebook I saw the Jupyter environment in my browser (
http://localhost:8888/tree) and I was able to also create a new notebook in Python 3.
Step 7: Clone the base environment and create a new one for tensorflow
So now the idea is to create a new environment for TensorFlow, but in addition keep the base packages. To do that I cloned the base env:
conda create -n tf — clone base
Step 8: Install TensorFlow (and Keras)
After activating the
tf environment with
conda activate tf the next step was to install TensorFlow in that environment. I did that with
conda install tensorflow.
I installed it from conda instead of tensorflow due to the extra speed-ups provided by the the conda TensorFlow packages are built using the Intel® MKL-DNN library,
As for Keras, it is included in TensorFlow and as recommend by the keras team:
At this time, we recommend that Keras users who use multi-backend Keras with the TensorFlow backend switch to
tf.kerasin TensorFlow 2.0.
Step 9: Testing
To ensure everything is working as planned (1) I re-run the
And (2) I created a notebook and “copied-pasted” this example changing:
from keras… to
The example run smoothly:
Since the Mac I have is old with no GPU (upgraded MacBook Pro late 2012) I stopped and this point.