-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: Allow forestci to work on general Bagging estimators #100
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4c95aa5
ENH: Allow BaggingClassifier and BaggingRegressor estimators
richford 9793b18
Add plot_mpg_svr.py example
richford af6f88a
Add pandas requirement
richford 47878d0
Refer to the openml dataset by its id, to remove ambiguity
arokem 42ed91e
DEP: Remove pandas dependence
richford 52947e7
Merge branch 'enh/bagging' of github.com:richford/forest-confidence-i…
richford File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
""" | ||
====================================== | ||
Plotting Bagging Regression Error Bars | ||
====================================== | ||
|
||
This example demonstrates using `forestci` to calculate the error bars of | ||
the predictions of a :class:`sklearn.ensemble.BaggingRegressor` object. | ||
|
||
The data used here are a classical machine learning data-set, describing | ||
various features of different cars, and their MPG. | ||
""" | ||
|
||
# Regression Forest Example | ||
import numpy as np | ||
from matplotlib import pyplot as plt | ||
from sklearn.ensemble import BaggingRegressor | ||
from sklearn.svm import SVR | ||
import sklearn.model_selection as xval | ||
from sklearn.datasets import fetch_openml | ||
import forestci as fci | ||
|
||
# retreive mpg data from machine learning library | ||
mpg_data = fetch_openml(data_id=196) | ||
|
||
# separate mpg data into predictors and outcome variable | ||
mpg_X = mpg_data["data"] | ||
mpg_y = mpg_data["target"] | ||
|
||
# remove rows where the data is nan | ||
not_null_sel = np.invert(np.sum(np.isnan(mpg_data["data"]), axis=1).astype(bool)) | ||
mpg_X = mpg_X[not_null_sel] | ||
mpg_y = mpg_y[not_null_sel] | ||
|
||
# split mpg data into training and test set | ||
mpg_X_train, mpg_X_test, mpg_y_train, mpg_y_test = xval.train_test_split( | ||
mpg_X, mpg_y, test_size=0.25, random_state=42 | ||
) | ||
|
||
# Create RandomForestRegressor | ||
n_estimators = 1000 | ||
mpg_bagger = BaggingRegressor( | ||
base_estimator=SVR(), n_estimators=n_estimators, random_state=42 | ||
) | ||
mpg_bagger.fit(mpg_X_train, mpg_y_train) | ||
mpg_y_hat = mpg_bagger.predict(mpg_X_test) | ||
|
||
# Plot predicted MPG without error bars | ||
plt.scatter(mpg_y_test, mpg_y_hat) | ||
plt.plot([5, 45], [5, 45], "k--") | ||
plt.xlabel("Reported MPG") | ||
plt.ylabel("Predicted MPG") | ||
plt.show() | ||
|
||
# Calculate the variance | ||
mpg_V_IJ_unbiased = fci.random_forest_error(mpg_bagger, mpg_X_train, mpg_X_test) | ||
|
||
# Plot error bars for predicted MPG using unbiased variance | ||
plt.errorbar(mpg_y_test, mpg_y_hat, yerr=np.sqrt(mpg_V_IJ_unbiased), fmt="o") | ||
plt.plot([5, 45], [5, 45], "k--") | ||
plt.xlabel("Reported MPG") | ||
plt.ylabel("Predicted MPG") | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am I correct that this is the only place that pandas is used? Is it here just to find nan values?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're correct that was the only pandas dependency. I was using it for
fillna
and then also forconcat
to merge on the sample indices. But I just pushed a commit that gets rid of the pandas dependency.