如何在Python中分配值?

2024-10-02 18:28:31 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我试图计算CSGO皮肤上漂浮物的可能性。 浮点值是介于0和1之间的值,它们在五个部分中进行区分

工厂新(0-0.07)3%,最小磨损(0.07-0.14)24%,现场测试(0.14-0.38)33%,磨损良好(0.38-0.45)24%,战痕(0.45-1.0)16%

正如您所见,浮点值之间的分布并不均匀,而是经过加权。但是,在每个部分中,值会均匀分布,例如: https://blog.csgofloat.com/content/images/2020/07/image-6.png

然后,当您引入浮点数时,它会变得棘手,这意味着浮点数不再介于0和1之间,而是介于0.14和0.65之间

该值的计算方法如下:

  1. 根据其权重选择节
  2. 该部分范围内的浮动是随机生成的
  3. 根据以下公式计算最终浮点数: 最终浮动=浮动*(最大浮动-最小浮动)+最小浮动 float是随机生成的值,max和min_浮动上下盖(在本例中为0.14和0.65)

现在,我想计算五个部分中具有cap的蒙皮的分布

我该怎么做? 先谢谢你


Tags: httpscom工厂blogcontent可能性现场区分
1条回答
网友
1楼 · 发布于 2024-10-02 18:28:31

使用numpy库很简单:

import numpy as np

# input data
n_types = 5
types_weights = np.array([0.03, 0.24, 0.33, 0.24, 0.16])
types_intervals = np.array([0.0, 0.07, 0.14, 0.38, 0.45, 1.0])

# simulate distribution, by generating `n_samples` random floats 
n_samples = 1000000
type_samples = np.random.choice(range(n_types), p=types_weights, size=n_samples, replace=True, )
float_ranges_begin = types_intervals[type_samples]
float_ranges_end = types_intervals[type_samples + 1]
float_samples = float_ranges_begin + np.random.rand(n_samples) * (float_ranges_end - float_ranges_begin)

# plot results
import matplotlib.pyplot as plt
plt.figure(figsize=(10,8))
plt.hist(float_samples, bins=100, density=True, rwidth=0.8)
# to see types names instead
# plt.xticks(types_intervals, types + ['1.0'], rotation='vertical', fontsize=16)
plt.xlabel('Float', fontsize=16)
plt.ylabel('Probability density', fontsize=16);

enter image description here

编辑

如果您想找到准确的发行版,那么也很容易,尽管我并不完全清楚您的“可伸缩”需求

n_types = 5
types = ['Factory New', 'Minimal Wear', 'Field-Tested', 'Well-Worn', 'Battle-Scarred']
types_weights = np.array([0.03, 0.24, 0.33, 0.24, 0.16])
types_intervals = np.array([-0.0001, 0.07, 0.14, 0.38, 0.45, 1.0])

# corerspond to top values on my plot, approximately [0.4 3.4 1.37 3.4 0.3]
types_probability_density = types_weights / (types_intervals[1:] - types_intervals[:-1])

def float_probability_density(x):
  types = np.searchsorted(types_intervals, x) - 1
  return types_probability_density[types]

sample_floats = np.linspace(0.0, 1.0, 100)
plt.figure(figsize=(16,8))
plt.bar(sample_floats, float_probability_density(sample_floats), width=0.005)

enter image description here

相关问题 更多 >