MCPcopy
hub / github.com/numpy/numpy / test_weights

Method test_weights

numpy/lib/tests/test_histograms.py:148–175  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

146 assert_array_equal(edges, int_edges)
147
148 def test_weights(self):
149 v = np.random.rand(100)
150 w = np.ones(100) * 5
151 a, b = histogram(v)
152 na, nb = histogram(v, density=True)
153 wa, wb = histogram(v, weights=w)
154 nwa, nwb = histogram(v, weights=w, density=True)
155 assert_array_almost_equal(a * 5, wa)
156 assert_array_almost_equal(na, nwa)
157
158 # Check weights are properly applied.
159 v = np.linspace(0, 10, 10)
160 w = np.concatenate((np.zeros(5), np.ones(5)))
161 wa, wb = histogram(v, bins=np.arange(11), weights=w)
162 assert_array_almost_equal(wa, w)
163
164 # Check with integer weights
165 wa, wb = histogram([1, 2, 2, 4], bins=4, weights=[4, 3, 2, 1])
166 assert_array_equal(wa, [4, 5, 0, 1])
167 wa, wb = histogram(
168 [1, 2, 2, 4], bins=4, weights=[4, 3, 2, 1], density=True)
169 assert_array_almost_equal(wa, np.array([4, 5, 0, 1]) / 10. / 3. * 4)
170
171 # Check weights with non-uniform bin widths
172 a, b = histogram(
173 np.arange(9), [0, 1, 3, 6, 10],
174 weights=[2, 1, 1, 1, 1, 1, 1, 1, 1], density=True)
175 assert_almost_equal(a, [.2, .1, .1, .075])
176
177 def test_exotic_weights(self):
178

Callers

nothing calls this directly

Calls 5

histogramFunction · 0.90
assert_array_equalFunction · 0.90
assert_almost_equalFunction · 0.90
linspaceMethod · 0.80

Tested by

no test coverage detected