<jupyter_start><jupyter_text>Important points:* Scaled the data and no other preprocessing of data* Used mean squared error as metric (Ignore use of word accuracy as metric)* Performed RandomizedSearchCV to get better $\gamma$ and C values* Achieved 0.48 mse on training data and 0.55 mse on validation<jupyter_code>import numpy as np
import pandas as pd 
import os
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt

np.random.seed(42)
%matplotlib inline
# Workound for urllib error
# https://stackoverflow.com/questions/27835619/urllib-and-ssl-certificate-verify-failed-error
# import ssl

# ssl._create_default_https_context = ssl._create_unverified_context
from sklearn.datasets import fetch_california_housing

data = fetch_california_housing()

x = data.data 
y = data.target
from sklearn.model_selection import train_test_split

x_train, x_valid, y_train, y_valid = train_test_split(x, y, test_size=0.3, random_state=42)
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
scaler.fit(x_train)

x_train = scaler.transform(x_train)
x_valid = scaler.transform(x_valid)
def score(y_true, y_pred, train=False):
    accuracy = np.sqrt(mean_squared_error(y_true, y_pred)) 
    if train:
        print("Train accuracy:{}".format(accuracy))
    else:
        print("Val accuracy:{}".format(accuracy))
from sklearn.svm import SVR

svr = SVR(kernel='linear')
svr.fit(x_train, y_train)
# Looks like it is working well
score(y_train, svr.predict(x_train), True)
score(y_valid, svr.predict(x_valid))
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import reciprocal, uniform
# Taken from solutions
params = {"gamma": reciprocal(0.0001, 5), "C": uniform(1, 10), 
         "kernel": ['linear', 'rbf']}
rand_search = RandomizedSearchCV(SVR(), params, n_iter = 30, verbose=2,
                                cv = 4, random_state=42, n_jobs=3)
rand_search.fit(x_train, y_train)
rand_search.best_estimator_
# Working better than the solution :-)
y_pred = rand_search.best_estimator_.predict(x_valid)
score(y_valid, y_pred)
y_pred = rand_search.best_estimator_.predict(x_train)
score(y_train, y_pred, True)<jupyter_output>Train accuracy:0.4821623953075446
{'fraction_non_alphanumeric': 0.09878213802435724, 'fraction_numerical': 0.02390617952187641, 'mean_word_length': 5.485380116959064, 'pattern_counts': {'":': 3, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '135776', 'license': 'MIT', 'n_tokens_mistral': 798, 'n_tokens_neox': 747, 'n_words': 189, 'path': 'exercise/chap_5/10_svm_reg_california.ipynb', 'repo_name': 'vaibhavkumar11/hands-on-ml', 'size': '2217'}