avatarAndrea D'Agostino

Summary

The website provides a comprehensive guide on setting up a machine learning development environment using Miniconda on OSX, emphasizing the importance of virtual environments for project-specific package management and system compatibility.

Abstract

The article "How to Set Up a Development Environment for Machine Learning" outlines the process of installing and using a virtual environment for machine learning tasks. It explains the benefits of using a virtual environment, such as Miniconda, to manage project dependencies without affecting the global system environment. The guide covers the installation of Miniconda on OSX, the creation of a virtual environment, and the installation of packages using both conda and pip. It also provides a link to an official Conda cheat sheet for quick reference to common commands. The author advocates for the use of Miniconda over Anaconda for a lighter installation and suggests using pip when packages are not available in the conda repository.

Opinions

  • The author prefers using Miniconda over Anaconda for a more streamlined installation that avoids unnecessary packages.
  • It is recommended to use pip for package installation when the desired package is not available via conda, as it is an efficient method without drawbacks.
  • The use of virtual environments is strongly encouraged as a best practice in data science and programming to maintain an organized system and avoid global incompatibility issues.
  • The author suggests that Windows users can utilize WSL (Windows Subsystem for Linux) or a virtual machine with Ubuntu 18.04 to follow along with the guide.
  • The article emphasizes the importance of having a dedicated virtual environment for each project to prevent conflicts between package versions and ensure reproducibility.
  • The provision of a Conda cheat sheet is presented as a valuable resource for developers to quickly access necessary commands, indicating its utility in the author's own career and personal work.

How to Set Up a Development Environment for Machine Learning

How to install, activate, and use a virtual environment for machine learning and data science-related tasks

Photo by Bradley Lembach on Unsplash

Before we start coding, it is essential to set up our machine with a new development environment. In these examples I will use the OSX terminal and Python 3.8 to just that and I will show you the basics to get started.

If you are a Windows user the basic logic does not change, but for some commands you may need to use Google. Starting with Windows 10, you can use WSL (Windows Subsystem for Linux) — this is a subsystem made available by Microsoft that can host several Linux distributions. Read here for an in-depth guide. Another option available would also be to install a virtual machine with Ubuntu 18.04 or use a VPS.

What a virtual environment is in layman’s terms

A brief introduction for those who are just starting their path in data science and programming in Python. A virtual environment is a development environment that acts as a container for our current project. While having the ability to install any system-wide library is there, having a virtual environment allows you to install such libraries and packages for specific projects. This allows you to have an orderly and easily navigable system, without risks of incompatibility or malfunctions on a global level.

Dedicating a virtual environment to a project is common practice, and should always be done for the reasons mentioned above.

In Python, there are several options for managing virtual environments. One of the most famous is certainly Anaconda, a software that helps (especially data scientists) to configure and manage development environments efficiently.

We will be using Miniconda to configure Python on our system. It is a lightweight installer of conda, an open-source data science-oriented development environment management system available for Linux, OSX and Windows.

The choice to opt for Miniconda and not for Anaconda is because the latter brings with it numerous packages that in my opinion we will never use except in sporadic cases and therefore having a small installation makes more sense.

How to install Miniconda on OSX

At the time of writing this article, the file that we will download from the official website will have a name like this: Miniconda3-py38_4.11.0-MacOSX-x86_64.sh

Miniconda download screen. Image by Author.

Depending on the architecture of our system we can choose between the basic version and the version for M1 ARM chipset.

Once the file is downloaded, we open the terminal and write

$ cd Downloads

$ sh Miniconda3-py38_4.11.0-MacOSX-x86_64.sh

and follow the onscreen instructions. We can check we have installed the software correctly if we write conda in our terminal

Image by Author.

At this point we have installed Miniconda on our Mac OSX system and we are ready to create a virtual environment.

How to create a virtual environment with Miniconda

Once Anaconda or Miniconda has been installed and their correct functioning has been validated using the conda command, we can create a new development environment as follows:

$ conda create -n name_of_my_environment

This command will create a virtual development environment called name_of_my_environment in the installation directory. To activate the virtual environment just run the command

$ conda activate name_of_my_environment

And we are done! Our virtual environment is ready for the development of our application. Now let’s see how to install or uninstall libraries and packages in our newly activated virtual environment.

How to install packages and libraries in the virtual environment

Now is the time to add tools to our toolbox. To add a Python package or library, just use the command

$ conda/pip install name_of_package

As we can see it is possible to use the pip command to install packages within conda as well. In fact, if we use the command

$ conda/pip list

conda or pip, respectively, will show us the packages installed via their command.

I recommend using the pip install command over conda install because some packages are not available in the conda repository. There may be several reasons, but using pip has no drawbacks and allows you to download and install packages efficiently.

Conda Cheat Sheet — every command at your fingertips

I will conclude this article by sharing with you a helpful asset that has helped me countless times during my career and personal work. I am talking about the official cheat sheet made available by Anaconda. Here’ a preview of what it looks like 👇

Preview of the cheat sheet. Image by Author.

I suggest you save it in a folder you open often, or save the single commands in your favorite note-keeping app.

I hope this short guide helps you getting started in your data science & analytics journey with Python. Leave a comment below if you have any questions or want to share your feedback!

Until next time 👋

Machine Learning
Data Science
Virtual Environment
Python
Programming
Recommended from ReadMedium