-
-
Notifications
You must be signed in to change notification settings - Fork 25.3k
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
[MRG+2] Implement two non-uniform strategies for KBinsDiscretizer (discrete branch) #11272
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9001878
Implement two new strategies for KBinsDiscretizer
TomDLT 54522af
FIX python viridis import
TomDLT 262813a
ENH store edges instead of widths
TomDLT 3b45b05
ENH change default to "quantile" and make "kmeans" deterministic
TomDLT de0a349
ENH improve tests and error messages
TomDLT 785c14f
CLN simplify branching
TomDLT b677ab3
FIX example
TomDLT 24a8745
FIX compatibility with numpy 1.8
TomDLT b204f46
CLN just renaming variables
TomDLT 33dec83
address comments
TomDLT c01d3dc
move constant feature check and improve logic
TomDLT d1d2369
simpler test
TomDLT 9d9f3f9
Update v0.20.rst
jnothman 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 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
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,94 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
========================================================== | ||
Demonstrating the different strategies of KBinsDiscretizer | ||
========================================================== | ||
|
||
This example presents the different strategies implemented in KBinsDiscretizer: | ||
- 'uniform': The discretization is uniform in each feature, which means that | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Formatting issue according to Circle. I guess a blank line will solve the problem. |
||
the bin widths are constant in each dimension. | ||
- quantile': The discretization is done on the quantiled values, which means | ||
that each bin has approximately the same number of samples. | ||
- 'kmeans': The discretization is based on the centroids of a KMeans clustering | ||
procedure. | ||
|
||
The plot shows the regions where the discretized encoding is constant. | ||
""" | ||
|
||
# Author: Tom Dupré la Tour | ||
# License: BSD 3 clause | ||
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
from sklearn.preprocessing import KBinsDiscretizer | ||
from sklearn.datasets import make_blobs | ||
|
||
print(__doc__) | ||
|
||
strategies = ['uniform', 'quantile', 'kmeans'] | ||
|
||
n_samples = 200 | ||
centers_0 = np.array([[0, 0], [0, 5], [2, 4], [8, 8]]) | ||
centers_1 = np.array([[0, 0], [3, 1]]) | ||
|
||
# construct the datasets | ||
random_state = 42 | ||
X_list = [ | ||
np.random.RandomState(random_state).uniform(-3, 3, size=(n_samples, 2)), | ||
make_blobs(n_samples=[n_samples // 10, n_samples * 4 // 10, | ||
n_samples // 10, n_samples * 4 // 10], | ||
cluster_std=0.5, centers=centers_0, | ||
random_state=random_state)[0], | ||
make_blobs(n_samples=[n_samples // 5, n_samples * 4 // 5], | ||
cluster_std=0.5, centers=centers_1, | ||
random_state=random_state)[0], | ||
] | ||
|
||
figure = plt.figure(figsize=(14, 9)) | ||
i = 1 | ||
for ds_cnt, X in enumerate(X_list): | ||
|
||
ax = plt.subplot(len(X_list), len(strategies) + 1, i) | ||
ax.scatter(X[:, 0], X[:, 1], edgecolors='k') | ||
if ds_cnt == 0: | ||
ax.set_title("Input data", size=14) | ||
|
||
xx, yy = np.meshgrid( | ||
np.linspace(X[:, 0].min(), X[:, 0].max(), 300), | ||
np.linspace(X[:, 1].min(), X[:, 1].max(), 300)) | ||
grid = np.c_[xx.ravel(), yy.ravel()] | ||
|
||
ax.set_xlim(xx.min(), xx.max()) | ||
ax.set_ylim(yy.min(), yy.max()) | ||
ax.set_xticks(()) | ||
ax.set_yticks(()) | ||
|
||
i += 1 | ||
# transform the dataset with KBinsDiscretizer | ||
for strategy in strategies: | ||
enc = KBinsDiscretizer(n_bins=4, encode='ordinal', strategy=strategy) | ||
enc.fit(X) | ||
grid_encoded = enc.transform(grid) | ||
|
||
ax = plt.subplot(len(X_list), len(strategies) + 1, i) | ||
|
||
# horizontal stripes | ||
horizontal = grid_encoded[:, 0].reshape(xx.shape) | ||
ax.contourf(xx, yy, horizontal, alpha=.5) | ||
# vertical stripes | ||
vertical = grid_encoded[:, 1].reshape(xx.shape) | ||
ax.contourf(xx, yy, vertical, alpha=.5) | ||
|
||
ax.scatter(X[:, 0], X[:, 1], edgecolors='k') | ||
ax.set_xlim(xx.min(), xx.max()) | ||
ax.set_ylim(yy.min(), yy.max()) | ||
ax.set_xticks(()) | ||
ax.set_yticks(()) | ||
if ds_cnt == 0: | ||
ax.set_title("strategy='%s'" % (strategy, ), size=14) | ||
|
||
i += 1 | ||
|
||
plt.tight_layout() | ||
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.
If I understand correctly, the result change because we're now using
strategy='quantile'
, right?If so, I think we need to modify the intervals above, otherwise they're not consistent:
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.
Good catch !