How to setup the Python environment for Machine Learning leveraging Anaconda
It can be tough to setup a Python machine learning environment on a few platforms.
Python itself must be setup to start with and then there are several packages to setup, and it can be intimidating for starters.
In this blog post by AICoreSpot, which functions as a tutorial, you will find out how to setup a Python Machine Learning development environment leveraging Anaconda.
After getting through this tutorial, you will possess a functional Python environment to start learning, practicing, and developing deep learning and machine learning software.
These guidelines are apt for Windows, Mac OS X, and Linux platforms.
This tutorial will encompass the following stages:
- Download Anaconda
- Setup Anaconda
- Begin and update Anaconda
- Update scikit-learn library
- Setup Deep Learning libraries
1. Download Anaconda
At this stage, you will download the Anaconda Python package for your respective platform.
Anaconda is free-of-cost, and very accessible environment developed for scientific python.
- Go to the Anaconda homepage.
- Click on ‘Anaconda’ from the menu and click ‘Download’ to get to the downloads page.
- Opt for the download apt for your particular platform (Windows, Mac OS X, or Linux)
- Opt for Python 3.5
- Opt for the Graphic Installer.
This will get the Anaconda Python Package on your workstation.
This is being demonstrated with OS X. You should now possess a file with a name as follows: Anaconda3-4.2.0-MacOSX-x86_64.pkg
2.Setup Anaconda
Now, we will setup the Anaconda Python software on the system.
This step goes by the assumption that you possess adequate administrative privileges to setup software on your system.
- Double-click on the downloaded file.
- Adhere to the setup wizard.
Setup is swift and hassle-free. There should be no tough questions or sticking points.
The setup should take not more than ten minutes and take up a tad more than 1GB of disk space on your hard disk.
3. Begin and update Anaconda
In the third step, you will confirm that your Anaconda Python environment is updated.
Anaconda carries with a suite of graphical utilities referred to as Anaconda Navigator. You can begin Anaconda Navigator by initiating it from your application launcher.
You can leverage the Anaconda Navigator and graphical development environments later, for the time being, it is recommended beginning with the Anaconda Command Line environment referred to as conda.
Conda is quick, simple, it’s difficult for error messaging to hide, and you can swiftly confirm the environment is setup and functioning as it should.
- Open a terminal (command line) window.
- Verify conda is setup correctly, through typing
- conda -V
- You should see the following, or something like it
- conda 4.2.9
- Verify Python is setup correctly through typing
- python -V
- You should see the following, or something like it
- Python 3.5.2 :: Anaconda 4.2.0 (x86_64)
If the commands do not function or display an error, refer to the documentation for assistance on your platform.
4. Verify that your conda environment is updated, through typing:
conda update conda
conda update anaconda
You may be required to setup a few packages and validate the updates.
5. Verify your SciPy environment.
The script below will print the version number of the key SciPy libraries you need for machine learning development, particularly SciPy, NumPy, Matplotlib, Pandas, Statsmodels, and Scikit-learn
You can enter “python” and input the commands directly. Otherwise it is recommended to open up a text editor and copy-pasting the script on to your editor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # scipy import scipy print(‘scipy: %s’ % scipy.__version__) # numpy import numpy print(‘numpy: %s’ % numpy.__version__) # matplotlib import matplotlib print(‘matplotlib: %s’ % matplotlib.__version__) # pandas import pandas print(‘pandas: %s’ % pandas.__version__) # statsmodels import statsmodels print(‘statsmodels: %s’ % statsmodels.__version__) # scikit-learn import sklearn print(‘sklearn: %s’ % sklearn.__version__) |
Save the script as a file with the name: versions py.
On the command line, alter your director to where you saved the script and input:
python versions.py
You should observe output that resembles the following:
ou should see output like the following:
[Control]
1 2 3 4 5 6 | scipy: 0.18.1 numpy: 1.11.1 matplotlib: 1.5.3 pandas: 0.18.1 statsmodels: 0.6.1 sklearn: 0.17.1 |
6. Update scikit-learn libraries
In the fourth step, we will update the primary library leveraged for Machine Learning in Python referred to as scikit-learn.
- Updating scikit-learn to the updated version.
You can update a particular library leveraging the conda command, below is an instance of updating scikit-learn to the updated version.
On the terminal, enter:
conda update scikit-learn
Otherwise, you can go about updating a library to a particular version by entering:
conda install -c anaconda scikit-learn=0.18.1
Verify the installation and scikit-learn was updated by re-executing the versions.py script by entering:
python versions.py
You should now observe output that resembles the following:
[Control]
1 2 3 4 5 6 | scipy: 0.18.1 numpy: 1.11.3 matplotlib: 1.5.3 pandas: 0.18.1 statsmodels: 0.6.1 sklearn: 0.18.1 |
You can leverage these commands to update machine learning and SciPy libraries as required.
7. Install Deep Learning libraries
In the seventh step, we will setup Python libraries leveraged for deep learning, particularly: Theano, TensorFlow, and Keras.
It is recommended to leverage Keras for deep learning and Keras only needs one of Theano or TensorFlow to be setup. You do not require both. There may be issues setting up TensorFlow on a few Windows configurations.
Setup the Theano deep learning library by entering:
conda install Theano
Setup the TensorFlow deep learning library (all except Windows) by entering:
conda install -c conda-forge tensorflow
Otherwise, you might opt to setup leveraging pip and a particular version of TensorFlow for your platform.
Setup Keras by entering:
pip install keras
Validate your deep learning environment is setup and operating correctly.
Develop a script that prints the version numbers of every library, as we did prior for the SciPy environment.
1 2 3 4 5 6 7 8 9 | # theano import theano print(‘theano: %s’ % theano.__version__) # tensorflow import tensorflow print(‘tensorflow: %s’ % tensorflow.__version__) # keras import keras print(‘keras: %s’ % keras.__version__) |
Save the script to a file deep_versions.py. Execute the script by entering:
python deep_versions.py
You should observe output resembling the following:
[Control]
1 2 3 4 | theano: 0.8.2.dev-901275534cbfe3fbbe290ce85d1abf8bb9a5b203 tensorflow: 0.12.1 Using TensorFlow backend. keras: 1.2.1 |
Conclusion
Well done, you now possess a working Python development environment for machine learning and deep learning.
You can now go about learning and practicing machine learning and deep learning on your workstation.