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

Method test_partition_iterative

numpy/core/tests/test_multiarray.py:3003–3069  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

3001 prev = k + 1
3002
3003 def test_partition_iterative(self):
3004 d = np.arange(17)
3005 kth = (0, 1, 2, 429, 231)
3006 assert_raises(ValueError, d.partition, kth)
3007 assert_raises(ValueError, d.argpartition, kth)
3008 d = np.arange(10).reshape((2, 5))
3009 assert_raises(ValueError, d.partition, kth, axis=0)
3010 assert_raises(ValueError, d.partition, kth, axis=1)
3011 assert_raises(ValueError, np.partition, d, kth, axis=1)
3012 assert_raises(ValueError, np.partition, d, kth, axis=None)
3013
3014 d = np.array([3, 4, 2, 1])
3015 p = np.partition(d, (0, 3))
3016 self.assert_partitioned(p, (0, 3))
3017 self.assert_partitioned(d[np.argpartition(d, (0, 3))], (0, 3))
3018
3019 assert_array_equal(p, np.partition(d, (-3, -1)))
3020 assert_array_equal(p, d[np.argpartition(d, (-3, -1))])
3021
3022 d = np.arange(17)
3023 np.random.shuffle(d)
3024 d.partition(range(d.size))
3025 assert_array_equal(np.arange(17), d)
3026 np.random.shuffle(d)
3027 assert_array_equal(np.arange(17), d[d.argpartition(range(d.size))])
3028
3029 # test unsorted kth
3030 d = np.arange(17)
3031 np.random.shuffle(d)
3032 keys = np.array([1, 3, 8, -2])
3033 np.random.shuffle(d)
3034 p = np.partition(d, keys)
3035 self.assert_partitioned(p, keys)
3036 p = d[np.argpartition(d, keys)]
3037 self.assert_partitioned(p, keys)
3038 np.random.shuffle(keys)
3039 assert_array_equal(np.partition(d, keys), p)
3040 assert_array_equal(d[np.argpartition(d, keys)], p)
3041
3042 # equal kth
3043 d = np.arange(20)[::-1]
3044 self.assert_partitioned(np.partition(d, [5]*4), [5])
3045 self.assert_partitioned(np.partition(d, [5]*4 + [6, 13]),
3046 [5]*4 + [6, 13])
3047 self.assert_partitioned(d[np.argpartition(d, [5]*4)], [5])
3048 self.assert_partitioned(d[np.argpartition(d, [5]*4 + [6, 13])],
3049 [5]*4 + [6, 13])
3050
3051 d = np.arange(12)
3052 np.random.shuffle(d)
3053 d1 = np.tile(np.arange(12), (4, 1))
3054 map(np.random.shuffle, d1)
3055 d0 = np.transpose(d1)
3056
3057 kth = (1, 6, 7, -1)
3058 p = np.partition(d1, kth, axis=1)
3059 pa = d1[np.arange(d1.shape[0])[:, None],
3060 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