How to Setup a Python Environment for Machine Learning

In there, we’ll install all of the python packages that we need for Machine Learning.We use virtual environments in order to separate our coding set ups..Imagine if at some point you wanted to do 2 different projects on your computer, which required different libraries of different versions..Having them all in the same working environment can be messy and you’ll likely run into the problem of conflicting library versions..Your ML code for project 1 needs version 1.0 of numpy, but project 2 needs version 1.15..Yikes!A virtual environment allows us to isolate our working areas to avoid those conflicts.First, install the relevant packages:sudo pip install virtualenv virtualenvwrapperOnce we have virtualenv and virtualenvwrapper installed, we’ll again need to edit our ~/.bashrc file..Place these 3 lines right at the bottom and save it.export WORKON_HOME=$HOME/.virtualenvsexport VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3source /usr/local/bin/virtualenvwrapper.shSave the file and reload your changes:source ~/.bashrcGreat!.Now we can finally create our virtual environment like so:mkvirtualenv mlWe’ve just created a virtual environment called ml ..To enter it, do this:workon mlNice!.Any library installations that you do while in the ml virtualenv will be isolated in there and never conflict with any other environments!.So whenever you wish to run code that depends on libraries installed in the ml environment, enter into first with the workon command and then run your code as normal.If you need to exit the virtualenv, run this command:deactivate(3) Install Machine Learning librariesNow we can install our ML libraries!.We’ll go with the most commonly used ones:numpy: for any work with matrices, especially math operationsscipy: scientific and technical computingpandas: data handling, manipulation, and analysismatplotlib: data visualisationscikit learn: machine learningHere’s a simple trick to install all of those libraries in one quick shot!.Create a requirements.txt file and list all of the packages you wish to install like so:numpyscipypandasmatplotlibscikit-learnOnce that’s done, just execute this command:pip install -r requirements.txtVoila!. More details

Leave a Reply