In classical programming, you write the rules: “if the amount exceeds €1,000 and the country is unusual, then flag the transaction”. In machine learning, you provide examples and an algorithm learns the rule from the data. It is useful when the rules are too numerous, too fuzzy, or change over time.
Basic vocabulary
- Sample (or observation): a row of the table, for example a customer or a flower.
- Feature (explanatory variable): a column used to predict, for example age or the length of a petal.
- Target (label): what you want to predict, for example “has churned” or “price”.
- Model: the learned function that links the features to the target.
In scikit-learn, X denotes the feature matrix, of shape (n_samples, n_features), and y the target vector, of shape (n_samples,).
Supervised or unsupervised?
- Supervised: each example has a known target. The model learns to predict it for new examples.
- Classification: the target is a category (spam / not spam, flower species).
- Regression: the target is a continuous number (apartment price, revenue).
- Unsupervised: no target. You look for structure in the data: groups of similar customers (clustering), compressing information (dimensionality reduction).
A first dataset
scikit-learn provides built-in datasets, available without an internet connection. The most famous is Iris: 150 flowers described by 4 measurements, split into 3 species.
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data # 2D numpy array: 150 rows, 4 columns
y = iris.target # 1D numpy array: 150 values (0, 1, or 2)
print(X.shape) # (150, 4)
print(y.shape) # (150,)
print(iris.feature_names) # names of the 4 features
print(iris.target_names) # ['setosa' 'versicolor' 'virginica']
You can also get a pandas DataFrame, which is handier for exploring:
iris_df = load_iris(as_frame=True)
df = iris_df.frame # features + 'target' column
print(df.head())
X = df.drop(columns="target") # X: 2D DataFrame
y = df["target"] # y: 1D Series
Here the target is a species: so this is a supervised classification problem with 3 classes.
What to remember
Before writing a single line of model code, ask yourself three questions: what is my target? is it categorical or numeric? which features do I have available at prediction time? This last question is essential: a feature that exists only after the event to predict (for example the cancellation date to predict cancellation) completely skews the model. This is called data leakage, and we will come back to it throughout the track.
Exercises
Solutions and tests stay on the server. In the student space, your code is run and checked, and the tutor gives hints before the solution.
Extracting X and y from the Iris dataset
Load the Iris dataset with
load_iris(as_frame=True). CreateX, a DataFrame containing only the 4 features, andy, the Series of thetargetcolumn. Then store the number of samples inn_echantillonsand the number of features inn_features.from sklearn.datasets import load_iris df = load_iris(as_frame=True).frame X = ... y = ... n_echantillons = ... n_features = ...Classifying problems
Complete the
typesdictionary by associating each problem with the value'classification','regression', or'non_supervise'.types = { "prix_appartement": ..., # prédire le prix en euros "email_spam": ..., # spam ou non spam "groupes_clients": ..., # trouver des groupes sans étiquette "temperature_demain": ..., # prédire une température "espece_fleur": ..., # setosa, versicolor ou virginica }Shape of X
In scikit-learn, what shape should the matrix X have for 200 customers described by 5 variables?
- (200, 5)
- (5, 200)
- (200,)
- (1000,)
End-of-lesson quiz
Predicting whether a customer will cancel their subscription (yes/no) is a problem of:
- Regression
- Classification
- Clustering
- Dimensionality reduction
What is the main difference between supervised and unsupervised learning?
- Supervised learning uses more data
- Unsupervised learning has no target variable
- Supervised learning uses no features
- Unsupervised learning is always more accurate
Why should the ‘cancellation date’ not be used as a feature to predict cancellation?
- Because dates are forbidden in scikit-learn
- Because it is data leakage: the information exists only after the event
- Because it takes too much memory
- Because it is categorical
The rest of the course
- The Estimator API: fit, predict, score15 min
- Splitting Training and Test Data15 min
- Preprocessing: Scaling the Data15 min
- Pipelines and Your First Complete Workflow20 min