Set up Python and notebooks
Set up Python and notebooks
Before we fit anything, let's make sure your Python program can load the tools used in this course. Python runs the code. NumPy handles numerical arrays, pandas organizes tables, and scikit-learn supplies learning algorithms and evaluation tools. Installing a library makes its tools available; importing it brings those tools into a particular program.
You can use a notebook, where code and explanations occupy separate cells, or save a script and run it from a terminal. Both approaches run Python. A notebook is convenient for exploring results, but its cells can be executed out of order, which sometimes leaves a beginner looking at variables created by an earlier experiment.
What you’ll learn
- Create an isolated environment and install the course libraries.
- Run the setup check in a script or notebook.
- Diagnose stale notebook state and interpreter mismatches.
Keep this course in its own environment
Environment
Packages belong to the selected Python interpreter.
Python + installed librariesCode cell or script
Imports and calculations run in that interpreter.
two recorded scoresFresh result
Restart and run in order to check reproducibility.
average = 76
Check the tools with two student scores
Keep this course in its own environment
A virtual environment gives the course a separate collection of installed Python packages. In a terminal, create it with python -m venv .venv. On macOS or Linux activate it with source .venv/bin/activate; on Windows PowerShell use .venv\Scripts\Activate.ps1. Your Python command may be python3 rather than python.
Install the libraries with python -m pip install numpy pandas scikit-learn matplotlib seaborn jupyterlab openpyxl. Start a notebook with python -m jupyter lab, or save the example as check_setup.py and run python check_setup.py. Select the same environment as your editor or notebook's interpreter; installing into another interpreter will not fix a missing import.
Prepare and run
Use the selected course environment described above. Run this complete Python code in a fresh cell or script.
Python · complete runnable example
import sys
import numpy as np
import pandas as pd
import sklearn
marks = pd.DataFrame({"student": ["Diya", "Saira"], "score": [72, 80]})
print("Students:", len(marks))
print("Average score:", float(np.mean(marks["score"])))Expected output
Students: 2
Average score: 76.0Follow the result
The imports check that the required libraries are available. A pandas DataFrame stores two named students and their scores. Selecting the score column gives NumPy two numbers to average, so the answer is (72 + 80) / 2 = 76. An import error means the environment needs attention before moving on; it is not a machine learning error.
Recover a fresh notebook result
Your task: Your notebook prints an old answer after you edit a cell. What should you do?
Worked answer
Run the edited cell and the cells that depend on it. For a reliable check, restart the kernel and run all cells from top to bottom. The kernel is the Python process holding variables in memory; editing text alone does not replace those variables.
Make a run reproducible
Keep the code, dataset source and package versions with your experiment. Record installed versions with python -m pip freeze. This course's model transcripts were checked with scikit-learn 1.9.1; the one-hot encoding examples need the sparse_output parameter introduced in version 1.2. Small numerical differences can occur across versions.
A random seed makes specified random choices repeatable within a compatible environment. It does not make a dataset representative, remove bias or guarantee identical results on every platform. Save your notebook and verify a fresh run before sharing a result.
Check your understanding
Keep this course in its own environment: what to remember
A correct package installation and a selected interpreter must refer to the same environment. A fresh, ordered notebook run checks that saved code can recreate its result without relying on old variables.
Read further: official supporting documentation.
Reaching the recap and completing the quizzes are separate milestones.