Practise machine learning online. With scikit-learn.

40 interview questions, Easy to Hard, on one churn dataset. Train real models, test them honestly, plot what they do. Free, in your browser, no sign-up.

Question 1 of 40

Easy

What is machine learning: supervised, unsupervised or reinforcement?

Unsupervised learning finds groups without labels. Fit KMeans(n_clusters=3, random_state=42) on the scaled tenure_months and complaints_90d, never showing it churned, then make result each cluster’s churn rate, rounded to 3, smallest first.

experiment.pyscikit-learn 1.8
Runs firstChurn data and modeldfX_trainX_testy_trainy_testmodel
import numpy as np
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler

rng = np.random.default_rng(42)
n = 5_000
df = pd.DataFrame({
    "tenure_months":  rng.integers(1, 73, n),
    "monthly_charge": rng.normal(499, 150, n).round(),
    "plan":           rng.choice(["prepaid", "postpaid"], n, p=[0.7, 0.3]),
    "city_tier":      rng.choice(["tier 1", "tier 2", "tier 3"], n),
    "complaints_90d": rng.poisson(0.4, n),
})
# churn is likelier with short tenure and more complaints
p = 1 / (1 + np.exp(2.3 + 0.03 * df["tenure_months"] - 0.9 * df["complaints_90d"]))
df["churned"] = (rng.random(n) < p).astype(int)
df.loc[rng.random(n) < 0.05, "monthly_charge"] = np.nan     # 5% missing, as in real data

X, y = df.drop(columns="churned"), df["churned"]
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, stratify=y, random_state=42)

num_cols = ["tenure_months", "monthly_charge", "complaints_90d"]
cat_cols = ["plan", "city_tier"]
prep = ColumnTransformer([
    ("num", Pipeline([("impute", SimpleImputer(strategy="median")),
                      ("scale", StandardScaler())]), num_cols),
    ("cat", OneHotEncoder(handle_unknown="ignore"), cat_cols),
])
model = Pipeline([("prep", prep),
                  ("clf", LogisticRegression(class_weight="balanced"))])
model.fit(X_train, y_train)
from sklearn.cluster import KMeans

X_seg = StandardScaler().fit_transform(df[["tenure_months", "complaints_90d"]])

Your result shows here.

Run trains and tests for real. The first run downloads Python and scikit-learn, about 32 MB, once.

How the lab works

  • Real scikit-learn

    scikit-learn 1.8 with pandas, NumPy, SciPy and Matplotlib, compiled to run inside your browser. Models train for real, warnings and errors included.

  • Checked by its result

    Every model here is seeded, so the same code gives the same numbers. Check my answer runs your code and ours from the same start and compares what each gives. Any code that gets there counts.

  • Stays in your browser

    The first run downloads about 32 MB, once (charts about 10 MB more, the first time you draw one). Your code never leaves your device, and your progress is kept on it.

What’s inside? Python’s standard library, NumPy, pandas, SciPy, scikit-learn 1.8 (the newest that runs in a browser; the guide is written for 1.9) and Matplotlib. Deep learning needs a GPU, so PyTorch isn’t here: download any question as a notebook and open it in Google Colab. Each answer comes with the guide’s explanation: Machine learning interview questions, with answers.