Skip to content

Commit c08eab4

Browse files
committed
Make documentation examples self-contained
1 parent f57e87b commit c08eab4

3 files changed

Lines changed: 57 additions & 70 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ ci = fci.random_forest_error(
6262
## Examples
6363

6464
The examples (gallery below) demonstrates the package functionality with random forest classifiers and regression models.
65-
The regression example uses a popular UCI Machine Learning data set on cars while the classifier example simulates how to add measurements of uncertainty to tasks like predicting spam emails.
65+
The regression examples use scikit-learn's bundled diabetes dataset, while the
66+
classifier example simulates how to add measurements of uncertainty to tasks
67+
like predicting spam emails. Keeping the regression data bundled makes
68+
documentation builds reproducible without relying on an external data service.
6669

6770
[Examples gallery](http://contrib.scikit-learn.org/forest-confidence-interval/auto_examples/index.html)
6871

examples/plot_mpg.py

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,49 @@
66
This example demonstrates using `forestci` to calculate the error bars of
77
the predictions of a :class:`sklearn.ensemble.RandomForestRegressor` object.
88
9-
The data used here are a classical machine learning data-set, describing
10-
various features of different cars, and their MPG.
9+
The data used here are scikit-learn's bundled diabetes regression dataset,
10+
which avoids requiring a network connection when building the documentation.
1111
"""
1212

1313
# Regression Forest Example
1414
import numpy as np
1515
from matplotlib import pyplot as plt
16+
from sklearn.datasets import load_diabetes
1617
from sklearn.ensemble import RandomForestRegressor
1718
import sklearn.model_selection as xval
18-
from sklearn.datasets import fetch_openml
1919
import forestci as fci
2020

21-
# retreive mpg data from machine learning library
22-
mpg_data = fetch_openml(data_id=196)
21+
# Load a regression dataset bundled with scikit-learn
22+
diabetes_X, diabetes_y = load_diabetes(return_X_y=True)
2323

24-
# separate mpg data into predictors and outcome variable
25-
mpg_X = mpg_data["data"]
26-
mpg_y = mpg_data["target"]
27-
28-
# remove rows where the data is nan
29-
not_null_sel = np.where(mpg_X.isna().sum(axis=1).values == 0)
30-
mpg_X = mpg_X.values[not_null_sel]
31-
mpg_y = mpg_y.values[not_null_sel]
32-
33-
# split mpg data into training and test set
34-
mpg_X_train, mpg_X_test, mpg_y_train, mpg_y_test = xval.train_test_split(
35-
mpg_X,
36-
mpg_y,
24+
# Split the data into training and test sets
25+
X_train, X_test, y_train, y_test = xval.train_test_split(
26+
diabetes_X,
27+
diabetes_y,
3728
test_size=0.25,
38-
random_state=42)
29+
random_state=42,
30+
)
3931

4032
# Create RandomForestRegressor
4133
n_trees = 2000
42-
mpg_forest = RandomForestRegressor(n_estimators=n_trees, random_state=42)
43-
mpg_forest.fit(mpg_X_train, mpg_y_train)
44-
mpg_y_hat = mpg_forest.predict(mpg_X_test)
45-
46-
# Plot predicted MPG without error bars
47-
plt.scatter(mpg_y_test, mpg_y_hat)
48-
plt.plot([5, 45], [5, 45], 'k--')
49-
plt.xlabel('Reported MPG')
50-
plt.ylabel('Predicted MPG')
34+
forest = RandomForestRegressor(n_estimators=n_trees, random_state=42)
35+
forest.fit(X_train, y_train)
36+
y_pred = forest.predict(X_test)
37+
target_range = [diabetes_y.min(), diabetes_y.max()]
38+
39+
# Plot predictions without error bars
40+
plt.scatter(y_test, y_pred)
41+
plt.plot(target_range, target_range, 'k--')
42+
plt.xlabel('Observed disease progression')
43+
plt.ylabel('Predicted disease progression')
5144
plt.show()
5245

5346
# Calculate the variance
54-
mpg_V_IJ_unbiased = fci.random_forest_error(mpg_forest, mpg_X_train.shape,
55-
mpg_X_test)
56-
57-
# Plot error bars for predicted MPG using unbiased variance
58-
plt.errorbar(mpg_y_test, mpg_y_hat, yerr=np.sqrt(mpg_V_IJ_unbiased), fmt='o')
59-
plt.plot([5, 45], [5, 45], 'k--')
60-
plt.xlabel('Reported MPG')
61-
plt.ylabel('Predicted MPG')
47+
variance = fci.random_forest_error(forest, X_train.shape, X_test)
48+
49+
# Plot error bars for predictions using unbiased variance
50+
plt.errorbar(y_test, y_pred, yerr=np.sqrt(variance), fmt='o')
51+
plt.plot(target_range, target_range, 'k--')
52+
plt.xlabel('Observed disease progression')
53+
plt.ylabel('Predicted disease progression')
6254
plt.show()

examples/plot_mpg_svr.py

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,49 @@
66
This example demonstrates using `forestci` to calculate the error bars of
77
the predictions of a :class:`sklearn.ensemble.BaggingRegressor` object.
88
9-
The data used here are a classical machine learning data-set, describing
10-
various features of different cars, and their MPG.
9+
The data used here are scikit-learn's bundled diabetes regression dataset,
10+
which avoids requiring a network connection when building the documentation.
1111
"""
1212

1313
# Regression Forest Example
1414
import numpy as np
1515
from matplotlib import pyplot as plt
16+
from sklearn.datasets import load_diabetes
1617
from sklearn.ensemble import BaggingRegressor
1718
from sklearn.svm import SVR
1819
import sklearn.model_selection as xval
19-
from sklearn.datasets import fetch_openml
2020
import forestci as fci
2121

22-
# retreive mpg data from machine learning library
23-
mpg_data = fetch_openml(data_id=196)
22+
# Load a regression dataset bundled with scikit-learn
23+
diabetes_X, diabetes_y = load_diabetes(return_X_y=True)
2424

25-
# separate mpg data into predictors and outcome variable
26-
mpg_X = mpg_data["data"]
27-
mpg_y = mpg_data["target"]
28-
29-
# remove rows where the data is nan
30-
not_null_sel = np.where(mpg_X.isna().sum(axis=1).values == 0)
31-
mpg_X = mpg_X.values[not_null_sel]
32-
mpg_y = mpg_y.values[not_null_sel]
33-
34-
# split mpg data into training and test set
35-
mpg_X_train, mpg_X_test, mpg_y_train, mpg_y_test = xval.train_test_split(
36-
mpg_X, mpg_y, test_size=0.25, random_state=42
25+
# Split the data into training and test sets
26+
X_train, X_test, y_train, y_test = xval.train_test_split(
27+
diabetes_X, diabetes_y, test_size=0.25, random_state=42
3728
)
3829

39-
# Create RandomForestRegressor
30+
# Create a bagged SVR model
4031
n_estimators = 1000
41-
mpg_bagger = BaggingRegressor(
32+
bagger = BaggingRegressor(
4233
estimator=SVR(), n_estimators=n_estimators, random_state=42
4334
)
44-
mpg_bagger.fit(mpg_X_train, mpg_y_train)
45-
mpg_y_hat = mpg_bagger.predict(mpg_X_test)
46-
47-
# Plot predicted MPG without error bars
48-
plt.scatter(mpg_y_test, mpg_y_hat)
49-
plt.plot([5, 45], [5, 45], "k--")
50-
plt.xlabel("Reported MPG")
51-
plt.ylabel("Predicted MPG")
35+
bagger.fit(X_train, y_train)
36+
y_pred = bagger.predict(X_test)
37+
target_range = [diabetes_y.min(), diabetes_y.max()]
38+
39+
# Plot predictions without error bars
40+
plt.scatter(y_test, y_pred)
41+
plt.plot(target_range, target_range, "k--")
42+
plt.xlabel("Observed disease progression")
43+
plt.ylabel("Predicted disease progression")
5244
plt.show()
5345

5446
# Calculate the variance
55-
mpg_V_IJ_unbiased = fci.random_forest_error(mpg_bagger, mpg_X_train.shape, mpg_X_test)
47+
variance = fci.random_forest_error(bagger, X_train.shape, X_test)
5648

57-
# Plot error bars for predicted MPG using unbiased variance
58-
plt.errorbar(mpg_y_test, mpg_y_hat, yerr=np.sqrt(mpg_V_IJ_unbiased), fmt="o")
59-
plt.plot([5, 45], [5, 45], "k--")
60-
plt.xlabel("Reported MPG")
61-
plt.ylabel("Predicted MPG")
49+
# Plot error bars for predictions using unbiased variance
50+
plt.errorbar(y_test, y_pred, yerr=np.sqrt(variance), fmt="o")
51+
plt.plot(target_range, target_range, "k--")
52+
plt.xlabel("Observed disease progression")
53+
plt.ylabel("Predicted disease progression")
6254
plt.show()

0 commit comments

Comments
 (0)