Chapter 2 – End-to-end Machine Learning project

Welcome to Machine Learning Housing Corp.! Your task is to predict median house values in Californian districts, given a number of features from these districts.

This notebook contains all the sample code and solutions to the exercices in chapter 2.

Note: You may find little differences between the code outputs in the book and in these Jupyter notebooks: these slight differences are mostly due to the random nature of many training algorithms: although I have tried to make these notebooks' outputs as constant as possible, it is impossible to guarantee that they will produce the exact same output on every platform. Also, some data structures (such as dictionaries) do not preserve the item order. Finally, I fixed a few minor bugs (I added notes next to the concerned cells) which lead to slightly different results, without changing the ideas presented in the book.

Setup

First, let's make sure this notebook works well in both python 2 and 3, import a few common modules, ensure MatplotLib plots figures inline and prepare a function to save the figures:


In [1]:
# To support both python 2 and python 3
from __future__ import division, print_function, unicode_literals

# Common imports
import numpy as np
import os

# to make this notebook's output stable across runs
np.random.seed(42)

# To plot pretty figures
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12

# Where to save the figures
PROJECT_ROOT_DIR = "."
CHAPTER_ID = "end_to_end_project"

def save_fig(fig_id, tight_layout=True):
    path = os.path.join(PROJECT_ROOT_DIR, "images", CHAPTER_ID, fig_id + ".png")
    print("Saving figure", fig_id)
    if tight_layout:
        plt.tight_layout()
    plt.savefig(path, format='png', dpi=300)

Get the data


In [2]:
import os
import tarfile
from six.moves import urllib

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

def fetch_housing_data(housing_url=HOUSING_URL, housing_path=HOUSING_PATH):
    if not os.path.isdir(housing_path):
        os.makedirs(housing_path)
    tgz_path = os.path.join(housing_path, "housing.tgz")
    urllib.request.urlretrieve(housing_url, tgz_path)
    housing_tgz = tarfile.open(tgz_path)
    housing_tgz.extractall(path=housing_path)
    housing_tgz.close()

In [3]:
fetch_housing_data()

In [4]:
import pandas as pd

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

In [5]:
housing = load_housing_data()
housing.head()


Out[5]:
longitude latitude housing_median_age total_rooms total_bedrooms population households median_income median_house_value ocean_proximity
0 -122.23 37.88 41.0 880.0 129.0 322.0 126.0 8.3252 452600.0 NEAR BAY
1 -122.22 37.86 21.0 7099.0 1106.0 2401.0 1138.0 8.3014 358500.0 NEAR BAY
2 -122.24 37.85 52.0 1467.0 190.0 496.0 177.0 7.2574 352100.0 NEAR BAY
3 -122.25 37.85 52.0 1274.0 235.0 558.0 219.0 5.6431 341300.0 NEAR BAY
4 -122.25 37.85 52.0 1627.0 280.0 565.0 259.0 3.8462 342200.0 NEAR BAY

In [6]:
housing.info()


<class 'pandas.core.frame.DataFrame'>
RangeIndex: 20640 entries, 0 to 20639
Data columns (total 10 columns):
longitude             20640 non-null float64
latitude              20640 non-null float64
housing_median_age    20640 non-null float64
total_rooms           20640 non-null float64
total_bedrooms        20433 non-null float64
population            20640 non-null float64
households            20640 non-null float64
median_income         20640 non-null float64
median_house_value    20640 non-null float64
ocean_proximity       20640 non-null object
dtypes: float64(9), object(1)
memory usage: 1.6+ MB

In [7]:
housing["ocean_proximity"].value_counts()


Out[7]:
<1H OCEAN     9136
INLAND        6551
NEAR OCEAN    2658
NEAR BAY      2290
ISLAND           5
Name: ocean_proximity, dtype: int64

In [8]:
housing.describe()


Out[8]:
longitude latitude housing_median_age total_rooms total_bedrooms population households median_income median_house_value
count 20640.000000 20640.000000 20640.000000 20640.000000 20433.000000 20640.000000 20640.000000 20640.000000 20640.000000
mean -119.569704 35.631861 28.639486 2635.763081 537.870553 1425.476744 499.539680 3.870671 206855.816909
std 2.003532 2.135952 12.585558 2181.615252 421.385070 1132.462122 382.329753 1.899822 115395.615874
min -124.350000 32.540000 1.000000 2.000000 1.000000 3.000000 1.000000 0.499900 14999.000000
25% -121.800000 33.930000 18.000000 1447.750000 296.000000 787.000000 280.000000 2.563400 119600.000000
50% -118.490000 34.260000 29.000000 2127.000000 435.000000 1166.000000 409.000000 3.534800 179700.000000
75% -118.010000 37.710000 37.000000 3148.000000 647.000000 1725.000000 605.000000 4.743250 264725.000000
max -114.310000 41.950000 52.000000 39320.000000 6445.000000 35682.000000 6082.000000 15.000100 500001.000000

In [9]:
%matplotlib inline
import matplotlib.pyplot as plt
housing.hist(bins=50, figsize=(20,15))
save_fig("attribute_histogram_plots")
plt.show()


Saving figure attribute_histogram_plots

In [10]:
# to make this notebook's output identical at every run
np.random.seed(42)

In [11]:
import numpy as np

# For illustration only. Sklearn has train_test_split()
def split_train_test(data, test_ratio):
    shuffled_indices = np.random.permutation(len(data))
    test_set_size = int(len(data) * test_ratio)
    test_indices = shuffled_indices[:test_set_size]
    train_indices = shuffled_indices[test_set_size:]
    return data.iloc[train_indices], data.iloc[test_indices]

In [12]:
train_set, test_set = split_train_test(housing, 0.2)
print(len(train_set), "train +", len(test_set), "test")


16512 train + 4128 test

In [13]:
import hashlib

def test_set_check(identifier, test_ratio, hash):
    return hash(np.int64(identifier)).digest()[-1] < 256 * test_ratio

def split_train_test_by_id(data, test_ratio, id_column, hash=hashlib.md5):
    ids = data[id_column]
    in_test_set = ids.apply(lambda id_: test_set_check(id_, test_ratio, hash))
    return data.loc[~in_test_set], data.loc[in_test_set]

In [14]:
# This version supports both Python 2 and Python 3, instead of just Python 3.
def test_set_check(identifier, test_ratio, hash):
    return bytearray(hash(np.int64(identifier)).digest())[-1] < 256 * test_ratio

In [15]:
housing_with_id = housing.reset_index()   # adds an `index` column
train_set, test_set = split_train_test_by_id(housing_with_id, 0.2, "index")

In [16]:
housing_with_id["id"] = housing["longitude"] * 1000 + housing["latitude"]
train_set, test_set = split_train_test_by_id(housing_with_id, 0.2, "id")

In [17]:
test_set.head()


Out[17]:
index longitude latitude housing_median_age total_rooms total_bedrooms population households median_income median_house_value ocean_proximity id
8 8 -122.26 37.84 42.0 2555.0 665.0 1206.0 595.0 2.0804 226700.0 NEAR BAY -122222.16
10 10 -122.26 37.85 52.0 2202.0 434.0 910.0 402.0 3.2031 281500.0 NEAR BAY -122222.15
11 11 -122.26 37.85 52.0 3503.0 752.0 1504.0 734.0 3.2705 241800.0 NEAR BAY -122222.15
12 12 -122.26 37.85 52.0 2491.0 474.0 1098.0 468.0 3.0750 213500.0 NEAR BAY -122222.15
13 13 -122.26 37.84 52.0 696.0 191.0 345.0 174.0 2.6736 191300.0 NEAR BAY -122222.16

In [18]:
from sklearn.model_selection import train_test_split

train_set, test_set = train_test_split(housing, test_size=0.2, random_state=42)

In [19]:
test_set.head()


Out[19]:
longitude latitude housing_median_age total_rooms total_bedrooms population households median_income median_house_value ocean_proximity
20046 -119.01 36.06 25.0 1505.0 NaN 1392.0 359.0 1.6812 47700.0 INLAND
3024 -119.46 35.14 30.0 2943.0 NaN 1565.0 584.0 2.5313 45800.0 INLAND
15663 -122.44 37.80 52.0 3830.0 NaN 1310.0 963.0 3.4801 500001.0 NEAR BAY
20484 -118.72 34.28 17.0 3051.0 NaN 1705.0 495.0 5.7376 218600.0 <1H OCEAN
9814 -121.93 36.62 34.0 2351.0 NaN 1063.0 428.0 3.7250 278000.0 NEAR OCEAN

In [20]:
housing["median_income"].hist()


Out[20]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f26e609fa20>

In [21]:
# Divide by 1.5 to limit the number of income categories
housing["income_cat"] = np.ceil(housing["median_income"] / 1.5)
# Label those above 5 as 5
housing["income_cat"].where(housing["income_cat"] < 5, 5.0, inplace=True)

In [22]:
housing["income_cat"].value_counts()


Out[22]:
3.0    7236
2.0    6581
4.0    3639
5.0    2362
1.0     822
Name: income_cat, dtype: int64

In [23]:
housing["income_cat"].hist()


Out[23]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f26e60a7390>

In [24]:
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"]):
    strat_train_set = housing.loc[train_index]
    strat_test_set = housing.loc[test_index]

In [25]:
housing["income_cat"].value_counts() / len(housing)


Out[25]:
3.0    0.350581
2.0    0.318847
4.0    0.176308
5.0    0.114438
1.0    0.039826
Name: income_cat, dtype: float64

In [26]:
def income_cat_proportions(data):
    return data["income_cat"].value_counts() / len(data)

train_set, test_set = train_test_split(housing, test_size=0.2, random_state=42)

compare_props = pd.DataFrame({
    "Overall": income_cat_proportions(housing),
    "Stratified": income_cat_proportions(strat_test_set),
    "Random": income_cat_proportions(test_set),
}).sort_index()
compare_props["Rand. %error"] = 100 * compare_props["Random"] / compare_props["Overall"] - 100
compare_props["Strat. %error"] = 100 * compare_props["Stratified"] / compare_props["Overall"] - 100

In [27]:
compare_props


Out[27]:
Overall Random Stratified Rand. %error Strat. %error
1.0 0.039826 0.040213 0.039729 0.973236 -0.243309
2.0 0.318847 0.324370 0.318798 1.732260 -0.015195
3.0 0.350581 0.358527 0.350533 2.266446 -0.013820
4.0 0.176308 0.167393 0.176357 -5.056334 0.027480
5.0 0.114438 0.109496 0.114583 -4.318374 0.127011

In [28]:
for set_ in (strat_train_set, strat_test_set):
    set_.drop("income_cat", axis=1, inplace=True)

Discover and visualize the data to gain insights


In [29]:
housing = strat_train_set.copy()

In [30]:
housing.plot(kind="scatter", x="longitude", y="latitude")
save_fig("bad_visualization_plot")


Saving figure bad_visualization_plot

In [31]:
housing.plot(kind="scatter", x="longitude", y="latitude", alpha=0.1)
save_fig("better_visualization_plot")


Saving figure better_visualization_plot

The argument sharex=False fixes a display bug (the x-axis values and legend were not displayed). This is a temporary fix (see: https://github.com/pandas-dev/pandas/issues/10611). Thanks to Wilmer Arellano for pointing it out.


In [32]:
housing.plot(kind="scatter", x="longitude", y="latitude", alpha=0.4,
    s=housing["population"]/100, label="population", figsize=(10,7),
    c="median_house_value", cmap=plt.get_cmap("jet"), colorbar=True,
    sharex=False)
plt.legend()
save_fig("housing_prices_scatterplot")


Saving figure housing_prices_scatterplot

In [33]:
import matplotlib.image as mpimg
california_img=mpimg.imread(PROJECT_ROOT_DIR + '/images/end_to_end_project/california.png')
ax = housing.plot(kind="scatter", x="longitude", y="latitude", figsize=(10,7),
                       s=housing['population']/100, label="Population",
                       c="median_house_value", cmap=plt.get_cmap("jet"),
                       colorbar=False, alpha=0.4,
                      )
plt.imshow(california_img, extent=[-124.55, -113.80, 32.45, 42.05], alpha=0.5)
plt.ylabel("Latitude", fontsize=14)
plt.xlabel("Longitude", fontsize=14)

prices = housing["median_house_value"]
tick_values = np.linspace(prices.min(), prices.max(), 11)
cbar = plt.colorbar()
cbar.ax.set_yticklabels(["$%dk"%(round(v/1000)) for v in tick_values], fontsize=14)
cbar.set_label('Median House Value', fontsize=16)

plt.legend(fontsize=16)
save_fig("california_housing_prices_plot")
plt.show()


Saving figure california_housing_prices_plot

In [34]:
corr_matrix = housing.corr()

In [35]:
corr_matrix["median_house_value"].sort_values(ascending=False)


Out[35]:
median_house_value    1.000000
median_income         0.687160
total_rooms           0.135097
housing_median_age    0.114110
households            0.064506
total_bedrooms        0.047689
population           -0.026920
longitude            -0.047432
latitude             -0.142724
Name: median_house_value, dtype: float64

In [36]:
# from pandas.tools.plotting import scatter_matrix # For older versions of Pandas
from pandas.plotting import scatter_matrix

attributes = ["median_house_value", "median_income", "total_rooms",
              "housing_median_age"]
scatter_matrix(housing[attributes], figsize=(12, 8))
save_fig("scatter_matrix_plot")


Saving figure scatter_matrix_plot

In [37]:
housing.plot(kind="scatter", x="median_income", y="median_house_value",
             alpha=0.1)
plt.axis([0, 16, 0, 550000])
save_fig("income_vs_house_value_scatterplot")


Saving figure income_vs_house_value_scatterplot

In [38]:
housing["rooms_per_household"] = housing["total_rooms"]/housing["households"]
housing["bedrooms_per_room"] = housing["total_bedrooms"]/housing["total_rooms"]
housing["population_per_household"]=housing["population"]/housing["households"]

Note: there was a bug in the previous cell, in the definition of the rooms_per_household attribute. This explains why the correlation value below differs slightly from the value in the book (unless you are reading the latest version).


In [39]:
corr_matrix = housing.corr()
corr_matrix["median_house_value"].sort_values(ascending=False)


Out[39]:
median_house_value          1.000000
median_income               0.687160
rooms_per_household         0.146285
total_rooms                 0.135097
housing_median_age          0.114110
households                  0.064506
total_bedrooms              0.047689
population_per_household   -0.021985
population                 -0.026920
longitude                  -0.047432
latitude                   -0.142724
bedrooms_per_room          -0.259984
Name: median_house_value, dtype: float64

In [40]:
housing.plot(kind="scatter", x="rooms_per_household", y="median_house_value",
             alpha=0.2)
plt.axis([0, 5, 0, 520000])
plt.show()



In [41]:
housing.describe()


Out[41]:
longitude latitude housing_median_age total_rooms total_bedrooms population households median_income median_house_value rooms_per_household bedrooms_per_room population_per_household
count 16512.000000 16512.000000 16512.000000 16512.000000 16354.000000 16512.000000 16512.000000 16512.000000 16512.000000 16512.000000 16354.000000 16512.000000
mean -119.575834 35.639577 28.653101 2622.728319 534.973890 1419.790819 497.060380 3.875589 206990.920724 5.440341 0.212878 3.096437
std 2.001860 2.138058 12.574726 2138.458419 412.699041 1115.686241 375.720845 1.904950 115703.014830 2.611712 0.057379 11.584826
min -124.350000 32.540000 1.000000 6.000000 2.000000 3.000000 2.000000 0.499900 14999.000000 1.130435 0.100000 0.692308
25% -121.800000 33.940000 18.000000 1443.000000 295.000000 784.000000 279.000000 2.566775 119800.000000 4.442040 0.175304 2.431287
50% -118.510000 34.260000 29.000000 2119.500000 433.000000 1164.000000 408.000000 3.540900 179500.000000 5.232284 0.203031 2.817653
75% -118.010000 37.720000 37.000000 3141.000000 644.000000 1719.250000 602.000000 4.744475 263900.000000 6.056361 0.239831 3.281420
max -114.310000 41.950000 52.000000 39320.000000 6210.000000 35682.000000 5358.000000 15.000100 500001.000000 141.909091 1.000000 1243.333333

Prepare the data for Machine Learning algorithms


In [42]:
housing = strat_train_set.drop("median_house_value", axis=1) # drop labels for training set
housing_labels = strat_train_set["median_house_value"].copy()

In [43]:
sample_incomplete_rows = housing[housing.isnull().any(axis=1)].head()
sample_incomplete_rows


Out[43]:
longitude latitude housing_median_age total_rooms total_bedrooms population households median_income ocean_proximity
4629 -118.30 34.07 18.0 3759.0 NaN 3296.0 1462.0 2.2708 <1H OCEAN
6068 -117.86 34.01 16.0 4632.0 NaN 3038.0 727.0 5.1762 <1H OCEAN
17923 -121.97 37.35 30.0 1955.0 NaN 999.0 386.0 4.6328 <1H OCEAN
13656 -117.30 34.05 6.0 2155.0 NaN 1039.0 391.0 1.6675 INLAND
19252 -122.79 38.48 7.0 6837.0 NaN 3468.0 1405.0 3.1662 <1H OCEAN

In [44]:
sample_incomplete_rows.dropna(subset=["total_bedrooms"])    # option 1


Out[44]:
longitude latitude housing_median_age total_rooms total_bedrooms population households median_income ocean_proximity

In [45]:
sample_incomplete_rows.drop("total_bedrooms", axis=1)       # option 2


Out[45]:
longitude latitude housing_median_age total_rooms population households median_income ocean_proximity
4629 -118.30 34.07 18.0 3759.0 3296.0 1462.0 2.2708 <1H OCEAN
6068 -117.86 34.01 16.0 4632.0 3038.0 727.0 5.1762 <1H OCEAN
17923 -121.97 37.35 30.0 1955.0 999.0 386.0 4.6328 <1H OCEAN
13656 -117.30 34.05 6.0 2155.0 1039.0 391.0 1.6675 INLAND
19252 -122.79 38.48 7.0 6837.0 3468.0 1405.0 3.1662 <1H OCEAN

In [46]:
median = housing["total_bedrooms"].median()
sample_incomplete_rows["total_bedrooms"].fillna(median, inplace=True) # option 3
sample_incomplete_rows


Out[46]:
longitude latitude housing_median_age total_rooms total_bedrooms population households median_income ocean_proximity
4629 -118.30 34.07 18.0 3759.0 433.0 3296.0 1462.0 2.2708 <1H OCEAN
6068 -117.86 34.01 16.0 4632.0 433.0 3038.0 727.0 5.1762 <1H OCEAN
17923 -121.97 37.35 30.0 1955.0 433.0 999.0 386.0 4.6328 <1H OCEAN
13656 -117.30 34.05 6.0 2155.0 433.0 1039.0 391.0 1.6675 INLAND
19252 -122.79 38.48 7.0 6837.0 433.0 3468.0 1405.0 3.1662 <1H OCEAN

In [47]:
from sklearn.preprocessing import Imputer

imputer = Imputer(strategy="median")

Remove the text attribute because median can only be calculated on numerical attributes:


In [48]:
housing_num = housing.drop("ocean_proximity", axis=1)

In [49]:
imputer.fit(housing_num)


Out[49]:
Imputer(axis=0, copy=True, missing_values='NaN', strategy='median', verbose=0)

In [50]:
imputer.statistics_


Out[50]:
array([ -118.51  ,    34.26  ,    29.    ,  2119.5   ,   433.    ,
        1164.    ,   408.    ,     3.5409])

Check that this is the same as manually computing the median of each attribute:


In [51]:
housing_num.median().values


Out[51]:
array([ -118.51  ,    34.26  ,    29.    ,  2119.5   ,   433.    ,
        1164.    ,   408.    ,     3.5409])

Transform the training set:


In [52]:
X = imputer.transform(housing_num)

In [53]:
housing_tr = pd.DataFrame(X, columns=housing_num.columns,
                          index = list(housing.index.values))

In [54]:
housing_tr.loc[sample_incomplete_rows.index.values]


Out[54]:
longitude latitude housing_median_age total_rooms total_bedrooms population households median_income
4629 -118.30 34.07 18.0 3759.0 433.0 3296.0 1462.0 2.2708
6068 -117.86 34.01 16.0 4632.0 433.0 3038.0 727.0 5.1762
17923 -121.97 37.35 30.0 1955.0 433.0 999.0 386.0 4.6328
13656 -117.30 34.05 6.0 2155.0 433.0 1039.0 391.0 1.6675
19252 -122.79 38.48 7.0 6837.0 433.0 3468.0 1405.0 3.1662

In [55]:
imputer.strategy


Out[55]:
'median'

In [56]:
housing_tr = pd.DataFrame(X, columns=housing_num.columns)
housing_tr.head()


Out[56]:
longitude latitude housing_median_age total_rooms total_bedrooms population households median_income
0 -121.89 37.29 38.0 1568.0 351.0 710.0 339.0 2.7042
1 -121.93 37.05 14.0 679.0 108.0 306.0 113.0 6.4214
2 -117.20 32.77 31.0 1952.0 471.0 936.0 462.0 2.8621
3 -119.61 36.31 25.0 1847.0 371.0 1460.0 353.0 1.8839
4 -118.59 34.23 17.0 6592.0 1525.0 4459.0 1463.0 3.0347

Now let's preprocess the categorical input feature, ocean_proximity:


In [57]:
housing_cat = housing["ocean_proximity"]
housing_cat.head(10)


Out[57]:
17606     <1H OCEAN
18632     <1H OCEAN
14650    NEAR OCEAN
3230         INLAND
3555      <1H OCEAN
19480        INLAND
8879      <1H OCEAN
13685        INLAND
4937      <1H OCEAN
4861      <1H OCEAN
Name: ocean_proximity, dtype: object

We can use Pandas' factorize() method to convert this string categorical feature to an integer categorical feature, which will be easier for Machine Learning algorithms to handle:


In [58]:
housing_cat_encoded, housing_categories = housing_cat.factorize()
housing_cat_encoded[:10]


Out[58]:
array([0, 0, 1, 2, 0, 2, 0, 2, 0, 0])

In [59]:
housing_categories


Out[59]:
Index(['<1H OCEAN', 'NEAR OCEAN', 'INLAND', 'NEAR BAY', 'ISLAND'], dtype='object')

Warning: earlier versions of the book used the LabelEncoder class instead of Pandas' factorize() method. This was incorrect: indeed, as its name suggests, the LabelEncoder class was designed for labels, not for input features. The code worked because we were handling a single categorical input feature, but it would break if you passed multiple categorical input features.

We can convert each categorical value to a one-hot vector using a OneHotEncoder:


In [60]:
from sklearn.preprocessing import OneHotEncoder

encoder = OneHotEncoder()
housing_cat_1hot = encoder.fit_transform(housing_cat_encoded.reshape(-1,1))
housing_cat_1hot


Out[60]:
<16512x5 sparse matrix of type '<class 'numpy.float64'>'
	with 16512 stored elements in Compressed Sparse Row format>

The OneHotEncoder returns a sparse array by default, but we can convert it to a dense array if needed:


In [61]:
housing_cat_1hot.toarray()


Out[61]:
array([[ 1.,  0.,  0.,  0.,  0.],
       [ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.],
       ..., 
       [ 0.,  0.,  1.,  0.,  0.],
       [ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  1.,  0.]])

Warning: earlier versions of the book used the LabelBinarizer class at this point. Again, this was incorrect: just like the LabelEncoder class, the LabelBinarizer class was designed to preprocess labels, not input features. A better solution is to use Scikit-Learn's upcoming CategoricalEncoder class: it will soon be added to Scikit-Learn, and in the meantime you can use the code below (copied from Pull Request #9151).


In [62]:
# Definition of the CategoricalEncoder class, copied from PR #9151.
# Just run this cell, or copy it to your code, do not try to understand it (yet).

from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.utils import check_array
from sklearn.preprocessing import LabelEncoder
from scipy import sparse

class CategoricalEncoder(BaseEstimator, TransformerMixin):
    """Encode categorical features as a numeric array.
    The input to this transformer should be a matrix of integers or strings,
    denoting the values taken on by categorical (discrete) features.
    The features can be encoded using a one-hot aka one-of-K scheme
    (``encoding='onehot'``, the default) or converted to ordinal integers
    (``encoding='ordinal'``).
    This encoding is needed for feeding categorical data to many scikit-learn
    estimators, notably linear models and SVMs with the standard kernels.
    Read more in the :ref:`User Guide <preprocessing_categorical_features>`.
    Parameters
    ----------
    encoding : str, 'onehot', 'onehot-dense' or 'ordinal'
        The type of encoding to use (default is 'onehot'):
        - 'onehot': encode the features using a one-hot aka one-of-K scheme
          (or also called 'dummy' encoding). This creates a binary column for
          each category and returns a sparse matrix.
        - 'onehot-dense': the same as 'onehot' but returns a dense array
          instead of a sparse matrix.
        - 'ordinal': encode the features as ordinal integers. This results in
          a single column of integers (0 to n_categories - 1) per feature.
    categories : 'auto' or a list of lists/arrays of values.
        Categories (unique values) per feature:
        - 'auto' : Determine categories automatically from the training data.
        - list : ``categories[i]`` holds the categories expected in the ith
          column. The passed categories are sorted before encoding the data
          (used categories can be found in the ``categories_`` attribute).
    dtype : number type, default np.float64
        Desired dtype of output.
    handle_unknown : 'error' (default) or 'ignore'
        Whether to raise an error or ignore if a unknown categorical feature is
        present during transform (default is to raise). When this is parameter
        is set to 'ignore' and an unknown category is encountered during
        transform, the resulting one-hot encoded columns for this feature
        will be all zeros.
        Ignoring unknown categories is not supported for
        ``encoding='ordinal'``.
    Attributes
    ----------
    categories_ : list of arrays
        The categories of each feature determined during fitting. When
        categories were specified manually, this holds the sorted categories
        (in order corresponding with output of `transform`).
    Examples
    --------
    Given a dataset with three features and two samples, we let the encoder
    find the maximum value per feature and transform the data to a binary
    one-hot encoding.
    >>> from sklearn.preprocessing import CategoricalEncoder
    >>> enc = CategoricalEncoder(handle_unknown='ignore')
    >>> enc.fit([[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]])
    ... # doctest: +ELLIPSIS
    CategoricalEncoder(categories='auto', dtype=<... 'numpy.float64'>,
              encoding='onehot', handle_unknown='ignore')
    >>> enc.transform([[0, 1, 1], [1, 0, 4]]).toarray()
    array([[ 1.,  0.,  0.,  1.,  0.,  0.,  1.,  0.,  0.],
           [ 0.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.]])
    See also
    --------
    sklearn.preprocessing.OneHotEncoder : performs a one-hot encoding of
      integer ordinal features. The ``OneHotEncoder assumes`` that input
      features take on values in the range ``[0, max(feature)]`` instead of
      using the unique values.
    sklearn.feature_extraction.DictVectorizer : performs a one-hot encoding of
      dictionary items (also handles string-valued features).
    sklearn.feature_extraction.FeatureHasher : performs an approximate one-hot
      encoding of dictionary items or strings.
    """

    def __init__(self, encoding='onehot', categories='auto', dtype=np.float64,
                 handle_unknown='error'):
        self.encoding = encoding
        self.categories = categories
        self.dtype = dtype
        self.handle_unknown = handle_unknown

    def fit(self, X, y=None):
        """Fit the CategoricalEncoder to X.
        Parameters
        ----------
        X : array-like, shape [n_samples, n_feature]
            The data to determine the categories of each feature.
        Returns
        -------
        self
        """

        if self.encoding not in ['onehot', 'onehot-dense', 'ordinal']:
            template = ("encoding should be either 'onehot', 'onehot-dense' "
                        "or 'ordinal', got %s")
            raise ValueError(template % self.handle_unknown)

        if self.handle_unknown not in ['error', 'ignore']:
            template = ("handle_unknown should be either 'error' or "
                        "'ignore', got %s")
            raise ValueError(template % self.handle_unknown)

        if self.encoding == 'ordinal' and self.handle_unknown == 'ignore':
            raise ValueError("handle_unknown='ignore' is not supported for"
                             " encoding='ordinal'")

        X = check_array(X, dtype=np.object, accept_sparse='csc', copy=True)
        n_samples, n_features = X.shape

        self._label_encoders_ = [LabelEncoder() for _ in range(n_features)]

        for i in range(n_features):
            le = self._label_encoders_[i]
            Xi = X[:, i]
            if self.categories == 'auto':
                le.fit(Xi)
            else:
                valid_mask = np.in1d(Xi, self.categories[i])
                if not np.all(valid_mask):
                    if self.handle_unknown == 'error':
                        diff = np.unique(Xi[~valid_mask])
                        msg = ("Found unknown categories {0} in column {1}"
                               " during fit".format(diff, i))
                        raise ValueError(msg)
                le.classes_ = np.array(np.sort(self.categories[i]))

        self.categories_ = [le.classes_ for le in self._label_encoders_]

        return self

    def transform(self, X):
        """Transform X using one-hot encoding.
        Parameters
        ----------
        X : array-like, shape [n_samples, n_features]
            The data to encode.
        Returns
        -------
        X_out : sparse matrix or a 2-d array
            Transformed input.
        """
        X = check_array(X, accept_sparse='csc', dtype=np.object, copy=True)
        n_samples, n_features = X.shape
        X_int = np.zeros_like(X, dtype=np.int)
        X_mask = np.ones_like(X, dtype=np.bool)

        for i in range(n_features):
            valid_mask = np.in1d(X[:, i], self.categories_[i])

            if not np.all(valid_mask):
                if self.handle_unknown == 'error':
                    diff = np.unique(X[~valid_mask, i])
                    msg = ("Found unknown categories {0} in column {1}"
                           " during transform".format(diff, i))
                    raise ValueError(msg)
                else:
                    # Set the problematic rows to an acceptable value and
                    # continue `The rows are marked `X_mask` and will be
                    # removed later.
                    X_mask[:, i] = valid_mask
                    X[:, i][~valid_mask] = self.categories_[i][0]
            X_int[:, i] = self._label_encoders_[i].transform(X[:, i])

        if self.encoding == 'ordinal':
            return X_int.astype(self.dtype, copy=False)

        mask = X_mask.ravel()
        n_values = [cats.shape[0] for cats in self.categories_]
        n_values = np.array([0] + n_values)
        indices = np.cumsum(n_values)

        column_indices = (X_int + indices[:-1]).ravel()[mask]
        row_indices = np.repeat(np.arange(n_samples, dtype=np.int32),
                                n_features)[mask]
        data = np.ones(n_samples * n_features)[mask]

        out = sparse.csc_matrix((data, (row_indices, column_indices)),
                                shape=(n_samples, indices[-1]),
                                dtype=self.dtype).tocsr()
        if self.encoding == 'onehot-dense':
            return out.toarray()
        else:
            return out

The CategoricalEncoder expects a 2D array containing one or more categorical input features. We need to reshape housing_cat to a 2D array:


In [63]:
#from sklearn.preprocessing import CategoricalEncoder # in future versions of Scikit-Learn

cat_encoder = CategoricalEncoder()
housing_cat_reshaped = housing_cat.values.reshape(-1, 1)
housing_cat_1hot = cat_encoder.fit_transform(housing_cat_reshaped)
housing_cat_1hot


Out[63]:
<16512x5 sparse matrix of type '<class 'numpy.float64'>'
	with 16512 stored elements in Compressed Sparse Row format>

The default encoding is one-hot, and it returns a sparse array. You can use toarray() to get a dense array:


In [64]:
housing_cat_1hot.toarray()


Out[64]:
array([[ 1.,  0.,  0.,  0.,  0.],
       [ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  1.],
       ..., 
       [ 0.,  1.,  0.,  0.,  0.],
       [ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  1.,  0.]])

Alternatively, you can specify the encoding to be "onehot-dense" to get a dense matrix rather than a sparse matrix:


In [65]:
cat_encoder = CategoricalEncoder(encoding="onehot-dense")
housing_cat_1hot = cat_encoder.fit_transform(housing_cat_reshaped)
housing_cat_1hot


Out[65]:
array([[ 1.,  0.,  0.,  0.,  0.],
       [ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  1.],
       ..., 
       [ 0.,  1.,  0.,  0.,  0.],
       [ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  1.,  0.]])

In [66]:
cat_encoder.categories_


Out[66]:
[array(['<1H OCEAN', 'INLAND', 'ISLAND', 'NEAR BAY', 'NEAR OCEAN'], dtype=object)]

Let's create a custom transformer to add extra attributes:


In [67]:
from sklearn.base import BaseEstimator, TransformerMixin

# column index
rooms_ix, bedrooms_ix, population_ix, household_ix = 3, 4, 5, 6

class CombinedAttributesAdder(BaseEstimator, TransformerMixin):
    def __init__(self, add_bedrooms_per_room = True): # no *args or **kargs
        self.add_bedrooms_per_room = add_bedrooms_per_room
    def fit(self, X, y=None):
        return self  # nothing else to do
    def transform(self, X, y=None):
        rooms_per_household = X[:, rooms_ix] / X[:, household_ix]
        population_per_household = X[:, population_ix] / X[:, household_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]

attr_adder = CombinedAttributesAdder(add_bedrooms_per_room=False)
housing_extra_attribs = attr_adder.transform(housing.values)

In [68]:
housing_extra_attribs = pd.DataFrame(housing_extra_attribs, columns=list(housing.columns)+["rooms_per_household", "population_per_household"])
housing_extra_attribs.head()


Out[68]:
longitude latitude housing_median_age total_rooms total_bedrooms population households median_income ocean_proximity rooms_per_household population_per_household
0 -121.89 37.29 38 1568 351 710 339 2.7042 <1H OCEAN 4.62537 2.0944
1 -121.93 37.05 14 679 108 306 113 6.4214 <1H OCEAN 6.00885 2.70796
2 -117.2 32.77 31 1952 471 936 462 2.8621 NEAR OCEAN 4.22511 2.02597
3 -119.61 36.31 25 1847 371 1460 353 1.8839 INLAND 5.23229 4.13598
4 -118.59 34.23 17 6592 1525 4459 1463 3.0347 <1H OCEAN 4.50581 3.04785

Now let's build a pipeline for preprocessing the numerical attributes:


In [69]:
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

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

housing_num_tr = num_pipeline.fit_transform(housing_num)

In [70]:
housing_num_tr


Out[70]:
array([[-1.15604281,  0.77194962,  0.74333089, ..., -0.31205452,
        -0.08649871,  0.15531753],
       [-1.17602483,  0.6596948 , -1.1653172 , ...,  0.21768338,
        -0.03353391, -0.83628902],
       [ 1.18684903, -1.34218285,  0.18664186, ..., -0.46531516,
        -0.09240499,  0.4222004 ],
       ..., 
       [ 1.58648943, -0.72478134, -1.56295222, ...,  0.3469342 ,
        -0.03055414, -0.52177644],
       [ 0.78221312, -0.85106801,  0.18664186, ...,  0.02499488,
         0.06150916, -0.30340741],
       [-1.43579109,  0.99645926,  1.85670895, ..., -0.22852947,
        -0.09586294,  0.10180567]])

And a transformer to just select a subset of the Pandas DataFrame columns:


In [71]:
from sklearn.base import BaseEstimator, TransformerMixin

# Create a class to select numerical or categorical columns 
# since Scikit-Learn doesn't handle DataFrames yet
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].values

Now let's join all these components into a big pipeline that will preprocess both the numerical and the categorical features:


In [72]:
num_attribs = list(housing_num)
cat_attribs = ["ocean_proximity"]

num_pipeline = Pipeline([
        ('selector', DataFrameSelector(num_attribs)),
        ('imputer', Imputer(strategy="median")),
        ('attribs_adder', CombinedAttributesAdder()),
        ('std_scaler', StandardScaler()),
    ])

cat_pipeline = Pipeline([
        ('selector', DataFrameSelector(cat_attribs)),
        ('cat_encoder', CategoricalEncoder(encoding="onehot-dense")),
    ])

In [73]:
from sklearn.pipeline import FeatureUnion

full_pipeline = FeatureUnion(transformer_list=[
        ("num_pipeline", num_pipeline),
        ("cat_pipeline", cat_pipeline),
    ])

In [74]:
housing_prepared = full_pipeline.fit_transform(housing)
housing_prepared


Out[74]:
array([[-1.15604281,  0.77194962,  0.74333089, ...,  0.        ,
         0.        ,  0.        ],
       [-1.17602483,  0.6596948 , -1.1653172 , ...,  0.        ,
         0.        ,  0.        ],
       [ 1.18684903, -1.34218285,  0.18664186, ...,  0.        ,
         0.        ,  1.        ],
       ..., 
       [ 1.58648943, -0.72478134, -1.56295222, ...,  0.        ,
         0.        ,  0.        ],
       [ 0.78221312, -0.85106801,  0.18664186, ...,  0.        ,
         0.        ,  0.        ],
       [-1.43579109,  0.99645926,  1.85670895, ...,  0.        ,
         1.        ,  0.        ]])

In [75]:
housing_prepared.shape


Out[75]:
(16512, 16)

Select and train a model


In [76]:
from sklearn.linear_model import LinearRegression

lin_reg = LinearRegression()
lin_reg.fit(housing_prepared, housing_labels)


Out[76]:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)

In [77]:
# let's try the full pipeline on a few training instances
some_data = housing.iloc[:5]
some_labels = housing_labels.iloc[:5]
some_data_prepared = full_pipeline.transform(some_data)

print("Predictions:", lin_reg.predict(some_data_prepared))


Predictions: [ 210644.60459286  317768.80697211  210956.43331178   59218.98886849
  189747.55849879]

Compare against the actual values:


In [78]:
print("Labels:", list(some_labels))


Labels: [286600.0, 340600.0, 196900.0, 46300.0, 254500.0]

In [79]:
some_data_prepared


Out[79]:
array([[-1.15604281,  0.77194962,  0.74333089, -0.49323393, -0.44543821,
        -0.63621141, -0.42069842, -0.61493744, -0.31205452, -0.08649871,
         0.15531753,  1.        ,  0.        ,  0.        ,  0.        ,
         0.        ],
       [-1.17602483,  0.6596948 , -1.1653172 , -0.90896655, -1.0369278 ,
        -0.99833135, -1.02222705,  1.33645936,  0.21768338, -0.03353391,
        -0.83628902,  1.        ,  0.        ,  0.        ,  0.        ,
         0.        ],
       [ 1.18684903, -1.34218285,  0.18664186, -0.31365989, -0.15334458,
        -0.43363936, -0.0933178 , -0.5320456 , -0.46531516, -0.09240499,
         0.4222004 ,  0.        ,  0.        ,  0.        ,  0.        ,
         1.        ],
       [-0.01706767,  0.31357576, -0.29052016, -0.36276217, -0.39675594,
         0.03604096, -0.38343559, -1.04556555, -0.07966124,  0.08973561,
        -0.19645314,  0.        ,  1.        ,  0.        ,  0.        ,
         0.        ],
       [ 0.49247384, -0.65929936, -0.92673619,  1.85619316,  2.41221109,
         2.72415407,  2.57097492, -0.44143679, -0.35783383, -0.00419445,
         0.2699277 ,  1.        ,  0.        ,  0.        ,  0.        ,
         0.        ]])

In [80]:
from sklearn.metrics import mean_squared_error

housing_predictions = lin_reg.predict(housing_prepared)
lin_mse = mean_squared_error(housing_labels, housing_predictions)
lin_rmse = np.sqrt(lin_mse)
lin_rmse


Out[80]:
68628.198198489219

In [81]:
from sklearn.metrics import mean_absolute_error

lin_mae = mean_absolute_error(housing_labels, housing_predictions)
lin_mae


Out[81]:
49439.895990018973

In [82]:
from sklearn.tree import DecisionTreeRegressor

tree_reg = DecisionTreeRegressor(random_state=42)
tree_reg.fit(housing_prepared, housing_labels)


Out[82]:
DecisionTreeRegressor(criterion='mse', max_depth=None, max_features=None,
           max_leaf_nodes=None, min_impurity_decrease=0.0,
           min_impurity_split=None, min_samples_leaf=1,
           min_samples_split=2, min_weight_fraction_leaf=0.0,
           presort=False, random_state=42, splitter='best')

In [83]:
housing_predictions = tree_reg.predict(housing_prepared)
tree_mse = mean_squared_error(housing_labels, housing_predictions)
tree_rmse = np.sqrt(tree_mse)
tree_rmse


Out[83]:
0.0

Fine-tune your model


In [84]:
from sklearn.model_selection import cross_val_score

scores = cross_val_score(tree_reg, housing_prepared, housing_labels,
                         scoring="neg_mean_squared_error", cv=10)
tree_rmse_scores = np.sqrt(-scores)

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

display_scores(tree_rmse_scores)


Scores: [ 70232.0136482   66828.46839892  72444.08721003  70761.50186201
  71125.52697653  75581.29319857  70169.59286164  70055.37863456
  75370.49116773  71222.39081244]
Mean: 71379.0744771
Standard deviation: 2458.31882043

In [86]:
lin_scores = cross_val_score(lin_reg, housing_prepared, housing_labels,
                             scoring="neg_mean_squared_error", cv=10)
lin_rmse_scores = np.sqrt(-lin_scores)
display_scores(lin_rmse_scores)


Scores: [ 66782.73843989  66960.118071    70347.95244419  74739.57052552
  68031.13388938  71193.84183426  64969.63056405  68281.61137997
  71552.91566558  67665.10082067]
Mean: 69052.4613635
Standard deviation: 2731.6740018

In [87]:
from sklearn.ensemble import RandomForestRegressor

forest_reg = RandomForestRegressor(random_state=42)
forest_reg.fit(housing_prepared, housing_labels)


Out[87]:
RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=None,
           max_features='auto', max_leaf_nodes=None,
           min_impurity_decrease=0.0, min_impurity_split=None,
           min_samples_leaf=1, min_samples_split=2,
           min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1,
           oob_score=False, random_state=42, verbose=0, warm_start=False)

In [88]:
housing_predictions = forest_reg.predict(housing_prepared)
forest_mse = mean_squared_error(housing_labels, housing_predictions)
forest_rmse = np.sqrt(forest_mse)
forest_rmse


Out[88]:
21941.911027380233

In [89]:
from sklearn.model_selection import cross_val_score

forest_scores = cross_val_score(forest_reg, housing_prepared, housing_labels,
                                scoring="neg_mean_squared_error", cv=10)
forest_rmse_scores = np.sqrt(-forest_scores)
display_scores(forest_rmse_scores)


Scores: [ 51650.94405471  48920.80645498  52979.16096752  54412.74042021
  50861.29381163  56488.55699727  51866.90120786  49752.24599537
  55399.50713191  53309.74548294]
Mean: 52564.1902524
Standard deviation: 2301.87380392

In [90]:
scores = cross_val_score(lin_reg, housing_prepared, housing_labels, scoring="neg_mean_squared_error", cv=10)
pd.Series(np.sqrt(-scores)).describe()


Out[90]:
count       10.000000
mean     69052.461363
std       2879.437224
min      64969.630564
25%      67136.363758
50%      68156.372635
75%      70982.369487
max      74739.570526
dtype: float64

In [91]:
from sklearn.svm import SVR

svm_reg = SVR(kernel="linear")
svm_reg.fit(housing_prepared, housing_labels)
housing_predictions = svm_reg.predict(housing_prepared)
svm_mse = mean_squared_error(housing_labels, housing_predictions)
svm_rmse = np.sqrt(svm_mse)
svm_rmse


Out[91]:
111094.6308539982

In [92]:
from sklearn.model_selection import GridSearchCV

param_grid = [
    # try 12 (3×4) combinations of hyperparameters
    {'n_estimators': [3, 10, 30], 'max_features': [2, 4, 6, 8]},
    # then try 6 (2×3) combinations with bootstrap set as False
    {'bootstrap': [False], 'n_estimators': [3, 10], 'max_features': [2, 3, 4]},
  ]

forest_reg = RandomForestRegressor(random_state=42)
# train across 5 folds, that's a total of (12+6)*5=90 rounds of training 
grid_search = GridSearchCV(forest_reg, param_grid, cv=5,
                           scoring='neg_mean_squared_error')
grid_search.fit(housing_prepared, housing_labels)


Out[92]:
GridSearchCV(cv=5, error_score='raise',
       estimator=RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=None,
           max_features='auto', max_leaf_nodes=None,
           min_impurity_decrease=0.0, min_impurity_split=None,
           min_samples_leaf=1, min_samples_split=2,
           min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1,
           oob_score=False, random_state=42, verbose=0, warm_start=False),
       fit_params=None, iid=True, n_jobs=1,
       param_grid=[{'max_features': [2, 4, 6, 8], 'n_estimators': [3, 10, 30]}, {'max_features': [2, 3, 4], 'n_estimators': [3, 10], 'bootstrap': [False]}],
       pre_dispatch='2*n_jobs', refit=True, return_train_score=True,
       scoring='neg_mean_squared_error', verbose=0)

The best hyperparameter combination found:


In [93]:
grid_search.best_params_


Out[93]:
{'max_features': 8, 'n_estimators': 30}

In [94]:
grid_search.best_estimator_


Out[94]:
RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=None,
           max_features=8, max_leaf_nodes=None, min_impurity_decrease=0.0,
           min_impurity_split=None, min_samples_leaf=1,
           min_samples_split=2, min_weight_fraction_leaf=0.0,
           n_estimators=30, n_jobs=1, oob_score=False, random_state=42,
           verbose=0, warm_start=False)

Let's look at the score of each hyperparameter combination tested during the grid search:


In [95]:
cvres = grid_search.cv_results_
for mean_score, params in zip(cvres["mean_test_score"], cvres["params"]):
    print(np.sqrt(-mean_score), params)


63647.854446 {'max_features': 2, 'n_estimators': 3}
55611.5015988 {'max_features': 2, 'n_estimators': 10}
53370.0640736 {'max_features': 2, 'n_estimators': 30}
60959.1388585 {'max_features': 4, 'n_estimators': 3}
52740.5841667 {'max_features': 4, 'n_estimators': 10}
50374.1421461 {'max_features': 4, 'n_estimators': 30}
58661.2866462 {'max_features': 6, 'n_estimators': 3}
52009.9739798 {'max_features': 6, 'n_estimators': 10}
50154.1177737 {'max_features': 6, 'n_estimators': 30}
57865.3616801 {'max_features': 8, 'n_estimators': 3}
51730.0755087 {'max_features': 8, 'n_estimators': 10}
49694.8514333 {'max_features': 8, 'n_estimators': 30}
62874.4073931 {'max_features': 2, 'n_estimators': 3, 'bootstrap': False}
54643.4998083 {'max_features': 2, 'n_estimators': 10, 'bootstrap': False}
59437.8922859 {'max_features': 3, 'n_estimators': 3, 'bootstrap': False}
52735.3582936 {'max_features': 3, 'n_estimators': 10, 'bootstrap': False}
57490.0168279 {'max_features': 4, 'n_estimators': 3, 'bootstrap': False}
51008.2615672 {'max_features': 4, 'n_estimators': 10, 'bootstrap': False}

In [96]:
pd.DataFrame(grid_search.cv_results_)


Out[96]:
mean_fit_time mean_score_time mean_test_score mean_train_score param_bootstrap param_max_features param_n_estimators params rank_test_score split0_test_score ... split2_test_score split2_train_score split3_test_score split3_train_score split4_test_score split4_train_score std_fit_time std_score_time std_test_score std_train_score
0 0.045302 0.002430 -4.051049e+09 -1.106013e+09 NaN 2 3 {'max_features': 2, 'n_estimators': 3} 18 -3.850668e+09 ... -4.194135e+09 -1.116843e+09 -3.906732e+09 -1.112813e+09 -4.169669e+09 -1.129842e+09 0.001158 0.000211 1.431223e+08 2.173798e+07
1 0.145982 0.007629 -3.092639e+09 -5.819353e+08 NaN 2 10 {'max_features': 2, 'n_estimators': 10} 11 -3.052380e+09 ... -3.124982e+09 -5.780873e+08 -2.865117e+09 -5.713421e+08 -3.169914e+09 -5.797944e+08 0.000994 0.000199 1.306954e+08 7.584886e+06
2 0.447888 0.023658 -2.848364e+09 -4.396234e+08 NaN 2 30 {'max_features': 2, 'n_estimators': 30} 9 -2.692176e+09 ... -2.943808e+09 -4.374429e+08 -2.619893e+09 -4.374715e+08 -2.968460e+09 -4.451903e+08 0.002696 0.000297 1.604534e+08 2.883885e+06
3 0.075246 0.002313 -3.716017e+09 -9.850011e+08 NaN 4 3 {'max_features': 4, 'n_estimators': 3} 16 -3.729600e+09 ... -3.736527e+09 -9.172986e+08 -3.404974e+09 -1.035901e+09 -3.914186e+09 -9.711998e+08 0.000913 0.000055 1.690029e+08 4.047487e+07
4 0.245712 0.007718 -2.781569e+09 -5.160154e+08 NaN 4 10 {'max_features': 4, 'n_estimators': 10} 8 -2.667093e+09 ... -2.891599e+09 -4.960301e+08 -2.613393e+09 -5.422542e+08 -2.949550e+09 -5.158794e+08 0.001580 0.000156 1.278498e+08 1.498960e+07
5 0.734909 0.023360 -2.537554e+09 -3.878685e+08 NaN 4 30 {'max_features': 4, 'n_estimators': 30} 3 -2.387199e+09 ... -2.663178e+09 -3.789712e+08 -2.397951e+09 -4.036920e+08 -2.649850e+09 -3.846171e+08 0.004653 0.000110 1.209935e+08 8.424973e+06
6 0.103654 0.002362 -3.441147e+09 -9.030212e+08 NaN 6 3 {'max_features': 6, 'n_estimators': 3} 14 -3.119576e+09 ... -3.587747e+09 -9.360639e+08 -3.331544e+09 -9.025026e+08 -3.577062e+09 -8.612945e+08 0.003427 0.000044 1.884229e+08 2.639683e+07
7 0.342629 0.007915 -2.705037e+09 -5.014210e+08 NaN 6 10 {'max_features': 6, 'n_estimators': 10} 6 -2.553481e+09 ... -2.762945e+09 -4.996537e+08 -2.519522e+09 -4.989516e+08 -2.906270e+09 -5.063617e+08 0.005475 0.000176 1.464963e+08 3.357661e+06
8 1.029902 0.023183 -2.515436e+09 -3.840197e+08 NaN 6 30 {'max_features': 6, 'n_estimators': 30} 2 -2.371924e+09 ... -2.607962e+09 -3.805596e+08 -2.351220e+09 -3.856159e+08 -2.662399e+09 -3.904866e+08 0.004482 0.000140 1.283580e+08 3.796810e+06
9 0.130636 0.002393 -3.348400e+09 -8.884890e+08 NaN 8 3 {'max_features': 8, 'n_estimators': 3} 13 -3.351347e+09 ... -3.396841e+09 -8.596460e+08 -3.131753e+09 -8.893698e+08 -3.509451e+09 -9.146734e+08 0.000712 0.000129 1.226683e+08 2.730057e+07
10 0.441364 0.007754 -2.676001e+09 -4.923247e+08 NaN 8 10 {'max_features': 8, 'n_estimators': 10} 5 -2.572358e+09 ... -2.844608e+09 -4.730979e+08 -2.462797e+09 -5.154156e+08 -2.777049e+09 -4.979127e+08 0.003378 0.000204 1.393253e+08 1.446900e+07
11 1.332903 0.023311 -2.469578e+09 -3.809175e+08 NaN 8 30 {'max_features': 8, 'n_estimators': 30} 1 -2.358884e+09 ... -2.591134e+09 -3.772512e+08 -2.319816e+09 -3.881153e+08 -2.528200e+09 -3.807496e+08 0.006315 0.000131 1.089395e+08 4.853344e+06
12 0.070815 0.002721 -3.953191e+09 0.000000e+00 False 2 3 {'max_features': 2, 'n_estimators': 3, 'bootst... 17 -3.792367e+09 ... -4.050371e+09 -0.000000e+00 -3.668520e+09 -0.000000e+00 -4.087237e+09 -0.000000e+00 0.001584 0.000097 1.898516e+08 0.000000e+00
13 0.235981 0.009598 -2.985912e+09 -6.056027e-01 False 2 10 {'max_features': 2, 'n_estimators': 10, 'boots... 10 -2.808029e+09 ... -3.125519e+09 -0.000000e+00 -2.788623e+09 -0.000000e+00 -3.100391e+09 -2.967449e+00 0.002296 0.000120 1.535103e+08 1.181156e+00
14 0.095853 0.002854 -3.532863e+09 -1.214568e+01 False 3 3 {'max_features': 3, 'n_estimators': 3, 'bootst... 15 -3.604830e+09 ... -3.552984e+09 -0.000000e+00 -3.610963e+09 -0.000000e+00 -3.455760e+09 -6.072840e+01 0.001642 0.000212 7.251650e+07 2.429136e+01
15 0.315569 0.009573 -2.781018e+09 -5.272080e+00 False 3 10 {'max_features': 3, 'n_estimators': 10, 'boots... 7 -2.756941e+09 ... -2.831963e+09 -0.000000e+00 -2.672258e+09 -0.000000e+00 -2.793018e+09 -5.465556e+00 0.002867 0.000179 6.329307e+07 8.093117e+00
16 0.120213 0.003053 -3.305102e+09 0.000000e+00 False 4 3 {'max_features': 4, 'n_estimators': 3, 'bootst... 12 -3.143457e+09 ... -3.440323e+09 -0.000000e+00 -3.047980e+09 -0.000000e+00 -3.337950e+09 -0.000000e+00 0.002222 0.000241 1.867866e+08 0.000000e+00
17 0.402905 0.009938 -2.601843e+09 -3.028238e-03 False 4 10 {'max_features': 4, 'n_estimators': 10, 'boots... 4 -2.531436e+09 ... -2.606596e+09 -0.000000e+00 -2.437626e+09 -0.000000e+00 -2.726341e+09 -0.000000e+00 0.000843 0.000070 1.082086e+08 6.056477e-03

18 rows × 23 columns


In [97]:
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import randint

param_distribs = {
        'n_estimators': randint(low=1, high=200),
        'max_features': randint(low=1, high=8),
    }

forest_reg = RandomForestRegressor(random_state=42)
rnd_search = RandomizedSearchCV(forest_reg, param_distributions=param_distribs,
                                n_iter=10, cv=5, scoring='neg_mean_squared_error', random_state=42)
rnd_search.fit(housing_prepared, housing_labels)


Out[97]:
RandomizedSearchCV(cv=5, error_score='raise',
          estimator=RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=None,
           max_features='auto', max_leaf_nodes=None,
           min_impurity_decrease=0.0, min_impurity_split=None,
           min_samples_leaf=1, min_samples_split=2,
           min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1,
           oob_score=False, random_state=42, verbose=0, warm_start=False),
          fit_params=None, iid=True, n_iter=10, n_jobs=1,
          param_distributions={'max_features': <scipy.stats._distn_infrastructure.rv_frozen object at 0x7f26e6519ac8>, 'n_estimators': <scipy.stats._distn_infrastructure.rv_frozen object at 0x7f26de22e080>},
          pre_dispatch='2*n_jobs', random_state=42, refit=True,
          return_train_score=True, scoring='neg_mean_squared_error',
          verbose=0)

In [98]:
cvres = rnd_search.cv_results_
for mean_score, params in zip(cvres["mean_test_score"], cvres["params"]):
    print(np.sqrt(-mean_score), params)


49147.1524172 {'max_features': 7, 'n_estimators': 180}
51396.8768969 {'max_features': 5, 'n_estimators': 15}
50797.0573732 {'max_features': 3, 'n_estimators': 72}
50840.744514 {'max_features': 5, 'n_estimators': 21}
49276.1753033 {'max_features': 7, 'n_estimators': 122}
50775.4633168 {'max_features': 3, 'n_estimators': 75}
50681.383925 {'max_features': 3, 'n_estimators': 88}
49612.1525305 {'max_features': 5, 'n_estimators': 100}
50473.0175142 {'max_features': 3, 'n_estimators': 150}
64458.2538503 {'max_features': 5, 'n_estimators': 2}

In [99]:
feature_importances = grid_search.best_estimator_.feature_importances_
feature_importances


Out[99]:
array([  7.33442355e-02,   6.29090705e-02,   4.11437985e-02,
         1.46726854e-02,   1.41064835e-02,   1.48742809e-02,
         1.42575993e-02,   3.66158981e-01,   5.64191792e-02,
         1.08792957e-01,   5.33510773e-02,   1.03114883e-02,
         1.64780994e-01,   6.02803867e-05,   1.96041560e-03,
         2.85647464e-03])

In [100]:
extra_attribs = ["rooms_per_hhold", "pop_per_hhold", "bedrooms_per_room"]
cat_encoder = cat_pipeline.named_steps["cat_encoder"]
cat_one_hot_attribs = list(cat_encoder.categories_[0])
attributes = num_attribs + extra_attribs + cat_one_hot_attribs
sorted(zip(feature_importances, attributes), reverse=True)


Out[100]:
[(0.36615898061813418, 'median_income'),
 (0.16478099356159051, 'INLAND'),
 (0.10879295677551573, 'pop_per_hhold'),
 (0.073344235516012421, 'longitude'),
 (0.062909070482620302, 'latitude'),
 (0.056419179181954007, 'rooms_per_hhold'),
 (0.053351077347675809, 'bedrooms_per_room'),
 (0.041143798478729635, 'housing_median_age'),
 (0.014874280890402767, 'population'),
 (0.014672685420543237, 'total_rooms'),
 (0.014257599323407807, 'households'),
 (0.014106483453584102, 'total_bedrooms'),
 (0.010311488326303787, '<1H OCEAN'),
 (0.0028564746373201579, 'NEAR OCEAN'),
 (0.0019604155994780701, 'NEAR BAY'),
 (6.0280386727365991e-05, 'ISLAND')]

In [101]:
final_model = grid_search.best_estimator_

X_test = strat_test_set.drop("median_house_value", axis=1)
y_test = strat_test_set["median_house_value"].copy()

X_test_prepared = full_pipeline.transform(X_test)
final_predictions = final_model.predict(X_test_prepared)

final_mse = mean_squared_error(y_test, final_predictions)
final_rmse = np.sqrt(final_mse)

In [102]:
final_rmse


Out[102]:
47766.003966433083

Extra material

A full pipeline with both preparation and prediction


In [103]:
full_pipeline_with_predictor = Pipeline([
        ("preparation", full_pipeline),
        ("linear", LinearRegression())
    ])

full_pipeline_with_predictor.fit(housing, housing_labels)
full_pipeline_with_predictor.predict(some_data)


Out[103]:
array([ 210644.60459286,  317768.80697211,  210956.43331178,
         59218.98886849,  189747.55849879])

Model persistence using joblib


In [104]:
my_model = full_pipeline_with_predictor

In [105]:
from sklearn.externals import joblib
joblib.dump(my_model, "my_model.pkl") # DIFF
#...
my_model_loaded = joblib.load("my_model.pkl") # DIFF

Example SciPy distributions for RandomizedSearchCV


In [106]:
from scipy.stats import geom, expon
geom_distrib=geom(0.5).rvs(10000, random_state=42)
expon_distrib=expon(scale=1).rvs(10000, random_state=42)
plt.hist(geom_distrib, bins=50)
plt.show()
plt.hist(expon_distrib, bins=50)
plt.show()


Exercise solutions

1.

Question: Try a Support Vector Machine regressor (sklearn.svm.SVR), with various hyperparameters such as kernel="linear" (with various values for the C hyperparameter) or kernel="rbf" (with various values for the C and gamma hyperparameters). Don't worry about what these hyperparameters mean for now. How does the best SVR predictor perform?


In [107]:
from sklearn.model_selection import GridSearchCV

param_grid = [
        {'kernel': ['linear'], 'C': [10., 30., 100., 300., 1000., 3000., 10000., 30000.0]},
        {'kernel': ['rbf'], 'C': [1.0, 3.0, 10., 30., 100., 300., 1000.0],
         'gamma': [0.01, 0.03, 0.1, 0.3, 1.0, 3.0]},
    ]

svm_reg = SVR()
grid_search = GridSearchCV(svm_reg, param_grid, cv=5, scoring='neg_mean_squared_error', verbose=2, n_jobs=4)
grid_search.fit(housing_prepared, housing_labels)


Fitting 5 folds for each of 50 candidates, totalling 250 fits
[CV] C=10.0, kernel=linear ...........................................
[CV] C=10.0, kernel=linear ...........................................
[CV] C=10.0, kernel=linear ...........................................
[CV] C=10.0, kernel=linear ...........................................
[CV] ............................ C=10.0, kernel=linear, total=   6.5s
[CV] C=10.0, kernel=linear ...........................................
[CV] ............................ C=10.0, kernel=linear, total=   6.5s
[CV] C=30.0, kernel=linear ...........................................
[CV] ............................ C=10.0, kernel=linear, total=   6.6s
[CV] C=30.0, kernel=linear ...........................................
[CV] ............................ C=10.0, kernel=linear, total=   6.6s
[CV] C=30.0, kernel=linear ...........................................
[CV] ............................ C=30.0, kernel=linear, total=   6.4s
[CV] C=30.0, kernel=linear ...........................................
[CV] ............................ C=30.0, kernel=linear, total=   6.5s
[CV] C=30.0, kernel=linear ...........................................
[CV] ............................ C=10.0, kernel=linear, total=   6.6s
[CV] C=100.0, kernel=linear ..........................................
[CV] ............................ C=30.0, kernel=linear, total=   6.7s
[CV] C=100.0, kernel=linear ..........................................
[CV] ............................ C=30.0, kernel=linear, total=   6.4s
[CV] C=100.0, kernel=linear ..........................................
[CV] ............................ C=30.0, kernel=linear, total=   6.6s
[CV] C=100.0, kernel=linear ..........................................
[CV] ........................... C=100.0, kernel=linear, total=   6.4s
[CV] C=100.0, kernel=linear ..........................................
[CV] ........................... C=100.0, kernel=linear, total=   6.4s
[CV] C=300.0, kernel=linear ..........................................
[CV] ........................... C=100.0, kernel=linear, total=   6.4s
[CV] C=300.0, kernel=linear ..........................................
[CV] ........................... C=100.0, kernel=linear, total=   6.3s
[CV] C=300.0, kernel=linear ..........................................
[CV] ........................... C=100.0, kernel=linear, total=   6.5s
[CV] C=300.0, kernel=linear ..........................................
[CV] ........................... C=300.0, kernel=linear, total=   6.5s
[CV] C=300.0, kernel=linear ..........................................
[CV] ........................... C=300.0, kernel=linear, total=   6.5s
[CV] C=1000.0, kernel=linear .........................................
[CV] ........................... C=300.0, kernel=linear, total=   6.6s
[CV] C=1000.0, kernel=linear .........................................
[CV] ........................... C=300.0, kernel=linear, total=   6.6s
[CV] C=1000.0, kernel=linear .........................................
[CV] ........................... C=300.0, kernel=linear, total=   6.5s
[CV] C=1000.0, kernel=linear .........................................
[CV] .......................... C=1000.0, kernel=linear, total=   6.6s
[CV] C=1000.0, kernel=linear .........................................
[CV] .......................... C=1000.0, kernel=linear, total=   6.7s
[CV] C=3000.0, kernel=linear .........................................
[CV] .......................... C=1000.0, kernel=linear, total=   6.9s
[CV] C=3000.0, kernel=linear .........................................
[CV] .......................... C=1000.0, kernel=linear, total=   6.8s
[CV] C=3000.0, kernel=linear .........................................
[CV] .......................... C=1000.0, kernel=linear, total=   6.6s
[CV] C=3000.0, kernel=linear .........................................
[CV] .......................... C=3000.0, kernel=linear, total=   7.3s
[CV] C=3000.0, kernel=linear .........................................
[CV] .......................... C=3000.0, kernel=linear, total=   7.1s
[CV] C=10000.0, kernel=linear ........................................
[CV] .......................... C=3000.0, kernel=linear, total=   7.3s
[CV] C=10000.0, kernel=linear ........................................
[CV] .......................... C=3000.0, kernel=linear, total=   7.6s
[CV] C=10000.0, kernel=linear ........................................
[CV] .......................... C=3000.0, kernel=linear, total=   7.2s
[CV] C=10000.0, kernel=linear ........................................
[CV] ......................... C=10000.0, kernel=linear, total=   9.4s
[CV] C=10000.0, kernel=linear ........................................
[CV] ......................... C=10000.0, kernel=linear, total=   9.6s
[CV] C=30000.0, kernel=linear ........................................
[CV] ......................... C=10000.0, kernel=linear, total=   9.0s
[CV] C=30000.0, kernel=linear ........................................
[Parallel(n_jobs=4)]: Done  33 tasks      | elapsed:  1.4min
[CV] ......................... C=10000.0, kernel=linear, total=   9.8s
[CV] C=30000.0, kernel=linear ........................................
[CV] ......................... C=10000.0, kernel=linear, total=   8.6s
[CV] C=30000.0, kernel=linear ........................................
[CV] ......................... C=30000.0, kernel=linear, total=  15.3s
[CV] C=30000.0, kernel=linear ........................................
[CV] ......................... C=30000.0, kernel=linear, total=  15.4s
[CV] gamma=0.01, C=1.0, kernel=rbf ...................................
[CV] ......................... C=30000.0, kernel=linear, total=  16.1s
[CV] gamma=0.01, C=1.0, kernel=rbf ...................................
[CV] ......................... C=30000.0, kernel=linear, total=  15.4s
[CV] gamma=0.01, C=1.0, kernel=rbf ...................................
[CV] ......................... C=30000.0, kernel=linear, total=  13.5s
[CV] gamma=0.01, C=1.0, kernel=rbf ...................................
[CV] .................... gamma=0.01, C=1.0, kernel=rbf, total=  24.5s
[CV] gamma=0.01, C=1.0, kernel=rbf ...................................
[CV] .................... gamma=0.01, C=1.0, kernel=rbf, total=  24.6s
[CV] gamma=0.03, C=1.0, kernel=rbf ...................................
[CV] .................... gamma=0.01, C=1.0, kernel=rbf, total=  24.8s
[CV] gamma=0.03, C=1.0, kernel=rbf ...................................
[CV] .................... gamma=0.01, C=1.0, kernel=rbf, total=  24.5s
[CV] gamma=0.03, C=1.0, kernel=rbf ...................................
[CV] .................... gamma=0.01, C=1.0, kernel=rbf, total=  11.4s
[CV] gamma=0.03, C=1.0, kernel=rbf ...................................
[CV] .................... gamma=0.03, C=1.0, kernel=rbf, total=  11.3s
[CV] gamma=0.03, C=1.0, kernel=rbf ...................................
[CV] .................... gamma=0.03, C=1.0, kernel=rbf, total=  11.6s
[CV] gamma=0.1, C=1.0, kernel=rbf ....................................
[CV] .................... gamma=0.03, C=1.0, kernel=rbf, total=  11.4s
[CV] gamma=0.1, C=1.0, kernel=rbf ....................................
[CV] .................... gamma=0.03, C=1.0, kernel=rbf, total=  11.5s
[CV] gamma=0.1, C=1.0, kernel=rbf ....................................
[CV] .................... gamma=0.03, C=1.0, kernel=rbf, total=  11.3s
[CV] gamma=0.1, C=1.0, kernel=rbf ....................................
[CV] ..................... gamma=0.1, C=1.0, kernel=rbf, total=  11.4s
[CV] gamma=0.1, C=1.0, kernel=rbf ....................................
[CV] ..................... gamma=0.1, C=1.0, kernel=rbf, total=  11.2s
[CV] gamma=0.3, C=1.0, kernel=rbf ....................................
[CV] ..................... gamma=0.1, C=1.0, kernel=rbf, total=  11.2s
[CV] gamma=0.3, C=1.0, kernel=rbf ....................................
[CV] ..................... gamma=0.1, C=1.0, kernel=rbf, total=  11.2s
[CV] gamma=0.3, C=1.0, kernel=rbf ....................................
[CV] ..................... gamma=0.1, C=1.0, kernel=rbf, total=  11.3s
[CV] gamma=0.3, C=1.0, kernel=rbf ....................................
[CV] ..................... gamma=0.3, C=1.0, kernel=rbf, total=  10.9s
[CV] gamma=0.3, C=1.0, kernel=rbf ....................................
[CV] ..................... gamma=0.3, C=1.0, kernel=rbf, total=  11.0s
[CV] gamma=1.0, C=1.0, kernel=rbf ....................................
[CV] ..................... gamma=0.3, C=1.0, kernel=rbf, total=  11.1s
[CV] gamma=1.0, C=1.0, kernel=rbf ....................................
[CV] ..................... gamma=0.3, C=1.0, kernel=rbf, total=  11.2s
[CV] gamma=1.0, C=1.0, kernel=rbf ....................................
[CV] ..................... gamma=0.3, C=1.0, kernel=rbf, total=  11.2s
[CV] gamma=1.0, C=1.0, kernel=rbf ....................................
[CV] ..................... gamma=1.0, C=1.0, kernel=rbf, total=  10.6s
[CV] gamma=1.0, C=1.0, kernel=rbf ....................................
[CV] ..................... gamma=1.0, C=1.0, kernel=rbf, total=  10.6s
[CV] gamma=3.0, C=1.0, kernel=rbf ....................................
[CV] ..................... gamma=1.0, C=1.0, kernel=rbf, total=  10.7s
[CV] gamma=3.0, C=1.0, kernel=rbf ....................................
[CV] ..................... gamma=1.0, C=1.0, kernel=rbf, total=  10.6s
[CV] gamma=3.0, C=1.0, kernel=rbf ....................................
[CV] ..................... gamma=1.0, C=1.0, kernel=rbf, total=  10.6s
[CV] gamma=3.0, C=1.0, kernel=rbf ....................................
[CV] ..................... gamma=3.0, C=1.0, kernel=rbf, total=  11.8s
[CV] gamma=3.0, C=1.0, kernel=rbf ....................................
[CV] ..................... gamma=3.0, C=1.0, kernel=rbf, total=  12.5s
[CV] gamma=0.01, C=3.0, kernel=rbf ...................................
[CV] ..................... gamma=3.0, C=1.0, kernel=rbf, total=  11.8s
[CV] gamma=0.01, C=3.0, kernel=rbf ...................................
[CV] ..................... gamma=3.0, C=1.0, kernel=rbf, total=  12.3s
[CV] gamma=0.01, C=3.0, kernel=rbf ...................................
[CV] ..................... gamma=3.0, C=1.0, kernel=rbf, total=  12.2s
[CV] gamma=0.01, C=3.0, kernel=rbf ...................................
[CV] .................... gamma=0.01, C=3.0, kernel=rbf, total=  11.7s
[CV] gamma=0.01, C=3.0, kernel=rbf ...................................
[CV] .................... gamma=0.01, C=3.0, kernel=rbf, total=  11.8s
[CV] gamma=0.03, C=3.0, kernel=rbf ...................................
[CV] .................... gamma=0.01, C=3.0, kernel=rbf, total=  11.7s
[CV] gamma=0.03, C=3.0, kernel=rbf ...................................
[CV] .................... gamma=0.01, C=3.0, kernel=rbf, total=  12.0s
[CV] gamma=0.03, C=3.0, kernel=rbf ...................................
[CV] .................... gamma=0.01, C=3.0, kernel=rbf, total=  11.7s
[CV] gamma=0.03, C=3.0, kernel=rbf ...................................
[CV] .................... gamma=0.03, C=3.0, kernel=rbf, total=  11.8s
[CV] gamma=0.03, C=3.0, kernel=rbf ...................................
[CV] .................... gamma=0.03, C=3.0, kernel=rbf, total=  11.5s
[CV] gamma=0.1, C=3.0, kernel=rbf ....................................
[CV] .................... gamma=0.03, C=3.0, kernel=rbf, total=  11.7s
[CV] gamma=0.1, C=3.0, kernel=rbf ....................................
[CV] .................... gamma=0.03, C=3.0, kernel=rbf, total=  11.6s
[CV] gamma=0.1, C=3.0, kernel=rbf ....................................
[CV] .................... gamma=0.03, C=3.0, kernel=rbf, total=  11.7s
[CV] gamma=0.1, C=3.0, kernel=rbf ....................................
[CV] ..................... gamma=0.1, C=3.0, kernel=rbf, total=  12.0s
[CV] gamma=0.1, C=3.0, kernel=rbf ....................................
[CV] ..................... gamma=0.1, C=3.0, kernel=rbf, total=  11.5s
[CV] gamma=0.3, C=3.0, kernel=rbf ....................................
[CV] ..................... gamma=0.1, C=3.0, kernel=rbf, total=  11.7s
[CV] gamma=0.3, C=3.0, kernel=rbf ....................................
[CV] ..................... gamma=0.1, C=3.0, kernel=rbf, total=  11.5s
[CV] gamma=0.3, C=3.0, kernel=rbf ....................................
[CV] ..................... gamma=0.1, C=3.0, kernel=rbf, total=  11.9s
[CV] gamma=0.3, C=3.0, kernel=rbf ....................................
[CV] ..................... gamma=0.3, C=3.0, kernel=rbf, total=  10.9s
[CV] gamma=0.3, C=3.0, kernel=rbf ....................................
[CV] ..................... gamma=0.3, C=3.0, kernel=rbf, total=  11.2s
[CV] gamma=1.0, C=3.0, kernel=rbf ....................................
[CV] ..................... gamma=0.3, C=3.0, kernel=rbf, total=  11.1s
[CV] gamma=1.0, C=3.0, kernel=rbf ....................................
[CV] ..................... gamma=0.3, C=3.0, kernel=rbf, total=  11.0s
[CV] gamma=1.0, C=3.0, kernel=rbf ....................................
[CV] ..................... gamma=0.3, C=3.0, kernel=rbf, total=  10.9s
[CV] gamma=1.0, C=3.0, kernel=rbf ....................................
[CV] ..................... gamma=1.0, C=3.0, kernel=rbf, total=  10.7s
[CV] gamma=1.0, C=3.0, kernel=rbf ....................................
[CV] ..................... gamma=1.0, C=3.0, kernel=rbf, total=  10.8s
[CV] gamma=3.0, C=3.0, kernel=rbf ....................................
[CV] ..................... gamma=1.0, C=3.0, kernel=rbf, total=  10.9s
[CV] gamma=3.0, C=3.0, kernel=rbf ....................................
[CV] ..................... gamma=1.0, C=3.0, kernel=rbf, total=  10.8s
[CV] gamma=3.0, C=3.0, kernel=rbf ....................................
[CV] ..................... gamma=1.0, C=3.0, kernel=rbf, total=  10.6s
[CV] gamma=3.0, C=3.0, kernel=rbf ....................................
[CV] ..................... gamma=3.0, C=3.0, kernel=rbf, total=  11.8s
[CV] gamma=3.0, C=3.0, kernel=rbf ....................................
[CV] ..................... gamma=3.0, C=3.0, kernel=rbf, total=  11.9s
[CV] gamma=0.01, C=10.0, kernel=rbf ..................................
[CV] ..................... gamma=3.0, C=3.0, kernel=rbf, total=  11.8s
[CV] gamma=0.01, C=10.0, kernel=rbf ..................................
[CV] ..................... gamma=3.0, C=3.0, kernel=rbf, total=  11.8s
[CV] gamma=0.01, C=10.0, kernel=rbf ..................................
[CV] ..................... gamma=3.0, C=3.0, kernel=rbf, total=  11.9s
[CV] gamma=0.01, C=10.0, kernel=rbf ..................................
[CV] ................... gamma=0.01, C=10.0, kernel=rbf, total=  11.5s
[CV] gamma=0.01, C=10.0, kernel=rbf ..................................
[CV] ................... gamma=0.01, C=10.0, kernel=rbf, total=  11.5s
[CV] gamma=0.03, C=10.0, kernel=rbf ..................................
[CV] ................... gamma=0.01, C=10.0, kernel=rbf, total=  11.4s
[CV] gamma=0.03, C=10.0, kernel=rbf ..................................
[CV] ................... gamma=0.01, C=10.0, kernel=rbf, total=  11.5s
[CV] gamma=0.03, C=10.0, kernel=rbf ..................................
[CV] ................... gamma=0.01, C=10.0, kernel=rbf, total=  11.6s
[CV] gamma=0.03, C=10.0, kernel=rbf ..................................
[CV] ................... gamma=0.03, C=10.0, kernel=rbf, total=  11.5s
[CV] gamma=0.03, C=10.0, kernel=rbf ..................................
[CV] ................... gamma=0.03, C=10.0, kernel=rbf, total=  11.5s
[CV] gamma=0.1, C=10.0, kernel=rbf ...................................
[CV] ................... gamma=0.03, C=10.0, kernel=rbf, total=  11.3s
[CV] gamma=0.1, C=10.0, kernel=rbf ...................................
[CV] ................... gamma=0.03, C=10.0, kernel=rbf, total=  11.7s
[CV] gamma=0.1, C=10.0, kernel=rbf ...................................
[CV] ................... gamma=0.03, C=10.0, kernel=rbf, total=  11.5s
[CV] gamma=0.1, C=10.0, kernel=rbf ...................................
[CV] .................... gamma=0.1, C=10.0, kernel=rbf, total=  11.2s
[CV] gamma=0.1, C=10.0, kernel=rbf ...................................
[CV] .................... gamma=0.1, C=10.0, kernel=rbf, total=  11.2s
[CV] gamma=0.3, C=10.0, kernel=rbf ...................................
[CV] .................... gamma=0.1, C=10.0, kernel=rbf, total=  11.5s
[CV] gamma=0.3, C=10.0, kernel=rbf ...................................
[CV] .................... gamma=0.1, C=10.0, kernel=rbf, total=  11.3s
[CV] gamma=0.3, C=10.0, kernel=rbf ...................................
[CV] .................... gamma=0.1, C=10.0, kernel=rbf, total=  11.2s
[CV] gamma=0.3, C=10.0, kernel=rbf ...................................
[CV] .................... gamma=0.3, C=10.0, kernel=rbf, total=  11.0s
[CV] gamma=0.3, C=10.0, kernel=rbf ...................................
[CV] .................... gamma=0.3, C=10.0, kernel=rbf, total=  11.0s
[CV] gamma=1.0, C=10.0, kernel=rbf ...................................
[CV] .................... gamma=0.3, C=10.0, kernel=rbf, total=  11.2s
[CV] gamma=1.0, C=10.0, kernel=rbf ...................................
[CV] .................... gamma=0.3, C=10.0, kernel=rbf, total=  11.7s
[CV] gamma=1.0, C=10.0, kernel=rbf ...................................
[CV] .................... gamma=0.3, C=10.0, kernel=rbf, total=  11.1s
[CV] gamma=1.0, C=10.0, kernel=rbf ...................................
[CV] .................... gamma=1.0, C=10.0, kernel=rbf, total=  10.7s
[CV] gamma=1.0, C=10.0, kernel=rbf ...................................
[CV] .................... gamma=1.0, C=10.0, kernel=rbf, total=  10.7s
[CV] gamma=3.0, C=10.0, kernel=rbf ...................................
[CV] .................... gamma=1.0, C=10.0, kernel=rbf, total=  10.8s
[CV] gamma=3.0, C=10.0, kernel=rbf ...................................
[CV] .................... gamma=1.0, C=10.0, kernel=rbf, total=  10.6s
[CV] gamma=3.0, C=10.0, kernel=rbf ...................................
[CV] .................... gamma=1.0, C=10.0, kernel=rbf, total=  10.7s
[CV] gamma=3.0, C=10.0, kernel=rbf ...................................
[CV] .................... gamma=3.0, C=10.0, kernel=rbf, total=  11.9s
[CV] gamma=3.0, C=10.0, kernel=rbf ...................................
[CV] .................... gamma=3.0, C=10.0, kernel=rbf, total=  12.0s
[CV] gamma=0.01, C=30.0, kernel=rbf ..................................
[CV] .................... gamma=3.0, C=10.0, kernel=rbf, total=  11.8s
[CV] gamma=0.01, C=30.0, kernel=rbf ..................................
[CV] .................... gamma=3.0, C=10.0, kernel=rbf, total=  12.0s
[CV] gamma=0.01, C=30.0, kernel=rbf ..................................
[CV] .................... gamma=3.0, C=10.0, kernel=rbf, total=  12.0s
[CV] gamma=0.01, C=30.0, kernel=rbf ..................................
[CV] ................... gamma=0.01, C=30.0, kernel=rbf, total=  11.5s
[CV] gamma=0.01, C=30.0, kernel=rbf ..................................
[CV] ................... gamma=0.01, C=30.0, kernel=rbf, total=  11.5s
[CV] gamma=0.03, C=30.0, kernel=rbf ..................................
[CV] ................... gamma=0.01, C=30.0, kernel=rbf, total=  11.4s
[CV] gamma=0.03, C=30.0, kernel=rbf ..................................
[CV] ................... gamma=0.01, C=30.0, kernel=rbf, total=  11.6s
[CV] gamma=0.03, C=30.0, kernel=rbf ..................................
[CV] ................... gamma=0.01, C=30.0, kernel=rbf, total=  11.4s
[CV] gamma=0.03, C=30.0, kernel=rbf ..................................
[CV] ................... gamma=0.03, C=30.0, kernel=rbf, total=  11.3s
[CV] gamma=0.03, C=30.0, kernel=rbf ..................................
[CV] ................... gamma=0.03, C=30.0, kernel=rbf, total=  11.4s
[CV] gamma=0.1, C=30.0, kernel=rbf ...................................
[CV] ................... gamma=0.03, C=30.0, kernel=rbf, total=  11.3s
[CV] gamma=0.1, C=30.0, kernel=rbf ...................................
[CV] ................... gamma=0.03, C=30.0, kernel=rbf, total=  11.3s
[CV] gamma=0.1, C=30.0, kernel=rbf ...................................
[CV] ................... gamma=0.03, C=30.0, kernel=rbf, total=  11.3s
[CV] gamma=0.1, C=30.0, kernel=rbf ...................................
[CV] .................... gamma=0.1, C=30.0, kernel=rbf, total=  11.7s
[CV] gamma=0.1, C=30.0, kernel=rbf ...................................
[CV] .................... gamma=0.1, C=30.0, kernel=rbf, total=  11.3s
[CV] gamma=0.3, C=30.0, kernel=rbf ...................................
[CV] .................... gamma=0.1, C=30.0, kernel=rbf, total=  11.3s
[CV] gamma=0.3, C=30.0, kernel=rbf ...................................
[CV] .................... gamma=0.1, C=30.0, kernel=rbf, total=  11.2s
[CV] gamma=0.3, C=30.0, kernel=rbf ...................................
[CV] .................... gamma=0.1, C=30.0, kernel=rbf, total=  11.4s
[CV] gamma=0.3, C=30.0, kernel=rbf ...................................
[CV] .................... gamma=0.3, C=30.0, kernel=rbf, total=  11.1s
[CV] gamma=0.3, C=30.0, kernel=rbf ...................................
[CV] .................... gamma=0.3, C=30.0, kernel=rbf, total=  11.1s
[CV] gamma=1.0, C=30.0, kernel=rbf ...................................
[CV] .................... gamma=0.3, C=30.0, kernel=rbf, total=  10.9s
[CV] gamma=1.0, C=30.0, kernel=rbf ...................................
[CV] .................... gamma=0.3, C=30.0, kernel=rbf, total=  10.9s
[CV] gamma=1.0, C=30.0, kernel=rbf ...................................
[CV] .................... gamma=0.3, C=30.0, kernel=rbf, total=  10.9s
[CV] gamma=1.0, C=30.0, kernel=rbf ...................................
[CV] .................... gamma=1.0, C=30.0, kernel=rbf, total=  10.7s
[CV] gamma=1.0, C=30.0, kernel=rbf ...................................
[CV] .................... gamma=1.0, C=30.0, kernel=rbf, total=  10.5s
[CV] gamma=3.0, C=30.0, kernel=rbf ...................................
[CV] .................... gamma=1.0, C=30.0, kernel=rbf, total=  10.9s
[CV] gamma=3.0, C=30.0, kernel=rbf ...................................
[CV] .................... gamma=1.0, C=30.0, kernel=rbf, total=  10.5s
[CV] gamma=3.0, C=30.0, kernel=rbf ...................................
[Parallel(n_jobs=4)]: Done 154 tasks      | elapsed:  9.9min
[CV] .................... gamma=1.0, C=30.0, kernel=rbf, total=  10.7s
[CV] gamma=3.0, C=30.0, kernel=rbf ...................................
[CV] .................... gamma=3.0, C=30.0, kernel=rbf, total=  11.7s
[CV] gamma=3.0, C=30.0, kernel=rbf ...................................
[CV] .................... gamma=3.0, C=30.0, kernel=rbf, total=  12.0s
[CV] gamma=0.01, C=100.0, kernel=rbf .................................
[CV] .................... gamma=3.0, C=30.0, kernel=rbf, total=  12.0s
[CV] gamma=0.01, C=100.0, kernel=rbf .................................
[CV] .................... gamma=3.0, C=30.0, kernel=rbf, total=  12.0s
[CV] gamma=0.01, C=100.0, kernel=rbf .................................
[CV] .................... gamma=3.0, C=30.0, kernel=rbf, total=  12.2s
[CV] gamma=0.01, C=100.0, kernel=rbf .................................
[CV] .................. gamma=0.01, C=100.0, kernel=rbf, total=  11.5s
[CV] gamma=0.01, C=100.0, kernel=rbf .................................
[CV] .................. gamma=0.01, C=100.0, kernel=rbf, total=  11.5s
[CV] gamma=0.03, C=100.0, kernel=rbf .................................
[CV] .................. gamma=0.01, C=100.0, kernel=rbf, total=  11.6s
[CV] gamma=0.03, C=100.0, kernel=rbf .................................
[CV] .................. gamma=0.01, C=100.0, kernel=rbf, total=  11.5s
[CV] gamma=0.03, C=100.0, kernel=rbf .................................
[CV] .................. gamma=0.03, C=100.0, kernel=rbf, total=  11.3s
[CV] gamma=0.03, C=100.0, kernel=rbf .................................
[CV] .................. gamma=0.01, C=100.0, kernel=rbf, total=  11.7s
[CV] gamma=0.03, C=100.0, kernel=rbf .................................
[CV] .................. gamma=0.03, C=100.0, kernel=rbf, total=  11.8s
[CV] gamma=0.1, C=100.0, kernel=rbf ..................................
[CV] .................. gamma=0.03, C=100.0, kernel=rbf, total=  11.6s
[CV] gamma=0.1, C=100.0, kernel=rbf ..................................
[CV] .................. gamma=0.03, C=100.0, kernel=rbf, total=  11.2s
[CV] gamma=0.1, C=100.0, kernel=rbf ..................................
[CV] .................. gamma=0.03, C=100.0, kernel=rbf, total=  11.5s
[CV] gamma=0.1, C=100.0, kernel=rbf ..................................
[CV] ................... gamma=0.1, C=100.0, kernel=rbf, total=  11.1s
[CV] gamma=0.1, C=100.0, kernel=rbf ..................................
[CV] ................... gamma=0.1, C=100.0, kernel=rbf, total=  11.2s
[CV] gamma=0.3, C=100.0, kernel=rbf ..................................
[CV] ................... gamma=0.1, C=100.0, kernel=rbf, total=  11.0s
[CV] gamma=0.3, C=100.0, kernel=rbf ..................................
[CV] ................... gamma=0.1, C=100.0, kernel=rbf, total=  11.1s
[CV] gamma=0.3, C=100.0, kernel=rbf ..................................
[CV] ................... gamma=0.1, C=100.0, kernel=rbf, total=  11.2s
[CV] gamma=0.3, C=100.0, kernel=rbf ..................................
[CV] ................... gamma=0.3, C=100.0, kernel=rbf, total=  11.0s
[CV] gamma=0.3, C=100.0, kernel=rbf ..................................
[CV] ................... gamma=0.3, C=100.0, kernel=rbf, total=  10.8s
[CV] gamma=1.0, C=100.0, kernel=rbf ..................................
[CV] ................... gamma=0.3, C=100.0, kernel=rbf, total=  10.9s
[CV] gamma=1.0, C=100.0, kernel=rbf ..................................
[CV] ................... gamma=0.3, C=100.0, kernel=rbf, total=  11.1s
[CV] gamma=1.0, C=100.0, kernel=rbf ..................................
[CV] ................... gamma=0.3, C=100.0, kernel=rbf, total=  11.0s
[CV] gamma=1.0, C=100.0, kernel=rbf ..................................
[CV] ................... gamma=1.0, C=100.0, kernel=rbf, total=  10.6s
[CV] gamma=1.0, C=100.0, kernel=rbf ..................................
[CV] ................... gamma=1.0, C=100.0, kernel=rbf, total=  10.8s
[CV] gamma=3.0, C=100.0, kernel=rbf ..................................
[CV] ................... gamma=1.0, C=100.0, kernel=rbf, total=  10.9s
[CV] gamma=3.0, C=100.0, kernel=rbf ..................................
[CV] ................... gamma=1.0, C=100.0, kernel=rbf, total=  10.6s
[CV] gamma=3.0, C=100.0, kernel=rbf ..................................
[CV] ................... gamma=1.0, C=100.0, kernel=rbf, total=  10.6s
[CV] gamma=3.0, C=100.0, kernel=rbf ..................................
[CV] ................... gamma=3.0, C=100.0, kernel=rbf, total=  12.0s
[CV] gamma=3.0, C=100.0, kernel=rbf ..................................
[CV] ................... gamma=3.0, C=100.0, kernel=rbf, total=  11.9s
[CV] gamma=0.01, C=300.0, kernel=rbf .................................
[CV] ................... gamma=3.0, C=100.0, kernel=rbf, total=  11.8s
[CV] gamma=0.01, C=300.0, kernel=rbf .................................
[CV] ................... gamma=3.0, C=100.0, kernel=rbf, total=  12.0s
[CV] gamma=0.01, C=300.0, kernel=rbf .................................
[CV] ................... gamma=3.0, C=100.0, kernel=rbf, total=  11.9s
[CV] gamma=0.01, C=300.0, kernel=rbf .................................
[CV] .................. gamma=0.01, C=300.0, kernel=rbf, total=  11.2s
[CV] gamma=0.01, C=300.0, kernel=rbf .................................
[CV] .................. gamma=0.01, C=300.0, kernel=rbf, total=  11.2s
[CV] gamma=0.03, C=300.0, kernel=rbf .................................
[CV] .................. gamma=0.01, C=300.0, kernel=rbf, total=  11.3s
[CV] gamma=0.03, C=300.0, kernel=rbf .................................
[CV] .................. gamma=0.01, C=300.0, kernel=rbf, total=  11.5s
[CV] gamma=0.03, C=300.0, kernel=rbf .................................
[CV] .................. gamma=0.01, C=300.0, kernel=rbf, total=  11.2s
[CV] gamma=0.03, C=300.0, kernel=rbf .................................
[CV] .................. gamma=0.03, C=300.0, kernel=rbf, total=  10.9s
[CV] gamma=0.03, C=300.0, kernel=rbf .................................
[CV] .................. gamma=0.03, C=300.0, kernel=rbf, total=  11.0s
[CV] gamma=0.1, C=300.0, kernel=rbf ..................................
[CV] .................. gamma=0.03, C=300.0, kernel=rbf, total=  10.9s
[CV] gamma=0.1, C=300.0, kernel=rbf ..................................
[CV] .................. gamma=0.03, C=300.0, kernel=rbf, total=  11.1s
[CV] gamma=0.1, C=300.0, kernel=rbf ..................................
[CV] .................. gamma=0.03, C=300.0, kernel=rbf, total=  10.9s
[CV] gamma=0.1, C=300.0, kernel=rbf ..................................
[CV] ................... gamma=0.1, C=300.0, kernel=rbf, total=  11.0s
[CV] gamma=0.1, C=300.0, kernel=rbf ..................................
[CV] ................... gamma=0.1, C=300.0, kernel=rbf, total=  10.9s
[CV] gamma=0.3, C=300.0, kernel=rbf ..................................
[CV] ................... gamma=0.1, C=300.0, kernel=rbf, total=  11.1s
[CV] gamma=0.3, C=300.0, kernel=rbf ..................................
[CV] ................... gamma=0.1, C=300.0, kernel=rbf, total=  11.0s
[CV] gamma=0.3, C=300.0, kernel=rbf ..................................
[CV] ................... gamma=0.1, C=300.0, kernel=rbf, total=  10.6s
[CV] gamma=0.3, C=300.0, kernel=rbf ..................................
[CV] ................... gamma=0.3, C=300.0, kernel=rbf, total=  10.8s
[CV] gamma=0.3, C=300.0, kernel=rbf ..................................
[CV] ................... gamma=0.3, C=300.0, kernel=rbf, total=  10.6s
[CV] gamma=1.0, C=300.0, kernel=rbf ..................................
[CV] ................... gamma=0.3, C=300.0, kernel=rbf, total=  10.6s
[CV] gamma=1.0, C=300.0, kernel=rbf ..................................
[CV] ................... gamma=0.3, C=300.0, kernel=rbf, total=  10.7s
[CV] gamma=1.0, C=300.0, kernel=rbf ..................................
[CV] ................... gamma=0.3, C=300.0, kernel=rbf, total=  10.9s
[CV] gamma=1.0, C=300.0, kernel=rbf ..................................
[CV] ................... gamma=1.0, C=300.0, kernel=rbf, total=  10.8s
[CV] gamma=1.0, C=300.0, kernel=rbf ..................................
[CV] ................... gamma=1.0, C=300.0, kernel=rbf, total=  10.7s
[CV] gamma=3.0, C=300.0, kernel=rbf ..................................
[CV] ................... gamma=1.0, C=300.0, kernel=rbf, total=  10.7s
[CV] gamma=3.0, C=300.0, kernel=rbf ..................................
[CV] ................... gamma=1.0, C=300.0, kernel=rbf, total=  10.9s
[CV] gamma=3.0, C=300.0, kernel=rbf ..................................
[CV] ................... gamma=1.0, C=300.0, kernel=rbf, total=  10.9s
[CV] gamma=3.0, C=300.0, kernel=rbf ..................................
[CV] ................... gamma=3.0, C=300.0, kernel=rbf, total=  12.2s
[CV] gamma=3.0, C=300.0, kernel=rbf ..................................
[CV] ................... gamma=3.0, C=300.0, kernel=rbf, total=  12.1s
[CV] gamma=0.01, C=1000.0, kernel=rbf ................................
[CV] ................... gamma=3.0, C=300.0, kernel=rbf, total=  11.8s
[CV] gamma=0.01, C=1000.0, kernel=rbf ................................
[CV] ................... gamma=3.0, C=300.0, kernel=rbf, total=  12.2s
[CV] gamma=0.01, C=1000.0, kernel=rbf ................................
[CV] ................... gamma=3.0, C=300.0, kernel=rbf, total=  12.4s
[CV] gamma=0.01, C=1000.0, kernel=rbf ................................
[CV] ................. gamma=0.01, C=1000.0, kernel=rbf, total=  10.9s
[CV] gamma=0.01, C=1000.0, kernel=rbf ................................
[CV] ................. gamma=0.01, C=1000.0, kernel=rbf, total=  10.8s
[CV] gamma=0.03, C=1000.0, kernel=rbf ................................
[CV] ................. gamma=0.01, C=1000.0, kernel=rbf, total=  10.8s
[CV] gamma=0.03, C=1000.0, kernel=rbf ................................
[CV] ................. gamma=0.01, C=1000.0, kernel=rbf, total=  11.0s
[CV] gamma=0.03, C=1000.0, kernel=rbf ................................
[CV] ................. gamma=0.01, C=1000.0, kernel=rbf, total=  11.1s
[CV] gamma=0.03, C=1000.0, kernel=rbf ................................
[CV] ................. gamma=0.03, C=1000.0, kernel=rbf, total=  10.7s
[CV] gamma=0.03, C=1000.0, kernel=rbf ................................
[CV] ................. gamma=0.03, C=1000.0, kernel=rbf, total=  10.6s
[CV] gamma=0.1, C=1000.0, kernel=rbf .................................
[CV] ................. gamma=0.03, C=1000.0, kernel=rbf, total=  10.7s
[CV] gamma=0.1, C=1000.0, kernel=rbf .................................
[CV] ................. gamma=0.03, C=1000.0, kernel=rbf, total=  11.0s
[CV] gamma=0.1, C=1000.0, kernel=rbf .................................
[CV] ................. gamma=0.03, C=1000.0, kernel=rbf, total=  10.6s
[CV] gamma=0.1, C=1000.0, kernel=rbf .................................
[CV] .................. gamma=0.1, C=1000.0, kernel=rbf, total=  10.5s
[CV] gamma=0.1, C=1000.0, kernel=rbf .................................
[CV] .................. gamma=0.1, C=1000.0, kernel=rbf, total=  10.5s
[CV] gamma=0.3, C=1000.0, kernel=rbf .................................
[CV] .................. gamma=0.1, C=1000.0, kernel=rbf, total=  10.6s
[CV] gamma=0.3, C=1000.0, kernel=rbf .................................
[CV] .................. gamma=0.1, C=1000.0, kernel=rbf, total=  10.7s
[CV] gamma=0.3, C=1000.0, kernel=rbf .................................
[CV] .................. gamma=0.1, C=1000.0, kernel=rbf, total=  10.6s
[CV] gamma=0.3, C=1000.0, kernel=rbf .................................
[CV] .................. gamma=0.3, C=1000.0, kernel=rbf, total=  10.5s
[CV] gamma=0.3, C=1000.0, kernel=rbf .................................
[CV] .................. gamma=0.3, C=1000.0, kernel=rbf, total=  10.7s
[CV] gamma=1.0, C=1000.0, kernel=rbf .................................
[CV] .................. gamma=0.3, C=1000.0, kernel=rbf, total=  11.0s
[CV] gamma=1.0, C=1000.0, kernel=rbf .................................
[CV] .................. gamma=0.3, C=1000.0, kernel=rbf, total=  10.7s
[CV] gamma=1.0, C=1000.0, kernel=rbf .................................
[CV] .................. gamma=0.3, C=1000.0, kernel=rbf, total=  10.7s
[CV] gamma=1.0, C=1000.0, kernel=rbf .................................
[CV] .................. gamma=1.0, C=1000.0, kernel=rbf, total=  10.9s
[CV] gamma=1.0, C=1000.0, kernel=rbf .................................
[CV] .................. gamma=1.0, C=1000.0, kernel=rbf, total=  10.6s
[CV] gamma=3.0, C=1000.0, kernel=rbf .................................
[CV] .................. gamma=1.0, C=1000.0, kernel=rbf, total=  10.6s
[CV] gamma=3.0, C=1000.0, kernel=rbf .................................
[CV] .................. gamma=1.0, C=1000.0, kernel=rbf, total=  10.9s
[CV] gamma=3.0, C=1000.0, kernel=rbf .................................
[CV] .................. gamma=1.0, C=1000.0, kernel=rbf, total=  11.2s
[CV] gamma=3.0, C=1000.0, kernel=rbf .................................
[CV] .................. gamma=3.0, C=1000.0, kernel=rbf, total=  11.8s
[CV] gamma=3.0, C=1000.0, kernel=rbf .................................
[CV] .................. gamma=3.0, C=1000.0, kernel=rbf, total=  11.9s
[CV] .................. gamma=3.0, C=1000.0, kernel=rbf, total=  12.4s
[CV] .................. gamma=3.0, C=1000.0, kernel=rbf, total=  11.7s
[CV] .................. gamma=3.0, C=1000.0, kernel=rbf, total=  11.8s
[Parallel(n_jobs=4)]: Done 250 out of 250 | elapsed: 16.5min finished
Out[107]:
GridSearchCV(cv=5, error_score='raise',
       estimator=SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='auto',
  kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False),
       fit_params=None, iid=True, n_jobs=4,
       param_grid=[{'C': [10.0, 30.0, 100.0, 300.0, 1000.0, 3000.0, 10000.0, 30000.0], 'kernel': ['linear']}, {'gamma': [0.01, 0.03, 0.1, 0.3, 1.0, 3.0], 'C': [1.0, 3.0, 10.0, 30.0, 100.0, 300.0, 1000.0], 'kernel': ['rbf']}],
       pre_dispatch='2*n_jobs', refit=True, return_train_score=True,
       scoring='neg_mean_squared_error', verbose=2)

The best model achieves the following score (evaluated using 5-fold cross validation):


In [108]:
negative_mse = grid_search.best_score_
rmse = np.sqrt(-negative_mse)
rmse


Out[108]:
70363.903139641669

That's much worse than the RandomForestRegressor. Let's check the best hyperparameters found:


In [109]:
grid_search.best_params_


Out[109]:
{'C': 30000.0, 'kernel': 'linear'}

The linear kernel seems better than the RBF kernel. Notice that the value of C is the maximum tested value. When this happens you definitely want to launch the grid search again with higher values for C (removing the smallest values), because it is likely that higher values of C will be better.

2.

Question: Try replacing GridSearchCV with RandomizedSearchCV.


In [110]:
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import expon, reciprocal

# see https://docs.scipy.org/doc/scipy-0.19.0/reference/stats.html
# for `expon()` and `reciprocal()` documentation and more probability distribution functions.

# Note: gamma is ignored when kernel is "linear"
param_distribs = {
        'kernel': ['linear', 'rbf'],
        'C': reciprocal(20, 200000),
        'gamma': expon(scale=1.0),
    }

svm_reg = SVR()
rnd_search = RandomizedSearchCV(svm_reg, param_distributions=param_distribs,
                                n_iter=50, cv=5, scoring='neg_mean_squared_error',
                                verbose=2, n_jobs=4, random_state=42)
rnd_search.fit(housing_prepared, housing_labels)


Fitting 5 folds for each of 50 candidates, totalling 250 fits
[CV] gamma=3.01012143092, C=629.782329591, kernel=linear .............
[CV] gamma=3.01012143092, C=629.782329591, kernel=linear .............
[CV] gamma=3.01012143092, C=629.782329591, kernel=linear .............
[CV] gamma=3.01012143092, C=629.782329591, kernel=linear .............
[CV]  gamma=3.01012143092, C=629.782329591, kernel=linear, total=   6.6s
[CV] gamma=3.01012143092, C=629.782329591, kernel=linear .............
[CV]  gamma=3.01012143092, C=629.782329591, kernel=linear, total=   6.6s
[CV] gamma=0.908446969632, C=26290.2064643, kernel=rbf ...............
[CV]  gamma=3.01012143092, C=629.782329591, kernel=linear, total=   6.7s
[CV] gamma=0.908446969632, C=26290.2064643, kernel=rbf ...............
[CV]  gamma=3.01012143092, C=629.782329591, kernel=linear, total=   6.8s
[CV] gamma=0.908446969632, C=26290.2064643, kernel=rbf ...............
[CV]  gamma=3.01012143092, C=629.782329591, kernel=linear, total=   6.6s
[CV] gamma=0.908446969632, C=26290.2064643, kernel=rbf ...............
[CV]  gamma=0.908446969632, C=26290.2064643, kernel=rbf, total=  25.6s
[CV] gamma=0.908446969632, C=26290.2064643, kernel=rbf ...............
[CV]  gamma=0.908446969632, C=26290.2064643, kernel=rbf, total=  25.7s
[CV] gamma=0.0598387686087, C=84.1410790058, kernel=rbf ..............
[CV]  gamma=0.908446969632, C=26290.2064643, kernel=rbf, total=  25.9s
[CV] gamma=0.0598387686087, C=84.1410790058, kernel=rbf ..............
[CV]  gamma=0.908446969632, C=26290.2064643, kernel=rbf, total=  25.9s
[CV] gamma=0.0598387686087, C=84.1410790058, kernel=rbf ..............
[CV]  gamma=0.0598387686087, C=84.1410790058, kernel=rbf, total=  11.1s
[CV] gamma=0.0598387686087, C=84.1410790058, kernel=rbf ..............
[CV]  gamma=0.0598387686087, C=84.1410790058, kernel=rbf, total=  11.3s
[CV] gamma=0.0598387686087, C=84.1410790058, kernel=rbf ..............
[CV]  gamma=0.908446969632, C=26290.2064643, kernel=rbf, total=  13.4s
[CV] gamma=0.154161967467, C=432.378848131, kernel=linear ............
[CV]  gamma=0.0598387686087, C=84.1410790058, kernel=rbf, total=  11.2s
[CV] gamma=0.154161967467, C=432.378848131, kernel=linear ............
[CV]  gamma=0.154161967467, C=432.378848131, kernel=linear, total=   6.3s
[CV] gamma=0.154161967467, C=432.378848131, kernel=linear ............
[CV]  gamma=0.0598387686087, C=84.1410790058, kernel=rbf, total=  11.2s
[CV] gamma=0.154161967467, C=432.378848131, kernel=linear ............
[CV]  gamma=0.0598387686087, C=84.1410790058, kernel=rbf, total=  11.4s
[CV] gamma=0.154161967467, C=432.378848131, kernel=linear ............
[CV]  gamma=0.154161967467, C=432.378848131, kernel=linear, total=   6.6s
[CV] gamma=3.50355747516, C=24.1750829461, kernel=rbf ................
[CV]  gamma=0.154161967467, C=432.378848131, kernel=linear, total=   6.6s
[CV] gamma=3.50355747516, C=24.1750829461, kernel=rbf ................
[CV]  gamma=0.154161967467, C=432.378848131, kernel=linear, total=   6.5s
[CV] gamma=3.50355747516, C=24.1750829461, kernel=rbf ................
[CV]  gamma=0.154161967467, C=432.378848131, kernel=linear, total=   6.6s
[CV] gamma=3.50355747516, C=24.1750829461, kernel=rbf ................
[CV] . gamma=3.50355747516, C=24.1750829461, kernel=rbf, total=  12.6s
[CV] gamma=3.50355747516, C=24.1750829461, kernel=rbf ................
[CV] . gamma=3.50355747516, C=24.1750829461, kernel=rbf, total=  12.5s
[CV] gamma=0.000779069236658, C=113564.039406, kernel=rbf ............
[CV] . gamma=3.50355747516, C=24.1750829461, kernel=rbf, total=  12.7s
[CV] gamma=0.000779069236658, C=113564.039406, kernel=rbf ............
[CV] . gamma=3.50355747516, C=24.1750829461, kernel=rbf, total=  12.4s
[CV] gamma=0.000779069236658, C=113564.039406, kernel=rbf ............
[CV]  gamma=0.000779069236658, C=113564.039406, kernel=rbf, total=  10.7s
[CV] gamma=0.000779069236658, C=113564.039406, kernel=rbf ............
[CV] . gamma=3.50355747516, C=24.1750829461, kernel=rbf, total=  12.4s
[CV] gamma=0.000779069236658, C=113564.039406, kernel=rbf ............
[CV]  gamma=0.000779069236658, C=113564.039406, kernel=rbf, total=  10.8s
[CV] gamma=0.36275372946, C=108.304882388, kernel=rbf ................
[CV]  gamma=0.000779069236658, C=113564.039406, kernel=rbf, total=  11.0s
[CV] gamma=0.36275372946, C=108.304882388, kernel=rbf ................
[CV]  gamma=0.000779069236658, C=113564.039406, kernel=rbf, total=  10.9s
[CV] gamma=0.36275372946, C=108.304882388, kernel=rbf ................
[CV]  gamma=0.000779069236658, C=113564.039406, kernel=rbf, total=  10.7s
[CV] gamma=0.36275372946, C=108.304882388, kernel=rbf ................
[CV] . gamma=0.36275372946, C=108.304882388, kernel=rbf, total=  10.8s
[CV] gamma=0.36275372946, C=108.304882388, kernel=rbf ................
[CV] . gamma=0.36275372946, C=108.304882388, kernel=rbf, total=  10.9s
[CV] gamma=0.0233325235983, C=21.3449536726, kernel=linear ...........
[CV]  gamma=0.0233325235983, C=21.3449536726, kernel=linear, total=   6.5s
[CV] gamma=0.0233325235983, C=21.3449536726, kernel=linear ...........
[Parallel(n_jobs=4)]: Done  33 tasks      | elapsed:  2.4min
[CV] . gamma=0.36275372946, C=108.304882388, kernel=rbf, total=  10.8s
[CV] gamma=0.0233325235983, C=21.3449536726, kernel=linear ...........
[CV] . gamma=0.36275372946, C=108.304882388, kernel=rbf, total=  10.9s
[CV] gamma=0.0233325235983, C=21.3449536726, kernel=linear ...........
[CV] . gamma=0.36275372946, C=108.304882388, kernel=rbf, total=  10.7s
[CV] gamma=0.0233325235983, C=21.3449536726, kernel=linear ...........
[CV]  gamma=0.0233325235983, C=21.3449536726, kernel=linear, total=   6.6s
[CV] gamma=0.150234528727, C=5603.27031743, kernel=rbf ...............
[CV]  gamma=0.0233325235983, C=21.3449536726, kernel=linear, total=   6.5s
[CV] gamma=0.150234528727, C=5603.27031743, kernel=rbf ...............
[CV]  gamma=0.0233325235983, C=21.3449536726, kernel=linear, total=   6.5s
[CV] gamma=0.150234528727, C=5603.27031743, kernel=rbf ...............
[CV]  gamma=0.0233325235983, C=21.3449536726, kernel=linear, total=   6.4s
[CV] gamma=0.150234528727, C=5603.27031743, kernel=rbf ...............
[CV]  gamma=0.150234528727, C=5603.27031743, kernel=rbf, total=  10.7s
[CV] gamma=0.150234528727, C=5603.27031743, kernel=rbf ...............
[CV]  gamma=0.150234528727, C=5603.27031743, kernel=rbf, total=  10.5s
[CV] gamma=0.26497040005, C=157055.109894, kernel=rbf ................
[CV]  gamma=0.150234528727, C=5603.27031743, kernel=rbf, total=  10.7s
[CV] gamma=0.26497040005, C=157055.109894, kernel=rbf ................
[CV]  gamma=0.150234528727, C=5603.27031743, kernel=rbf, total=  10.5s
[CV] gamma=0.26497040005, C=157055.109894, kernel=rbf ................
[CV]  gamma=0.150234528727, C=5603.27031743, kernel=rbf, total=  10.6s
[CV] gamma=0.26497040005, C=157055.109894, kernel=rbf ................
[CV] . gamma=0.26497040005, C=157055.109894, kernel=rbf, total=  25.8s
[CV] gamma=0.26497040005, C=157055.109894, kernel=rbf ................
[CV] . gamma=0.26497040005, C=157055.109894, kernel=rbf, total=  27.3s
[CV] gamma=0.222735862129, C=27652.4643587, kernel=linear ............
[CV] . gamma=0.26497040005, C=157055.109894, kernel=rbf, total=  30.9s
[CV] gamma=0.222735862129, C=27652.4643587, kernel=linear ............
[CV] . gamma=0.26497040005, C=157055.109894, kernel=rbf, total=  25.5s
[CV] gamma=0.222735862129, C=27652.4643587, kernel=linear ............
[CV]  gamma=0.222735862129, C=27652.4643587, kernel=linear, total=  14.1s
[CV] gamma=0.222735862129, C=27652.4643587, kernel=linear ............
[CV]  gamma=0.222735862129, C=27652.4643587, kernel=linear, total=  15.0s
[CV] gamma=0.222735862129, C=27652.4643587, kernel=linear ............
[CV] . gamma=0.26497040005, C=157055.109894, kernel=rbf, total=  27.8s
[CV] gamma=0.628789100541, C=171377.395704, kernel=linear ............
[CV]  gamma=0.222735862129, C=27652.4643587, kernel=linear, total=  15.6s
[CV] gamma=0.628789100541, C=171377.395704, kernel=linear ............
[CV]  gamma=0.222735862129, C=27652.4643587, kernel=linear, total=  14.1s
[CV] gamma=0.628789100541, C=171377.395704, kernel=linear ............
[CV]  gamma=0.222735862129, C=27652.4643587, kernel=linear, total=  12.4s
[CV] gamma=0.628789100541, C=171377.395704, kernel=linear ............
[CV]  gamma=0.628789100541, C=171377.395704, kernel=linear, total=  46.0s
[CV] gamma=0.628789100541, C=171377.395704, kernel=linear ............
[CV]  gamma=0.628789100541, C=171377.395704, kernel=linear, total=  57.9s
[CV] gamma=0.186961251977, C=5385.29382017, kernel=linear ............
[CV]  gamma=0.628789100541, C=171377.395704, kernel=linear, total=  59.5s
[CV] gamma=0.186961251977, C=5385.29382017, kernel=linear ............
[CV]  gamma=0.628789100541, C=171377.395704, kernel=linear, total=  50.6s
[CV] gamma=0.186961251977, C=5385.29382017, kernel=linear ............
[CV]  gamma=0.186961251977, C=5385.29382017, kernel=linear, total=   8.0s
[CV] gamma=0.186961251977, C=5385.29382017, kernel=linear ............
[CV]  gamma=0.186961251977, C=5385.29382017, kernel=linear, total=   8.2s
[CV] gamma=0.186961251977, C=5385.29382017, kernel=linear ............
[CV]  gamma=0.186961251977, C=5385.29382017, kernel=linear, total=   8.2s
[CV] gamma=2.85079687894, C=22.5990321662, kernel=rbf ................
[CV]  gamma=0.186961251977, C=5385.29382017, kernel=linear, total=   7.9s
[CV] gamma=2.85079687894, C=22.5990321662, kernel=rbf ................
[CV]  gamma=0.186961251977, C=5385.29382017, kernel=linear, total=   8.1s
[CV] gamma=2.85079687894, C=22.5990321662, kernel=rbf ................
[CV] . gamma=2.85079687894, C=22.5990321662, kernel=rbf, total=  11.8s
[CV] gamma=2.85079687894, C=22.5990321662, kernel=rbf ................
[CV]  gamma=0.628789100541, C=171377.395704, kernel=linear, total=  39.8s
[CV] gamma=2.85079687894, C=22.5990321662, kernel=rbf ................
[CV] . gamma=2.85079687894, C=22.5990321662, kernel=rbf, total=  11.8s
[CV] gamma=0.363287859969, C=34246.7519463, kernel=linear ............
[CV] . gamma=2.85079687894, C=22.5990321662, kernel=rbf, total=  11.8s
[CV] gamma=0.363287859969, C=34246.7519463, kernel=linear ............
[CV] . gamma=2.85079687894, C=22.5990321662, kernel=rbf, total=  11.9s
[CV] gamma=0.363287859969, C=34246.7519463, kernel=linear ............
[CV] . gamma=2.85079687894, C=22.5990321662, kernel=rbf, total=  11.6s
[CV] gamma=0.363287859969, C=34246.7519463, kernel=linear ............
[CV]  gamma=0.363287859969, C=34246.7519463, kernel=linear, total=  16.4s
[CV] gamma=0.363287859969, C=34246.7519463, kernel=linear ............
[CV]  gamma=0.363287859969, C=34246.7519463, kernel=linear, total=  16.3s
[CV] gamma=0.275787054226, C=167.727895608, kernel=rbf ...............
[CV]  gamma=0.363287859969, C=34246.7519463, kernel=linear, total=  16.2s
[CV] gamma=0.275787054226, C=167.727895608, kernel=rbf ...............
[CV]  gamma=0.363287859969, C=34246.7519463, kernel=linear, total=  16.9s
[CV] gamma=0.275787054226, C=167.727895608, kernel=rbf ...............
[CV]  gamma=0.363287859969, C=34246.7519463, kernel=linear, total=  14.6s
[CV] gamma=0.275787054226, C=167.727895608, kernel=rbf ...............
[CV]  gamma=0.275787054226, C=167.727895608, kernel=rbf, total=  11.1s
[CV] gamma=0.275787054226, C=167.727895608, kernel=rbf ...............
[CV]  gamma=0.275787054226, C=167.727895608, kernel=rbf, total=  10.7s
[CV] gamma=0.683547228134, C=61.543605425, kernel=linear .............
[CV]  gamma=0.275787054226, C=167.727895608, kernel=rbf, total=  10.8s
[CV] gamma=0.683547228134, C=61.543605425, kernel=linear .............
[CV]  gamma=0.683547228134, C=61.543605425, kernel=linear, total=   6.4s
[CV] gamma=0.683547228134, C=61.543605425, kernel=linear .............
[CV]  gamma=0.275787054226, C=167.727895608, kernel=rbf, total=  10.7s
[CV] gamma=0.683547228134, C=61.543605425, kernel=linear .............
[CV]  gamma=0.275787054226, C=167.727895608, kernel=rbf, total=  10.9s
[CV] gamma=0.683547228134, C=61.543605425, kernel=linear .............
[CV]  gamma=0.683547228134, C=61.543605425, kernel=linear, total=   6.3s
[CV] gamma=0.496036536049, C=98.7389738992, kernel=rbf ...............
[CV]  gamma=0.683547228134, C=61.543605425, kernel=linear, total=   6.5s
[CV] gamma=0.496036536049, C=98.7389738992, kernel=rbf ...............
[CV]  gamma=0.683547228134, C=61.543605425, kernel=linear, total=   6.4s
[CV] gamma=0.496036536049, C=98.7389738992, kernel=rbf ...............
[CV]  gamma=0.683547228134, C=61.543605425, kernel=linear, total=   6.3s
[CV] gamma=0.496036536049, C=98.7389738992, kernel=rbf ...............
[CV]  gamma=0.496036536049, C=98.7389738992, kernel=rbf, total=  10.7s
[CV] gamma=0.496036536049, C=98.7389738992, kernel=rbf ...............
[CV]  gamma=0.496036536049, C=98.7389738992, kernel=rbf, total=  10.7s
[CV] gamma=0.373546581658, C=8935.50563595, kernel=rbf ...............
[CV]  gamma=0.496036536049, C=98.7389738992, kernel=rbf, total=  10.7s
[CV] gamma=0.373546581658, C=8935.50563595, kernel=rbf ...............
[CV]  gamma=0.496036536049, C=98.7389738992, kernel=rbf, total=  11.1s
[CV] gamma=0.373546581658, C=8935.50563595, kernel=rbf ...............
[CV]  gamma=0.496036536049, C=98.7389738992, kernel=rbf, total=  10.7s
[CV] gamma=0.373546581658, C=8935.50563595, kernel=rbf ...............
[CV]  gamma=0.373546581658, C=8935.50563595, kernel=rbf, total=  10.6s
[CV] gamma=0.373546581658, C=8935.50563595, kernel=rbf ...............
[CV]  gamma=0.373546581658, C=8935.50563595, kernel=rbf, total=  10.6s
[CV] gamma=0.838636245625, C=135.767758248, kernel=linear ............
[CV]  gamma=0.373546581658, C=8935.50563595, kernel=rbf, total=  11.0s
[CV] gamma=0.838636245625, C=135.767758248, kernel=linear ............
[CV]  gamma=0.373546581658, C=8935.50563595, kernel=rbf, total=  10.8s
[CV] gamma=0.838636245625, C=135.767758248, kernel=linear ............
[CV]  gamma=0.838636245625, C=135.767758248, kernel=linear, total=   6.4s
[CV] gamma=0.838636245625, C=135.767758248, kernel=linear ............
[CV]  gamma=0.838636245625, C=135.767758248, kernel=linear, total=   6.6s
[CV] gamma=0.838636245625, C=135.767758248, kernel=linear ............
[CV]  gamma=0.373546581658, C=8935.50563595, kernel=rbf, total=  10.9s
[CV] gamma=1.49224537714, C=151136.202825, kernel=rbf ................
[CV]  gamma=0.838636245625, C=135.767758248, kernel=linear, total=   6.8s
[CV] gamma=1.49224537714, C=151136.202825, kernel=rbf ................
[CV]  gamma=0.838636245625, C=135.767758248, kernel=linear, total=   6.6s
[CV] gamma=1.49224537714, C=151136.202825, kernel=rbf ................
[CV]  gamma=0.838636245625, C=135.767758248, kernel=linear, total=   6.6s
[CV] gamma=1.49224537714, C=151136.202825, kernel=rbf ................
[CV] . gamma=1.49224537714, C=151136.202825, kernel=rbf, total= 1.8min
[CV] gamma=1.49224537714, C=151136.202825, kernel=rbf ................
[CV] . gamma=1.49224537714, C=151136.202825, kernel=rbf, total= 1.9min
[CV] gamma=2.61263365142, C=761.43167585, kernel=linear ..............
[CV]  gamma=2.61263365142, C=761.43167585, kernel=linear, total=   6.6s
[CV] gamma=2.61263365142, C=761.43167585, kernel=linear ..............
[CV]  gamma=2.61263365142, C=761.43167585, kernel=linear, total=   6.7s
[CV] gamma=2.61263365142, C=761.43167585, kernel=linear ..............
[CV] . gamma=1.49224537714, C=151136.202825, kernel=rbf, total= 2.3min
[CV] gamma=2.61263365142, C=761.43167585, kernel=linear ..............
[CV]  gamma=2.61263365142, C=761.43167585, kernel=linear, total=   6.7s
[CV] gamma=2.61263365142, C=761.43167585, kernel=linear ..............
[CV]  gamma=2.61263365142, C=761.43167585, kernel=linear, total=   6.6s
[CV] gamma=0.0926554589531, C=97392.8188304, kernel=linear ...........
[CV] . gamma=1.49224537714, C=151136.202825, kernel=rbf, total= 2.5min
[CV] gamma=0.0926554589531, C=97392.8188304, kernel=linear ...........
[CV]  gamma=2.61263365142, C=761.43167585, kernel=linear, total=   6.6s
[CV] gamma=0.0926554589531, C=97392.8188304, kernel=linear ...........
[CV]  gamma=0.0926554589531, C=97392.8188304, kernel=linear, total=  32.9s
[CV] gamma=0.0926554589531, C=97392.8188304, kernel=linear ...........
[CV]  gamma=0.0926554589531, C=97392.8188304, kernel=linear, total=  32.0s
[CV] gamma=0.0926554589531, C=97392.8188304, kernel=linear ...........
[CV]  gamma=0.0926554589531, C=97392.8188304, kernel=linear, total=  50.7s
[CV] gamma=3.24861427024, C=2423.07599849, kernel=linear .............
[CV]  gamma=0.0926554589531, C=97392.8188304, kernel=linear, total=  27.2s
[CV] gamma=3.24861427024, C=2423.07599849, kernel=linear .............
[CV]  gamma=3.24861427024, C=2423.07599849, kernel=linear, total=   7.3s
[CV] gamma=3.24861427024, C=2423.07599849, kernel=linear .............
[CV]  gamma=0.0926554589531, C=97392.8188304, kernel=linear, total=  33.6s
[CV] gamma=3.24861427024, C=2423.07599849, kernel=linear .............
[CV]  gamma=3.24861427024, C=2423.07599849, kernel=linear, total=   7.1s
[CV] gamma=3.24861427024, C=2423.07599849, kernel=linear .............
[CV]  gamma=3.24861427024, C=2423.07599849, kernel=linear, total=   7.5s
[CV] gamma=0.316560443209, C=717.363299726, kernel=linear ............
[CV]  gamma=3.24861427024, C=2423.07599849, kernel=linear, total=   7.4s
[CV] gamma=0.316560443209, C=717.363299726, kernel=linear ............
[CV]  gamma=0.316560443209, C=717.363299726, kernel=linear, total=   6.6s
[CV] gamma=0.316560443209, C=717.363299726, kernel=linear ............
[CV]  gamma=3.24861427024, C=2423.07599849, kernel=linear, total=   7.1s
[CV] gamma=0.316560443209, C=717.363299726, kernel=linear ............
[CV]  gamma=0.316560443209, C=717.363299726, kernel=linear, total=   6.6s
[CV] gamma=0.316560443209, C=717.363299726, kernel=linear ............
[CV]  gamma=0.316560443209, C=717.363299726, kernel=linear, total=   6.8s
[CV] gamma=3.35972844566, C=4446.66752118, kernel=rbf ................
[CV]  gamma=0.316560443209, C=717.363299726, kernel=linear, total=   6.7s
[CV] gamma=3.35972844566, C=4446.66752118, kernel=rbf ................
[CV]  gamma=0.316560443209, C=717.363299726, kernel=linear, total=   6.5s
[CV] gamma=3.35972844566, C=4446.66752118, kernel=rbf ................
[CV] . gamma=1.49224537714, C=151136.202825, kernel=rbf, total= 2.2min
[CV] gamma=3.35972844566, C=4446.66752118, kernel=rbf ................
[CV] . gamma=3.35972844566, C=4446.66752118, kernel=rbf, total=  12.3s
[CV] gamma=3.35972844566, C=4446.66752118, kernel=rbf ................
[CV] . gamma=3.35972844566, C=4446.66752118, kernel=rbf, total=  12.4s
[CV] gamma=0.151898147821, C=2963.56412121, kernel=linear ............
[CV] . gamma=3.35972844566, C=4446.66752118, kernel=rbf, total=  12.3s
[CV] gamma=0.151898147821, C=2963.56412121, kernel=linear ............
[CV] . gamma=3.35972844566, C=4446.66752118, kernel=rbf, total=  12.3s
[CV] gamma=0.151898147821, C=2963.56412121, kernel=linear ............
[CV]  gamma=0.151898147821, C=2963.56412121, kernel=linear, total=   7.2s
[CV] gamma=0.151898147821, C=2963.56412121, kernel=linear ............
[CV]  gamma=0.151898147821, C=2963.56412121, kernel=linear, total=   7.6s
[CV] gamma=0.151898147821, C=2963.56412121, kernel=linear ............
[CV]  gamma=0.151898147821, C=2963.56412121, kernel=linear, total=   7.7s
[CV] gamma=0.0157599448359, C=91.6426738169, kernel=linear ...........
[CV] . gamma=3.35972844566, C=4446.66752118, kernel=rbf, total=  12.8s
[CV] gamma=0.0157599448359, C=91.6426738169, kernel=linear ...........
[CV]  gamma=0.151898147821, C=2963.56412121, kernel=linear, total=   7.4s
[CV] gamma=0.0157599448359, C=91.6426738169, kernel=linear ...........
[CV]  gamma=0.151898147821, C=2963.56412121, kernel=linear, total=   7.1s
[CV] gamma=0.0157599448359, C=91.6426738169, kernel=linear ...........
[CV]  gamma=0.0157599448359, C=91.6426738169, kernel=linear, total=   6.3s
[CV] gamma=0.0157599448359, C=91.6426738169, kernel=linear ...........
[CV]  gamma=0.0157599448359, C=91.6426738169, kernel=linear, total=   6.4s
[CV] gamma=0.221539440506, C=24547.6019757, kernel=rbf ...............
[CV]  gamma=0.0157599448359, C=91.6426738169, kernel=linear, total=   6.8s
[CV] gamma=0.221539440506, C=24547.6019757, kernel=rbf ...............
[CV]  gamma=0.0157599448359, C=91.6426738169, kernel=linear, total=   6.4s
[CV] gamma=0.221539440506, C=24547.6019757, kernel=rbf ...............
[CV]  gamma=0.0157599448359, C=91.6426738169, kernel=linear, total=   6.3s
[CV] gamma=0.221539440506, C=24547.6019757, kernel=rbf ...............
[CV]  gamma=0.221539440506, C=24547.6019757, kernel=rbf, total=  11.3s
[CV] gamma=0.221539440506, C=24547.6019757, kernel=rbf ...............
[CV]  gamma=0.221539440506, C=24547.6019757, kernel=rbf, total=  11.2s
[CV] gamma=0.221697602314, C=22.7692794106, kernel=rbf ...............
[CV]  gamma=0.221539440506, C=24547.6019757, kernel=rbf, total=  11.1s
[CV] gamma=0.221697602314, C=22.7692794106, kernel=rbf ...............
[CV]  gamma=0.221539440506, C=24547.6019757, kernel=rbf, total=  11.1s
[CV] gamma=0.221697602314, C=22.7692794106, kernel=rbf ...............
[CV]  gamma=0.221539440506, C=24547.6019757, kernel=rbf, total=  11.3s
[CV] gamma=0.221697602314, C=22.7692794106, kernel=rbf ...............
[CV]  gamma=0.221697602314, C=22.7692794106, kernel=rbf, total=  11.0s
[CV] gamma=0.221697602314, C=22.7692794106, kernel=rbf ...............
[CV]  gamma=0.221697602314, C=22.7692794106, kernel=rbf, total=  10.9s
[CV] gamma=1.47521452604, C=16483.8505298, kernel=linear .............
[CV]  gamma=0.221697602314, C=22.7692794106, kernel=rbf, total=  10.9s
[CV] gamma=1.47521452604, C=16483.8505298, kernel=linear .............
[CV]  gamma=1.47521452604, C=16483.8505298, kernel=linear, total=  10.4s
[CV] gamma=1.47521452604, C=16483.8505298, kernel=linear .............
[CV]  gamma=0.221697602314, C=22.7692794106, kernel=rbf, total=  11.2s
[CV] gamma=1.47521452604, C=16483.8505298, kernel=linear .............
[CV]  gamma=0.221697602314, C=22.7692794106, kernel=rbf, total=  11.0s
[CV] gamma=1.47521452604, C=16483.8505298, kernel=linear .............
[CV]  gamma=1.47521452604, C=16483.8505298, kernel=linear, total=  11.1s
[CV] gamma=1.05290408458, C=101445.668813, kernel=rbf ................
[CV]  gamma=1.47521452604, C=16483.8505298, kernel=linear, total=  11.3s
[CV] gamma=1.05290408458, C=101445.668813, kernel=rbf ................
[CV]  gamma=1.47521452604, C=16483.8505298, kernel=linear, total=  10.2s
[CV] gamma=1.05290408458, C=101445.668813, kernel=rbf ................
[Parallel(n_jobs=4)]: Done 154 tasks      | elapsed: 13.2min
[CV]  gamma=1.47521452604, C=16483.8505298, kernel=linear, total=  11.9s
[CV] gamma=1.05290408458, C=101445.668813, kernel=rbf ................
[CV] . gamma=1.05290408458, C=101445.668813, kernel=rbf, total=  51.0s
[CV] gamma=1.05290408458, C=101445.668813, kernel=rbf ................
[CV] . gamma=1.05290408458, C=101445.668813, kernel=rbf, total=  47.6s
[CV] gamma=0.976301191712, C=56681.8085903, kernel=rbf ...............
[CV] . gamma=1.05290408458, C=101445.668813, kernel=rbf, total= 1.0min
[CV] gamma=0.976301191712, C=56681.8085903, kernel=rbf ...............
[CV] . gamma=1.05290408458, C=101445.668813, kernel=rbf, total= 1.1min
[CV] gamma=0.976301191712, C=56681.8085903, kernel=rbf ...............
[CV]  gamma=0.976301191712, C=56681.8085903, kernel=rbf, total=  21.3s
[CV] gamma=0.976301191712, C=56681.8085903, kernel=rbf ...............
[CV]  gamma=0.976301191712, C=56681.8085903, kernel=rbf, total=  21.4s
[CV] gamma=0.976301191712, C=56681.8085903, kernel=rbf ...............
[CV]  gamma=0.976301191712, C=56681.8085903, kernel=rbf, total=  20.9s
[CV] gamma=0.463335116798, C=48.1582239093, kernel=rbf ...............
[CV] . gamma=1.05290408458, C=101445.668813, kernel=rbf, total=  53.6s
[CV] gamma=0.463335116798, C=48.1582239093, kernel=rbf ...............
[CV]  gamma=0.976301191712, C=56681.8085903, kernel=rbf, total=  23.5s
[CV] gamma=0.463335116798, C=48.1582239093, kernel=rbf ...............
[CV]  gamma=0.463335116798, C=48.1582239093, kernel=rbf, total=  10.9s
[CV] gamma=0.463335116798, C=48.1582239093, kernel=rbf ...............
[CV]  gamma=0.463335116798, C=48.1582239093, kernel=rbf, total=  10.7s
[CV] gamma=0.463335116798, C=48.1582239093, kernel=rbf ...............
[CV]  gamma=0.976301191712, C=56681.8085903, kernel=rbf, total=  22.9s
[CV] gamma=1.30787578396, C=399.726815571, kernel=rbf ................
[CV]  gamma=0.463335116798, C=48.1582239093, kernel=rbf, total=  10.7s
[CV] gamma=1.30787578396, C=399.726815571, kernel=rbf ................
[CV]  gamma=0.463335116798, C=48.1582239093, kernel=rbf, total=  10.7s
[CV] gamma=1.30787578396, C=399.726815571, kernel=rbf ................
[CV]  gamma=0.463335116798, C=48.1582239093, kernel=rbf, total=  10.8s
[CV] gamma=1.30787578396, C=399.726815571, kernel=rbf ................
[CV] . gamma=1.30787578396, C=399.726815571, kernel=rbf, total=  10.7s
[CV] gamma=1.30787578396, C=399.726815571, kernel=rbf ................
[CV] . gamma=1.30787578396, C=399.726815571, kernel=rbf, total=  10.6s
[CV] gamma=0.823810520491, C=251.140738863, kernel=linear ............
[CV] . gamma=1.30787578396, C=399.726815571, kernel=rbf, total=  10.5s
[CV] gamma=0.823810520491, C=251.140738863, kernel=linear ............
[CV]  gamma=0.823810520491, C=251.140738863, kernel=linear, total=   6.3s
[CV] gamma=0.823810520491, C=251.140738863, kernel=linear ............
[CV] . gamma=1.30787578396, C=399.726815571, kernel=rbf, total=  10.7s
[CV] gamma=0.823810520491, C=251.140738863, kernel=linear ............
[CV]  gamma=0.823810520491, C=251.140738863, kernel=linear, total=   6.4s
[CV] gamma=0.823810520491, C=251.140738863, kernel=linear ............
[CV] . gamma=1.30787578396, C=399.726815571, kernel=rbf, total=  10.8s
[CV] gamma=1.24912634432, C=60.1737364289, kernel=linear .............
[CV]  gamma=0.823810520491, C=251.140738863, kernel=linear, total=   6.6s
[CV] gamma=1.24912634432, C=60.1737364289, kernel=linear .............
[CV]  gamma=0.823810520491, C=251.140738863, kernel=linear, total=   6.6s
[CV] gamma=1.24912634432, C=60.1737364289, kernel=linear .............
[CV]  gamma=1.24912634432, C=60.1737364289, kernel=linear, total=   6.5s
[CV] gamma=1.24912634432, C=60.1737364289, kernel=linear .............
[CV]  gamma=0.823810520491, C=251.140738863, kernel=linear, total=   6.8s
[CV] gamma=1.24912634432, C=60.1737364289, kernel=linear .............
[CV]  gamma=1.24912634432, C=60.1737364289, kernel=linear, total=   6.9s
[CV] gamma=0.269167751462, C=15415.1615449, kernel=rbf ...............
[CV]  gamma=1.24912634432, C=60.1737364289, kernel=linear, total=   6.5s
[CV] gamma=0.269167751462, C=15415.1615449, kernel=rbf ...............
[CV]  gamma=1.24912634432, C=60.1737364289, kernel=linear, total=   6.4s
[CV] gamma=0.269167751462, C=15415.1615449, kernel=rbf ...............
[CV]  gamma=1.24912634432, C=60.1737364289, kernel=linear, total=   6.5s
[CV] gamma=0.269167751462, C=15415.1615449, kernel=rbf ...............
[CV]  gamma=0.269167751462, C=15415.1615449, kernel=rbf, total=  10.8s
[CV] gamma=0.269167751462, C=15415.1615449, kernel=rbf ...............
[CV]  gamma=0.269167751462, C=15415.1615449, kernel=rbf, total=  11.3s
[CV] gamma=0.739678838777, C=1888.914851, kernel=linear ..............
[CV]  gamma=0.269167751462, C=15415.1615449, kernel=rbf, total=  10.9s
[CV] gamma=0.739678838777, C=1888.914851, kernel=linear ..............
[CV]  gamma=0.269167751462, C=15415.1615449, kernel=rbf, total=  11.3s
[CV] gamma=0.739678838777, C=1888.914851, kernel=linear ..............
[CV]  gamma=0.739678838777, C=1888.914851, kernel=linear, total=   7.0s
[CV] gamma=0.739678838777, C=1888.914851, kernel=linear ..............
[CV]  gamma=0.739678838777, C=1888.914851, kernel=linear, total=   7.1s
[CV] gamma=0.739678838777, C=1888.914851, kernel=linear ..............
[CV]  gamma=0.269167751462, C=15415.1615449, kernel=rbf, total=  10.8s
[CV] gamma=0.578634378499, C=55.5383891123, kernel=linear ............
[CV]  gamma=0.739678838777, C=1888.914851, kernel=linear, total=   7.4s
[CV] gamma=0.578634378499, C=55.5383891123, kernel=linear ............
[CV]  gamma=0.739678838777, C=1888.914851, kernel=linear, total=   7.1s
[CV] gamma=0.578634378499, C=55.5383891123, kernel=linear ............
[CV]  gamma=0.578634378499, C=55.5383891123, kernel=linear, total=   6.7s
[CV] gamma=0.578634378499, C=55.5383891123, kernel=linear ............
[CV]  gamma=0.578634378499, C=55.5383891123, kernel=linear, total=   6.5s
[CV] gamma=0.578634378499, C=55.5383891123, kernel=linear ............
[CV]  gamma=0.739678838777, C=1888.914851, kernel=linear, total=   6.9s
[CV] gamma=1.01172955093, C=26.7144808239, kernel=rbf ................
[CV]  gamma=0.578634378499, C=55.5383891123, kernel=linear, total=   7.0s
[CV] gamma=1.01172955093, C=26.7144808239, kernel=rbf ................
[CV]  gamma=0.578634378499, C=55.5383891123, kernel=linear, total=   6.3s
[CV] gamma=1.01172955093, C=26.7144808239, kernel=rbf ................
[CV]  gamma=0.578634378499, C=55.5383891123, kernel=linear, total=   6.7s
[CV] gamma=1.01172955093, C=26.7144808239, kernel=rbf ................
[CV] . gamma=1.01172955093, C=26.7144808239, kernel=rbf, total=  10.9s
[CV] gamma=1.01172955093, C=26.7144808239, kernel=rbf ................
[CV] . gamma=1.01172955093, C=26.7144808239, kernel=rbf, total=  10.7s
[CV] gamma=1.18913702221, C=3582.05527805, kernel=linear .............
[CV] . gamma=1.01172955093, C=26.7144808239, kernel=rbf, total=  10.7s
[CV] gamma=1.18913702221, C=3582.05527805, kernel=linear .............
[CV] . gamma=1.01172955093, C=26.7144808239, kernel=rbf, total=  10.8s
[CV] gamma=1.18913702221, C=3582.05527805, kernel=linear .............
[CV] . gamma=1.01172955093, C=26.7144808239, kernel=rbf, total=  10.6s
[CV] gamma=1.18913702221, C=3582.05527805, kernel=linear .............
[CV]  gamma=1.18913702221, C=3582.05527805, kernel=linear, total=   7.7s
[CV] gamma=1.18913702221, C=3582.05527805, kernel=linear .............
[CV]  gamma=1.18913702221, C=3582.05527805, kernel=linear, total=   7.6s
[CV] gamma=0.528281974883, C=198.700478181, kernel=linear ............
[CV]  gamma=1.18913702221, C=3582.05527805, kernel=linear, total=   7.8s
[CV] gamma=0.528281974883, C=198.700478181, kernel=linear ............
[CV]  gamma=1.18913702221, C=3582.05527805, kernel=linear, total=   7.2s
[CV] gamma=0.528281974883, C=198.700478181, kernel=linear ............
[CV]  gamma=1.18913702221, C=3582.05527805, kernel=linear, total=   7.5s
[CV] gamma=0.528281974883, C=198.700478181, kernel=linear ............
[CV]  gamma=0.528281974883, C=198.700478181, kernel=linear, total=   6.3s
[CV] gamma=0.528281974883, C=198.700478181, kernel=linear ............
[CV]  gamma=0.528281974883, C=198.700478181, kernel=linear, total=   6.5s
[CV] gamma=2.86213836765, C=129.800060414, kernel=linear .............
[CV]  gamma=0.528281974883, C=198.700478181, kernel=linear, total=   6.5s
[CV] gamma=2.86213836765, C=129.800060414, kernel=linear .............
[CV]  gamma=0.528281974883, C=198.700478181, kernel=linear, total=   6.5s
[CV] gamma=2.86213836765, C=129.800060414, kernel=linear .............
[CV]  gamma=0.528281974883, C=198.700478181, kernel=linear, total=   6.3s
[CV] gamma=2.86213836765, C=129.800060414, kernel=linear .............
[CV]  gamma=2.86213836765, C=129.800060414, kernel=linear, total=   6.7s
[CV] gamma=2.86213836765, C=129.800060414, kernel=linear .............
[CV]  gamma=2.86213836765, C=129.800060414, kernel=linear, total=   6.3s
[CV] gamma=0.1758083585, C=288.426929959, kernel=rbf .................
[CV]  gamma=2.86213836765, C=129.800060414, kernel=linear, total=   6.5s
[CV] gamma=0.1758083585, C=288.426929959, kernel=rbf .................
[CV]  gamma=2.86213836765, C=129.800060414, kernel=linear, total=   6.4s
[CV] gamma=0.1758083585, C=288.426929959, kernel=rbf .................
[CV]  gamma=2.86213836765, C=129.800060414, kernel=linear, total=   6.5s
[CV] gamma=0.1758083585, C=288.426929959, kernel=rbf .................
[CV] .. gamma=0.1758083585, C=288.426929959, kernel=rbf, total=  10.7s
[CV] gamma=0.1758083585, C=288.426929959, kernel=rbf .................
[CV] .. gamma=0.1758083585, C=288.426929959, kernel=rbf, total=  10.7s
[CV] gamma=0.350456725533, C=6287.03948943, kernel=linear ............
[CV] .. gamma=0.1758083585, C=288.426929959, kernel=rbf, total=  11.0s
[CV] gamma=0.350456725533, C=6287.03948943, kernel=linear ............
[CV] .. gamma=0.1758083585, C=288.426929959, kernel=rbf, total=  11.1s
[CV] gamma=0.350456725533, C=6287.03948943, kernel=linear ............
[CV]  gamma=0.350456725533, C=6287.03948943, kernel=linear, total=   8.2s
[CV] gamma=0.350456725533, C=6287.03948943, kernel=linear ............
[CV]  gamma=0.350456725533, C=6287.03948943, kernel=linear, total=   8.1s
[CV] gamma=0.350456725533, C=6287.03948943, kernel=linear ............
[CV]  gamma=0.350456725533, C=6287.03948943, kernel=linear, total=   8.7s
[CV] gamma=1.62796894074, C=61217.0442134, kernel=rbf ................
[CV] .. gamma=0.1758083585, C=288.426929959, kernel=rbf, total=  10.7s
[CV] gamma=1.62796894074, C=61217.0442134, kernel=rbf ................
[CV]  gamma=0.350456725533, C=6287.03948943, kernel=linear, total=   8.2s
[CV] gamma=1.62796894074, C=61217.0442134, kernel=rbf ................
[CV]  gamma=0.350456725533, C=6287.03948943, kernel=linear, total=   7.9s
[CV] gamma=1.62796894074, C=61217.0442134, kernel=rbf ................
[CV] . gamma=1.62796894074, C=61217.0442134, kernel=rbf, total=  39.0s
[CV] gamma=1.62796894074, C=61217.0442134, kernel=rbf ................
[CV] . gamma=1.62796894074, C=61217.0442134, kernel=rbf, total=  43.5s
[CV] gamma=2.14797959306, C=926.97876841, kernel=rbf .................
[CV] . gamma=1.62796894074, C=61217.0442134, kernel=rbf, total=  40.8s
[CV] gamma=2.14797959306, C=926.97876841, kernel=rbf .................
[CV] . gamma=1.62796894074, C=61217.0442134, kernel=rbf, total=  41.6s
[CV] gamma=2.14797959306, C=926.97876841, kernel=rbf .................
[CV] .. gamma=2.14797959306, C=926.97876841, kernel=rbf, total=  11.1s
[CV] gamma=2.14797959306, C=926.97876841, kernel=rbf .................
[CV] .. gamma=2.14797959306, C=926.97876841, kernel=rbf, total=  11.1s
[CV] gamma=2.14797959306, C=926.97876841, kernel=rbf .................
[CV] .. gamma=2.14797959306, C=926.97876841, kernel=rbf, total=  11.0s
[CV] gamma=2.26424264929, C=33946.1570649, kernel=linear .............
[CV] .. gamma=2.14797959306, C=926.97876841, kernel=rbf, total=  11.0s
[CV] gamma=2.26424264929, C=33946.1570649, kernel=linear .............
[CV] .. gamma=2.14797959306, C=926.97876841, kernel=rbf, total=  11.0s
[CV] gamma=2.26424264929, C=33946.1570649, kernel=linear .............
[CV] . gamma=1.62796894074, C=61217.0442134, kernel=rbf, total=  39.4s
[CV] gamma=2.26424264929, C=33946.1570649, kernel=linear .............
[CV]  gamma=2.26424264929, C=33946.1570649, kernel=linear, total=  16.3s
[CV] gamma=2.26424264929, C=33946.1570649, kernel=linear .............
[CV]  gamma=2.26424264929, C=33946.1570649, kernel=linear, total=  15.9s
[CV] gamma=0.31763590853, C=84789.8294774, kernel=linear .............
[CV]  gamma=2.26424264929, C=33946.1570649, kernel=linear, total=  14.6s
[CV] gamma=0.31763590853, C=84789.8294774, kernel=linear .............
[CV]  gamma=2.26424264929, C=33946.1570649, kernel=linear, total=  15.1s
[CV] gamma=0.31763590853, C=84789.8294774, kernel=linear .............
[CV]  gamma=2.26424264929, C=33946.1570649, kernel=linear, total=  16.9s
[CV] gamma=0.31763590853, C=84789.8294774, kernel=linear .............
[CV]  gamma=0.31763590853, C=84789.8294774, kernel=linear, total=  29.9s
[CV] gamma=0.31763590853, C=84789.8294774, kernel=linear .............
[CV]  gamma=0.31763590853, C=84789.8294774, kernel=linear, total=  40.3s
[CV]  gamma=0.31763590853, C=84789.8294774, kernel=linear, total=  33.2s
[CV]  gamma=0.31763590853, C=84789.8294774, kernel=linear, total=  44.2s
[CV]  gamma=0.31763590853, C=84789.8294774, kernel=linear, total=  24.7s
[Parallel(n_jobs=4)]: Done 250 out of 250 | elapsed: 20.9min finished
Out[110]:
RandomizedSearchCV(cv=5, error_score='raise',
          estimator=SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='auto',
  kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False),
          fit_params=None, iid=True, n_iter=50, n_jobs=4,
          param_distributions={'gamma': <scipy.stats._distn_infrastructure.rv_frozen object at 0x7f26de25b828>, 'C': <scipy.stats._distn_infrastructure.rv_frozen object at 0x7f26de25b8d0>, 'kernel': ['linear', 'rbf']},
          pre_dispatch='2*n_jobs', random_state=42, refit=True,
          return_train_score=True, scoring='neg_mean_squared_error',
          verbose=2)

The best model achieves the following score (evaluated using 5-fold cross validation):


In [111]:
negative_mse = rnd_search.best_score_
rmse = np.sqrt(-negative_mse)
rmse


Out[111]:
54767.99053704409

Now this is much closer to the performance of the RandomForestRegressor (but not quite there yet). Let's check the best hyperparameters found:


In [112]:
rnd_search.best_params_


Out[112]:
{'C': 157055.10989448498, 'gamma': 0.26497040005002437, 'kernel': 'rbf'}

This time the search found a good set of hyperparameters for the RBF kernel. Randomized search tends to find better hyperparameters than grid search in the same amount of time.

Let's look at the exponential distribution we used, with scale=1.0. Note that some samples are much larger or smaller than 1.0, but when you look at the log of the distribution, you can see that most values are actually concentrated roughly in the range of exp(-2) to exp(+2), which is about 0.1 to 7.4.


In [113]:
expon_distrib = expon(scale=1.)
samples = expon_distrib.rvs(10000, random_state=42)
plt.figure(figsize=(10, 4))
plt.subplot(121)
plt.title("Exponential distribution (scale=1.0)")
plt.hist(samples, bins=50)
plt.subplot(122)
plt.title("Log of this distribution")
plt.hist(np.log(samples), bins=50)
plt.show()


The distribution we used for C looks quite different: the scale of the samples is picked from a uniform distribution within a given range, which is why the right graph, which represents the log of the samples, looks roughly constant. This distribution is useful when you don't have a clue of what the target scale is:


In [114]:
reciprocal_distrib = reciprocal(20, 200000)
samples = reciprocal_distrib.rvs(10000, random_state=42)
plt.figure(figsize=(10, 4))
plt.subplot(121)
plt.title("Reciprocal distribution (scale=1.0)")
plt.hist(samples, bins=50)
plt.subplot(122)
plt.title("Log of this distribution")
plt.hist(np.log(samples), bins=50)
plt.show()


The reciprocal distribution is useful when you have no idea what the scale of the hyperparameter should be (indeed, as you can see on the figure on the right, all scales are equally likely, within the given range), whereas the exponential distribution is best when you know (more or less) what the scale of the hyperparameter should be.

3.

Question: Try adding a transformer in the preparation pipeline to select only the most important attributes.


In [115]:
from sklearn.base import BaseEstimator, TransformerMixin

def indices_of_top_k(arr, k):
    return np.sort(np.argpartition(np.array(arr), -k)[-k:])

class TopFeatureSelector(BaseEstimator, TransformerMixin):
    def __init__(self, feature_importances, k):
        self.feature_importances = feature_importances
        self.k = k
    def fit(self, X, y=None):
        self.feature_indices_ = indices_of_top_k(self.feature_importances, self.k)
        return self
    def transform(self, X):
        return X[:, self.feature_indices_]

Note: this feature selector assumes that you have already computed the feature importances somehow (for example using a RandomForestRegressor). You may be tempted to compute them directly in the TopFeatureSelector's fit() method, however this would likely slow down grid/randomized search since the feature importances would have to be computed for every hyperparameter combination (unless you implement some sort of cache).

Let's define the number of top features we want to keep:


In [116]:
k = 5

Now let's look for the indices of the top k features:


In [117]:
top_k_feature_indices = indices_of_top_k(feature_importances, k)
top_k_feature_indices


Out[117]:
array([ 0,  1,  7,  9, 12])

In [118]:
np.array(attributes)[top_k_feature_indices]


Out[118]:
array(['longitude', 'latitude', 'median_income', 'pop_per_hhold', 'INLAND'],
      dtype='<U18')

Let's double check that these are indeed the top k features:


In [119]:
sorted(zip(feature_importances, attributes), reverse=True)[:k]


Out[119]:
[(0.36615898061813418, 'median_income'),
 (0.16478099356159051, 'INLAND'),
 (0.10879295677551573, 'pop_per_hhold'),
 (0.073344235516012421, 'longitude'),
 (0.062909070482620302, 'latitude')]

Looking good... Now let's create a new pipeline that runs the previously defined preparation pipeline, and adds top k feature selection:


In [120]:
preparation_and_feature_selection_pipeline = Pipeline([
    ('preparation', full_pipeline),
    ('feature_selection', TopFeatureSelector(feature_importances, k))
])

In [121]:
housing_prepared_top_k_features = preparation_and_feature_selection_pipeline.fit_transform(housing)

Let's look at the features of the first 3 instances:


In [122]:
housing_prepared_top_k_features[0:3]


Out[122]:
array([[-1.15604281,  0.77194962, -0.61493744, -0.08649871,  0.        ],
       [-1.17602483,  0.6596948 ,  1.33645936, -0.03353391,  0.        ],
       [ 1.18684903, -1.34218285, -0.5320456 , -0.09240499,  0.        ]])

Now let's double check that these are indeed the top k features:


In [123]:
housing_prepared[0:3, top_k_feature_indices]


Out[123]:
array([[-1.15604281,  0.77194962, -0.61493744, -0.08649871,  0.        ],
       [-1.17602483,  0.6596948 ,  1.33645936, -0.03353391,  0.        ],
       [ 1.18684903, -1.34218285, -0.5320456 , -0.09240499,  0.        ]])

Works great! :)

4.

Question: Try creating a single pipeline that does the full data preparation plus the final prediction.


In [124]:
prepare_select_and_predict_pipeline = Pipeline([
    ('preparation', full_pipeline),
    ('feature_selection', TopFeatureSelector(feature_importances, k)),
    ('svm_reg', SVR(**rnd_search.best_params_))
])

In [125]:
prepare_select_and_predict_pipeline.fit(housing, housing_labels)


Out[125]:
Pipeline(memory=None,
     steps=[('preparation', FeatureUnion(n_jobs=1,
       transformer_list=[('num_pipeline', Pipeline(memory=None,
     steps=[('selector', DataFrameSelector(attribute_names=['longitude', 'latitude', 'housing_median_age', 'total_rooms', 'total_bedrooms', 'population', 'households', 'median_income'])), ('... gamma=0.26497040005002437, kernel='rbf', max_iter=-1, shrinking=True,
  tol=0.001, verbose=False))])

Let's try the full pipeline on a few instances:


In [126]:
some_data = housing.iloc[:4]
some_labels = housing_labels.iloc[:4]

print("Predictions:\t", prepare_select_and_predict_pipeline.predict(some_data))
print("Labels:\t\t", list(some_labels))


Predictions:	 [ 203214.28978849  371846.88152572  173295.65441612   47328.3970888 ]
Labels:		 [286600.0, 340600.0, 196900.0, 46300.0]

Well, the full pipeline seems to work fine. Of course, the predictions are not fantastic: they would be better if we used the best RandomForestRegressor that we found earlier, rather than the best SVR.

5.

Question: Automatically explore some preparation options using GridSearchCV.


In [127]:
param_grid = [
        {'preparation__num_pipeline__imputer__strategy': ['mean', 'median', 'most_frequent'],
         'feature_selection__k': [3, 4, 5, 6, 7]}
]

grid_search_prep = GridSearchCV(prepare_select_and_predict_pipeline, param_grid, cv=5,
                                scoring='neg_mean_squared_error', verbose=2, n_jobs=4)
grid_search_prep.fit(housing, housing_labels)


Fitting 5 folds for each of 15 candidates, totalling 75 fits
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=3 
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=3 
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=3 
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=3 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=3, total=   9.0s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=3 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=3, total=   9.0s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=3 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=3, total=   9.0s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=3 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=3, total=   9.0s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=3 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=3, total=   8.8s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=3 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=3, total=   8.9s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=3 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=3, total=   8.9s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=3 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=3, total=   8.9s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=3 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=3, total=   9.0s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=3 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=3, total=   8.9s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=3 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=3, total=   9.7s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=3 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=3, total=   9.9s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=4 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=3, total=   9.4s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=4 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=3, total=   9.5s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=4 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=3, total=   9.5s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=4 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=4, total=   9.6s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=4 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=4, total=   9.3s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=4 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=4, total=   9.5s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=4 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=4, total=   9.6s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=4 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=4, total=   9.5s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=4 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=4, total=   9.6s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=4 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=4, total=   9.3s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=4 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=4, total=   9.6s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=4 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=4, total=   9.5s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=4 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=4, total=   9.3s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=4 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=4, total=  10.2s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=4 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=4, total=  10.0s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=5 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=4, total=  10.1s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=5 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=4, total=  10.1s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=5 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=4, total=  10.1s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=5 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=5, total=   9.8s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=5 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=5, total=   9.9s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=5 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=5, total=   9.8s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=5 
[Parallel(n_jobs=4)]: Done  33 tasks      | elapsed:  1.9min
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=5, total=  10.0s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=5 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=5, total=   9.7s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=5 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=5, total=  10.0s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=5 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=5, total=   9.9s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=5 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=5, total=   9.8s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=5 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=5, total=   9.9s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=5 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=5, total=   9.8s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=5 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=5, total=  10.4s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=5 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=5, total=  10.6s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=6 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=5, total=  10.6s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=6 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=5, total=  10.9s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=6 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=5, total=  10.3s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=6 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=6, total=  10.0s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=6 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=6, total=  10.3s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=6 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=6, total=  10.2s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=6 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=6, total=   9.8s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=6 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=6, total=  10.3s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=6 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=6, total=  10.0s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=6 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=6, total=  10.3s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=6 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=6, total=  10.1s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=6 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=6, total=   9.8s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=6 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=6, total=  10.5s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=6 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=6, total=  10.8s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=6 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=6, total=  10.9s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=7 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=6, total=  10.7s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=7 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=6, total=  10.5s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=7 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=6, total=  11.3s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=7 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=7, total=  10.9s
[CV] preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=7 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=7, total=  10.5s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=7 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=7, total=  10.9s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=7 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=7, total=  11.1s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=7 
[CV]  preparation__num_pipeline__imputer__strategy=mean, feature_selection__k=7, total=  10.7s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=7 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=7, total=  11.5s
[CV] preparation__num_pipeline__imputer__strategy=median, feature_selection__k=7 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=7, total=  10.8s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=7 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=7, total=  10.8s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=7 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=7, total=  10.4s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=7 
[CV]  preparation__num_pipeline__imputer__strategy=median, feature_selection__k=7, total=  10.6s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=7 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=7, total=  11.9s
[CV] preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=7 
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=7, total=  11.2s
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=7, total=  11.7s
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=7, total=  11.1s
[CV]  preparation__num_pipeline__imputer__strategy=most_frequent, feature_selection__k=7, total=  11.8s
[Parallel(n_jobs=4)]: Done  75 out of  75 | elapsed:  4.4min finished
Out[127]:
GridSearchCV(cv=5, error_score='raise',
       estimator=Pipeline(memory=None,
     steps=[('preparation', FeatureUnion(n_jobs=1,
       transformer_list=[('num_pipeline', Pipeline(memory=None,
     steps=[('selector', DataFrameSelector(attribute_names=['longitude', 'latitude', 'housing_median_age', 'total_rooms', 'total_bedrooms', 'population', 'households', 'median_income'])), ('... gamma=0.26497040005002437, kernel='rbf', max_iter=-1, shrinking=True,
  tol=0.001, verbose=False))]),
       fit_params=None, iid=True, n_jobs=4,
       param_grid=[{'preparation__num_pipeline__imputer__strategy': ['mean', 'median', 'most_frequent'], 'feature_selection__k': [3, 4, 5, 6, 7]}],
       pre_dispatch='2*n_jobs', refit=True, return_train_score=True,
       scoring='neg_mean_squared_error', verbose=2)

In [128]:
grid_search_prep.best_params_


Out[128]:
{'feature_selection__k': 7,
 'preparation__num_pipeline__imputer__strategy': 'median'}

Great! It seems that we had the right imputer strategy (mean), and apparently only the top 7 features are useful (out of 9), the last 2 seem to just add some noise.


In [129]:
housing.shape


Out[129]:
(16512, 9)

Congratulations! You already know quite a lot about Machine Learning. :)