首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >随机分配一些元素,但加权到一个列表中,该列表应该只包含整数

随机分配一些元素,但加权到一个列表中,该列表应该只包含整数
EN

Stack Overflow用户
提问于 2021-11-11 12:24:36
回答 1查看 35关注 0票数 0

我有一个名为employees_summedlen(employees_summed)=9081列表,其中包含员工数量(每个公司)。

我的目标是根据员工的数量,在这个列表中分配一定数量的元素bev_calc,比如bev_calc=2000

我尝试使用numpy.random.choice,但它没有分配给定的数字,而是返回列表本身的加权平均值:

代码语言:javascript
复制
n = sum(employees_summed)
percentage_list = [x / n for x in employees_summed]
    weightedList = choice(employees_summed, len(employees_summed), percentage_list)

我们的目标是做类似以下的事情:

代码语言:javascript
复制
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?随机分布,但根据一家公司有多少员工来权衡?

EN

回答 1

Stack Overflow用户

发布于 2021-11-11 13:21:45

这不是一种有效的方法,但我认为下面的代码可以满足您的需求:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69928284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档