Skip to content

COSMIT Avoid writing out vectorizable operations in sparsefuncs_fast #10615

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 2 commits into from
Feb 12, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions sklearn/utils/sparsefuncs_fast.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -297,27 +297,22 @@ def _incr_mean_variance_axis0(np.ndarray[floating, ndim=1] X_data,
# First pass
if last_n == 0:
return new_mean, new_var, new_n
# Next passes
else:
updated_n = last_n + new_n
last_over_new_n = last_n / new_n

for i in xrange(n_features):
# Unnormalized old stats
last_mean[i] *= last_n
last_var[i] *= last_n

# Unnormalized new stats
new_mean[i] *= new_n
new_var[i] *= new_n

# Update stats
updated_var[i] = (last_var[i] + new_var[i] +
last_over_new_n / updated_n *
(last_mean[i] / last_over_new_n - new_mean[i]) ** 2)

updated_mean[i] = (last_mean[i] + new_mean[i]) / updated_n
updated_var[i] = updated_var[i] / updated_n
# Next passes
updated_n = last_n + new_n
last_over_new_n = last_n / new_n

# Unnormalized stats
last_mean *= last_n
last_var *= last_n
new_mean *= new_n
new_var *= new_n

# Update stats
updated_var = (last_var + new_var + last_over_new_n / updated_n *
(last_mean / last_over_new_n - new_mean) ** 2)
updated_mean = (last_mean + new_mean) / updated_n
updated_var /= updated_n

return updated_mean, updated_var, updated_n

Expand Down