forked from INRIA/scikit-learn-mooc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_models_sol_02.py
More file actions
123 lines (101 loc) · 3.25 KB
/
Copy pathlinear_models_sol_02.py
File metadata and controls
123 lines (101 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# ---
# jupyter:
# kernelspec:
# display_name: Python 3
# name: python3
# ---
# %% [markdown]
# # 📃 Solution for Exercise M4.02
#
# The goal of this exercise is to build an intuition on what will be the
# parameters' values of a linear model when the link between the data and the
# target is non-linear.
#
# First, we will generate such non-linear data.
#
# ```{tip}
# `np.random.RandomState` allows to create a random number generator which can
# be later used to get deterministic results.
# ```
# %%
import numpy as np
# Set the seed for reproduction
rng = np.random.RandomState(0)
# Generate data
n_sample = 100
data_max, data_min = 1.4, -1.4
len_data = (data_max - data_min)
data = rng.rand(n_sample) * len_data - len_data / 2
noise = rng.randn(n_sample) * .3
target = data ** 3 - 0.5 * data ** 2 + noise
# %% [markdown]
# ```{note}
# To ease the plotting, we will create a Pandas dataframe containing the data
# and target
# ```
# %%
import pandas as pd
full_data = pd.DataFrame({"data": data, "target": target})
# %%
import seaborn as sns
_ = sns.scatterplot(data=full_data, x="data", y="target", color="black",
alpha=0.5)
# %% [markdown]
# We observe that the link between the data `data` and vector `target` is
# non-linear. For instance, `data` could represent the years of
# experience (normalized) and `target` the salary (normalized). Therefore, the
# problem here would be to infer the salary given the years of experience.
#
# Using the function `f` defined below, find both the `weight` and the
# `intercept` that you think will lead to a good linear model. Plot both the
# data and the predictions of this model.
# %%
def f(data, weight=0, intercept=0):
target_predict = weight * data + intercept
return target_predict
# %%
# solution
predictions = f(data, weight=1.2, intercept=-0.2)
# %% tags=["solution"]
ax = sns.scatterplot(data=full_data, x="data", y="target", color="black",
alpha=0.5)
_ = ax.plot(data, predictions)
# %% [markdown]
# Compute the mean squared error for this model
# %%
# solution
from sklearn.metrics import mean_squared_error
error = mean_squared_error(target, f(data, weight=1.2, intercept=-0.2))
print(f"The MSE is {error}")
# %% [markdown]
# Train a linear regression model on this dataset.
#
# ```{warning}
# In scikit-learn, by convention `data` (also called `X` in the scikit-learn
# documentation) should be a 2D matrix of shape `(n_samples, n_features)`.
# If `data` is a 1D vector, you need to reshape it into a matrix with a
# single column if the vector represents a feature or a single row if the
# vector represents a sample.
# ```
# %%
from sklearn.linear_model import LinearRegression
# solution
linear_regression = LinearRegression()
data_2d = data.reshape(-1, 1)
linear_regression.fit(data_2d, target)
# %% [markdown]
# Compute predictions from the linear regression model and plot both the data
# and the predictions.
# %%
# solution
predictions = linear_regression.predict(data_2d)
# %% tags=["solution"]
ax = sns.scatterplot(data=full_data, x="data", y="target", color="black",
alpha=0.5)
_ = ax.plot(data, predictions)
# %% [markdown]
# Compute the mean squared error
# %%
# solution
error = mean_squared_error(target, predictions)
print(f"The MSE is {error}")