Skip to content

Find and fix any potentially unstable cumsums #7359

Closed

Description

@jnothman

#6842 reported a problem where cumsum over float32 data led to bad sums. With #7331 merged, we should find other uses of cumsum in the repo where the input could be either float32 data or very large arrays of float64 data that could lead to instability. In such cases, sklearn.utils.extmath.stable_cumsum should be used.

Activity

  1. added
    Moderate
    on Sep 8, 2016
  2. yangarbiter commented on Sep 9, 2016

    @yangarbiter
    Contributor

    I would like to help to fix this issue, though this it my first time trying to contribute to scikit-learn

    I've greped cumsum and find the following possible places that should be fixed:
    I'll start with these place first.

    sklearn/utils/random.py:146:            cdf = p.cumsum()
    sklearn/utils/random.py:170:                cdf = np.cumsum(p)
    sklearn/utils/random.py:281:            classes_ind = np.searchsorted(class_probability_nz_norm.cumsum(),
    sklearn/utils/stats.py:56:    weight_cdf = sample_weight[sorted_idx].cumsum()
    sklearn/ensemble/weight_boosting.py:1009:        cdf = sample_weight.cumsum()
    sklearn/ensemble/weight_boosting.py:1066:        weight_cdf = self.estimator_weights_[sorted_idx].cumsum(axis=1)
    sklearn/decomposition/pca.py:388:            ratio_cumsum = explained_variance_ratio_.cumsum()
    sklearn/manifold/locally_linear.py:423:        evals_cumsum = np.cumsum(evals, 1)
    sklearn/datasets/samples_generator.py:336:    cumulative_p_c = np.cumsum(p_c)
    sklearn/mixture/dpgmm.py:458:        cz = np.cumsum(z[:, ::-1], axis=-1)[:, -2::-1]
    sklearn/mixture/gmm.py:397:        weight_cdf = np.cumsum(self.weights_)
    sklearn/cluster/k_means_.py:109:        candidate_ids = np.searchsorted(closest_dist_sq.cumsum(), rand_vals)
    

    The following places cumsum seems to be dealing with integers

    examples/ensemble/plot_gradient_boosting_oob.py:95:cumsum = -np.cumsum(clf.oob_improvement_)
    sklearn/utils/fixes.py:428:        dense = obs.cumsum()[inv]
    sklearn/utils/stats.py:39:    cum_count = count.cumsum()
    sklearn/utils/sparsetools/_graph_tools.pyx:68:    indptr[1:] = mask.sum(1).cumsum()
    sklearn/preprocessing/data.py:1865:        indices = np.cumsum(n_values)
    sklearn/preprocessing/label.py:497:        indptr = np.hstack((0, np.cumsum(y_in_classes)))
    sklearn/ensemble/base.py:107:    starts = np.cumsum(n_estimators_per_job)
    sklearn/ensemble/forest.py:210:        n_nodes_ptr = np.array(n_nodes).cumsum()
    sklearn/neighbors/base.py:719:                                   np.cumsum(n_neighbors)))
    sklearn/feature_selection/base.py:108:            indptr = np.concatenate([[0], np.cumsum(col_nonzeros)])
    sklearn/feature_extraction/text.py:719:        new_indices = np.cumsum(mask) - 1  # maps old indices to new
    sklearn/metrics/tests/test_ranking.py:791:        corr_rank = np.bincount(rank, minlength=n_ranks + 1).cumsum()
    sklearn/svm/base.py:42:    sv_locs = np.cumsum(np.hstack([[0], n_support]))
    sklearn/cluster/dbscan_.py:122:        masked_indptr = np.cumsum(X_mask)[X.indptr[1:] - 1]
    
  3. yangarbiter commented on Sep 9, 2016

    @yangarbiter
    Contributor

    Do we need to let sklearn.utils.extmath.stable_cumsum able to pass in the axis parameter?

  4. TomDLT commented on Sep 9, 2016

    @TomDLT
    Member

    yes

  5. dalmia commented on Dec 5, 2016

    @dalmia
    Contributor

    Please close this.

  6. jnothman commented on Dec 5, 2016

    @jnothman
    MemberAuthor

    thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Moderate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions