MCPcopy Create free account
hub / github.com/scikit-learn/scikit-learn / test_binarizer

Function test_binarizer

sklearn/preprocessing/tests/test_data.py:2098–2149  ·  view source on GitHub ↗
(constructor)

Source from the content-addressed store, hash-verified

2096 "constructor", [np.array, list] + CSC_CONTAINERS + CSR_CONTAINERS
2097)
2098def test_binarizer(constructor):
2099 X_ = np.array([[1, 0, 5], [2, 3, -1]])
2100 X = constructor(X_.copy())
2101
2102 binarizer = Binarizer(threshold=2.0, copy=True)
2103 X_bin = toarray(binarizer.transform(X))
2104 assert np.sum(X_bin == 0) == 4
2105 assert np.sum(X_bin == 1) == 2
2106 X_bin = binarizer.transform(X)
2107 assert sparse.issparse(X) == sparse.issparse(X_bin)
2108
2109 binarizer = Binarizer(copy=True).fit(X)
2110 X_bin = toarray(binarizer.transform(X))
2111 assert X_bin is not X
2112 assert np.sum(X_bin == 0) == 2
2113 assert np.sum(X_bin == 1) == 4
2114
2115 binarizer = Binarizer(copy=True)
2116 X_bin = binarizer.transform(X)
2117 assert X_bin is not X
2118 X_bin = toarray(X_bin)
2119 assert np.sum(X_bin == 0) == 2
2120 assert np.sum(X_bin == 1) == 4
2121
2122 binarizer = Binarizer(copy=False)
2123 X_bin = binarizer.transform(X)
2124 if constructor is not list:
2125 assert X_bin is X
2126
2127 binarizer = Binarizer(copy=False)
2128 X_float = np.array([[1, 0, 5], [2, 3, -1]], dtype=np.float64)
2129 X_bin = binarizer.transform(X_float)
2130 if constructor is not list:
2131 assert X_bin is X_float
2132
2133 X_bin = toarray(X_bin)
2134 assert np.sum(X_bin == 0) == 2
2135 assert np.sum(X_bin == 1) == 4
2136
2137 binarizer = Binarizer(threshold=-0.5, copy=True)
2138 if constructor in (np.array, list):
2139 X = constructor(X_.copy())
2140
2141 X_bin = toarray(binarizer.transform(X))
2142 assert np.sum(X_bin == 0) == 1
2143 assert np.sum(X_bin == 1) == 5
2144 X_bin = binarizer.transform(X)
2145
2146 # Cannot use threshold < 0 for sparse
2147 if constructor in CSC_CONTAINERS:
2148 with pytest.raises(ValueError):
2149 binarizer.transform(constructor(X))
2150
2151
2152@pytest.mark.parametrize(

Callers

nothing calls this directly

Calls 4

transformMethod · 0.95
BinarizerClass · 0.90
toarrayFunction · 0.70
fitMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…