## Regression

### Introduction

Most of the other chapters of our machine learning tutorial with Python are dealing with classification problems. Classification is the task of predicting a discrete class label, whereas regression is the task of predicting a continuous quantity. Some algorithms can be used for both classification and regression, if we apply small modifications: Decision trees and artificial neural networks.

The topics of this chapter will be regression, but what are typcial regression problems?

Typcial regression problems are for example the prediction of

• house prices
• car prices
• exchange rates
• the price of shares

This chapter of our regression tutorial will start with the LinearRegression class of sklearn.

Yet, the bulk of this chapter will deal with the MLPRegressor model from sklearn.neural network. It is a Neural Network model for regression problems. The name is an acronym for multi-layer perceptron regression system. MLP or multi-layer perceptron is an artificial neural network (ANN), which consists of a minimum of three layers:

• an input layer,
• one or more hidden layers and
• an output layer.

Yet, before we start with the MLPRegressor, we will have a look at simpler models.

### Simple Example with Linear Regression

LinearRegression from the sklearnmodule is an ordinary least squares Linear Regression.

LinearRegression fits a linear model with coefficients $w = (w_1, …, w_p)$ to minimize the residual sum of squares between the observed targets in the dataset, and the targets predicted by the linear approximation.

Linear regression is about finding a line of the form

$$y= a \cdot x + b$$

where x is the explanatory variable and y is the dependent variable. The slope of the line is a, and b is the intercept, which corresponds to the value of y if y is equal to 0.

Our first example uses the data of the file regr_example_data1.csv. We will first load the data and separate the data in the X and y values:

import numpy as np

data[:4]

Output: :
array([[0. , 2.8],
[0.2, 4.8],
[0.4, 4.4],
[0.6, 5.6]])
X = data[:, 0]
y = data[:, 1]


The data was artificially created. When we created the data, we modified the data created by $4.5 \cdot x + 2.8$. This has been the idealdata and we name it in the following plot as y_opt:

    import matplotlib.pyplot as plt
fig, ax = plt.subplots()

y_opt = 4.5 * X + 2.8
ax.scatter(X, y)
ax.plot(X, y_opt)

Output: :
[<matplotlib.lines.Line2D at 0x7f0b2003f690>]

We will use now LinearRegression from sklearn to calculate a linear approximation. Of course, we pretend not to know the equation $4.5 \cdot x + 2.8$ with which we had created the test data. We fit the values X to y by using the fit function:

from sklearn.linear_model import LinearRegression
X = X.reshape(X.shape[0], 1)
reg = LinearRegression().fit(X, y)


We can use the method predict which uses the linear model to predict result. We can apply it to the X values to see, if it works well:

y_predict = reg.predict(X)
y_predict[-10:]

Output: :
array([39.6799633 , 40.60471101, 41.52945872, 42.45420642, 43.37895413,
44.30370183, 45.22844954, 46.15319725, 47.07794495, 48.00269266])
help(reg.predict)

Help on method predict in module sklearn.linear_model._base:

predict(X) method of sklearn.linear_model._base.LinearRegression instance
Predict using the linear model.

Parameters
----------
X : array_like or sparse matrix, shape (n_samples, n_features)
Samples.

Returns
-------
C : array, shape (n_samples,)
Returns predicted values.



We get also the value for the intercept and the coefficient:

reg.intercept_, reg.coef_

Output: :
(1.7653073394495422, array([4.62373853]))

This means, we can write the straight line equation calculated by the regression:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

y_opt = X * 4.5 + 2.8
y_reg = X *reg.coef_[0] + reg.intercept_
ax.scatter(X, y)
ax.scatter(X, y_predict, color="orange")
ax.plot(X, y_opt)
ax.plot(X, y_reg, color="green")

Output: :
[<matplotlib.lines.Line2D at 0x7f0afca1e990>]

### Linear Regression with scipy.stats.linregress

We can also calculate linear regression with the linregress function from the scipy.stats module.

We can use again the data from before and use now linregress:

# we have to reshape X from two dimensional to one-dimensional
X = X.reshape(-1)

from scipy.stats import linregress
slope, intercept, r_value, p_value, std_err = linregress(X, y)


We can visualize the data and the results:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

ax.scatter(X, y)
ax.plot(X, X*slope + intercept)

Output: :
[<matplotlib.lines.Line2D at 0x7f0afc995b10>]

## Another Example

We will use some simple artificial data, which gives the price of a car (same model and type) and the age. The file is called car_prices.txt:

from scipy import stats
import numpy as np

years = data[:,0]
prices = data[:,1]
slope, intercept, r_value, p_value, std_err = stats.linregress(years, prices)

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

ax.scatter(years, prices)
ax.plot(years, years*slope + intercept)

Output: :
[<matplotlib.lines.Line2D at 0x7f0afc46cd90>]
import numpy as np
from sklearn.model_selection import train_test_split

from sklearn import datasets, linear_model, metrics

data_sets = train_test_split(years.reshape(-1,1),
prices.reshape(-1,1),
train_size=0.8,
test_size=0.2,
random_state=42)

train_data, test_data, train_targets, test_targets = data_sets
train_data[-5:], train_targets[-5:]

Output: :
(array([[10.],
[ 4.],
[ 7.],
[ 7.],
[ 3.]]),
array([[    0.  ],
[18432.36],
[ 7829.35],
[ 9042.8 ],
[18381.84]]))
regr = linear_model.LinearRegression()
regr.fit(train_data, train_targets)
regr.predict(train_data)

Output: :
array([[25850.95101352],
[17234.9152252 ],
[ 8618.87943687],
...,
[ 8618.87943687],
[ 8618.87943687],
[20106.92715464]])
train_targets[:20]

Output: :
array([[23978.3 ],
[17302.83],
[ 8992.9 ],
[11226.09],
[24430.87],
[ 6158.63],
[    0.  ],
[ 5421.7 ],
[14040.26],
[20246.37],
[    0.  ],
[    0.  ],
[27107.66],
[ 3108.44],
[ 6203.36],
[    0.  ],
[18620.77],
[    0.  ],
[ 5398.64],
[14905.14]])

### Non-linear Example

We use a file car_prices.txt, where each line contains a sample with the age and the value of a car:

8 5332.82
3 12079.80
9 2754.36
4 14382.52
from scipy import stats
import numpy as np

years = data[:, 0]
prices = data[:, 1]

data_sets = train_test_split(years.reshape(-1,1),
prices,
train_size=0.8,
test_size=0.2,
random_state=42)
print(years.shape)
train_data, test_data, train_targets, test_targets = data_sets

(4999,)

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

ax.scatter(years, prices)

Output: :
<matplotlib.collections.PathCollection at 0x7f0afc46ce90>
from sklearn.neural_network import MLPRegressor

clf = MLPRegressor(solver='lbfgs',
alpha=1e-5,     # used for regularization, ovoiding overfitting by penalizing large magnitudes
hidden_layer_sizes=(5, 2), random_state=24)
clf.fit(train_data, train_targets)
res = clf.predict(train_data)

res

Output: :
array([ 8769.29762279,  2866.74419242,  3994.62057336, ...,
7378.24971619, 22411.61185359,  5122.49695431])

An explanation of the parameters of MLPRegressor comes further down in this chapter.

predictions = clf.predict(train_data)
predictions[:10]

Output: :
array([ 8769.29762279,  2866.74419242,  3994.62057336, 15590.45473819,
6250.37333525,  3994.62057336, 19001.03329589,  2866.74419242,
8769.29762279,  6250.37333525])
train_targets[:10]

Output: :
array([ 9416.01,  4320.32,  4813.04, 15306.8 ,  5293.85,  3922.08,
19262.51,  3427.23,  9502.91,  8615.61])

### Create Regression Test Data

import matplotlib.pyplot as plt
from sklearn.datasets import make_regression
data, targets = make_regression(n_samples=100,
n_features=1,
noise=0.1)

data[:5]

Output: :
array([[ 0.68516439],
[-0.95668177],
[ 0.99794127],
[ 0.46459311],
[-0.40262384]])
from sklearn import datasets

plt.scatter(data, targets)
plt.show()

import matplotlib.pyplot as plt
from sklearn.datasets import make_regression
data, targets = make_regression(n_samples=100,
n_features=3,
#shuffle=True,
noise=0.1)

data[:5]

Output: :
array([[ 1.06722429, -0.08150314, -0.55725103],
[ 0.41349323,  0.08621112,  2.69978003],
[ 0.69882357,  0.42972911,  0.62047607],
[-0.02673955, -0.79328404, -0.21512227],
[-0.11595927, -0.55070498,  0.75305196]])
import pandas as pd
data_df = pd.DataFrame(data)
data_df.insert(len(data_df.columns),
column="result",
value=targets)
data_df.columns = "blue", "green", "red", "result"


Output: :
blue green red result
0 1.067224 -0.081503 -0.557251 -54.052990
1 0.413493 0.086211 2.699780 260.558605
2 0.698824 0.429729 0.620476 85.700976
3 -0.026740 -0.793284 -0.215122 -67.099549
4 -0.115959 -0.550705 0.753052 38.066860
from sklearn.model_selection import train_test_split
data_sets = train_test_split(data,
targets,
test_size=0.30,
random_state=42)

data_train, data_test, targets_train, targets_test = data_sets

clf = MLPRegressor(solver='lbfgs',        #  ‘lbfgs’, ‘sgd’, ‘adam’ (default)
alpha=1e-5,            # used for regularization, ovoiding overfitting by penalizing large magnitudes
hidden_layer_sizes=(3, 1),
activation='logistic', # ‘identity’, ‘logistic’, ‘tanh’, ‘relu’ (default)
max_iter=10000,
random_state=42)
clf.fit(data_train, targets_train)
clf.predict(data_train)

Output: :
array([ 94.56833304,  94.56833304, -75.58254494, -75.58254494,
-75.58254494, -75.58254494, -75.58254494,  94.56833304,
94.56833304, -75.58254494,  94.56833304, -75.58254494,
94.56833304, -75.58254494,  94.56833304,  94.56833304,
-75.58254494, -75.58254494,  94.56833304,  94.56833304,
-75.58254494, -75.58254494,  94.56833304,  94.56833304,
94.56833304,  94.56833304, -75.58254494, -75.58254494,
94.56833304,  94.56833304,  94.56833304,  94.56833304,
-75.58254494, -75.58254494, -75.58254494,  94.56833304,
-75.58254494, -75.58254494, -75.58254494,  94.56833304,
-75.58254494, -75.58254494, -75.58254494,  94.56833304,
-75.58254494,  94.56833304, -75.58254494,  94.56833304,
-75.58254494,  94.56833304, -75.58254494,  94.56833304,
-75.58254494, -75.58254494,  94.56833304,  94.56833304,
94.56833304,  94.56833304,  94.56833304,  94.56833304,
94.56833304,  94.56833304, -75.58254494, -75.58254494,
-75.58254494, -75.58254494, -75.58254494,  94.56833304,
-75.58254494,  94.56833304])

### California-Housing-Dataset

Now, we will use a dataset derived from the 1990 U.S. census. The data is solely from California. The data is organized as one row per census block group. A block group is the smallest geographical unit for which the U.S. Census Bureau publishes sample data (a block group typically has a population of 600 to 3,000 people).

The dataset contains 20640 samples. Each sample with 8 numeric, predictive attributes and the target value.

The attributes are:

• MedInc: median income in block
• HouseAge: median house age in block
• AveRooms: average number of rooms
• AveBedrms: average number of bedrooms
• Population: block population
• AveOccup: average house occupancy
• Latitude: house block latitude
• Longitude: house block longitude

This dataset was obtained from the StatLib repository: http://lib.stat.cmu.edu/datasets/

earn.datasets.fetch_california_housing

We download the California housing dataset in the following Python code lines:

from sklearn.datasets import fetch_california_housing
dataset = fetch_california_housing()


Let us look at the feauture names:

feature_names = dataset['feature_names']
print("Feature names: {}\n".format(feature_names))

Feature names: ['MedInc', 'HouseAge', 'AveRooms', 'AveBedrms', 'Population', 'AveOccup', 'Latitude', 'Longitude']



As usual the data contains the data for the samples. Each line contains eight float values, which correspond to the features:

print("number of samples in the file (number of rows): ", dataset.data.shape[0])
print("number of features per row (columns): ", dataset.data.shape[1])
dataset.data[:4]

number of samples in the file (number of rows):  20640
number of features per row (columns):  8

Output: :
array([[ 8.32520000e+00,  4.10000000e+01,  6.98412698e+00,
1.02380952e+00,  3.22000000e+02,  2.55555556e+00,
3.78800000e+01, -1.22230000e+02],
[ 8.30140000e+00,  2.10000000e+01,  6.23813708e+00,
9.71880492e-01,  2.40100000e+03,  2.10984183e+00,
3.78600000e+01, -1.22220000e+02],
[ 7.25740000e+00,  5.20000000e+01,  8.28813559e+00,
1.07344633e+00,  4.96000000e+02,  2.80225989e+00,
3.78500000e+01, -1.22240000e+02],
[ 5.64310000e+00,  5.20000000e+01,  5.81735160e+00,
1.07305936e+00,  5.58000000e+02,  2.54794521e+00,
3.78500000e+01, -1.22250000e+02]])

### A Closer Look at the California Housing Data

As we have already mentioned, each sample (row) of the data corresponds to a block (district). A clock contains an unspecified number of houses. The second column (index == 1) contains the median house age of the block. We can filter the data by looking at the blocks with an avarage age less than 10 years:

n = 10   # median house age in a block
# data where the houses in the block are less than 10 years old:
dataset.data[dataset.data[:,1]<n]

Output: :
array([[   2.5625    ,    2.        ,    2.77192982, ...,    1.64912281,
37.82      , -122.29      ],
[   7.611     ,    5.        ,    6.85577631, ...,    2.73252391,
37.72      , -122.24      ],
[   7.0568    ,    5.        ,    7.0234375 , ...,    3.39453125,
37.73      , -122.06      ],
...,
[   3.4186    ,    6.        ,    5.66256158, ...,    3.15394089,
38.53      , -121.99      ],
[   2.0474    ,    5.        ,    4.37813212, ...,    2.13667426,
39.15      , -121.59      ],
[   3.        ,    5.        ,    6.06779661, ...,    2.86440678,
39.13      , -121.32      ]])

The target variable is the median house value for California districts:

dataset.target

Output: :
array([4.526, 3.585, 3.521, ..., 0.923, 0.847, 0.894])

### Visualizing the Data

We can get a better view on the data by turning it into a Pandas DataFrame:

import pandas as pd
data_df = pd.DataFrame(dataset.data)

data_df.columns = ["MedInc", "HouseAge", "AveRooms",
"AveBedrms", "Population", "AveOccup",
"Latitude", "Longitude"]


Output: :
MedInc HouseAge AveRooms AveBedrms Population AveOccup Latitude Longitude
0 8.3252 41.0 6.984127 1.023810 322.0 2.555556 37.88 -122.23
1 8.3014 21.0 6.238137 0.971880 2401.0 2.109842 37.86 -122.22
2 7.2574 52.0 8.288136 1.073446 496.0 2.802260 37.85 -122.24
3 5.6431 52.0 5.817352 1.073059 558.0 2.547945 37.85 -122.25
4 3.8462 52.0 6.281853 1.081081 565.0 2.181467 37.85 -122.25

We will insert now the average house value to our DataFrame:

data_df.insert(loc=len(data_df.columns),
column="AvePropVal",
value=dataset.target.reshape(-1,1))

data_df[:5]

Output: :
MedInc HouseAge AveRooms AveBedrms Population AveOccup Latitude Longitude AvePropVal
0 8.3252 41.0 6.984127 1.023810 322.0 2.555556 37.88 -122.23 4.526
1 8.3014 21.0 6.238137 0.971880 2401.0 2.109842 37.86 -122.22 3.585
2 7.2574 52.0 8.288136 1.073446 496.0 2.802260 37.85 -122.24 3.521
3 5.6431 52.0 5.817352 1.073059 558.0 2.547945 37.85 -122.25 3.413
4 3.8462 52.0 6.281853 1.081081 565.0 2.181467 37.85 -122.25 3.422

There are blocks where the average number of rooms is more than a hundred. Not very likely? At least the average number of bedrooms correlates:

data_df[data_df['AveRooms']>100]

Output: :
MedInc HouseAge AveRooms AveBedrms Population AveOccup Latitude Longitude AvePropVal
1914 1.875 33.0 141.909091 25.636364 30.0 2.727273 38.91 -120.10 5.00001
1979 4.625 34.0 132.533333 34.066667 36.0 2.400000 38.80 -120.08 1.62500

We can assume that the samples (districts) where the average number of rooms is larger than 11 must be rubbish. So we have a look at the data where the number is less than 12:

no_of_districts_before_cleansing = len(data_df)
data_df = data_df[data_df['AveRooms']<12]

print(no_of_districts_before_cleansing)
print("number of removed districts: ", no_of_districts_before_cleansing - len(data_df))

20640
number of removed districts:  145


Now, we can have a look at the historgram of the cleansed data:

data_df['AveRooms'].hist()

Output: :
<matplotlib.axes._subplots.AxesSubplot at 0x7f0af81ddd10>

We can have a similar reasoning for the number of people living in a house. Let's check the number of districts, where the AveOccup is greater than 13:

data_df[data_df['AveOccup']>13]

Output: :
MedInc HouseAge AveRooms AveBedrms Population AveOccup Latitude Longitude AvePropVal
1039 4.3029 16.0 6.090592 1.121951 4930.0 17.177700 38.42 -120.97 1.21900
3364 5.5179 36.0 5.142857 1.142857 4198.0 599.714286 40.41 -120.51 0.67500
4479 3.5769 41.0 4.940000 1.100000 925.0 18.500000 34.07 -118.16 1.35700
5986 1.8750 52.0 4.500000 1.206349 2688.0 21.333333 34.10 -117.71 2.12500
7164 2.1845 36.0 4.812155 1.110497 2862.0 15.812155 34.06 -118.17 1.23800
8874 9.3370 52.0 7.285714 1.214286 1154.0 41.214286 34.06 -118.45 5.00001
9172 4.2391 5.0 5.123810 0.933333 8733.0 83.171429 34.47 -118.59 1.54600
12104 1.6250 8.0 7.600000 0.950000 1275.0 63.750000 33.97 -117.33 1.62500
13034 6.1359 52.0 8.275862 1.517241 6675.0 230.172414 38.69 -121.15 2.25000
13366 4.2578 36.0 5.258824 1.117647 2886.0 33.952941 33.94 -117.63 1.83300
14756 2.5625 23.0 4.589091 1.069091 5176.0 18.821818 32.56 -116.97 1.53300
15790 4.3958 52.0 2.117647 0.926471 1061.0 15.602941 37.77 -122.40 2.25000
16420 5.7485 26.0 5.366667 0.900000 1542.0 51.400000 37.89 -121.29 1.62500
16643 1.0298 20.0 2.398467 0.812261 3574.0 13.693487 35.32 -120.65 3.00000
16669 4.2639 46.0 9.076923 1.307692 6532.0 502.461538 35.32 -120.70 3.50000
16672 1.6563 16.0 5.125000 0.937500 542.0 16.937500 35.34 -120.69 0.42500
18520 2.4732 14.0 4.445402 1.074713 4731.0 13.594828 37.00 -122.06 1.31300
19006 10.2264 45.0 3.166667 0.833333 7460.0 1243.333333 38.32 -121.98 1.37500
19435 0.5360 16.0 2.111111 2.111111 166.0 18.444444 37.67 -121.04 1.62500
19524 0.9573 19.0 2.951220 1.000000 658.0 16.048780 37.64 -121.00 1.62500
20121 3.1719 8.0 6.979221 1.285714 5087.0 13.212987 37.79 -120.45 1.15400
20352 4.0208 8.0 2.500000 0.625000 309.0 19.312500 34.22 -119.09 0.52500
data_df['AveOccup'][data_df['AveOccup']<=13].hist()

Output: :
<matplotlib.axes._subplots.AxesSubplot at 0x7f0af815e790>

Let's have a look at all the feature histograms:

data_df.hist(bins=50, figsize=(15,15))
plt.show()

from pandas.plotting import scatter_matrix
attributes = ['HouseAge', 'MedInc',
'AveRooms', 'AveOccup']
scatter_matrix(data_df[attributes], figsize=(12,8));

data_df.insert(len(data_df.columns),
column="AveHouseValue",
value=dataset.target)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-44-12f6f1cffd85> in <module>
1 data_df.insert(len(data_df.columns),
2                column="AveHouseValue",
----> 3                value=dataset.target)

/data/anaconda/lib/python3.7/site-packages/pandas/core/frame.py in insert(self, loc, column, value, allow_duplicates)
3493         """
3494         self._ensure_valid_index(value)
-> 3495         value = self._sanitize_column(column, value, broadcast=False)
3496         self._data.insert(loc, column, value, allow_duplicates=allow_duplicates)
3497

/data/anaconda/lib/python3.7/site-packages/pandas/core/frame.py in _sanitize_column(self, key, value, broadcast)
3634
3635             # turn me into an ndarray
-> 3636             value = sanitize_index(value, self.index, copy=False)
3637             if not isinstance(value, (np.ndarray, Index)):
3638                 if isinstance(value, list) and len(value) > 0:

/data/anaconda/lib/python3.7/site-packages/pandas/core/internals/construction.py in sanitize_index(data, index, copy)
609
610     if len(data) != len(index):
--> 611         raise ValueError("Length of values does not match length of index")
612
613     if isinstance(data, ABCIndexClass) and not copy:

ValueError: Length of values does not match length of index
In [ ]:
data_df.plot(kind='scatter', x='Population', y='AveHouseValue',
alpha=0.1, figsize=(8,5))


### Splitting the Data

In [ ]:
from sklearn.model_selection import train_test_split
data_sets = train_test_split(dataset.data,
dataset.target,
test_size=0.30,
random_state=42)

data_train, data_test, targets_train, targets_test = data_sets


### A First Classifier

In [ ]:
clf = MLPRegressor(solver='lbfgs',        #  ‘lbfgs’, ‘sgd’, ‘adam’ (default)
alpha=1e-5,            # used for regularization, ovoiding overfitting by penalizing large magnitudes
hidden_layer_sizes=(10, 2),
activation='logistic', # ‘identity’, ‘logistic’, ‘tanh’, ‘relu’ (default)
max_iter=10000,
random_state=42)
clf.fit(data_train, targets_train)
clf.predict(data_train)


Parameters:

• activation{‘identity’, ‘logistic’, ‘tanh’, ‘relu’}, default=’relu’ Activation function for the hidden layer.

• ‘identity’, no-op activation, useful to implement linear bottleneck, returns f(x) = x
• ‘logistic’, the logistic sigmoid function, returns f(x) = 1 / (1 + exp(-x)).
• ‘tanh’, the hyperbolic tan function, returns f(x) = tanh(x).
• ‘relu’, the rectified linear unit function, returns f(x) = max(0, x)

• ‘lbfgs’ is an optimizer in the family of quasi-Newton methods.
• ‘sgd’ refers to stochastic gradient descent.
• ‘adam’ refers to a stochastic gradient-based optimizer proposed by Kingma, Diederik, and Jimmy Ba

Note: The default solver ‘adam’ works pretty well on relatively large datasets (with thousands of training samples or more) in terms of both training time and validation score. For small datasets, however, ‘lbfgs’ can converge faster and perform better.

### Data Preprocessing

#### Removing Outliers

In [ ]:
max_ave_rooms = 12
# column with index 2 corresponds to average number of rooms
shape = dataset.data.shape
cleansed_shape = dataset.data[dataset.data[:,2] <= max_ave_rooms].shape
print(shape, cleansed_shape)
n_outliers = shape[0]-cleansed_shape[0]
print(f"Number of outliers, more than {max_ave_rooms} bedrooms: {n_outliers}")


Let us remove all data with an average number of rooms greater than max_ave_rooms:

In [ ]:
x = dataset.data[:,2] <= max_ave_rooms  # Boolean array
data = dataset.data[x]
targets = dataset.target[x]
data.shape, targets.shape

In [ ]:
data.shape


Before we go on like this, let us have a look at the statistics for each feature:

In [ ]:
data_df.describe()

In [ ]:
# "MedInc", "HouseAge", "AveRooms", "AveBedrms", "Population", "AveOccup", "Latitude", "Longitude"

x = data[:,5] <= 10   # AveOccup
data = data[x]
targets = targets[x]
data.shape, targets.shape

In [ ]:
avebedrms_index = 3

np.min(data[:,3]), np.max(data[:,3])

In [ ]:
np.max(dataset.data[:3])


The outliers for this feature have already disappeared due to the previous cleaning actions.

#### Another try on classification:

In [ ]:
from sklearn.model_selection import train_test_split
data_sets = train_test_split(data,
targets,
test_size=0.30,
random_state=42)

data_train, data_test, targets_train, targets_test = data_sets

In [ ]:
from sklearn.model_selection import train_test_split
data_sets = train_test_split(dataset.data,
dataset.target,
test_size=0.30,
random_state=42)

data_train2, data_test2, targets_train2, targets_test2 = data_sets

In [ ]:
data_train.shape, data_train2.shape

In [ ]:
clf = MLPRegressor(solver='lbfgs',        #  ‘lbfgs’, ‘sgd’, ‘adam’ (default)
alpha=1e-5,            # used for regularization, ovoiding overfitting by penalizing large magnitudes
hidden_layer_sizes=(10, 2),
activation='logistic', # ‘identity’, ‘logistic’, ‘tanh’, ‘relu’ (default)
max_iter=10000,
random_state=42)
clf.fit(data_train, targets_train)
print(clf.score(data_train, targets_train))
print(clf.score(data_test, targets_test))


We can see that classifying with uncleansed data gives us slightly worse results:

In [ ]:
clf = MLPRegressor(solver='lbfgs',        #  ‘lbfgs’, ‘sgd’, ‘adam’ (default)
alpha=1e-5,            # used for regularization, ovoiding overfitting by penalizing large magnitudes
hidden_layer_sizes=(10, 2),
activation='logistic', # ‘identity’, ‘logistic’, ‘tanh’, ‘relu’ (default)
max_iter=10000,
random_state=42)
clf.fit(data_train, targets_train)
print(clf.score(data_train2, targets_train2))
print(clf.score(data_test2, targets_test2))


#### Scaling the Data

As the data varies a lot in magnitude, we can or should scale the data:

In [ ]:
from sklearn import preprocessing

data_scaled = preprocessing.scale(data)
data_scaled.shape

In [ ]:
data_scaled[:5]

In [ ]:
data[:5]


#### Generating Polynomial Features

In [ ]:
from sklearn.preprocessing import PolynomialFeatures


Polynomial Features:

PolynomialFeatures does the following: Generate a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree. For example, if an input sample is two dimensional and of the form $[a, b]$, the degree-2 polynomial features are $[1, a, b, a^2, ab, b^2]$.

In [ ]:
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
X = np.arange(6).reshape(3, 2)
X

In [ ]:
poly = PolynomialFeatures(degree=2)
poly.fit_transform(X)


If the interaction_only parameter is set to True, only interaction features are produced: features that are products of at most degree distinct input features (so not x[1] 2, x[0] * x[2] 3, etc.).

In [ ]:
poly = PolynomialFeatures(interaction_only=True)
poly.fit_transform(X)


Let's get back to our housing data:

In [ ]:
pft = PolynomialFeatures(degree=2)
data_poly = pft.fit_transform(data_scaled)
data_poly
data_poly


### Splitting our Dataset

In [ ]:
from sklearn.model_selection import train_test_split
data_sets = train_test_split(data_poly,
targets,
test_size=0.30,
random_state=42)

data_train, data_test, targets_train, targets_test = data_sets


### Creating and Training a Classifier

In [ ]:
clf = MLPRegressor(solver='lbfgs',   #  ‘lbfgs’, ‘sgd’, ‘adam’ (default)
alpha=1e-5,     # used for regularization, ovoiding overfitting by penalizing large magnitudes
hidden_layer_sizes=(5, 2),
activation='relu', # ‘identity’, ‘logistic’, ‘tanh’, ‘relu’ (default)
max_iter=10000,
early_stopping=True,
random_state=42)
clf.fit(data_train, targets_train)
print(clf.score(data_train, targets_train))
print(clf.score(data_test, targets_test))

In [ ]:
clf.predict(data_train)

In [ ]:
targets_train
`