Binary Encoding: Predict Auto Insurance Premiums
Part 3 of the ML Regression Lab. Models only understand numbers — learn to convert a yes/no feature into 1/0 with binary encoding, then predict insurance premiums.
@shvinn
Machine Learning Engineer
Linear models do arithmetic — they can't multiply a weight by the word "yes".
Part 3 introduces feature encoding: the step that turns categorical data
into numbers a model can use. We start with the simplest case, a two-value
(binary) feature.
Follow along:
03_auto_insurance_premium_predictionin github.com/shvinn/ai-lab-99.
The goal
Predict an annual insurance premium from a driver's age (numeric) and whether they caused an accident in the past two years (yes/no).
Step 1 — Inspect the categorical column
import pandas as pd
df = pd.read_csv("auto_insurance_data.csv")
df["caused_an_accidient_in_past_two_years"].value_counts()Step 2 — Binary-encode yes/no → 1/0
A single column with exactly two categories maps cleanly to 1/0. pandas
.map() does it in one line:
df["caused_an_accidient_in_past_two_years"] = (
df["caused_an_accidient_in_past_two_years"].map({"yes": 1, "no": 0})
)Now both features are numeric and the column is ready for the model.
Step 3 — Train
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
X = df[["age", "caused_an_accidient_in_past_two_years"]]
y = df["annual_premium_usd"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
model = LinearRegression()
model.fit(X_train, y_train)Step 4 — Interpret
model.coef_ # → [-9.95, 499.19]
model.intercept_ # → 1997.92The encoded feature's coefficient has a clean reading: having caused an accident
(1 instead of 0) adds about **10. This interpretability is exactly why the encoding
matters — the number means something.
Step 5 — Evaluate
from sklearn.metrics import mean_squared_error
rmse_test = mean_squared_error(y_test, model.predict(X_test)) ** 0.5
print(f"RMSE Test: {rmse_test:.2f}")Step 6 — Deploy
The app collects the raw yes/no choice and applies the same encoding before prediction — your serving code must mirror your training preprocessing exactly.
import streamlit as st
def predict_auto_insurance_premium(age, accident_history):
return -9.95088805 * age + 499.18950284 * accident_history + 1997.9182518985917
st.title("🚗 Auto Insurance Premium Predictor")
age = st.number_input("Driver Age", 16, 100, 30, 1)
accident_option = st.selectbox("Accident in the past 2 years?", ["yes", "no"])
accident_history = 1 if accident_option == "yes" else 0 # same map as training
if st.button("Predict Premium"):
premium = predict_auto_insurance_premium(age, accident_history)
st.success(f"💵 Estimated Annual Premium: ${premium:.2f}")What's next
Binary encoding works for two categories. But what about three or more, like a
subscription tier? Using 1, 2, 3 would falsely imply an ordering.
Part 4 solves it with one-hot encoding.
Related reading
Multiple Linear Regression: Predict Energy Consumption
Part 2 of the ML Regression Lab. Extend regression to several features — predict energy use from temperature and humidity — and learn to read multi-feature coefficients.
One-Hot Encoding: Predict Streaming Revenue
Part 4 of the ML Regression Lab. Handle a multi-category feature the right way — one-hot encode subscription tiers and predict a streaming user's monthly revenue.