Пример #1
0
 def test_choice_sum_of_probs_tolerance(self):
     # The sum of probs should be 1.0 with some tolerance.
     # For low precision dtypes the tolerance was too tight.
     # See numpy github issue 6123.
     rnd.seed(1234)
     a = [1, 2, 3]
     counts = [4, 4, 2]
     for dt in np.float16, np.float32, np.float64:
         probs = np.array(counts, dtype=dt) / sum(counts)
         c = rnd.choice(a, p=probs)
         assert_(c in a)
         assert_raises(ValueError, rnd.choice, a, p=probs * 0.9)
Пример #2
0
    def test_choice_return_shape(self):
        p = [0.1, 0.9]
        # Check scalar
        assert_(np.isscalar(rnd.choice(2, replace=True)))
        assert_(np.isscalar(rnd.choice(2, replace=False)))
        assert_(np.isscalar(rnd.choice(2, replace=True, p=p)))
        assert_(np.isscalar(rnd.choice(2, replace=False, p=p)))
        assert_(np.isscalar(rnd.choice([1, 2], replace=True)))
        assert_(rnd.choice([None], replace=True) is None)
        a = np.array([1, 2])
        arr = np.empty(1, dtype=object)
        arr[0] = a
        assert_(rnd.choice(arr, replace=True) is a)

        # Check 0-d array
        s = tuple()
        assert_(not np.isscalar(rnd.choice(2, s, replace=True)))
        assert_(not np.isscalar(rnd.choice(2, s, replace=False)))
        assert_(not np.isscalar(rnd.choice(2, s, replace=True, p=p)))
        assert_(not np.isscalar(rnd.choice(2, s, replace=False, p=p)))
        assert_(not np.isscalar(rnd.choice([1, 2], s, replace=True)))
        assert_(rnd.choice([None], s, replace=True).ndim == 0)
        a = np.array([1, 2])
        arr = np.empty(1, dtype=object)
        arr[0] = a
        assert_(rnd.choice(arr, s, replace=True).item() is a)

        # Check multi dimensional array
        s = (2, 3)
        p = [0.1, 0.1, 0.1, 0.1, 0.4, 0.2]
        assert_(rnd.choice(6, s, replace=True).shape, s)
        assert_(rnd.choice(6, s, replace=False).shape, s)
        assert_(rnd.choice(6, s, replace=True, p=p).shape, s)
        assert_(rnd.choice(6, s, replace=False, p=p).shape, s)
        assert_(rnd.choice(np.arange(6), s, replace=True).shape, s)
Пример #3
0
 def test_choice_noninteger(self):
     rnd.seed(self.seed, self.brng)
     actual = rnd.choice(['a', 'b', 'c', 'd'], 4)
     desired = np.array(['d', 'a', 'a', 'c'])
     np.testing.assert_array_equal(actual, desired)
Пример #4
0
 def test_choice_nonuniform_noreplace(self):
     rnd.seed(self.seed, self.brng)
     actual = rnd.choice(4, 3, replace=False, p=[0.1, 0.3, 0.5, 0.1])
     desired = np.array([3, 0, 1])
     np.testing.assert_array_equal(actual, desired)
Пример #5
0
 def test_choice_uniform_noreplace(self):
     rnd.seed(self.seed, self.brng)
     actual = rnd.choice(4, 3, replace=False)
     desired = np.array([2, 1, 3])
     np.testing.assert_array_equal(actual, desired)
Пример #6
0
 def test_choice_uniform_replace(self):
     rnd.seed(self.seed, self.brng)
     actual = rnd.choice(4, 4)
     desired = np.array([3, 0, 0, 2])
     np.testing.assert_array_equal(actual, desired)
 def test_choice_nonuniform_replace(self):
     rnd.seed(self.seed, brng=self.brng)
     actual = rnd.choice(4, 4, p=[0.4, 0.4, 0.1, 0.1])
     desired = np.array([3, 0, 0, 1])
     np.testing.assert_array_equal(actual, desired)