# --------------------------------------------------
# 10. Create the complete machine-learning pipeline
# --------------------------------------------------
model = Pipeline(
steps=[
(
"preprocessing",
preprocessor,
),
(
"regressor",
LinearRegression(),
),
]
)
# --------------------------------------------------
# 11. Train the pipeline
# --------------------------------------------------
model.fit(
X_train,
y_train,
)
# --------------------------------------------------
# 12. Make predictions
# --------------------------------------------------
y_pred = model.predict(X_test)
# --------------------------------------------------
# 13. Evaluate the model
# --------------------------------------------------
mae = mean_absolute_error(
y_test,
y_pred,
)
mse = mean_squared_error(
y_test,
y_pred,
)
rmse = np.sqrt(mse)
r2 = r2_score(
y_test,
y_pred,
)
print(f"MAE: {mae:,.2f}")
print(f"MSE: {mse:,.2f}")
print(f"RMSE: {rmse:,.2f}")
print(f"R²: {r2:.4f}")
# --------------------------------------------------
# 14. Create a prediction report
# --------------------------------------------------
results = pd.DataFrame(
{
"actual_price": y_test,
"predicted_price": y_pred,
}
)
results["residual"] = (
results["actual_price"]
- results["predicted_price"]
)
results["absolute_error"] = (
results["residual"].abs()
)
print("\nPrediction results:")
print(results.head(10))
# --------------------------------------------------
# 15. Inspect the fitted preprocessing steps
# --------------------------------------------------
fitted_preprocessor = (
model.named_steps["preprocessing"]
)
fitted_regressor = (
model.named_steps["regressor"]
)
fitted_scaler = (
fitted_preprocessor
.named_transformers_["num"]
.named_steps["scaler"]
)
fitted_encoder = (
fitted_preprocessor
.named_transformers_["cat"]
.named_steps["onehot"]
)
transformed_feature_names = (
fitted_preprocessor
.get_feature_names_out()
)
print("\nScaler means:")
print(fitted_scaler.mean_)
print("\nScaler standard deviations:")
print(fitted_scaler.scale_)
print("\nEncoder categories:")
print(fitted_encoder.categories_)
print("\nRegression intercept:")
print(fitted_regressor.intercept_)
print("\nRegression coefficients:")
print(fitted_regressor.coef_)
# --------------------------------------------------
# 16. Inspect the transformed training matrix
# --------------------------------------------------
X_train_transformed = (
fitted_preprocessor.transform(X_train)
)
X_train_transformed_df = pd.DataFrame(
X_train_transformed,
columns=transformed_feature_names,
index=X_train.index,
)
print("\nTransformed training data:")
print(X_train_transformed_df.head())
# --------------------------------------------------
# 17. Compare with NumPy least squares
# --------------------------------------------------
X_design = np.column_stack(
[
np.ones(
X_train_transformed.shape[0]
),
X_train_transformed,
]
)
beta, *_ = np.linalg.lstsq(
X_design,
y_train.to_numpy(),
rcond=None,
)
print("\nNumPy intercept:")
print(beta[0])
print("\nNumPy coefficients:")
print(beta[1:])
print(
"\nIntercepts are close:",
np.allclose(
beta[0],
fitted_regressor.intercept_,
),
)
print(
"Coefficients are close:",
np.allclose(
beta[1:],
fitted_regressor.coef_,
),
)
# --------------------------------------------------
# 18. Predict the price of a new car
# --------------------------------------------------
new_car = pd.DataFrame(
{
"mileage": [50_000],
"age": [4],
"engine_size": [2.0],
"brand": ["B"],
"automatic": ["yes"],
}
)
predicted_price = model.predict(
new_car
)[0]
print(
"\nPredicted price for the new car:",
f"{predicted_price:,.2f}",
)
153 ·