Compute mean and variance along an axis on a CSR or CSC matrix. Parameters ---------- X : sparse matrix of shape (n_samples, n_features) Input data. It can be of CSR or CSC format. axis : {0, 1} Axis along which the axis should be computed. weights : ndarray of
(X, axis, weights=None, return_sum_weights=False)
| 99 | |
| 100 | |
| 101 | def mean_variance_axis(X, axis, weights=None, return_sum_weights=False): |
| 102 | """Compute mean and variance along an axis on a CSR or CSC matrix. |
| 103 | |
| 104 | Parameters |
| 105 | ---------- |
| 106 | X : sparse matrix of shape (n_samples, n_features) |
| 107 | Input data. It can be of CSR or CSC format. |
| 108 | |
| 109 | axis : {0, 1} |
| 110 | Axis along which the axis should be computed. |
| 111 | |
| 112 | weights : ndarray of shape (n_samples,) or (n_features,), default=None |
| 113 | If axis is set to 0 shape is (n_samples,) or |
| 114 | if axis is set to 1 shape is (n_features,). |
| 115 | If it is set to None, then samples are equally weighted. |
| 116 | |
| 117 | .. versionadded:: 0.24 |
| 118 | |
| 119 | return_sum_weights : bool, default=False |
| 120 | If True, returns the sum of weights seen for each feature |
| 121 | if `axis=0` or each sample if `axis=1`. |
| 122 | |
| 123 | .. versionadded:: 0.24 |
| 124 | |
| 125 | Returns |
| 126 | ------- |
| 127 | |
| 128 | means : ndarray of shape (n_features,), dtype=floating |
| 129 | Feature-wise means. |
| 130 | |
| 131 | variances : ndarray of shape (n_features,), dtype=floating |
| 132 | Feature-wise variances. |
| 133 | |
| 134 | sum_weights : ndarray of shape (n_features,), dtype=floating |
| 135 | Returned if `return_sum_weights` is `True`. |
| 136 | |
| 137 | Examples |
| 138 | -------- |
| 139 | >>> from sklearn.utils import sparsefuncs |
| 140 | >>> from scipy import sparse |
| 141 | >>> import numpy as np |
| 142 | >>> indptr = np.array([0, 3, 4, 4, 4]) |
| 143 | >>> indices = np.array([0, 1, 2, 2]) |
| 144 | >>> data = np.array([8, 1, 2, 5]) |
| 145 | >>> scale = np.array([2, 3, 2]) |
| 146 | >>> csr = sparse.csr_array((data, indices, indptr)) |
| 147 | >>> csr.todense() |
| 148 | array([[8, 1, 2], |
| 149 | [0, 0, 5], |
| 150 | [0, 0, 0], |
| 151 | [0, 0, 0]]) |
| 152 | >>> sparsefuncs.mean_variance_axis(csr, axis=0) |
| 153 | (array([2. , 0.25, 1.75]), array([12. , 0.1875, 4.1875])) |
| 154 | """ |
| 155 | _raise_error_wrong_axis(axis) |
| 156 | |
| 157 | if sp.issparse(X) and X.format == "csr": |
| 158 | if axis == 0: |
searching dependent graphs…