Issue
There is a program that needs to get 100 samples from 1000 randomly generated number distributed in [1, 500]
. How can do I get a random sample from the output?
I wrote the following:
N = 1000
x = 1 + 500 * np.random.rand(N)
sp_x = random.sample(x, 100)
I am getting an error:
TypeError: Population must be a sequence or set. For dicts, use list(d).
Solution
From your code, the resulting x
is a numpy array.
To check type(x)
output: numpy.ndarray
But the function random.sample(sequence, k)
can only take in a sequence that is a list
, tuple
, string
, or set
. So your code could be:
import random
N=1000
x = 1+500*np.random.rand(N)
x = list(x)
sp_x = random.sample(x,100)
print(len(sp_x))
output: 100
Answered By - perpetualstudent
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.