-
-
Notifications
You must be signed in to change notification settings - Fork 26k
[WIP] Disregard nan fix #10457
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
Closed
Closed
[WIP] Disregard nan fix #10457
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d0b7985
Partially addressed disregard NaN capabilities for Sparse Matrix in S…
pinakinathc 110977d
removed pyflakes8 errors
pinakinathc f4adbb9
removing pyflakes8 errors
pinakinathc a44b9ba
removing pep8 errors
pinakinathc ceb268d
adresses error encountered in appveyor
pinakinathc 430231c
addressing appveyor error
pinakinathc f98823f
added test cases for _incremental_mean_and_var and sparse matrix
pinakinathc fec502f
fixed test cases for sparse matrix
pinakinathc 5b5f72d
silenced warnings in _incremental_mean_and_var
pinakinathc a287392
Revert "silenced warnings in _incremental_mean_and_var"
pinakinathc 3f64821
fixed error in _incremental_mean_and_var
pinakinathc 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
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
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
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 |
---|---|---|
|
@@ -643,7 +643,7 @@ def make_nonnegative(X, min_value=0): | |
|
||
|
||
def _incremental_mean_and_var(X, last_mean=.0, last_variance=None, | ||
last_sample_count=0): | ||
last_sample_count=0, ignore_nan=True): | ||
"""Calculate mean update and a Youngs and Cramer variance update. | ||
|
||
last_mean and last_variance are statistics computed at the last step by the | ||
|
@@ -688,29 +688,55 @@ def _incremental_mean_and_var(X, last_mean=.0, last_variance=None, | |
# old = stats until now | ||
# new = the current increment | ||
# updated = the aggregated stats | ||
flag = 0 # if flag == 1 then last_sample_count was an array | ||
n_features = X.shape[1] | ||
if isinstance(last_sample_count, np.ndarray): | ||
flag = 1 | ||
else: | ||
last_sample_count *= np.ones(n_features) | ||
|
||
last_sum = last_mean * last_sample_count | ||
new_sum = X.sum(axis=0) | ||
sum_func = np.nansum if ignore_nan else np.sum | ||
new_sum = sum_func(X, axis=0) | ||
new_sum[np.isnan(new_sum)] = 0 | ||
|
||
new_sample_count = X.shape[0] | ||
new_sample_count = np.count_nonzero(~np.isnan(X), axis=0) | ||
if not isinstance(new_sample_count, np.ndarray): | ||
new_sample_count *= np.ones(n_features) | ||
updated_sample_count = last_sample_count + new_sample_count | ||
|
||
updated_mean = (last_sum + new_sum) / updated_sample_count | ||
updated_variance = np.zeros(n_features) | ||
|
||
if last_variance is None: | ||
updated_variance = None | ||
else: | ||
new_unnormalized_variance = X.var(axis=0) * new_sample_count | ||
if last_sample_count == 0: # Avoid division by 0 | ||
updated_unnormalized_variance = new_unnormalized_variance | ||
else: | ||
last_over_new_count = last_sample_count / new_sample_count | ||
last_unnormalized_variance = last_variance * last_sample_count | ||
updated_unnormalized_variance = ( | ||
last_unnormalized_variance + | ||
new_unnormalized_variance + | ||
last_over_new_count / updated_sample_count * | ||
(last_sum / last_over_new_count - new_sum) ** 2) | ||
updated_variance = updated_unnormalized_variance / updated_sample_count | ||
var_func = np.nanvar if ignore_nan else np.var | ||
new_unnormalized_variance = var_func(X, axis=0) | ||
new_unnormalized_variance[np.isnan(new_unnormalized_variance)] = 0 | ||
new_unnormalized_variance = (new_unnormalized_variance * | ||
new_sample_count) | ||
for i in xrange(n_features): | ||
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 suspect you can do everything here with vectorized numpy operations and should avoid an explicit loop over each feature. |
||
if updated_sample_count[i] == 0: # Avoid division by 0 | ||
continue | ||
# Avoid division by 0 | ||
elif last_sample_count[i] == 0 or new_sample_count[i] == 0: | ||
updated_unnormalized_variance = new_unnormalized_variance[i] | ||
else: | ||
last_over_new_count = (last_sample_count[i] / | ||
new_sample_count[i]) | ||
last_unnormalized_variance = (last_variance[i] * | ||
last_sample_count[i]) | ||
updated_unnormalized_variance = ( | ||
last_unnormalized_variance + | ||
new_unnormalized_variance[i] + | ||
last_over_new_count / updated_sample_count[i] * | ||
(last_sum[i] / last_over_new_count - new_sum[i]) ** 2) | ||
updated_variance[i] = (updated_unnormalized_variance / | ||
updated_sample_count[i]) | ||
|
||
if flag == 0: # If n_sample_count was not an array | ||
updated_sample_count = updated_sample_count[0] | ||
|
||
return updated_mean, updated_variance, updated_sample_count | ||
|
||
|
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
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.
this is a bad variable name