我有一个名为employees_summed的len(employees_summed)=9081列表,其中包含员工数量(每个公司)。
我的目标是根据员工的数量,在这个列表中分配一定数量的元素bev_calc,比如bev_calc=2000。
我尝试使用numpy.random.choice,但它没有分配给定的数字,而是返回列表本身的加权平均值:
n = sum(employees_summed)
percentage_list = [x / n for x in employees_summed]
weightedList = choice(employees_summed, len(employees_summed), percentage_list)我们的目标是做类似以下的事情:
bev_company = []
percentage = bev_calc / n
for i in employees_summed:
cars_per_building = percentage * i
bev_company.append(cars_per_building)但是由于employees_summed的长度,返回的数字大多小于1,并且round(i)会删除太多的bev_calc,因为它将舍入为0。
有没有办法做到这一点,这样bev_company就有整数作为值,这些整数加起来大致等于2000?随机分布,但根据一家公司有多少员工来权衡?
发布于 2021-11-11 13:21:45
这不是一种有效的方法,但我认为下面的代码可以满足您的需求:
import numpy as np
np.random.seed(0)
Companies = 10
employees = np.random.randint(5, 100, size = (Companies,))
cars = 100
# make an initial guess of the car distribution and floor its values
guess = employees/employees.sum()*cars
floor_guess = np.floor(guess)
# if the floored values dont add up the the number of cars, scale the
# distribution such that the company with the closest value is rounded up.
while floor_guess.sum()!=100:
distances_up = guess%1
closest = np.argmax(distances_up)
# if there are more than two companies that are the same distance to
# their ceiling, choose one randomly.
if np.sum(distances_up==distances_up[closest])>1:
idx = np.random.choice(np.where(distances_up==distances_up[closest])[0])
guess[idx] = np.ceil(guess[idx])
floor_guess = np.floor(guess)
continue
scale = np.ceil(guess[closest])/guess[closest]
guess *= scale
floor_guess = np.floor(guess)https://stackoverflow.com/questions/69928284
复制相似问题