import numpy as np
import pandas as pd
import joblib
from sklearn.model_selection import train_test_split
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LinearRegression
from sklearn.metrics import (
mean_absolute_error,
mean_squared_error,
r2_score,
)
# --------------------------------------------------
# 1. Create a reproducible random number generator
# --------------------------------------------------
rng = np.random.default_rng(42)
n_samples = 300
brand_names = ["A", "B", "C", "D"]
# --------------------------------------------------
# 2. Generate synthetic input features
# --------------------------------------------------
mileage = rng.integers(
low=5_000,
high=220_001,
size=n_samples,
)
age = rng.integers(
low=0,
high=16,
size=n_samples,
)
engine_size = rng.choice(
[1.3, 1.6, 2.0, 2.5],
size=n_samples,
)
brand = rng.choice(
brand_names,
size=n_samples,
p=[0.30, 0.30, 0.25, 0.15],
)
automatic = rng.choice(
["yes", "no"],
size=n_samples,
p=[0.65, 0.35],
)
# --------------------------------------------------
# 3. Generate the synthetic target
# --------------------------------------------------
brand_effect = {
"A": 3_500,
"B": 1_500,
"C": 0,
"D": -2_000,
}
brand_bonus = np.array(
[brand_effect[current_brand] for current_brand in brand]
)
automatic_bonus = np.where(
automatic == "yes",
1_200,
0,
)
noise = rng.normal(
loc=0,
scale=1_800,
size=n_samples,
)
price = (
42_000
- 0.09 * mileage
- 950 * age
+ 1_800 * engine_size
+ brand_bonus
+ automatic_bonus
+ noise
)
price = np.maximum(price, 2_500)
price = price.round(0)
# --------------------------------------------------
# 4. Create the DataFrame
# --------------------------------------------------
cars = pd.DataFrame(
{
"mileage": mileage,
"age": age,
"engine_size": engine_size,
"brand": brand,
"automatic": automatic,
"price": price,
}
)
# --------------------------------------------------
# 5. Separate features and target
# --------------------------------------------------
feature_columns = [
"mileage",
"age",
"engine_size",
"brand",
"automatic",
]
X = cars[feature_columns].copy()
y = cars["price"].copy()
# --------------------------------------------------
# 6. Split into training and test sets
# --------------------------------------------------
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.20,
random_state=42,
)
# --------------------------------------------------
# 7. Define feature groups
# --------------------------------------------------
numeric_features = [
"mileage",
"age",
"engine_size",
]
categorical_features = [
"brand",
"automatic",
]
# --------------------------------------------------
# 8. Create preprocessing pipelines
# --------------------------------------------------
numeric_pipeline = Pipeline(
steps=[
(
"scaler",
StandardScaler(),
)
]
)
categorical_pipeline = Pipeline(
steps=[
(
"onehot",
OneHotEncoder(
drop="first",
handle_unknown="ignore",
sparse_output=False,
),
)
]
)
# --------------------------------------------------
# 9. Apply transformations to selected columns
# --------------------------------------------------
preprocessor = ColumnTransformer(
transformers=[
(
"num",
numeric_pipeline,
numeric_features,
),
(
"cat",
categorical_pipeline,
categorical_features,
),
],
remainder="drop",
verbose_feature_names_out=False,
)
128 ·