MCPcopy Index your code
hub / github.com/numpy/numpy / test_weights

Method test_weights

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

Source from the content-addressed store, hash-verified

382 assert_array_equal(wsum, expected_wsum)
383
384 def test_weights(self):
385 y = np.arange(10)
386 w = np.arange(10)
387 actual = average(y, weights=w)
388 desired = (np.arange(10) ** 2).sum() * 1. / np.arange(10).sum()
389 assert_almost_equal(actual, desired)
390
391 y1 = np.array([[1, 2, 3], [4, 5, 6]])
392 w0 = [1, 2]
393 actual = average(y1, weights=w0, axis=0)
394 desired = np.array([3., 4., 5.])
395 assert_almost_equal(actual, desired)
396
397 w1 = [0, 0, 1]
398 actual = average(y1, weights=w1, axis=1)
399 desired = np.array([3., 6.])
400 assert_almost_equal(actual, desired)
401
402 # weights and input have different shapes but no axis is specified
403 with pytest.raises(
404 TypeError,
405 match="Axis must be specified when shapes of a "
406 "and weights differ"):
407 average(y1, weights=w1)
408
409 # 2D Case
410 w2 = [[0, 0, 1], [0, 0, 2]]
411 desired = np.array([3., 6.])
412 assert_array_equal(average(y1, weights=w2, axis=1), desired)
413 assert_equal(average(y1, weights=w2), 5.)
414
415 y3 = rand(5).astype(np.float32)
416 w3 = rand(5).astype(np.float64)
417
418 assert_(np.average(y3, weights=w3).dtype == np.result_type(y3, w3))
419
420 # test weights with `keepdims=False` and `keepdims=True`
421 x = np.array([2, 3, 4]).reshape(3, 1)
422 w = np.array([4, 5, 6]).reshape(3, 1)
423
424 actual = np.average(x, weights=w, axis=1, keepdims=False)
425 desired = np.array([2., 3., 4.])
426 assert_array_equal(actual, desired)
427
428 actual = np.average(x, weights=w, axis=1, keepdims=True)
429 desired = np.array([[2.], [3.], [4.]])
430 assert_array_equal(actual, desired)
431
432 def test_weight_and_input_dims_different(self):
433 y = np.arange(12).reshape(2, 2, 3)

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