Codepython
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, RandomizedSearchCV
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, r2_score
# Load data
housing = pd.read_csv("../datasets/housing.csv")
# Features and target
X = housing.drop("median_house_value", axis=1)
y = housing["median_house_value"]
# Identify column types
categorical_cols = X.select_dtypes(include=["object"]).columns
numerical_cols = X.select_dtypes(exclude=["object"]).columns
# Numeric pipeline
num_pipeline = Pipeline([
("imputer", SimpleImputer(strategy="median"))
])
# Categorical pipeline
cat_pipeline = Pipeline([
("imputer", SimpleImputer(strategy="most_frequent")),
("encoder", OneHotEncoder(drop="first", handle_unknown="ignore"))
])
# Combine preprocessing
preprocessor = ColumnTransformer([
("num", num_pipeline, numerical_cols),
("cat", cat_pipeline, categorical_cols)
])
# Full pipeline
pipeline = Pipeline([
("preprocessing", preprocessor),
("model", RandomForestRegressor(random_state=42))
])
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Hyperparameter search space
param_dist = {
"model__n_estimators": [100, 200, 300, 500],
"model__max_depth": [None, 10, 20, 30],
"model__min_samples_split": [2, 5, 10],
"model__min_samples_leaf": [1, 2, 4],
"model__max_features": ["sqrt", "log2", None]
}
# Random Search
random_search = RandomizedSearchCV(
pipeline,
param_distributions=param_dist,
n_iter=4,
cv=2,
scoring="neg_mean_squared_error",
n_jobs=1,
verbose=2,
random_state=42
)
# Train model
random_search.fit(X_train, y_train)
# Best model
best_model = random_search.best_estimator_
# Predictions
y_pred = best_model.predict(X_test)
# Evaluation
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
r2 = r2_score(y_test, y_pred)
print("Best Parameters:", random_search.best_params_)
print("RMSE:", rmse)
print("R2 Score:", r2)
Output
Fitting 2 folds for each of 4 candidates, totalling 8 fits
[CV] END model__max_depth=None, model__max_features=None, model__min_samples_leaf=4, model__min_samples_split=5, model__n_estimators=300; total time= 9.0s
[CV] END model__max_depth=None, model__max_features=None, model__min_samples_leaf=4, model__min_samples_split=5, model__n_estimators=300; total time= 9.0s
[CV] END model__max_depth=30, model__max_features=sqrt, model__min_samples_leaf=4, model__min_samples_split=2, model__n_estimators=100; total time= 0.9s
[CV] END model__max_depth=30, model__max_features=sqrt, model__min_samples_leaf=4, model__min_samples_split=2, model__n_estimators=100; total time= 0.9s
[CV] END model__max_depth=20, model__max_features=log2, model__min_samples_leaf=2, model__min_samples_split=5, model__n_estimators=300; total time= 3.1s
[CV] END model__max_depth=20, model__max_features=log2, model__min_samples_leaf=2, model__min_samples_split=5, model__n_estimators=300; total time= 3.0s
[CV] END model__max_depth=None, model__max_features=None, model__min_samples_leaf=4, model__min_samples_split=10, model__n_estimators=300; total time= 8.8s
[CV] END model__max_depth=None, model__max_features=None, model__min_samples_leaf=4, model__min_samples_split=10, model__n_estimators=300; total time= 8.8s
Best Parameters: {'model__n_estimators': 300, 'model__min_samples_split': 5, 'model__min_samples_leaf': 4, 'model__max_features': None, 'model__max_depth': None}
RMSE: 49218.586716376674
R2 Score: 0.8151363949284425