# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: tf2
#     language: python
#     name: tf2
# ---

# # Load Library

import pandas as pd
import os
import numpy as np

# # Load Data

DOWNLOAD_ROOT = "https://raw.githubusercontent.com/ageron/handson-ml2/master/"
HOUSING_PATH = os.path.join("datasets", "housing")
HOUSING_URL = DOWNLOAD_ROOT + "datasets/housing/housing.tgz"


def load_housing_data(housing_path=HOUSING_PATH):
    csv_path = os.path.join(housing_path, "housing.csv")
    return pd.read_csv(csv_path)


housing = load_housing_data()
housing.head()

# # Split into train test

housing['income_cat'] = pd.cut(housing['median_income'], bins=[0, 1.5, 3.0, 4.5, 6.0, np.inf], labels=[1,2,3,4,5])

# +
from sklearn.model_selection import StratifiedShuffleSplit

split = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=42)
for train_index, test_index in split.split(housing, housing['income_cat']):
    train = housing.loc[train_index]
    test = housing.loc[test_index]
# -

for set_ in (train, test):
    set_.drop("income_cat", axis=1, inplace=True)

# +
train_labels = train["median_house_value"].copy()
train = train.drop("median_house_value", axis=1)

test_labels = test["median_house_value"].copy()
test = test.drop("median_house_value", axis=1)
# -

# # Prepare the data

# +
from sklearn.base import BaseEstimator, TransformerMixin

rooms_ix, bedrooms_ix, population_ix, households_ix = 3, 4, 5, 6

class CombinedAttributesAdder(BaseEstimator, TransformerMixin):
    def __init__(self, add_bedrooms_per_room = True):
        self.add_bedrooms_per_room = add_bedrooms_per_room
    def fit(self, X, y=None):
        return self
    def transform(self, X):
        rooms_per_household = X[:, rooms_ix] / X[:, households_ix]
        population_per_household = X[:, population_ix] / X[:, households_ix]
        if self.add_bedrooms_per_room:
            bedrooms_per_room = X[:, bedrooms_ix] / X[:, rooms_ix]
            return np.c_[X, rooms_per_household, population_per_household, bedrooms_per_room]
        else:
            return np.c_[X, rooms_per_household, population_per_household]


# +
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder

num_pipeline = Pipeline([
    ('imputer', SimpleImputer(strategy='median')),
    ('attribs_adder', CombinedAttributesAdder()),
    ('std_scaler', StandardScaler())
])

from sklearn.compose import ColumnTransformer

train_num = train.drop('ocean_proximity', axis=1)
num_attribs = list(train_num)
cat_attribs = ['ocean_proximity']

full_pipeline = ColumnTransformer([
    ("num", num_pipeline, num_attribs),
    ("cat", OneHotEncoder(), cat_attribs)
])
# -

train_prepared = full_pipeline.fit_transform(train)

# +
from sklearn.svm import SVR

regr = SVR(C=1.0, kernel='linear')
regr.fit(train_prepared, train_labels)

# +
from sklearn.model_selection import cross_val_score

scores1 = cross_val_score(regr, train_prepared, train_labels, scoring='neg_mean_squared_error')
scores1_rmse = np.sqrt(-scores1)
# -

regr2 = SVR(C=3.0, kernel='rbf')
regr2.fit(train_prepared, train_labels)
scores2 = cross_val_score(regr, train_prepared, train_labels, scoring='neg_mean_squared_error')
scores2_rmse = np.sqrt(-scores2)


def display_scores(scores):
    print("Scores:", scores)
    print("Mean:", scores.mean())
    print("Standard deviation:", scores.std())


display_scores(scores1_rmse)
display_scores(scores2_rmse)

StarcoderdataJupyterScriptsDedupFiltered