Validate sample weights. Note that passing sample_weight=None will output an array of ones. Therefore, in some cases, you may want to protect the call with: if sample_weight is not None: sample_weight = _check_sample_weight(...) Parameters ---------- sample_weight :
(
sample_weight,
X,
*,
dtype=None,
force_float_dtype=True,
ensure_non_negative=False,
ensure_same_device=True,
copy=False,
allow_all_zero_weights=False,
)
| 2090 | |
| 2091 | |
| 2092 | def _check_sample_weight( |
| 2093 | sample_weight, |
| 2094 | X, |
| 2095 | *, |
| 2096 | dtype=None, |
| 2097 | force_float_dtype=True, |
| 2098 | ensure_non_negative=False, |
| 2099 | ensure_same_device=True, |
| 2100 | copy=False, |
| 2101 | allow_all_zero_weights=False, |
| 2102 | ): |
| 2103 | """Validate sample weights. |
| 2104 | |
| 2105 | Note that passing sample_weight=None will output an array of ones. |
| 2106 | Therefore, in some cases, you may want to protect the call with: |
| 2107 | if sample_weight is not None: |
| 2108 | sample_weight = _check_sample_weight(...) |
| 2109 | |
| 2110 | Parameters |
| 2111 | ---------- |
| 2112 | sample_weight : {ndarray, Number or None}, shape (n_samples,) |
| 2113 | Input sample weights. |
| 2114 | |
| 2115 | X : {ndarray, list, sparse matrix} |
| 2116 | Input data. |
| 2117 | |
| 2118 | dtype : dtype, default=None |
| 2119 | dtype of the validated `sample_weight`. |
| 2120 | If None, and `sample_weight` is an array: |
| 2121 | |
| 2122 | - If `sample_weight.dtype` is one of `{np.float64, np.float32}`, |
| 2123 | then the dtype is preserved. |
| 2124 | - Else the output has NumPy's default dtype: `np.float64`. |
| 2125 | |
| 2126 | If `dtype` is not `{np.float32, np.float64, None}`, then output will |
| 2127 | be `np.float64`. |
| 2128 | |
| 2129 | force_float_dtype : bool, default=True |
| 2130 | Whether `X` should be forced to be float dtype, when `dtype` is a non-float |
| 2131 | dtype or None. |
| 2132 | |
| 2133 | ensure_non_negative : bool, default=False, |
| 2134 | Whether or not the weights are expected to be non-negative. |
| 2135 | |
| 2136 | .. versionadded:: 1.0 |
| 2137 | |
| 2138 | ensure_same_device : bool, default=True |
| 2139 | Whether `sample_weight` should be forced to be on the same device as `X`. |
| 2140 | |
| 2141 | copy : bool, default=False |
| 2142 | If True, a copy of sample_weight will be created. |
| 2143 | |
| 2144 | allow_all_zero_weights : bool, default=False, |
| 2145 | Whether or not to raise an error when sample weights are all zero. |
| 2146 | |
| 2147 | Returns |
| 2148 | ------- |
| 2149 | sample_weight : ndarray of shape (n_samples,) |
searching dependent graphs…