Error: random_sample() takes at most 1 positional argument (2 given)
Solution 1
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)
Solution 2
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)
Solution 3
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()
Toly
Updated on August 16, 2022Comments
-
Toly over 1 year
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 over 8 yearsWow!! 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 over 8 years@ 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:)
-
Anand S Kumar over 8 yearsGlad we could be helpful. I would also like to request you to accept an answer, by clicking on the tick mark on the left side of the answer (whichever helped you the most) , it would be helpful for the community.
-
Toly over 8 yearsBe happy to (and did) but my rep is still too low (a newbie:)) Will try to get my rep high enough:)
-
Anand S Kumar over 8 yearsNo, you don't need rep to accept the answer, you need to click on the
tick
mark , not the up arrow.