Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate number array based on a range, std and mean condition
import numpy as np
import scipy.stats
from pprint import pprint
import matplotlib.pyplot as plt
import logging
logging.basicConfig(level=logging.DEBUG)
# source: https://stackoverflow.com/a/50629604/4257991
def my_distribution(min_val, max_val, mean, std):
scale = max_val - min_val
location = min_val
# Mean and standard deviation of the unscaled beta distribution
unscaled_mean = (mean - min_val) / scale
unscaled_var = (std / scale) ** 2
# Computation of alpha and beta can be derived from mean and variance formulas
t = unscaled_mean / (1 - unscaled_mean)
beta = ((t / unscaled_var) - (t * t) - (2 * t) - 1) / ((t * t * t) + (3 * t * t) + (3 * t) + 1)
alpha = beta * t
# Not all parameters may produce a valid distribution
if alpha <= 0 or beta <= 0:
raise ValueError('Cannot create distribution for the given parameters.')
# Make scaled beta distribution with computed parameters
return scipy.stats.beta(alpha, beta, scale=scale, loc=location)
def generate_number(number_of_person):
# np.random.seed(13)
# stat value from real eye images
min_val = 1
max_val = 47
mean = 8.369697
std = 5.630085
my_dist = my_distribution(min_val, max_val, mean, std)
# # Plot distribution PDF
# x = np.linspace(min_val, max_val, 165)
#
# # print(my_dist.pdf(x))
# plt.plot(x, my_dist.pdf(x))
# # Stats
# print('mean:', my_dist.mean(), 'std:', my_dist.std())
# Get a large sample to check bounds
sample = my_dist.rvs(size=number_of_person)
# print('min:', sample.min(), 'max:', sample.max())
# print(sample)
# plt.show()
sample = np.round(sample).astype('int16')
return sample
num_images = generate_number(50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.