MCPcopy
hub / github.com/scikit-learn/scikit-learn / _check_sample_weight

Function _check_sample_weight

sklearn/utils/validation.py:2092–2206  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

2090
2091
2092def _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,)

Callers 15

fitMethod · 0.90
fitMethod · 0.90
fitMethod · 0.90
fitMethod · 0.90
fitMethod · 0.90
isotonic_regressionFunction · 0.90
_build_yMethod · 0.90
_partial_fitMethod · 0.90
partial_fitMethod · 0.90
fitMethod · 0.90
fitMethod · 0.90

Calls 7

get_namespace_and_deviceFunction · 0.90
move_toFunction · 0.90
_num_samplesFunction · 0.85
check_arrayFunction · 0.85
check_non_negativeFunction · 0.85
formatMethod · 0.80

Tested by 3

test_check_sample_weightFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…