diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 19 | ||||
-rw-r--r-- | numpy/fft/tests/test_fftpack.py | 2 | ||||
-rw-r--r-- | numpy/lib/tests/test_index_tricks.py | 2 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 2 | ||||
-rw-r--r-- | numpy/lib/tests/test_regression.py | 2 | ||||
-rw-r--r-- | numpy/random/__init__.py | 7 | ||||
-rw-r--r-- | numpy/random/mtrand/mtrand.c | 114 | ||||
-rw-r--r-- | numpy/random/mtrand/mtrand.pyx | 20 | ||||
-rw-r--r-- | numpy/random/tests/test_random.py | 24 |
9 files changed, 91 insertions, 101 deletions
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 5f97f1bb3..118e0b4dd 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -1088,7 +1088,7 @@ class TestClip(TestCase): for byteorder in byte_orders: dtype = np.dtype(T).newbyteorder(byteorder) - x = (np.random.random_sample(1000) * array_max).astype(dtype) + x = (np.random.random(1000) * array_max).astype(dtype) if inplace: x.clip(clip_min,clip_max,x) else: @@ -1108,8 +1108,7 @@ class TestClip(TestCase): self._clip_type('int',1024,-120,100.5, inplace=inplace) self._clip_type('int',1024,0,0, inplace=inplace) - x = self._clip_type('uint',1024,-120,100,expected_min=0, - inplace=inplace) + x = self._clip_type('uint',1024,-120,100,expected_min=0, inplace=inplace) x = self._clip_type('uint',1024,0,0, inplace=inplace) def test_record_array(self): @@ -1137,7 +1136,7 @@ class TestPutmask(object): def test_ip_types(self): unchecked_types = [str, unicode, np.void, object] - x = np.random.random_sample(1000)*100 + x = np.random.random(1000)*100 mask = x < 40 for val in [-100,0,15]: @@ -1186,7 +1185,7 @@ class TestTake(object): def test_ip_types(self): unchecked_types = [str, unicode, np.void, object] - x = np.random.random_sample(24)*100 + x = np.random.random(24)*100 x.shape = 2,3,4 for types in np.sctypes.itervalues(): for T in types: @@ -1194,20 +1193,20 @@ class TestTake(object): yield self.tst_basic,x.copy().astype(T) def test_raise(self): - x = np.random.random_sample(24)*100 + x = np.random.random(24)*100 x.shape = 2,3,4 assert_raises(IndexError, x.take, [0,1,2], axis=0) assert_raises(IndexError, x.take, [-3], axis=0) assert_array_equal(x.take([-1], axis=0)[0], x[1]) def test_clip(self): - x = np.random.random_sample(24)*100 + x = np.random.random(24)*100 x.shape = 2,3,4 assert_array_equal(x.take([-1], axis=0, mode='clip')[0], x[0]) assert_array_equal(x.take([2], axis=0, mode='clip')[0], x[1]) def test_wrap(self): - x = np.random.random_sample(24)*100 + x = np.random.random(24)*100 x.shape = 2,3,4 assert_array_equal(x.take([-1], axis=0, mode='wrap')[0], x[1]) assert_array_equal(x.take([2], axis=0, mode='wrap')[0], x[0]) @@ -1249,7 +1248,7 @@ class TestIO(object): def setUp(self): shape = (2,4,3) - rand = np.random.random_sample + rand = np.random.random self.x = rand(shape) + rand(shape).astype(np.complex)*1j self.x[0,:,1] = [nan, inf, -inf, nan] self.dtype = self.x.dtype @@ -1450,7 +1449,7 @@ class TestFromBuffer(object): for byteorder in ['<','>']: for dtype in [float,int,np.complex]: dt = np.dtype(dtype).newbyteorder(byteorder) - x = (np.random.random_sample((4,7))*5).astype(dt) + x = (np.random.random((4,7))*5).astype(dt) buf = x.tostring() yield self.tst_basic,buf,x.flat,{'dtype':dt} diff --git a/numpy/fft/tests/test_fftpack.py b/numpy/fft/tests/test_fftpack.py index ceb6a8dd7..4f70d3bc5 100644 --- a/numpy/fft/tests/test_fftpack.py +++ b/numpy/fft/tests/test_fftpack.py @@ -14,7 +14,7 @@ class TestFFTShift(TestCase): class TestFFT1D(TestCase): def test_basic(self): - rand = np.random.random_sample + rand = np.random.random x = rand(30) + 1j*rand(30) assert_array_almost_equal(fft1(x), np.fft.fft(x)) diff --git a/numpy/lib/tests/test_index_tricks.py b/numpy/lib/tests/test_index_tricks.py index 2c095b71b..2c6500a57 100644 --- a/numpy/lib/tests/test_index_tricks.py +++ b/numpy/lib/tests/test_index_tricks.py @@ -193,7 +193,7 @@ def test_diag_indices(): [0, 1]]]) ) def test_diag_indices_from(): - x = np.random.random_sample((4, 4)) + x = np.random.random((4, 4)) r, c = diag_indices_from(x) assert_array_equal(r, np.arange(4)) assert_array_equal(c, np.arange(4)) diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 6d6b10c0c..54c2ddabb 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -1406,7 +1406,7 @@ M 33 21.99 def test_gzip_load(): - a = np.random.random_sample((5, 5)) + a = np.random.random((5, 5)) s = StringIO() f = gzip.GzipFile(fileobj=s, mode="w") diff --git a/numpy/lib/tests/test_regression.py b/numpy/lib/tests/test_regression.py index 33685f638..c244aea87 100644 --- a/numpy/lib/tests/test_regression.py +++ b/numpy/lib/tests/test_regression.py @@ -12,7 +12,7 @@ class TestRegression(TestCase): def test_cov_parameters(self,level=rlevel): """Ticket #91""" - x = np.random.random_sample((3,3)) + x = np.random.random((3,3)) y = x.copy() np.cov(x, rowvar=1) np.cov(y, rowvar=0) diff --git a/numpy/random/__init__.py b/numpy/random/__init__.py index 24ce0dd5d..73b5fdabc 100644 --- a/numpy/random/__init__.py +++ b/numpy/random/__init__.py @@ -10,7 +10,10 @@ random Uniformly distributed values of a given shape. bytes Uniformly distributed random bytes. random_integers Uniformly distributed integers in a given range. random_sample Uniformly distributed floats in a given range. -sample Generate a weighted random sample from a given array-like +random Alias for random_sample +ranf Alias for random_sample +sample Alias for random_sample +choice Generate a weighted random sample from a given array-like permutation Randomly permute a sequence / generate a random sequence. shuffle Randomly permute a sequence in place. seed Seed the random number generator. @@ -88,6 +91,8 @@ from info import __doc__, __all__ from mtrand import * # Some aliases: +ranf = random = sample = random_sample +__all__.extend(['ranf','random','sample']) def __RandomState_ctor(): """Return a RandomState instance. diff --git a/numpy/random/mtrand/mtrand.c b/numpy/random/mtrand/mtrand.c index 5726598bb..53713221c 100644 --- a/numpy/random/mtrand/mtrand.c +++ b/numpy/random/mtrand/mtrand.c @@ -1,4 +1,4 @@ -/* Generated by Cython 0.15 on Thu Sep 1 13:51:31 2011 */ +/* Generated by Cython 0.15 on Fri Sep 2 12:00:06 2011 */ #define PY_SSIZE_T_CLEAN #include "Python.h" @@ -756,12 +756,12 @@ static char __pyx_k_197[] = "RandomState.randint (line 809)"; static char __pyx_k_198[] = "\n randint(low, high=None, size=None)\n\n Return random integers from `low` (inclusive) to `high` (exclusive).\n\n Return random integers from the \"discrete uniform\" distribution in the\n \"half-open\" interval [`low`, `high`). If `high` is None (the default),\n then results are from [0, `low`).\n\n Parameters\n ----------\n low : int\n Lowest (signed) integer to be drawn from the distribution (unless\n ``high=None``, in which case this parameter is the *highest* such\n integer).\n high : int, optional\n If provided, one above the largest (signed) integer to be drawn\n from the distribution (see above for behavior if ``high=None``).\n size : int or tuple of ints, optional\n Output shape. Default is None, in which case a single int is\n returned.\n\n Returns\n -------\n out : int or ndarray of ints\n `size`-shaped array of random integers from the appropriate\n distribution, or a single such random int if `size` not provided.\n\n See Also\n --------\n random.random_integers : similar to `randint`, only for the closed\n interval [`low`, `high`], and 1 is the lowest value if `high` is\n omitted. In particular, this other one is the one to use to generate\n uniformly distributed discrete non-integers.\n\n Examples\n --------\n >>> np.random.randint(2, size=10)\n array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])\n >>> np.random.randint(1, size=10)\n array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])\n\n Generate a 2 x 4 array of ints between 0 and 4, inclusive:\n\n >>> np.random.randint(5, size=(2, 4))\n array([[4, 0, 2, 1],\n [3, 2, 2, 0]])\n\n "; static char __pyx_k_199[] = "RandomState.bytes (line 889)"; static char __pyx_k_200[] = "\n bytes(length)\n\n Return random bytes.\n\n Parameters\n ----------\n length : int\n Number of random bytes.\n\n Returns\n -------\n out : str\n String of length `length`.\n\n Examples\n --------\n >>> np.random.bytes(10)\n ' eh\\x85\\x022SZ\\xbf\\xa4' #random\n\n "; -static char __pyx_k_201[] = "RandomState.sample (line 917)"; -static char __pyx_k_202[] = "\n sample(a, size[, replace, p])\n\n Generates a random sample from a given 1-D array\n\n Parameters\n -----------\n a : 1-D array-like or int\n If an ndarray, a random sample is generated from its elements.\n If an int, the random sample is generated as if a was np.arange(n)\n size : int\n Positive integer, the size of the sample.\n replace : boolean, optional\n Whether the sample is with or without replacement\n p : 1-D array-like, optional\n The probabilities associated with each entry in a.\n If not given the sample assumes a uniform distribtion over all\n entries in a.\n\n Returns\n --------\n samples : 1-D ndarray, shape (size,)\n The generated random samples\n\n Raises\n -------\n ValueError\n If a is an int and less than zero, if a or p are not 1-dimensional,\n if a is an array-like of size 0, if p is not a vector of\n probabilities, if a and p have different lengths, or if\n replace=False and the sample size is greater than the population\n size\n\n See Also\n ---------\n randint, shuffle, permutation\n\n Examples\n ---------\n Generate a uniform random sample from np.arange(5) of size 3:\n \n >>> np.random.sample(5, 3)\n array([0, 3, 4])\n >>> #This is equivalent to np.random.randint(0,5,3) \n\n Generate a non-uniform random sample from np.arange(5) of size 3:\n \n >>> np.random.sample(5, 3, p=[0.1, 0, 0.3, 0.6, 0])\n array([3, 3, 0])\n\n Generate a uniform random sample from np.arange(5) of size 3 without\n replacement:\n \n >>> np.random.sample(5, 3, replace=False)\n array([3,1,0])\n >>> #This is equivalent to np.random.shuffle(np.arange(5))[:3]\n\n Generate a non-uniform ra""ndom sample from np.arange(5) of size\n 3 without replacement:\n\n >>> np.random.sample(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])\n array([2, 3, 0])\n\n Any of the above can be repeated with an arbitrary array-like\n instead of just integers. For instance:\n\n >>> aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']\n >>> np.random.sample(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])\n array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'], \n dtype='|S11')\n\n "; +static char __pyx_k_201[] = "RandomState.choice (line 917)"; +static char __pyx_k_202[] = "\n choice(a, size=1, replace=True, p=None)\n\n Generates a random sample from a given 1-D array\n\n Parameters\n -----------\n a : 1-D array-like or int\n If an ndarray, a random sample is generated from its elements.\n If an int, the random sample is generated as if a was np.arange(n)\n size : int\n Positive integer, the size of the sample.\n replace : boolean, optional\n Whether the sample is with or without replacement\n p : 1-D array-like, optional\n The probabilities associated with each entry in a.\n If not given the sample assumes a uniform distribtion over all\n entries in a.\n\n Returns\n --------\n samples : 1-D ndarray, shape (size,)\n The generated random samples\n\n Raises\n -------\n ValueError\n If a is an int and less than zero, if a or p are not 1-dimensional,\n if a is an array-like of size 0, if p is not a vector of\n probabilities, if a and p have different lengths, or if\n replace=False and the sample size is greater than the population\n size\n\n See Also\n ---------\n randint, shuffle, permutation\n\n Examples\n ---------\n Generate a uniform random sample from np.arange(5) of size 3:\n \n >>> np.random.choice(5, 3)\n array([0, 3, 4])\n >>> #This is equivalent to np.random.randint(0,5,3) \n\n Generate a non-uniform random sample from np.arange(5) of size 3:\n \n >>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])\n array([3, 3, 0])\n\n Generate a uniform random sample from np.arange(5) of size 3 without\n replacement:\n \n >>> np.random.choice(5, 3, replace=False)\n array([3,1,0])\n >>> #This is equivalent to np.random.shuffle(np.arange(5))[:3]\n\n Generate a non-""uniform random sample from np.arange(5) of size\n 3 without replacement:\n\n >>> np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])\n array([2, 3, 0])\n\n Any of the above can be repeated with an arbitrary array-like\n instead of just integers. For instance:\n\n >>> aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']\n >>> np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])\n array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'], \n dtype='|S11')\n\n "; static char __pyx_k_203[] = "RandomState.uniform (line 1057)"; static char __pyx_k_204[] = "\n uniform(low=0.0, high=1.0, size=1)\n\n Draw samples from a uniform distribution.\n\n Samples are uniformly distributed over the half-open interval\n ``[low, high)`` (includes low, but excludes high). In other words,\n any value within the given interval is equally likely to be drawn\n by `uniform`.\n\n Parameters\n ----------\n low : float, optional\n Lower boundary of the output interval. All values generated will be\n greater than or equal to low. The default value is 0.\n high : float\n Upper boundary of the output interval. All values generated will be\n less than high. The default value is 1.0.\n size : int or tuple of ints, optional\n Shape of output. If the given size is, for example, (m,n,k),\n m*n*k samples are generated. If no shape is specified, a single sample\n is returned.\n\n Returns\n -------\n out : ndarray\n Drawn samples, with shape `size`.\n\n See Also\n --------\n randint : Discrete uniform distribution, yielding integers.\n random_integers : Discrete uniform distribution over the closed\n interval ``[low, high]``.\n random_sample : Floats uniformly distributed over ``[0, 1)``.\n random : Alias for `random_sample`.\n rand : Convenience function that accepts dimensions as input, e.g.,\n ``rand(2,2)`` would generate a 2-by-2 array of floats,\n uniformly distributed over ``[0, 1)``.\n\n Notes\n -----\n The probability density function of the uniform distribution is\n\n .. math:: p(x) = \\frac{1}{b - a}\n\n anywhere within the interval ``[a, b)``, and zero elsewhere.\n\n Examples\n --------\n Draw samples from the distribution:\n\n >>> s = np.random.uniform(-1,0,1000)\n\n All values are w""ithin the given interval:\n\n >>> np.all(s >= -1)\n True\n >>> np.all(s < 0)\n True\n\n Display the histogram of the samples, along with the\n probability density function:\n\n >>> import matplotlib.pyplot as plt\n >>> count, bins, ignored = plt.hist(s, 15, normed=True)\n >>> plt.plot(bins, np.ones_like(bins), linewidth=2, color='r')\n >>> plt.show()\n\n "; static char __pyx_k_205[] = "RandomState.rand (line 1144)"; -static char __pyx_k_206[] = "\n rand(d0, d1, ..., dn)\n\n Random values in a given shape.\n\n Create an array of the given shape and propagate it with\n random samples from a uniform distribution\n over ``[0, 1)``.\n\n Parameters\n ----------\n d0, d1, ..., dn : int\n Shape of the output.\n\n Returns\n -------\n out : ndarray, shape ``(d0, d1, ..., dn)``\n Random values.\n\n See Also\n --------\n random\n\n Notes\n -----\n This is a convenience function. If you want an interface that\n takes a shape-tuple as the first argument, refer to\n `random`.\n\n Examples\n --------\n >>> np.random.rand(3,2)\n array([[ 0.14022471, 0.96360618], #random\n [ 0.37601032, 0.25528411], #random\n [ 0.49313049, 0.94909878]]) #random\n\n "; +static char __pyx_k_206[] = "\n rand(d0, d1, ..., dn)\n\n Random values in a given shape.\n\n Create an array of the given shape and propagate it with\n random samples from a uniform distribution\n over ``[0, 1)``.\n\n Parameters\n ----------\n d0, d1, ..., dn : int\n Shape of the output.\n\n Returns\n -------\n out : ndarray, shape ``(d0, d1, ..., dn)``\n Random values.\n\n See Also\n --------\n random\n\n Notes\n -----\n This is a convenience function. If you want an interface that\n takes a shape-tuple as the first argument, refer to\n np.random.random_sample . \n\n Examples\n --------\n >>> np.random.rand(3,2)\n array([[ 0.14022471, 0.96360618], #random\n [ 0.37601032, 0.25528411], #random\n [ 0.49313049, 0.94909878]]) #random\n\n "; static char __pyx_k_207[] = "RandomState.randn (line 1187)"; static char __pyx_k_208[] = "\n randn([d1, ..., dn])\n\n Return a sample (or samples) from the \"standard normal\" distribution.\n\n If positive, int_like or int-convertible arguments are provided,\n `randn` generates an array of shape ``(d1, ..., dn)``, filled\n with random floats sampled from a univariate \"normal\" (Gaussian)\n distribution of mean 0 and variance 1 (if any of the :math:`d_i` are\n floats, they are first converted to integers by truncation). A single\n float randomly sampled from the distribution is returned if no\n argument is provided.\n\n This is a convenience function. If you want an interface that takes a\n tuple as the first argument, use `numpy.random.standard_normal` instead.\n\n Parameters\n ----------\n d1, ..., dn : `n` ints, optional\n The dimensions of the returned array, should be all positive.\n\n Returns\n -------\n Z : ndarray or float\n A ``(d1, ..., dn)``-shaped array of floating-point samples from\n the standard normal distribution, or a single such float if\n no parameters were supplied.\n\n See Also\n --------\n random.standard_normal : Similar, but takes a tuple as its argument.\n\n Notes\n -----\n For random samples from :math:`N(\\mu, \\sigma^2)`, use:\n\n ``sigma * np.random.randn(...) + mu``\n\n Examples\n --------\n >>> np.random.randn()\n 2.1923875335537315 #random\n\n Two-by-four array of samples from N(3, 6.25):\n\n >>> 2.5 * np.random.randn(2, 4) + 3\n array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], #random\n [ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) #random\n\n "; static char __pyx_k_209[] = "RandomState.random_integers (line 1243)"; @@ -889,6 +889,7 @@ static char __pyx_k__shape[] = "shape"; static char __pyx_k__sigma[] = "sigma"; static char __pyx_k__zeros[] = "zeros"; static char __pyx_k__arange[] = "arange"; +static char __pyx_k__choice[] = "choice"; static char __pyx_k__cumsum[] = "cumsum"; static char __pyx_k__gumbel[] = "gumbel"; static char __pyx_k__normal[] = "normal"; @@ -896,7 +897,6 @@ static char __pyx_k__pareto[] = "pareto"; static char __pyx_k__random[] = "random"; static char __pyx_k__reduce[] = "reduce"; static char __pyx_k__repeat[] = "repeat"; -static char __pyx_k__sample[] = "sample"; static char __pyx_k__uint32[] = "uint32"; static char __pyx_k__unique[] = "unique"; static char __pyx_k__MT19937[] = "MT19937"; @@ -1112,6 +1112,7 @@ static PyObject *__pyx_n_s__beta; static PyObject *__pyx_n_s__binomial; static PyObject *__pyx_n_s__bytes; static PyObject *__pyx_n_s__chisquare; +static PyObject *__pyx_n_s__choice; static PyObject *__pyx_n_s__copy; static PyObject *__pyx_n_s__cov; static PyObject *__pyx_n_s__cumsum; @@ -1185,7 +1186,6 @@ static PyObject *__pyx_n_s__reduce; static PyObject *__pyx_n_s__repeat; static PyObject *__pyx_n_s__replace; static PyObject *__pyx_n_s__right; -static PyObject *__pyx_n_s__sample; static PyObject *__pyx_n_s__scale; static PyObject *__pyx_n_s__searchsorted; static PyObject *__pyx_n_s__seed; @@ -6394,16 +6394,16 @@ static PyObject *__pyx_pf_6mtrand_11RandomState_11bytes(PyObject *__pyx_v_self, } /* "mtrand.pyx":917 - * * - * def sample(self, a, size, replace=True, p=None): # <<<<<<<<<<<<<< + * + * def choice(self, a, size=1, replace=True, p=None): # <<<<<<<<<<<<<< * """ - * sample(a, size[, replace, p]) + * choice(a, size=1, replace=True, p=None) */ -static PyObject *__pyx_pf_6mtrand_11RandomState_12sample(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ -static char __pyx_doc_6mtrand_11RandomState_12sample[] = "\n sample(a, size[, replace, p])\n\n Generates a random sample from a given 1-D array\n\n Parameters\n -----------\n a : 1-D array-like or int\n If an ndarray, a random sample is generated from its elements.\n If an int, the random sample is generated as if a was np.arange(n)\n size : int\n Positive integer, the size of the sample.\n replace : boolean, optional\n Whether the sample is with or without replacement\n p : 1-D array-like, optional\n The probabilities associated with each entry in a.\n If not given the sample assumes a uniform distribtion over all\n entries in a.\n\n Returns\n --------\n samples : 1-D ndarray, shape (size,)\n The generated random samples\n\n Raises\n -------\n ValueError\n If a is an int and less than zero, if a or p are not 1-dimensional,\n if a is an array-like of size 0, if p is not a vector of\n probabilities, if a and p have different lengths, or if\n replace=False and the sample size is greater than the population\n size\n\n See Also\n ---------\n randint, shuffle, permutation\n\n Examples\n ---------\n Generate a uniform random sample from np.arange(5) of size 3:\n \n >>> np.random.sample(5, 3)\n array([0, 3, 4])\n >>> #This is equivalent to np.random.randint(0,5,3) \n\n Generate a non-uniform random sample from np.arange(5) of size 3:\n \n >>> np.random.sample(5, 3, p=[0.1, 0, 0.3, 0.6, 0])\n array([3, 3, 0])\n\n Generate a uniform random sample from np.arange(5) of size 3 without\n replacement:\n \n >>> np.random.sample(5, 3, replace=False)\n array([3,1,0])\n >>> #This is equivalent to np.random.shuffle(np.arange(5))[:3]\n\n Generate a non-uniform ra""ndom sample from np.arange(5) of size\n 3 without replacement:\n\n >>> np.random.sample(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])\n array([2, 3, 0])\n\n Any of the above can be repeated with an arbitrary array-like\n instead of just integers. For instance:\n\n >>> aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']\n >>> np.random.sample(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])\n array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'], \n dtype='|S11')\n\n "; -static PyObject *__pyx_pf_6mtrand_11RandomState_12sample(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { +static PyObject *__pyx_pf_6mtrand_11RandomState_12choice(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static char __pyx_doc_6mtrand_11RandomState_12choice[] = "\n choice(a, size=1, replace=True, p=None)\n\n Generates a random sample from a given 1-D array\n\n Parameters\n -----------\n a : 1-D array-like or int\n If an ndarray, a random sample is generated from its elements.\n If an int, the random sample is generated as if a was np.arange(n)\n size : int\n Positive integer, the size of the sample.\n replace : boolean, optional\n Whether the sample is with or without replacement\n p : 1-D array-like, optional\n The probabilities associated with each entry in a.\n If not given the sample assumes a uniform distribtion over all\n entries in a.\n\n Returns\n --------\n samples : 1-D ndarray, shape (size,)\n The generated random samples\n\n Raises\n -------\n ValueError\n If a is an int and less than zero, if a or p are not 1-dimensional,\n if a is an array-like of size 0, if p is not a vector of\n probabilities, if a and p have different lengths, or if\n replace=False and the sample size is greater than the population\n size\n\n See Also\n ---------\n randint, shuffle, permutation\n\n Examples\n ---------\n Generate a uniform random sample from np.arange(5) of size 3:\n \n >>> np.random.choice(5, 3)\n array([0, 3, 4])\n >>> #This is equivalent to np.random.randint(0,5,3) \n\n Generate a non-uniform random sample from np.arange(5) of size 3:\n \n >>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])\n array([3, 3, 0])\n\n Generate a uniform random sample from np.arange(5) of size 3 without\n replacement:\n \n >>> np.random.choice(5, 3, replace=False)\n array([3,1,0])\n >>> #This is equivalent to np.random.shuffle(np.arange(5))[:3]\n\n Generate a non-""uniform random sample from np.arange(5) of size\n 3 without replacement:\n\n >>> np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])\n array([2, 3, 0])\n\n Any of the above can be repeated with an arbitrary array-like\n instead of just integers. For instance:\n\n >>> aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']\n >>> np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])\n array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'], \n dtype='|S11')\n\n "; +static PyObject *__pyx_pf_6mtrand_11RandomState_12choice(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { PyObject *__pyx_v_a = 0; PyObject *__pyx_v_size = 0; PyObject *__pyx_v_replace = 0; @@ -6431,10 +6431,11 @@ static PyObject *__pyx_pf_6mtrand_11RandomState_12sample(PyObject *__pyx_v_self, const char *__pyx_filename = NULL; int __pyx_clineno = 0; static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__a,&__pyx_n_s__size,&__pyx_n_s__replace,&__pyx_n_s__p,0}; - __Pyx_RefNannySetupContext("sample"); + __Pyx_RefNannySetupContext("choice"); if (unlikely(__pyx_kwds)) { Py_ssize_t kw_args = PyDict_Size(__pyx_kwds); PyObject* values[4] = {0,0,0,0}; + values[1] = ((PyObject *)__pyx_int_1); values[2] = __pyx_k_15; values[3] = ((PyObject *)Py_None); switch (PyTuple_GET_SIZE(__pyx_args)) { @@ -6451,10 +6452,9 @@ static PyObject *__pyx_pf_6mtrand_11RandomState_12sample(PyObject *__pyx_v_self, if (likely(values[0])) kw_args--; else goto __pyx_L5_argtuple_error; case 1: - values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__size); - if (likely(values[1])) kw_args--; - else { - __Pyx_RaiseArgtupleInvalid("sample", 0, 2, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 917; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + if (kw_args > 0) { + PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__size); + if (value) { values[1] = value; kw_args--; } } case 2: if (kw_args > 0) { @@ -6468,32 +6468,30 @@ static PyObject *__pyx_pf_6mtrand_11RandomState_12sample(PyObject *__pyx_v_self, } } if (unlikely(kw_args > 0)) { - if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "sample") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 917; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "choice") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 917; __pyx_clineno = __LINE__; goto __pyx_L3_error;} } __pyx_v_a = values[0]; __pyx_v_size = values[1]; __pyx_v_replace = values[2]; __pyx_v_p = values[3]; } else { + __pyx_v_size = ((PyObject *)__pyx_int_1); __pyx_v_replace = __pyx_k_15; __pyx_v_p = ((PyObject *)Py_None); switch (PyTuple_GET_SIZE(__pyx_args)) { - case 4: - __pyx_v_p = PyTuple_GET_ITEM(__pyx_args, 3); - case 3: - __pyx_v_replace = PyTuple_GET_ITEM(__pyx_args, 2); - case 2: - __pyx_v_size = PyTuple_GET_ITEM(__pyx_args, 1); - __pyx_v_a = PyTuple_GET_ITEM(__pyx_args, 0); + case 4: __pyx_v_p = PyTuple_GET_ITEM(__pyx_args, 3); + case 3: __pyx_v_replace = PyTuple_GET_ITEM(__pyx_args, 2); + case 2: __pyx_v_size = PyTuple_GET_ITEM(__pyx_args, 1); + case 1: __pyx_v_a = PyTuple_GET_ITEM(__pyx_args, 0); break; default: goto __pyx_L5_argtuple_error; } } goto __pyx_L4_argument_unpacking_done; __pyx_L5_argtuple_error:; - __Pyx_RaiseArgtupleInvalid("sample", 0, 2, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 917; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __Pyx_RaiseArgtupleInvalid("choice", 0, 1, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 917; __pyx_clineno = __LINE__; goto __pyx_L3_error;} __pyx_L3_error:; - __Pyx_AddTraceback("mtrand.RandomState.sample", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_AddTraceback("mtrand.RandomState.choice", __pyx_clineno, __pyx_lineno, __pyx_filename); __Pyx_RefNannyFinishContext(); return NULL; __pyx_L4_argument_unpacking_done:; @@ -6857,7 +6855,7 @@ static PyObject *__pyx_pf_6mtrand_11RandomState_12sample(PyObject *__pyx_v_self, * if replace: * if None != p: # <<<<<<<<<<<<<< * x = np.arange(pop_size) - * number_each = np.random.multinomial(size, p) + * number_each = self.multinomial(size, p) */ __pyx_t_1 = PyObject_RichCompare(Py_None, __pyx_v_p, Py_NE); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1018; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_1); @@ -6869,7 +6867,7 @@ static PyObject *__pyx_pf_6mtrand_11RandomState_12sample(PyObject *__pyx_v_self, * if replace: * if None != p: * x = np.arange(pop_size) # <<<<<<<<<<<<<< - * number_each = np.random.multinomial(size, p) + * number_each = self.multinomial(size, p) * idx = np.repeat(x, number_each) */ __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1019; __pyx_clineno = __LINE__; goto __pyx_L1_error;} @@ -6892,18 +6890,12 @@ static PyObject *__pyx_pf_6mtrand_11RandomState_12sample(PyObject *__pyx_v_self, /* "mtrand.pyx":1020 * if None != p: * x = np.arange(pop_size) - * number_each = np.random.multinomial(size, p) # <<<<<<<<<<<<<< + * number_each = self.multinomial(size, p) # <<<<<<<<<<<<<< * idx = np.repeat(x, number_each) * self.shuffle(idx) */ - __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_3); - __pyx_t_1 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__random); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_1); - __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; - __pyx_t_3 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__multinomial); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_3 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__multinomial); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_3); - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1020; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(((PyObject *)__pyx_t_1)); __Pyx_INCREF(__pyx_v_size); @@ -6921,7 +6913,7 @@ static PyObject *__pyx_pf_6mtrand_11RandomState_12sample(PyObject *__pyx_v_self, /* "mtrand.pyx":1021 * x = np.arange(pop_size) - * number_each = np.random.multinomial(size, p) + * number_each = self.multinomial(size, p) * idx = np.repeat(x, number_each) # <<<<<<<<<<<<<< * self.shuffle(idx) * else: @@ -6947,7 +6939,7 @@ static PyObject *__pyx_pf_6mtrand_11RandomState_12sample(PyObject *__pyx_v_self, __pyx_t_3 = 0; /* "mtrand.pyx":1022 - * number_each = np.random.multinomial(size, p) + * number_each = self.multinomial(size, p) * idx = np.repeat(x, number_each) * self.shuffle(idx) # <<<<<<<<<<<<<< * else: @@ -7146,7 +7138,7 @@ static PyObject *__pyx_pf_6mtrand_11RandomState_12sample(PyObject *__pyx_v_self, * p = p.copy() * found = np.zeros(size, dtype=np.int) # <<<<<<<<<<<<<< * while n_uniq < size: - * x = np.random.rand(size-n_uniq) + * x = self.rand(size-n_uniq) */ __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1035; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_3); @@ -7179,7 +7171,7 @@ static PyObject *__pyx_pf_6mtrand_11RandomState_12sample(PyObject *__pyx_v_self, * p = p.copy() * found = np.zeros(size, dtype=np.int) * while n_uniq < size: # <<<<<<<<<<<<<< - * x = np.random.rand(size-n_uniq) + * x = self.rand(size-n_uniq) * if n_uniq > 0: */ while (1) { @@ -7192,18 +7184,12 @@ static PyObject *__pyx_pf_6mtrand_11RandomState_12sample(PyObject *__pyx_v_self, /* "mtrand.pyx":1037 * found = np.zeros(size, dtype=np.int) * while n_uniq < size: - * x = np.random.rand(size-n_uniq) # <<<<<<<<<<<<<< + * x = self.rand(size-n_uniq) # <<<<<<<<<<<<<< * if n_uniq > 0: * p[found[0:n_uniq]] = 0 */ - __pyx_t_8 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_8 = PyObject_GetAttr(__pyx_v_self, __pyx_n_s__rand); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_8); - __pyx_t_4 = PyObject_GetAttr(__pyx_t_8, __pyx_n_s__random); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_4); - __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0; - __pyx_t_8 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__rand); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_8); - __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; __pyx_t_4 = PyNumber_Subtract(__pyx_v_size, __pyx_v_n_uniq); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_4); __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1037; __pyx_clineno = __LINE__; goto __pyx_L1_error;} @@ -7221,7 +7207,7 @@ static PyObject *__pyx_pf_6mtrand_11RandomState_12sample(PyObject *__pyx_v_self, /* "mtrand.pyx":1038 * while n_uniq < size: - * x = np.random.rand(size-n_uniq) + * x = self.rand(size-n_uniq) * if n_uniq > 0: # <<<<<<<<<<<<<< * p[found[0:n_uniq]] = 0 * p = p/p.sum() @@ -7233,7 +7219,7 @@ static PyObject *__pyx_pf_6mtrand_11RandomState_12sample(PyObject *__pyx_v_self, if (__pyx_t_6) { /* "mtrand.pyx":1039 - * x = np.random.rand(size-n_uniq) + * x = self.rand(size-n_uniq) * if n_uniq > 0: * p[found[0:n_uniq]] = 0 # <<<<<<<<<<<<<< * p = p/p.sum() @@ -7482,7 +7468,7 @@ static PyObject *__pyx_pf_6mtrand_11RandomState_12sample(PyObject *__pyx_v_self, __Pyx_XDECREF(__pyx_t_4); __Pyx_XDECREF(__pyx_t_7); __Pyx_XDECREF(__pyx_t_8); - __Pyx_AddTraceback("mtrand.RandomState.sample", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_AddTraceback("mtrand.RandomState.choice", __pyx_clineno, __pyx_lineno, __pyx_filename); __pyx_r = NULL; __pyx_L0:; __Pyx_XDECREF(__pyx_v_pop_size); @@ -7758,7 +7744,7 @@ static PyObject *__pyx_pf_6mtrand_11RandomState_13uniform(PyObject *__pyx_v_self */ static PyObject *__pyx_pf_6mtrand_11RandomState_14rand(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ -static char __pyx_doc_6mtrand_11RandomState_14rand[] = "\n rand(d0, d1, ..., dn)\n\n Random values in a given shape.\n\n Create an array of the given shape and propagate it with\n random samples from a uniform distribution\n over ``[0, 1)``.\n\n Parameters\n ----------\n d0, d1, ..., dn : int\n Shape of the output.\n\n Returns\n -------\n out : ndarray, shape ``(d0, d1, ..., dn)``\n Random values.\n\n See Also\n --------\n random\n\n Notes\n -----\n This is a convenience function. If you want an interface that\n takes a shape-tuple as the first argument, refer to\n `random`.\n\n Examples\n --------\n >>> np.random.rand(3,2)\n array([[ 0.14022471, 0.96360618], #random\n [ 0.37601032, 0.25528411], #random\n [ 0.49313049, 0.94909878]]) #random\n\n "; +static char __pyx_doc_6mtrand_11RandomState_14rand[] = "\n rand(d0, d1, ..., dn)\n\n Random values in a given shape.\n\n Create an array of the given shape and propagate it with\n random samples from a uniform distribution\n over ``[0, 1)``.\n\n Parameters\n ----------\n d0, d1, ..., dn : int\n Shape of the output.\n\n Returns\n -------\n out : ndarray, shape ``(d0, d1, ..., dn)``\n Random values.\n\n See Also\n --------\n random\n\n Notes\n -----\n This is a convenience function. If you want an interface that\n takes a shape-tuple as the first argument, refer to\n np.random.random_sample . \n\n Examples\n --------\n >>> np.random.rand(3,2)\n array([[ 0.14022471, 0.96360618], #random\n [ 0.37601032, 0.25528411], #random\n [ 0.49313049, 0.94909878]]) #random\n\n "; static PyObject *__pyx_pf_6mtrand_11RandomState_14rand(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { PyObject *__pyx_v_args = 0; PyObject *__pyx_r = NULL; @@ -19692,7 +19678,7 @@ static PyMethodDef __pyx_methods_6mtrand_RandomState[] = { {__Pyx_NAMESTR("tomaxint"), (PyCFunction)__pyx_pf_6mtrand_11RandomState_9tomaxint, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_6mtrand_11RandomState_9tomaxint)}, {__Pyx_NAMESTR("randint"), (PyCFunction)__pyx_pf_6mtrand_11RandomState_10randint, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_6mtrand_11RandomState_10randint)}, {__Pyx_NAMESTR("bytes"), (PyCFunction)__pyx_pf_6mtrand_11RandomState_11bytes, METH_O, __Pyx_DOCSTR(__pyx_doc_6mtrand_11RandomState_11bytes)}, - {__Pyx_NAMESTR("sample"), (PyCFunction)__pyx_pf_6mtrand_11RandomState_12sample, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_6mtrand_11RandomState_12sample)}, + {__Pyx_NAMESTR("choice"), (PyCFunction)__pyx_pf_6mtrand_11RandomState_12choice, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_6mtrand_11RandomState_12choice)}, {__Pyx_NAMESTR("uniform"), (PyCFunction)__pyx_pf_6mtrand_11RandomState_13uniform, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_6mtrand_11RandomState_13uniform)}, {__Pyx_NAMESTR("rand"), (PyCFunction)__pyx_pf_6mtrand_11RandomState_14rand, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_6mtrand_11RandomState_14rand)}, {__Pyx_NAMESTR("randn"), (PyCFunction)__pyx_pf_6mtrand_11RandomState_15randn, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_6mtrand_11RandomState_15randn)}, @@ -20072,6 +20058,7 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = { {&__pyx_n_s__binomial, __pyx_k__binomial, sizeof(__pyx_k__binomial), 0, 0, 1, 1}, {&__pyx_n_s__bytes, __pyx_k__bytes, sizeof(__pyx_k__bytes), 0, 0, 1, 1}, {&__pyx_n_s__chisquare, __pyx_k__chisquare, sizeof(__pyx_k__chisquare), 0, 0, 1, 1}, + {&__pyx_n_s__choice, __pyx_k__choice, sizeof(__pyx_k__choice), 0, 0, 1, 1}, {&__pyx_n_s__copy, __pyx_k__copy, sizeof(__pyx_k__copy), 0, 0, 1, 1}, {&__pyx_n_s__cov, __pyx_k__cov, sizeof(__pyx_k__cov), 0, 0, 1, 1}, {&__pyx_n_s__cumsum, __pyx_k__cumsum, sizeof(__pyx_k__cumsum), 0, 0, 1, 1}, @@ -20145,7 +20132,6 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = { {&__pyx_n_s__repeat, __pyx_k__repeat, sizeof(__pyx_k__repeat), 0, 0, 1, 1}, {&__pyx_n_s__replace, __pyx_k__replace, sizeof(__pyx_k__replace), 0, 0, 1, 1}, {&__pyx_n_s__right, __pyx_k__right, sizeof(__pyx_k__right), 0, 0, 1, 1}, - {&__pyx_n_s__sample, __pyx_k__sample, sizeof(__pyx_k__sample), 0, 0, 1, 1}, {&__pyx_n_s__scale, __pyx_k__scale, sizeof(__pyx_k__scale), 0, 0, 1, 1}, {&__pyx_n_s__searchsorted, __pyx_k__searchsorted, sizeof(__pyx_k__searchsorted), 0, 0, 1, 1}, {&__pyx_n_s__seed, __pyx_k__seed, sizeof(__pyx_k__seed), 0, 0, 1, 1}, @@ -22001,9 +21987,9 @@ PyMODINIT_FUNC PyInit_mtrand(void) /* "mtrand.pyx":917 * * - * def sample(self, a, size, replace=True, p=None): # <<<<<<<<<<<<<< + * def choice(self, a, size=1, replace=True, p=None): # <<<<<<<<<<<<<< * """ - * sample(a, size[, replace, p]) + * choice(a, size=1, replace=True, p=None) */ __pyx_t_4 = __Pyx_PyBool_FromLong(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 917; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_4); @@ -22218,7 +22204,7 @@ PyMODINIT_FUNC PyInit_mtrand(void) * get_state = _rand.get_state * set_state = _rand.set_state # <<<<<<<<<<<<<< * random_sample = _rand.random_sample - * sample = _rand.sample + * choice = _rand.choice */ __pyx_t_4 = __Pyx_GetName(__pyx_m, __pyx_n_s___rand); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4473; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_4); @@ -22232,7 +22218,7 @@ PyMODINIT_FUNC PyInit_mtrand(void) * get_state = _rand.get_state * set_state = _rand.set_state * random_sample = _rand.random_sample # <<<<<<<<<<<<<< - * sample = _rand.sample + * choice = _rand.choice * randint = _rand.randint */ __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s___rand); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4474; __pyx_clineno = __LINE__; goto __pyx_L1_error;} @@ -22246,21 +22232,21 @@ PyMODINIT_FUNC PyInit_mtrand(void) /* "mtrand.pyx":4475 * set_state = _rand.set_state * random_sample = _rand.random_sample - * sample = _rand.sample # <<<<<<<<<<<<<< + * choice = _rand.choice # <<<<<<<<<<<<<< * randint = _rand.randint * bytes = _rand.bytes */ __pyx_t_4 = __Pyx_GetName(__pyx_m, __pyx_n_s___rand); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_4); - __pyx_t_1 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__sample); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_1 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__choice); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_1); __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; - if (PyObject_SetAttr(__pyx_m, __pyx_n_s__sample, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyObject_SetAttr(__pyx_m, __pyx_n_s__choice, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4475; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; /* "mtrand.pyx":4476 * random_sample = _rand.random_sample - * sample = _rand.sample + * choice = _rand.choice * randint = _rand.randint # <<<<<<<<<<<<<< * bytes = _rand.bytes * uniform = _rand.uniform @@ -22274,7 +22260,7 @@ PyMODINIT_FUNC PyInit_mtrand(void) __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; /* "mtrand.pyx":4477 - * sample = _rand.sample + * choice = _rand.choice * randint = _rand.randint * bytes = _rand.bytes # <<<<<<<<<<<<<< * uniform = _rand.uniform diff --git a/numpy/random/mtrand/mtrand.pyx b/numpy/random/mtrand/mtrand.pyx index 3772fa07a..3aaefe92e 100644 --- a/numpy/random/mtrand/mtrand.pyx +++ b/numpy/random/mtrand/mtrand.pyx @@ -914,9 +914,9 @@ cdef class RandomState: return bytestring - def sample(self, a, size, replace=True, p=None): + def choice(self, a, size=1, replace=True, p=None): """ - sample(a, size[, replace, p]) + choice(a, size=1, replace=True, p=None) Generates a random sample from a given 1-D array @@ -956,33 +956,33 @@ cdef class RandomState: --------- Generate a uniform random sample from np.arange(5) of size 3: - >>> np.random.sample(5, 3) + >>> np.random.choice(5, 3) array([0, 3, 4]) >>> #This is equivalent to np.random.randint(0,5,3) Generate a non-uniform random sample from np.arange(5) of size 3: - >>> np.random.sample(5, 3, p=[0.1, 0, 0.3, 0.6, 0]) + >>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0]) array([3, 3, 0]) Generate a uniform random sample from np.arange(5) of size 3 without replacement: - >>> np.random.sample(5, 3, replace=False) + >>> np.random.choice(5, 3, replace=False) array([3,1,0]) >>> #This is equivalent to np.random.shuffle(np.arange(5))[:3] Generate a non-uniform random sample from np.arange(5) of size 3 without replacement: - >>> np.random.sample(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0]) + >>> np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0]) array([2, 3, 0]) Any of the above can be repeated with an arbitrary array-like instead of just integers. For instance: >>> aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher'] - >>> np.random.sample(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3]) + >>> np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3]) array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'], dtype='|S11') @@ -1017,7 +1017,7 @@ cdef class RandomState: if replace: if None != p: x = np.arange(pop_size) - number_each = np.random.multinomial(size, p) + number_each = self.multinomial(size, p) idx = np.repeat(x, number_each) self.shuffle(idx) else: @@ -1034,7 +1034,7 @@ cdef class RandomState: p = p.copy() found = np.zeros(size, dtype=np.int) while n_uniq < size: - x = np.random.rand(size-n_uniq) + x = self.rand(size-n_uniq) if n_uniq > 0: p[found[0:n_uniq]] = 0 p = p/p.sum() @@ -4472,7 +4472,7 @@ seed = _rand.seed get_state = _rand.get_state set_state = _rand.set_state random_sample = _rand.random_sample -sample = _rand.sample +choice = _rand.choice randint = _rand.randint bytes = _rand.bytes uniform = _rand.uniform diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 25277b6ba..449d6b410 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -116,39 +116,39 @@ class TestRandomDist(TestCase): [ 0.4575674820298663 , 0.7781880808593471 ]]) np.testing.assert_array_almost_equal(actual, desired, decimal=15) - def test_sample_uniform_replace(self): + def test_choice_uniform_replace(self): np.random.seed(self.seed) - actual = np.random.sample(4, 4) + actual = np.random.choice(4, 4) desired = np.array([2, 3, 2, 3]) np.testing.assert_array_equal(actual, desired) - def test_sample_nonuniform_replace(self): + def test_choice_nonuniform_replace(self): np.random.seed(self.seed) - actual = np.random.sample(4, 4, p=[0.4, 0.4, 0.1, 0.1]) + actual = np.random.choice(4, 4, p=[0.4, 0.4, 0.1, 0.1]) desired = np.array([1, 0, 3, 0]) np.testing.assert_array_equal(actual, desired) - def test_sample_uniform_noreplace(self): + def test_choice_uniform_noreplace(self): np.random.seed(self.seed) - actual = np.random.sample(4, 3, replace=False) + actual = np.random.choice(4, 3, replace=False) desired = np.array([0, 1, 3]) np.testing.assert_array_equal(actual, desired) - def test_sample_nonuniform_noreplace(self): + def test_choice_nonuniform_noreplace(self): np.random.seed(self.seed) - actual = np.random.sample(4, 3, replace=False, + actual = np.random.choice(4, 3, replace=False, p=[0.1, 0.3, 0.5, 0.1]) desired = np.array([2, 1, 3]) np.testing.assert_array_equal(actual, desired) - def test_sample_noninteger(self): + def test_choice_noninteger(self): np.random.seed(self.seed) - actual = np.random.sample(['a', 'b', 'c', 'd'], 4) + actual = np.random.choice(['a', 'b', 'c', 'd'], 4) desired = np.array(['c', 'd', 'c', 'd']) np.testing.assert_array_equal(actual, desired) - def test_sample_exceptions(self): - sample = np.random.sample + def test_choice_exceptions(self): + sample = np.random.choice assert_raises(ValueError, sample, -1,3) assert_raises(ValueError, sample, [[1,2],[3,4]], 3) assert_raises(ValueError, sample, [], 3) |