W3docs

Getting Started

Learn how to set up Python for machine learning, understand the ML workflow, and run your first model with scikit-learn, NumPy, and pandas.

Getting Started with Python Machine Learning

This chapter introduces machine learning with Python. You will learn what machine learning is, why Python is the dominant language for it, how to set up a working environment, and how the end-to-end ML workflow fits together — from raw data to a trained, evaluated model.

By the end you will have run a complete classification example using the three core libraries: NumPy, pandas, and scikit-learn.

What Is Machine Learning?

Machine learning (ML) is a branch of artificial intelligence in which a program learns patterns from data rather than following hand-written rules. You provide examples (data), specify what you want to predict or discover, and an ML algorithm figures out the mapping.

There are three broad categories:

CategoryWhat it doesTypical example
Supervised learningLearns from labeled examples (input → known output)Spam detection, house price prediction
Unsupervised learningFinds hidden structure in unlabeled dataCustomer segmentation, anomaly detection
Reinforcement learningAn agent learns by trial-and-error with rewardsGame-playing AI, robotics

This series focuses on supervised learning because it is the most widely used category in practice.

Why Python for Machine Learning?

Python has become the default language for ML for several reasons:

  • Readable syntax — algorithms can be prototyped quickly without boilerplate.
  • Rich ecosystem — scikit-learn, TensorFlow, PyTorch, and Keras all have first-class Python APIs.
  • Data tooling — NumPy and pandas make data wrangling straightforward.
  • Community — the largest ML community, meaning abundant tutorials, Stack Overflow answers, and pre-trained models.

Setting Up Your Environment

Install Python

Download Python 3.10 or newer from python.org. Verify the installation:

python3 --version

You should see output like Python 3.10.15 (or newer).

Create a Virtual Environment

A virtual environment isolates your project's packages from the system Python installation. This prevents version conflicts between projects.

python3 -m venv ml-env

Activate it:

# macOS / Linux
source ml-env/bin/activate

# Windows (Command Prompt)
ml-env\Scripts\activate.bat

Your prompt will now show (ml-env) to confirm the environment is active.

Install the Core Libraries

With the virtual environment active, install the three libraries you will use throughout this series:

pip install numpy pandas scikit-learn

Save the exact versions for reproducibility:

pip freeze > requirements.txt

Anyone cloning your project can reproduce your environment with pip install -r requirements.txt.

The Machine Learning Workflow

Every supervised ML project follows the same five-step pipeline:

Raw data → Clean & prepare → Split → Train model → Evaluate

Understanding this pipeline is more important than memorising any single algorithm. The sections below walk through each step with code.

Step 1 — Understand Your Data with NumPy and pandas

Before training anything you need to know what your data looks like. NumPy provides fast array arithmetic; pandas adds labelled tables (DataFrames) that make exploration easy.

NumPy: inspecting a numeric array

import numpy as np

data = np.array([2.1, 3.4, 1.8, 5.0, 2.7])
print('Mean:', data.mean())   # 3.0
print('Std: ', round(data.std(), 4))  # 1.1402
print('Max: ', data.max())    # 5.0

pandas: building a small dataset

import pandas as pd

df = pd.DataFrame({
    'age':    [25, 30, 22, 35, 28],
    'income': [40000, 55000, 32000, 70000, 48000],
    'bought': [0,     1,     0,     1,     0],
})

print(df.shape)             # (5, 3)
print(df['income'].mean())  # 49000.0
print(df.isnull().sum())    # 0 missing values in each column

df.shape tells you the number of rows and columns. isnull().sum() counts missing values per column — always check this before modelling.

Step 2 — Clean and Prepare the Data

Real datasets almost always have missing values, inconsistent formats, or features on very different scales. You need to address these before training.

Handle Missing Values

Replace missing numbers with the column's median (robust to outliers) or mean:

import pandas as pd

df = pd.DataFrame({
    'age':    [25, None, 22, 35, 28],
    'income': [40000, 55000, None, 70000, 48000],
})

df['age']    = df['age'].fillna(df['age'].median())
df['income'] = df['income'].fillna(df['income'].mean())

print(df.isnull().sum())
# age       0
# income    0

Scale Features

Many algorithms (k-nearest neighbours, SVMs, neural networks) are sensitive to feature scale. A column with values in the thousands will dominate a column in single digits unless you normalise them. StandardScaler subtracts the mean and divides by the standard deviation, so every feature has mean 0 and standard deviation 1:

from sklearn.preprocessing import StandardScaler
import numpy as np

X = np.array([[1.0, 200.0], [2.0, 400.0], [3.0, 300.0]])
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
print(X_scaled.round(2))
# [[-1.22 -1.22]
#  [ 0.    1.22]
#  [ 1.22  0.  ]]

See Feature Scaling for more detail.

Step 3 — Split into Training and Test Sets

Never evaluate a model on the data it was trained on — that would be like marking your own exam with the answer sheet open. Split the data so the model trains on one portion and is evaluated on a held-out portion it has never seen.

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

test_size=0.2 reserves 20 % of the data for testing. random_state=42 makes the split reproducible. See the Train/Test Split chapter for strategies like stratified splitting and cross-validation.

Step 4 — Train a Model

With clean, split data you can train your first model. The example below uses the Iris dataset — a classic benchmark with 150 samples, 4 numeric features, and 3 flower species to classify.

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# 1. Load
iris = load_iris()

# 2. Split
X_train, X_test, y_train, y_test = train_test_split(
    iris.data, iris.target, test_size=0.2, random_state=42
)

# 3. Train
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# 4. Predict & evaluate
predictions = model.predict(X_test)
print(f'Test samples: {len(X_test)}')   # 30
print(f'Accuracy: {accuracy_score(y_test, predictions):.2f}')  # 1.00

A RandomForestClassifier trains many decision trees on random subsets of the data and combines their votes. It handles non-linear relationships well and is a strong baseline for classification tasks. n_estimators=100 specifies the number of trees.

Step 5 — Evaluate the Model

Accuracy (the proportion of correct predictions) is easy to understand but can be misleading when one class is much rarer than the others. scikit-learn provides a full metrics suite:

MetricWhat it measures
AccuracyOverall fraction correct
PrecisionOf predicted positives, how many were really positive
RecallOf actual positives, how many did we predict correctly
F1 scoreHarmonic mean of precision and recall

For a deeper look at evaluation, see the Confusion Matrix chapter.

Choosing the Right Algorithm

Different problems call for different algorithms. Here is a quick orientation:

Problem typeTry first
ClassificationRandomForestClassifier, LogisticRegression
RegressionLinearRegression, RandomForestRegressor
ClusteringKMeans
Dimensionality reductionPCA

You will explore most of these in the rest of this series. Good starting points are K-Nearest Neighbors, Decision Tree, and Linear Regression.

Gotchas for Beginners

  • Data leakage — fitting the scaler on the full dataset (before splitting) leaks test-set statistics into training. Always fit transformers on the training set only, then apply them to the test set.
  • Overfitting — a model that memorises training data performs poorly on new data. Use Cross-Validation to detect this early.
  • Ignoring class imbalance — if 95 % of your labels are "negative", a model that always predicts "negative" scores 95 % accuracy but is useless. Check class distributions before choosing a metric.
  • Skipping exploration — always look at your data before modelling. Check ranges, distributions, and missing-value counts.

Conclusion

You now have a working Python ML environment and understand the full supervised-learning pipeline: load data, clean it, split it, train a model, and evaluate on held-out data. The chapters that follow go deeper into each step — starting with data preparation, moving through individual algorithms, and finishing with advanced topics like cross-validation and hyperparameter tuning.

Was this page helpful?