import pandas as pd
# Read Data
data = pd.read_csv('housing.csv')
# Create Training Set and Test Set
from sklearn.model_selection import train_test_split
# train_test_split
# test_size, ratio of test set
# train_size, ratio of trainning set
# random_state, random seed
# shuffle, whether or not to shuffle the data before splitting
# stratify, data is split in a stratified fashion
import numpy as np
data_X = data.drop(['median_house_value'], axis = 1) # DataFrame
data_Y = data['median_house_value'] # Series
# split the dataset by categories of median income
data['income_cat'] = np.ceil(data['median_income']/1.5)
data['income_cat'].where(data['income_cat'] < 5, 5, inplace=True)
train_X, test_X, train_Y, test_Y = train_test_split(data_X, data_Y, test_size=0.2, random_state=42, stratify = data['income_cat'])
# Custom Transformer, add three features
from sklearn.base import BaseEstimator, TransformerMixin
class AddAttributes(BaseEstimator, TransformerMixin):
def __init__(self): # no *args or **kargs
pass
def fit(self, X, y=None):
return self # nothing else to do
def transform(self, X):
X["rooms_per_household"] = X["total_rooms"]/X["households"]
X["bedrooms_per_room"] = X["total_bedrooms"]/X["total_rooms"]
X["population_per_household"]=X["population"]/X["households"]
return X
# Select specific columns
class DataFrameSelector(BaseEstimator, TransformerMixin):
def __init__(self, attribute_names):
self.attribute_names = attribute_names
def fit(self, X, y=None):
return self
def transform(self, X):
return X[self.attribute_names]
# Create Pipeline for Numeric Columns
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
num_pipeline = Pipeline([
("select_numeric", DataFrameSelector(['longitude','latitude','housing_median_age','total_rooms','total_bedrooms','population','households','median_income',])),
('attribs_adder', AddAttributes()),
('imputer', SimpleImputer(strategy="median")),
('std_scaler', StandardScaler()),
])
#train_X_tr = num_pipeline.fit_transform(train_X)
# Create Pipeline for Categorical Columns
from sklearn.preprocessing import OneHotEncoder
cat_pipeline = Pipeline([
("select_numeric", DataFrameSelector(['ocean_proximity'])),
('cat', OneHotEncoder()),
])
#data_cat_tr = cat_pipeline.fit_transform(train_X)
# Merge Numeric Columns and Categorical Columns
from sklearn.pipeline import FeatureUnion
preprocess_pipeline = FeatureUnion(transformer_list=[
("num_pipeline", num_pipeline),
("cat_pipeline", cat_pipeline),
])
train_X = preprocess_pipeline.fit_transform(train_X).toarray()
test_X = preprocess_pipeline.transform(test_X).toarray()
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestRegressor
param_grid = [{'n_estimators': [40, 50, 100], 'max_features': [2, 4, 6, 8, 10, 12]}]
grid_search = GridSearchCV(RandomForestRegressor(), param_grid, cv=5, scoring='neg_mean_squared_error')
grid_search.fit(train_X, train_Y)
grid_search.best_params_ # best combination of parameters
grid_search.best_estimator_ # best model
# If GridSearchCV is initialized with refit=Ture (which is the default),
# then once it finds the best estimator using cross-validation, it retrains it on the whole training set
# evaluation scores
cvres = grid_search.cv_results_
for mean_score, params in zip(cvres["mean_test_score"], cvres["params"]):
print(np.sqrt(-mean_score), params)
# Evaluate test dataset
from sklearn.metrics import mean_squared_error
final_model = grid_search.best_estimator_
predictions = final_model.predict(test_X)
MSE = mean_squared_error(test_Y, predictions)
RMSE = np.sqrt(MSE)
RMSE
# 95% confidence interval for the generalizaiton error
from scipy import stats
confidence = 0.95
squared_errors = (predictions - test_Y) ** 2
np.sqrt(stats.t.interval(confidence, len(squared_errors) - 1,
loc=squared_errors.mean(),
scale=stats.sem(squared_errors)))