MCPcopy
hub / github.com/numpy/numpy / test_partition_iterative

Method test_partition_iterative

numpy/_core/tests/test_multiarray.py:3597–3663  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

3595 prev = k + 1
3596
3597 def test_partition_iterative(self):
3598 d = np.arange(17)
3599 kth = (0, 1, 2, 429, 231)
3600 assert_raises(ValueError, d.partition, kth)
3601 assert_raises(ValueError, d.argpartition, kth)
3602 d = np.arange(10).reshape((2, 5))
3603 assert_raises(ValueError, d.partition, kth, axis=0)
3604 assert_raises(ValueError, d.partition, kth, axis=1)
3605 assert_raises(ValueError, np.partition, d, kth, axis=1)
3606 assert_raises(ValueError, np.partition, d, kth, axis=None)
3607
3608 d = np.array([3, 4, 2, 1])
3609 p = np.partition(d, (0, 3))
3610 self.assert_partitioned(p, (0, 3))
3611 self.assert_partitioned(d[np.argpartition(d, (0, 3))], (0, 3))
3612
3613 assert_array_equal(p, np.partition(d, (-3, -1)))
3614 assert_array_equal(p, d[np.argpartition(d, (-3, -1))])
3615
3616 d = np.arange(17)
3617 np.random.shuffle(d)
3618 d.partition(range(d.size))
3619 assert_array_equal(np.arange(17), d)
3620 np.random.shuffle(d)
3621 assert_array_equal(np.arange(17), d[d.argpartition(range(d.size))])
3622
3623 # test unsorted kth
3624 d = np.arange(17)
3625 np.random.shuffle(d)
3626 keys = np.array([1, 3, 8, -2])
3627 np.random.shuffle(d)
3628 p = np.partition(d, keys)
3629 self.assert_partitioned(p, keys)
3630 p = d[np.argpartition(d, keys)]
3631 self.assert_partitioned(p, keys)
3632 np.random.shuffle(keys)
3633 assert_array_equal(np.partition(d, keys), p)
3634 assert_array_equal(d[np.argpartition(d, keys)], p)
3635
3636 # equal kth
3637 d = np.arange(20)[::-1]
3638 self.assert_partitioned(np.partition(d, [5] * 4), [5])
3639 self.assert_partitioned(np.partition(d, [5] * 4 + [6, 13]),
3640 [5] * 4 + [6, 13])
3641 self.assert_partitioned(d[np.argpartition(d, [5] * 4)], [5])
3642 self.assert_partitioned(d[np.argpartition(d, [5] * 4 + [6, 13])],
3643 [5] * 4 + [6, 13])
3644
3645 d = np.arange(12)
3646 np.random.shuffle(d)
3647 d1 = np.tile(np.arange(12), (4, 1))
3648 map(np.random.shuffle, d1)
3649 d0 = np.transpose(d1)
3650
3651 kth = (1, 6, 7, -1)
3652 p = np.partition(d1, kth, axis=1)
3653 pa = d1[np.arange(d1.shape[0])[:, None],
3654 d1.argpartition(kth, axis=1)]

Callers

nothing calls this directly

Calls 6

assert_partitionedMethod · 0.95
assert_raisesFunction · 0.90
assert_array_equalFunction · 0.90
reshapeMethod · 0.80
argpartitionMethod · 0.80
partitionMethod · 0.45

Tested by

no test coverage detected