DEV Community

Cover image for Setting up a python machine learning environment on windows
Gabriele Boccarusso
Gabriele Boccarusso

Posted on • Updated on

Setting up a python machine learning environment on windows

Table of contents:

  1. introduction
  2. First steps
  3. Setting up the environment folder
  4. creating the custom environment
    1. Jupyter notebook interface
  5. How to exit from the environment
  6. Final thoughts

Introduction

to be able to learn machine learning with python we have to set up the environment first.
To create machine learning environments for python we need Anaconda, a software that has all the packages that we need to actually begin.
Anaconda weight circa 3 gigabytes of space and has all the tools that we may need, but for beginners it is overwhelming and we'll never use all of it, at least for now, all of it, that's why we'll download miniconda, with all the packages that we need for then import them in jupyter notebook, a good software to display data.

First steps

The link for miniconda is here.
On the page you'll see something similar to this:
miniconda download documentation - windows versionsclick the first one

To download miniconda you have to go through all the same steps for installing any other software, so I'll not cover this part. You can just click next without changing anything for then waiting for the extraction.
miniconda installation This is an old image, so expect to install a higher version

After the installation is completed to set up the environment you have to open the anaconda prompt, a special command prompt used for activating our machine learning environment.
anaconda prompt icon in windows 10

After you have opened it you'll see in front of you the anaconda command prompt.
miniconda command prompt

So now that we have all the basics done, let's go through the next part.

Setting up the environment folder

First of all, we want to create the folder on the desktop so that you can access it easily, to do so you have to do

(base) C:\Users\name> cd Desktop
Enter fullscreen mode Exit fullscreen mode

where "name" is the name of your users, as you have already seen, mine was "gabri"
with this, you are now on the desktop and the command prompt will now look like

(base) C:\Users\name\Desktop>
Enter fullscreen mode Exit fullscreen mode

and now we have to just create the folder that will contain the project for then download the packages in it.

(base) C:\Users\name\Desktop> mkdir sample_project
Enter fullscreen mode Exit fullscreen mode

and then

(base) C:\Users\name\Desktop> cd sample_project
Enter fullscreen mode Exit fullscreen mode

Now that we are in the folder where everything about our project will be stored, we can proceed with making it our custom environment.

Creating the custom environment

To effectively create the custom environment in the sample folder you just type this in the command prompt

(base) C:\Users\name\Desktop\sample_project> conda create --prefix ./env pandas numpy matplotlib scikit-learn
Enter fullscreen mode Exit fullscreen mode

But what are we doing here?
We are creating the folder env that will be our custom environment.
After typing the command you'll see something very similar to
installing packages - machine learning environment set up
Scrolling down you'll see the request to proceed.
Just type y.
give confirm to install the various packages

After the end of the downloads, the environment will be created and to activate you'll have to type

(base) C:\Users\name\Desktop\sample_project\env> conda activate C:\Users\name\Desktop\sample_project\env
Enter fullscreen mode Exit fullscreen mode

Now the "(base)" will vanish and the path will look similar to

(C:\Users\name\Desktop\sample_project\env) C:\Users\name\Desktop\sample_project\env> 
Enter fullscreen mode Exit fullscreen mode

Now I have, on purpose, not installed yet jupyter notebook.
I did so to show you how to install packages/components even after the setup of an environment is completed.

You type

(C:\Users\name\Desktop\sample_project\env) C:\Users\name\Desktop\sample_project\env> conda install jupyter notebook 
Enter fullscreen mode Exit fullscreen mode

After the download is finished you can open jupyter notebook writing its name in the anaconda command prompt

(C:\Users\name\Desktop\sample_project\env) C:\Users\name\Desktop\sample_project\env> jupyter notebook 
Enter fullscreen mode Exit fullscreen mode

Jupyter notebook interface

Jupyter will open its interface in your browser, where you'll see something similar to this
jupyter notebook main page
To create a notebook file we go on new and then click on python 3
open a new file in notebook
And after the click, it will open a new tab
jupyter notebook page
This is similar to the shell in python and you can actually write commands for then execute them with shift + enter
generic python line executed in jupyter notebook
Now, the last step is to import all the libraries that we'll need to begin machine learning.
Type in the jupyter notebook shell

import pandas as pd
import numpy as np
import matplotlib.pyplot as ply
import sklearn # abbreviation of sci kit
Enter fullscreen mode Exit fullscreen mode

all the libraries imported in jupyter notebook
And this is all to set up our machine learning environment

How to exit from the environment

To go out from our environment we have to open the anaconda command prompt and press control + c and then enter
exiting form jupyter notebook
then we simply type conda deactivate
deactivating conda environment
Now the this is no more a machine learning environment but a simple folder again.

Final thoughts

Now that we have done all that we needed to set up our machine learning environment we have to just begin working.

Oldest comments (0)