MCPcopy Create free account
hub / github.com/numpy/numpy / test_weights

Method test_weights

numpy/lib/tests/test_function_base.py:332–374  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

330 assert_array_equal(wsum, expected_wsum)
331
332 def test_weights(self):
333 y = np.arange(10)
334 w = np.arange(10)
335 actual = average(y, weights=w)
336 desired = (np.arange(10) ** 2).sum() * 1. / np.arange(10).sum()
337 assert_almost_equal(actual, desired)
338
339 y1 = np.array([[1, 2, 3], [4, 5, 6]])
340 w0 = [1, 2]
341 actual = average(y1, weights=w0, axis=0)
342 desired = np.array([3., 4., 5.])
343 assert_almost_equal(actual, desired)
344
345 w1 = [0, 0, 1]
346 actual = average(y1, weights=w1, axis=1)
347 desired = np.array([3., 6.])
348 assert_almost_equal(actual, desired)
349
350 # This should raise an error. Can we test for that ?
351 # assert_equal(average(y1, weights=w1), 9./2.)
352
353 # 2D Case
354 w2 = [[0, 0, 1], [0, 0, 2]]
355 desired = np.array([3., 6.])
356 assert_array_equal(average(y1, weights=w2, axis=1), desired)
357 assert_equal(average(y1, weights=w2), 5.)
358
359 y3 = rand(5).astype(np.float32)
360 w3 = rand(5).astype(np.float64)
361
362 assert_(np.average(y3, weights=w3).dtype == np.result_type(y3, w3))
363
364 # test weights with `keepdims=False` and `keepdims=True`
365 x = np.array([2, 3, 4]).reshape(3, 1)
366 w = np.array([4, 5, 6]).reshape(3, 1)
367
368 actual = np.average(x, weights=w, axis=1, keepdims=False)
369 desired = np.array([2., 3., 4.])
370 assert_array_equal(actual, desired)
371
372 actual = np.average(x, weights=w, axis=1, keepdims=True)
373 desired = np.array([[2.], [3.], [4.]])
374 assert_array_equal(actual, desired)
375
376 def test_returned(self):
377 y = np.array([[1, 2, 3], [4, 5, 6]])

Callers

nothing calls this directly

Calls 9

averageFunction · 0.90
assert_almost_equalFunction · 0.90
assert_array_equalFunction · 0.90
assert_equalFunction · 0.90
assert_Function · 0.90
randFunction · 0.85
astypeMethod · 0.80
reshapeMethod · 0.80
sumMethod · 0.45

Tested by

no test coverage detected