-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[ENH] t-SNE: Add Normalize data checkbox #3570
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -6,8 +6,8 @@ | |
| from AnyQt.QtWidgets import QFormLayout | ||
|
|
||
| from Orange.data import Table, Domain | ||
| from Orange.preprocess.preprocess import Preprocess, ApplyDomain | ||
| from Orange.projection import PCA, TSNE, TruncatedSVD | ||
| from Orange.preprocess import preprocess | ||
| from Orange.projection import PCA, TSNE | ||
| from Orange.projection.manifold import TSNEModel | ||
| from Orange.widgets import gui | ||
| from Orange.widgets.settings import Setting, SettingProvider | ||
|
|
@@ -76,6 +76,7 @@ class OWtSNE(OWDataProjectionWidget): | |
| multiscale = Setting(True) | ||
| exaggeration = Setting(1) | ||
| pca_components = Setting(20) | ||
| normalize = Setting(True) | ||
|
|
||
| GRAPH_CLASS = OWtSNEGraph | ||
| graph = SettingProvider(OWtSNEGraph) | ||
|
|
@@ -85,7 +86,7 @@ class OWtSNE(OWDataProjectionWidget): | |
| Running, Finished, Waiting, Paused = 1, 2, 3, 4 | ||
|
|
||
| class Outputs(OWDataProjectionWidget.Outputs): | ||
| preprocessor = Output("Preprocessor", Preprocess) | ||
| preprocessor = Output("Preprocessor", preprocess.Preprocess) | ||
|
|
||
| class Error(OWDataProjectionWidget.Error): | ||
| not_enough_rows = Msg("Input data needs at least 2 rows") | ||
|
|
@@ -143,15 +144,25 @@ def _add_controls_start_box(self): | |
| sbp = gui.hBox(self.controlArea, False, addToLayout=False) | ||
| gui.hSlider( | ||
| sbp, self, "pca_components", minValue=2, maxValue=50, step=1, | ||
| callback=self._params_changed | ||
| callback=self._invalidate_pca_projection | ||
| ) | ||
| form.addRow("PCA components:", sbp) | ||
|
|
||
| self.normalize_cbx = gui.checkBox( | ||
| box, self, "normalize", "Normalize data", | ||
| callback=self._invalidate_pca_projection, | ||
| ) | ||
| form.addRow(self.normalize_cbx) | ||
|
|
||
| box.layout().addLayout(form) | ||
|
|
||
| gui.separator(box, 10) | ||
| self.runbutton = gui.button(box, self, "Run", callback=self._toggle_run) | ||
|
|
||
| def _invalidate_pca_projection(self): | ||
| self.pca_data = None | ||
| self._params_changed() | ||
|
|
||
| def _params_changed(self): | ||
| self.__state = OWtSNE.Finished | ||
| self.__set_update_loop(None) | ||
|
|
@@ -215,12 +226,32 @@ def stop(self): | |
| def resume(self): | ||
| self.__set_update_loop(self.tsne_iterator) | ||
|
|
||
| def set_data(self, data: Table): | ||
| super().set_data(data) | ||
|
|
||
| if data is not None: | ||
| # PCA doesn't support normalization on sparse data, as this would | ||
| # require centering and normalizing the matrix | ||
| self.normalize_cbx.setDisabled(data.is_sparse()) | ||
| if data.is_sparse(): | ||
| self.normalize = False | ||
| self.normalize_cbx.setToolTip( | ||
| "Data normalization is not supported on sparse matrices." | ||
| ) | ||
| else: | ||
| self.normalize_cbx.setToolTip("") | ||
|
|
||
| def pca_preprocessing(self): | ||
| if self.pca_data is not None and \ | ||
| self.pca_data.X.shape[1] == self.pca_components: | ||
|
Collaborator
Author
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. I removed this check because I changed line 146 to invalidate the PCA projection, therefore it will be set to |
||
| """Perform PCA preprocessing before passing off the data to t-SNE.""" | ||
| if self.pca_data is not None: | ||
| return | ||
| cls = TruncatedSVD if self.data.is_sparse() else PCA | ||
| projector = cls(n_components=self.pca_components, random_state=0) | ||
|
|
||
| projector = PCA(n_components=self.pca_components, random_state=0) | ||
| # If the normalization box is ticked, we'll add the `Normalize` | ||
| # preprocessor to PCA | ||
| if self.normalize: | ||
| projector.preprocessors += (preprocess.Normalize(),) | ||
|
|
||
| model = projector(self.data) | ||
| self.pca_data = model(self.data) | ||
|
|
||
|
|
@@ -343,7 +374,7 @@ def _get_projection_data(self): | |
| def send_preprocessor(self): | ||
| prep = None | ||
| if self.data is not None and self.projection is not None: | ||
| prep = ApplyDomain(self.projection.domain, self.projection.name) | ||
| prep = preprocess.ApplyDomain(self.projection.domain, self.projection.name) | ||
| self.Outputs.preprocessor.send(prep) | ||
|
|
||
| def clear(self): | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.