Train Your First Model
Machine Learning
- You should already know
- The notebook workflow from Explore Real Data; tables, functions and missing values.
- You'll build
- A trained classifier, a baseline comparison and a confusion matrix on held-out passengers.
- You'll be able to
- Separate features from labels, split data before fitting and evaluate predictions on unseen rows.
- You need
- Your local setup, any plain-text editor, uv and internet access for installing packages. Python 3.14 is selected for these examples.
Until now, you supplied the rule: count a word, compare a rank or filter a magnitude. A machine-learning classifier learns a rule from examples with known answers.
We'll use historical Titanic passenger records to predict the survived label from four fields. This is a small, local model trained on your computer. It doesn't need an AI account, a paid API or a GPU.
Prepare the Experiment
mkdir titanic-model cd titanic-model uv init --bare --vcs none --no-workspace --python 3.14 uv python pin 3.14 uv add jupyterlab pandas matplotlib scikit-learn
Save titanic.ipynb, titanic.csv and the source metadata together in the project folder. Run uv run jupyter lab and open the notebook with its Python 3 kernel.
Run cells one at a time. The notebook contains the complete experiment and explanations; add your own predictions as Markdown cells before changing it.
Inputs and Known Answers
The notebook chooses its fields explicitly:
features = ['age', 'fare', 'pclass', 'sex'] X = data[features] y = data['survived']
A feature is an input used for a prediction. The label, or target, is the answer associated with a training example. Here, 0 means the passenger died and 1 means they survived. X and y are conventional names for those inputs and answers.
The CSV includes other columns. One, alive, directly encodes the answer. Including it would let the model read the answer instead of learning a useful predictive pattern. This is data leakage. Explicit field selection helps us avoid it.
These four fields reflect patterns in a particular historical sample. A prediction doesn't establish what caused survival or determine what would happen to an individual.
Keep Some Answers for Checking
The notebook splits the rows:
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.25, random_state=42, stratify=y )
Three quarters become training examples; one quarter is held out for checking. stratify=y keeps the label proportions similar. random_state makes this particular random split reproducible.
Establish a Baseline First
Run the baseline cell on its own:
baseline = DummyClassifier(strategy='most_frequent') baseline.fit(X_train, y_train) baseline_predictions = baseline.predict(X_test) print('First five baseline predictions:', baseline_predictions[:5]) print('Baseline accuracy:', round(accuracy_score(y_test, baseline_predictions), 3))
The imports are supplied in the notebook immediately before this code. A DummyClassifier always predicts the training set's most common label here. Predict which label that is from y_train.value_counts() before running it.
.fit() learns a rule from training examples. .predict() supplies labels for input rows without receiving their answers. accuracy_score() then compares those predictions with the known held-out answers.
Checkpoint: Explain how this model can get many answers right while ignoring every passenger's age and fare. Keep its score for comparison.
Fill One Missing Value
A passenger's age may be missing. Before dealing with the full table, the notebook practices on invented ages: 10, missing, 30.
practice = pd.DataFrame({'age': [10, None, 30]}) practice_imputer = SimpleImputer(strategy='median') print(practice_imputer.fit_transform(practice)) print(practice_imputer.transform(pd.DataFrame({'age': [None]})))
An imputer supplies missing values according to a rule. The median, or middle value, of 10 and 30 is 20. Both missing entries above should become 20. fit_transform() learns the median and fills the practice table; transform() reuses that learned value for a new row.
This practice table is separate from our passenger experiment. For the real data, learn filling rules from training rows only. Otherwise the held-out data would influence the experiment before evaluation.
Train With Numeric Fields
Start with age, fare and passenger class, leaving sex aside for now. Run the notebook's numeric-model cell:
numeric_features = ['age', 'fare', 'pclass'] numeric = make_pipeline(SimpleImputer(strategy='median'), StandardScaler()) numeric_model = make_pipeline(numeric, LogisticRegression(max_iter=1000)) numeric_model.fit(X_train[numeric_features], y_train) numeric_predictions = numeric_model.predict(X_test[numeric_features])
A pipeline applies operations in order. The first fills gaps; StandardScaler() puts fields measured in different units on comparable scales using training statistics; the last fits a classifier. During prediction, the pipeline applies the same learned transformations to the new inputs.
Logistic regression is a classification model despite its name. This project uses its interface; studying its mathematics can be a later direction.
Checkpoint: There should be one prediction for every held-out row. Compare the numeric-only accuracy with the baseline. Explain which rows supplied the median and scaling statistics before continuing.
Add a Category in Two Steps
Our fourth field, sex, contains category names. The notebook first shows one-hot encoding on three invented rows:
practice_encoder = OneHotEncoder(handle_unknown='ignore', sparse_output=False) encoded = practice_encoder.fit_transform([['female'], ['male'], ['female']]) print(practice_encoder.get_feature_names_out(['sex'])) print(encoded)
Each category gets an indicator column containing 1 when present and 0 otherwise. Read the printed column names before interpreting the numbers. Predict why the first and third rows should match, then run and check.
Now connect the category treatment to the passenger model:
categorical = make_pipeline( SimpleImputer(strategy='most_frequent'), OneHotEncoder(handle_unknown='ignore') ) prepare = ColumnTransformer([ ('numbers', numeric, numeric_features), ('categories', categorical, ['sex']) ]) model = make_pipeline(prepare, LogisticRegression(max_iter=1000)) model.fit(X_train, y_train) predicted = model.predict(X_test)
ColumnTransformer sends the named columns to the appropriate steps. Numeric fields reuse the numeric treatment; category gaps use the most common training category, then encoding makes numeric indicators. An unknown category during prediction becomes all-zero indicators for that field. It doesn't become a newly learned category.
The full pipeline keeps this processing with the classifier. All fitted statistics still come from training rows. The notebook prints the four-feature accuracy. Compare it with the numeric-only result once; adding a feature doesn't guarantee a better score.
Evaluate What It Got Wrong
Accuracy is the fraction of held-out labels predicted correctly. Compare it with the baseline, then inspect the confusion matrix: a table separating correct predictions from the two kinds of mistake.
Its vertical axis contains actual labels and its horizontal axis contains predicted labels. Diagonal entries are correct predictions; the other two entries are errors. Work out where a passenger who survived but was predicted to die belongs. The four counts should add up to the number of held-out passengers.
The next notebook cell builds a review table with actual and predicted labels. Read one incorrect row before running the invented-passenger example.
The notebook also predicts two invented passengers. They aren't extra test evidence because we don't have known outcomes for them. Their purpose is to show how new rows use the trained pipeline.
Your numbers describe this dataset and split. Predictive performance on another passenger sample is a separate question.
Your Turn
Inspect several incorrect predictions in X_test. What information might the selected columns leave out? Write the question before proposing more features.
Use the numeric-only and four-feature results you already produced. Compare their predictions and discuss how historical social patterns enter a model through its data. A changed prediction isn't proof that changing a person's attribute would cause a different historical outcome.
Repeatedly choosing changes based on this same test set makes it less independent. A larger experiment should use validation data for model choices and reserve a fresh final test. Don't keep adjusting until a test number merely looks good.
Restart the kernel and run all cells. The source, split and complete fitting steps should be sufficient to reproduce the experiment.
In Your Own Words
Explain the jobs of training data, a baseline and held-out data. Give one example of information that would leak the answer into this model.
The scikit-learn getting-started guide develops the estimator and pipeline ideas. You have trained a model; the next project applies a related workflow to words.