forked from INRIA/scikit-learn-mooc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathensemble_sol_02.py
More file actions
97 lines (79 loc) · 2.98 KB
/
Copy pathensemble_sol_02.py
File metadata and controls
97 lines (79 loc) · 2.98 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
# ---
# jupyter:
# kernelspec:
# display_name: Python 3
# name: python3
# ---
# %% [markdown]
# # 📃 Solution for Exercise M6.02
#
# The aim of this exercise it to explore some attributes available in
# scikit-learn's random forest.
#
# First, we will fit the penguins regression dataset.
# %%
import pandas as pd
from sklearn.model_selection import train_test_split
penguins = pd.read_csv("../datasets/penguins_regression.csv")
feature_name = "Flipper Length (mm)"
target_name = "Body Mass (g)"
data, target = penguins[[feature_name]], penguins[target_name]
data_train, data_test, target_train, target_test = train_test_split(
data, target, random_state=0)
# %% [markdown]
# ```{note}
# If you want a deeper overview regarding this dataset, you can refer to the
# Appendix - Datasets description section at the end of this MOOC.
# ```
# %% [markdown]
# Create a random forest containing three trees. Train the forest and
# check the generalization performance on the testing set in terms of mean
# absolute error.
# %%
# solution
from sklearn.metrics import mean_absolute_error
from sklearn.ensemble import RandomForestRegressor
forest = RandomForestRegressor(n_estimators=3)
forest.fit(data_train, target_train)
target_predicted = forest.predict(data_test)
print(f"Mean absolute error: "
f"{mean_absolute_error(target_test, target_predicted):.3f} grams")
# %% [markdown]
# The next steps of this exercise are to:
#
# - create a new dataset containing the penguins with a flipper length
# between 170 mm and 230 mm;
# - plot the training data using a scatter plot;
# - plot the decision of each individual tree by predicting on the newly
# created dataset;
# - plot the decision of the random forest using this newly created dataset.
# ```{tip}
# The trees contained in the forest that you created can be accessed
# with the attribute `estimators_`.
# ```
# %% [markdown] tags=["solution"]
# In a first cell, we will collect all the required predictions from the
# different trees and forest.
# %%
# solution
import numpy as np
data_range = pd.DataFrame(np.linspace(170, 235, num=300),
columns=data.columns)
tree_predictions = []
for tree in forest.estimators_:
# we convert `data_range` into a NumPy array to avoid a warning raised in scikit-learn
tree_predictions.append(tree.predict(data_range.to_numpy()))
forest_predictions = forest.predict(data_range)
# %% [markdown] tags=["solution"]
# Now, we can plot the predictions that we collected.
# %% tags=["solution"]
import matplotlib.pyplot as plt
import seaborn as sns
sns.scatterplot(data=penguins, x=feature_name, y=target_name,
color="black", alpha=0.5)
# plot tree predictions
for tree_idx, predictions in enumerate(tree_predictions):
plt.plot(data_range[feature_name], predictions, label=f"Tree #{tree_idx}",
linestyle="--", alpha=0.8)
plt.plot(data_range[feature_name], forest_predictions, label=f"Random forest")
_ = plt.legend(bbox_to_anchor=(1.05, 0.8), loc="upper left")