diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2017-03-12 15:25:53 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-12 15:25:53 +0000 |
commit | cf349c91e22b2195b32290f14777be79acc9af15 (patch) | |
tree | e389b0d878a4b87388e476fff36975588004334b /numpy/random | |
parent | f819cc17baf37c790d5ff539f1621cf1f02e4d92 (diff) | |
parent | 7a73bad2d9c04e4f16e87dbed9d7b627327fe814 (diff) | |
download | python-numpy-cf349c91e22b2195b32290f14777be79acc9af15.tar.gz python-numpy-cf349c91e22b2195b32290f14777be79acc9af15.tar.bz2 python-numpy-cf349c91e22b2195b32290f14777be79acc9af15.zip |
Merge pull request #8570 from evanlimanto/random-permutation-shuffle-fix
BUG: fix issue #8250 when np.array gets called on an invalid sequence
Diffstat (limited to 'numpy/random')
-rw-r--r-- | numpy/random/mtrand/mtrand.pyx | 3 | ||||
-rw-r--r-- | numpy/random/tests/test_random.py | 40 |
2 files changed, 42 insertions, 1 deletions
diff --git a/numpy/random/mtrand/mtrand.pyx b/numpy/random/mtrand/mtrand.pyx index bf3a385a9..ee0e31172 100644 --- a/numpy/random/mtrand/mtrand.pyx +++ b/numpy/random/mtrand/mtrand.pyx @@ -143,6 +143,7 @@ cdef extern from "initarray.h": import_array() cimport cython +import copy import numpy as np import operator import warnings @@ -4871,7 +4872,7 @@ cdef class RandomState: if isinstance(x, (int, long, np.integer)): arr = np.arange(x) else: - arr = np.array(x) + arr = copy.copy(x) self.shuffle(arr) return arr diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index e4c58e2bd..8b05cc724 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -419,6 +419,46 @@ class TestRandomDist(TestCase): desired = conv([0, 1, 9, 6, 2, 4, 5, 8, 7, 3]) assert_array_equal(actual, desired) + def test_permutation(self): + # Test that permutation works on a list of tuples, and integers. + + # list of (1, np.array([1, 1]), (2, np.array([2, 2])), and so on. + N = 5 + A = np.arange(N)[:,None] + A = np.concatenate((A, A), axis=1) + B = range(N) + c = list(zip(B, A)) + assert_(sorted(c) == sorted(np.random.permutation(c))) + + d = np.arange(N) + assert_array_equal(d, sorted(np.random.permutation(N))) + + # only integer arguments are accepted. + assert_raises(TypeError, np.random.permutation, 3.0) + + # same test as shuffle. + for conv in [lambda x: np.array([]), + lambda x: x, + lambda x: np.asarray(x).astype(np.int8), + lambda x: np.asarray(x).astype(np.float32), + lambda x: np.asarray(x).astype(np.complex64), + lambda x: np.asarray(x).astype(object), + lambda x: [(i, i) for i in x], + lambda x: np.asarray([[i, i] for i in x]), + lambda x: np.vstack([x, x]).T, + # gh-4270 + lambda x: np.asarray([(i, i) for i in x], + [("a", object, 1), + ("b", np.int32, 1)])]: + np.random.seed(self.seed) + alist = conv([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) + blist = np.random.permutation(alist) + # check that array is not mutated. + assert_array_equal(alist, conv([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])) + actual = blist + desired = conv([0, 1, 9, 6, 2, 4, 5, 8, 7, 3]) + assert_array_equal(actual, desired) + def test_shuffle_masked(self): # gh-3263 a = np.ma.masked_values(np.reshape(range(20), (5, 4)) % 3 - 1, -1) |