Study interactive :: Progress tools open in the Study Hub reader.

Environment Setup for Machine Learning

Set up a local Python environment for the curriculum lessons and projects.

Table of Contents


Installing Python

Windows

Method 1: Official Installer (Recommended)

  1. Download Python from python.org
  2. Run installer
  3. Important: Check "Add Python to PATH"
  4. Click "Install Now"
  5. Verify installation:
python --version
# Output: Python 3.11.x

Method 2: Using Microsoft Store

# Open Microsoft Store
# Search for "Python 3.11"
# Click Install

Mac

Method 1: Official Installer

  1. Download from python.org
  2. Run installer
  3. Verify:
python3 --version

Method 2: Using Homebrew (Recommended)

# Install Homebrew first (if not installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Install Python
brew install python

# Verify
python3 --version

Linux (Ubuntu/Debian)

# Update package list
sudo apt update

# Install Python
sudo apt install python3 python3-pip

# Verify
python3 --version

Verify Installation

# Check Python version
python --version  # or python3 --version

# Check pip (package manager)
pip --version  # or pip3 --version

# Should see something like:
# Python 3.11.5
# pip 23.2.1

Virtual Environments

Why Virtual Environments?

Virtual environments isolate project dependencies, preventing conflicts between different projects.

Creating Virtual Environment

Windows:

# Create virtual environment
python -m venv ml-env

# Activate
ml-env\Scripts\activate

# You should see (ml-env) in your prompt

Mac/Linux:

# Create virtual environment
python3 -m venv ml-env

# Activate
source ml-env/bin/activate

# You should see (ml-env) in your prompt

Using Virtual Environment

# Activate (do this every time you work on project)
# Windows: ml-env\Scripts\activate
# Mac/Linux: source ml-env/bin/activate

# Install packages (they'll be isolated to this environment)
pip install numpy pandas

# Deactivate when done
deactivate

Best Practices

  1. One environment per project
  2. Always activate before working
  3. Create requirements.txt:
pip freeze > requirements.txt
  1. Share requirements.txt with your project
  2. Recreate environment from requirements:
pip install -r requirements.txt

Installing Essential Libraries

Core Data Science Libraries

# Activate your virtual environment first!

# NumPy - Numerical computing
pip install numpy

# Pandas - Data manipulation
pip install pandas

# Matplotlib - Plotting
pip install matplotlib

# Seaborn - Statistical visualization
pip install seaborn

# Scikit-learn - Machine learning
pip install scikit-learn

Install All at Once

# Create requirements.txt with:
numpy>=1.24.0
pandas>=2.0.0
matplotlib>=3.7.0
seaborn>=0.12.0
scikit-learn>=1.3.0

# Install all
pip install -r requirements.txt

Verify Installations

# Test in Python
python

>>> import numpy as np
>>> import pandas as pd
>>> import matplotlib.pyplot as plt
>>> import seaborn as sns
>>> from sklearn import datasets

>>> print(np.__version__)  # Should print version number
>>> print(pd.__version__)
>>> # If no errors, everything is installed correctly!

Jupyter Notebook Setup

What is Jupyter Notebook?

Interactive environment for data science. Allows you to write code, see results, and add documentation in one place.

Installation

# Install Jupyter
pip install jupyter notebook

# Or install JupyterLab (more features)
pip install jupyterlab

Launching Jupyter

# Start Jupyter Notebook
jupyter notebook

# Or JupyterLab
jupyter lab

# Browser will open automatically
# If not, go to http://localhost:8888

Creating Your First Notebook

  1. Click "New" → "Python 3"
  2. Write code in cells
  3. Press Shift + Enter to run cell
  4. Add markdown cells for documentation

Useful Jupyter Shortcuts

Installing Jupyter Extensions (Optional)

# Install extensions
pip install jupyter_contrib_nbextensions

# Enable extensions
jupyter contrib nbextension install --user

IDE Setup

Installation:

  1. Download from code.visualstudio.com
  2. Install Python extension
  3. Install Jupyter extension

Setup:

  1. Open VS Code
  2. Install extensions:
    • Python (by Microsoft)
    • Jupyter (by Microsoft)
    • Pylance (by Microsoft)
  3. Select Python interpreter:
    • Ctrl + Shift + P (Windows) or Cmd + Shift + P (Mac)
    • Type "Python: Select Interpreter"
    • Choose your virtual environment

Using Jupyter in VS Code:

  1. Create .ipynb file
  2. VS Code will recognize it
  3. Run cells with play button or Shift + Enter

PyCharm

Installation:

  1. Download from jetbrains.com/pycharm
  2. Choose Community Edition (free)

Setup:

  1. Create new project
  2. Set Python interpreter to virtual environment
  3. Install packages through PyCharm's package manager

Google Colab (Cloud Alternative)

No installation needed!

  1. Go to colab.research.google.com
  2. Sign in with Google account
  3. Create new notebook
  4. Free GPU access available!

Git & GitHub Setup

Installing Git

Windows:

  1. Download from git-scm.com
  2. Run installer (use default options)
  3. Verify:
git --version

Mac:

# Using Homebrew
brew install git

# Or download from git-scm.com

Linux:

sudo apt install git

Initial Git Configuration

# Set your name
git config --global user.name "Your Name"

# Set your email
git config --global user.email "[email protected]"

# Set default branch name
git config --global init.defaultBranch main

# Verify
git config --list

GitHub Setup

  1. Create account at github.com
  2. Generate SSH key (optional but recommended):
# Generate SSH key
ssh-keygen -t ed25519 -C "[email protected]"

# Add to GitHub:
# 1. Copy public key: cat ~/.ssh/id_ed25519.pub
# 2. Go to GitHub → Settings → SSH Keys → New SSH Key
# 3. Paste and save

First Repository

# Create project directory
mkdir my-ml-project
cd my-ml-project

# Initialize Git
git init

# Create .gitignore
echo "ml-env/" >> .gitignore
echo "__pycache__/" >> .gitignore
echo "*.pyc" >> .gitignore

# Add files
git add .

# First commit
git commit -m "Initial commit"

# Connect to GitHub (create repo on GitHub first)
git remote add origin https://github.com/yourusername/my-ml-project.git
git push -u origin main

See Complete Git Guide for detailed Git tutorial.


Verification

Complete Setup Check

Run this Python script to verify everything:

# verification.py
import sys

print("Python version:", sys.version)
print("\nChecking libraries...")

try:
    import numpy as np
    print("OK: NumPy:", np.__version__)
except ImportError:
    print("FAIL: NumPy not installed")

try:
    import pandas as pd
    print("OK: Pandas:", pd.__version__)
except ImportError:
    print("FAIL: Pandas not installed")

try:
    import matplotlib
    print("OK: Matplotlib:", matplotlib.__version__)
except ImportError:
    print("FAIL: Matplotlib not installed")

try:
    import seaborn as sns
    print("OK: Seaborn:", sns.__version__)
except ImportError:
    print("FAIL: Seaborn not installed")

try:
    import sklearn
    print("OK: Scikit-learn:", sklearn.__version__)
except ImportError:
    print("FAIL: Scikit-learn not installed")

print("\nSetup complete.")

Run:

python verification.py

Troubleshooting

Problem: "python is not recognized"

Solution:

Problem: "pip is not recognized"

Solution:

# Use pip3 instead
pip3 install numpy

# Or install pip
python -m ensurepip --upgrade

Problem: "Permission denied" when installing

Solution:

Problem: Jupyter won't start

Solution:

# Reinstall Jupyter
pip install --upgrade jupyter

# Clear Jupyter cache
jupyter --paths
# Delete cache directories if needed

Problem: Import errors in Jupyter

Solution:

import sys
print(sys.executable)

Quick Start Checklist


Next Steps

  1. Practice: Create a test notebook and import all libraries
  2. Explore: Try loading a dataset with Pandas
  3. Move Forward: Proceed to 01-python-for-data-science

You're now ready to start your ML journey!


Additional Resources

Try next: Copy the full traceback into search. Fix the first root cause, not the last symptom.

Recall ::

Why use a virtual environment for ML projects?

Isolate package versions per project so installs do not break other work.