-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
[MRG+2] discrete branch: add an example for KBinsDiscretizer #10192
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
Merged
qinhanmin2014
merged 10 commits into
scikit-learn:discrete
from
qinhanmin2014:discrete-example
Nov 27, 2017
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
527309f
add example
qinhanmin2014 6e5fa5a
add encode
qinhanmin2014 93e0121
minor format
qinhanmin2014 290293f
change color
qinhanmin2014 4cbe31a
use sparse output & set random state
qinhanmin2014 daf6de6
address comment
qinhanmin2014 f557991
update comment
qinhanmin2014 1ac8cc0
flake8 fix
qinhanmin2014 83090f9
doc + get rid of sca
qinhanmin2014 367c64f
typo
qinhanmin2014 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,87 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
================================================================ | ||
Using KBinsDiscretizer to discretize continuous features | ||
================================================================ | ||
|
||
The example compares prediction result of linear regression (linear model) | ||
and decision tree (tree based model) with and without discretization of | ||
real-valued features. | ||
|
||
As is shown in the result before discretization, linear model is fast to | ||
build and relatively straightforward to interpret, but can only model | ||
linear relationships, while decision tree can build a much more complex model | ||
of the data. One way to make linear model more powerful on continuous data | ||
is to use discretization (also known as binning). In the example, we | ||
discretize the feature and one-hot encode the transformed data. Note that if | ||
the bins are not reasonably wide, there would appear to be a substantially | ||
increased risk of overfitting, so the discretizer parameters should usually | ||
be tuned under cross validation. | ||
|
||
After discretization, linear regression and decision tree make exactly the | ||
same prediction. As features are constant within each bin, any model must | ||
predict the same value for all points within a bin. Compared with the result | ||
before discretization, linear model become much more flexible while decision | ||
tree gets much less flexible. Note that binning features generally has no | ||
beneficial effect for tree-based models, as these models can learn to split | ||
up the data anywhere. | ||
|
||
""" | ||
|
||
# Author: Andreas Müller | ||
# Hanmin Qin <qinhanmin2005@sina.com> | ||
# License: BSD 3 clause | ||
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
from sklearn.linear_model import LinearRegression | ||
from sklearn.preprocessing import KBinsDiscretizer | ||
from sklearn.tree import DecisionTreeRegressor | ||
|
||
print(__doc__) | ||
|
||
# construct the dataset | ||
rnd = np.random.RandomState(42) | ||
X = rnd.uniform(-3, 3, size=100) | ||
y = np.sin(X) + rnd.normal(size=len(X)) / 3 | ||
X = X.reshape(-1, 1) | ||
|
||
# transform the dataset with KBinsDiscretizer | ||
enc = KBinsDiscretizer(n_bins=10, encode='onehot') | ||
X_binned = enc.fit_transform(X) | ||
|
||
# predict with original dataset | ||
fig, (ax1, ax2) = plt.subplots(ncols=2, sharey=True, figsize=(10, 4)) | ||
line = np.linspace(-3, 3, 1000, endpoint=False).reshape(-1, 1) | ||
reg = LinearRegression().fit(X, y) | ||
ax1.plot(line, reg.predict(line), linewidth=2, color='green', | ||
label="linear regression") | ||
reg = DecisionTreeRegressor(min_samples_split=3, random_state=0).fit(X, y) | ||
ax1.plot(line, reg.predict(line), linewidth=2, color='red', | ||
label="decision tree") | ||
ax1.plot(X[:, 0], y, 'o', c='k') | ||
ax1.legend(loc="best") | ||
ax1.set_ylabel("Regression output") | ||
ax1.set_xlabel("Input feature") | ||
ax1.set_title("Result before discretization") | ||
|
||
# predict with transformed dataset | ||
line_binned = enc.transform(line) | ||
reg = LinearRegression().fit(X_binned, y) | ||
ax2.plot(line, reg.predict(line_binned), linewidth=2, color='green', | ||
linestyle='-', label='linear regression') | ||
reg = DecisionTreeRegressor(min_samples_split=3, | ||
random_state=0).fit(X_binned, y) | ||
ax2.plot(line, reg.predict(line_binned), linewidth=2, color='red', | ||
linestyle=':', label='decision tree') | ||
ax2.plot(X[:, 0], y, 'o', c='k') | ||
bins = enc.offset_[0] + enc.bin_width_[0] * np.arange(1, enc.n_bins_[0]) | ||
ax2.vlines(bins, *plt.gca().get_ylim(), linewidth=1, alpha=.2) | ||
ax2.legend(loc="best") | ||
ax2.set_xlabel("Input feature") | ||
ax2.set_title("Result after discretization") | ||
|
||
plt.tight_layout() | ||
plt.show() |
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.
Note that the linear model is fast to build and relatively straightforward to interpret.