zzz11411
zzz11411

Reputation: 45

python: how do I randomly sample a number of samples from a population?

I have generated 100 samples with specific mean and variance:

import numpy as np
mean = 0
variance = 0.1
std_dev = np.sqrt(variance)
t = np.random.normal(mean, std_dev, 100)
print(t)

I want to randomly sample 10 samples from this population. Is there a way to extract samples randomly?

Upvotes: 0

Views: 701

Answers (1)

Michael Gallo
Michael Gallo

Reputation: 73

You can use the np.random.choice() function to get a numpy array of 10 random samples (second parameter is the size of the array you want)

import numpy as np
mean = 0
variance = 0.1
std_dev = np.sqrt(variance)
t = np.random.normal(mean, std_dev, 100)
print(t)

sample = np.random.choice(t,10)
print(sample)

Upvotes: 1

Related Questions