6

I have an issue with random.sample function. Here is the code:

import random
import numpy as np


simulateData = np.random.normal(30, 2, 10000)

meanValues = np.zeros(1000)

for i in range(1000):


    dRange = range(0, len(simulateData))
    randIndex = np.random.sample(dRange, 30)
    randIndex.sort()
    rand = [simulateData[j] for j in randIndex]
    meanValues[i] = rand.mean()

This is the error:

TypeError                                 Traceback (most recent call last)
<ipython-input-368-92c8d9b7ecb0> in <module>()
 20 
 21     dRange = range(0, len(simulateData))
---> 22     randIndex = np.random.sample(dRange, 30)
 23     randIndex.sort()
 24     rand = [simulateData[i] for i in randIndex]

mtrand.pyx in mtrand.RandomState.random_sample   (numpy\random\mtrand\mtrand.c:10022)()

TypeError: random_sample() takes at most 1 positional argument (2 given)

I found several past references where such an error was supposedly addressed via changing import order like in my case above (random, before numpy). Supposedly random module gets overwritten somehow during the import while I can not imagine why would that be in a high level language. However in my case it did not work. I tried all possible variations but came with no solution

The problem in itself is an attempt to bootstrap: get random samples (equal size) from the initial distribution and measure the mean and std.

I am puzzled, especially since the solution which is supposed to work does not. I have Python 2.7

Please, help

Toly
  • 2,981
  • 8
  • 25
  • 35
  • Why do you think [`np.random.sample`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.sample.html#numpy.random.sample) takes two arguments? – user2357112 Sep 15 '15 at 03:52
  • in the definition there are 2: the array from which sample is taken and the second is the size of the sample. This is how numpy defines it – Toly Sep 15 '15 at 03:53
  • Uh, no. No it doesn't. What definition are you looking at? – user2357112 Sep 15 '15 at 03:56
  • http://stackoverflow.com/questions/23977524/pythons-random-module-made-inaccessible – Toly Sep 15 '15 at 04:01
  • And if you want to use the standard `random.sample` why not do `rand = random.sample(simulateData, 30)` – Pynchia Sep 15 '15 at 04:12
  • http://stackoverflow.com/questions/22741319/what-does-random-sample-method-in-python-do – Toly Sep 15 '15 at 04:41

5 Answers5

6

I guess you are confusing random.sample with np.random.sample() -

np.random.sample(size=None) - Return random floats in the half-open interval [0.0, 1.0).
       size : int or tuple of ints, optional Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.

random.sample(population, k) - Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.

You are using np.random.sample , but trying to pass it arguments as random.sample. I think you want to use random.sample , if so you should use it like -

randIndex = random.sample(dRange, 30)
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • @ Anand - Wow!! So much difference between np.random.sample and random.sample!! Unbelievable! It works now!! Thank you Something is wrong with the use of names:) – Toly Sep 15 '15 at 04:51
  • Be happy to (and did) but my rep is still too low (a newbie:)) Will try to get my rep high enough:) – Toly Sep 15 '15 at 16:35
4

You are trying to pass two arguments -- dRange and 30 -- to the sample function, but sample only allows one argument. Here's some of the documentation where it says this:

random_sample(size=None)

Return random floats in the half-open interval [0.0, 1.0).

Parameters
----------
size : int or tuple of ints, optional

The order of your imports should not be a problem.

For taking 30 random samples from an array, maybe you want numpy.choice instead:

np.random.choice(dRange, 30)
Jeff G
  • 908
  • 8
  • 18
  • Wow!! It worked! Actually in numpy random.choice has only one argument! I could not realize that np.random.choice and random.choice are SO different!!! – Toly Sep 15 '15 at 04:50
1

The problem is that you are using wrong module. For your purpose you need to use random.sample() not np.random.sample(). Meanwhile, in the last line of your code, you are using mean function with a list which should be corrected.

Corrected code:

import random
import numpy as np


simulateData = np.random.normal(30, 2, 10000)

meanValues = np.zeros(1000)

for i in range(1000):


    dRange = range(0, len(simulateData))
    randIndex = random.sample(dRange, 30)
    randIndex.sort()
    rand = [simulateData[j] for j in randIndex]
    meanValues[i] = np.asarray(rand).mean()
coatless
  • 20,011
  • 13
  • 69
  • 84
Tohid
  • 11
  • 1
1

The size parameter can be a tuple, so I guess what you wanted to achieve is:

randIndex = np.random.random(size=(dRange, 30))
emirc
  • 1,948
  • 1
  • 23
  • 38
0

you have to use

np.random.randint(a,b)
ou are using wrong module
Olives99
  • 64
  • 1
  • 10