从条形图Zipf分布中获取条形图的百分比

2024-04-26 09:32:17 发布

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

我有一个数据集,它有20列和10.000行。我的计划是将数据集中的一些数据替换为NaN。我的任务是观察缺失值对数据集的影响

我的计划是使用Zipf分布生成每列的缺失百分比,然后根据这些百分比将一些值替换为NaN

例如,下面是我的代码:

import matplotlib.pyplot as plt
from scipy import special

import numpy as np
a = 1.01 # parameter
s = np.random.zipf(a, 200000)
count, bins, ignored = plt.hist(s[s<20], 20, density=True)

plt.show()

条形图如下所示: enter image description here

这是否可以得到条形图的百分比,这样我就可以根据条形图的百分比替换每列中的一些值?例如,第一列缺失80%,第二列缺失40%,第三列缺失25%,等等


Tags: 数据代码fromimportmatplotlibasnpplt
1条回答
网友
1楼 · 发布于 2024-04-26 09:32:17

你需要理解Zipf分布的定义。维基百科对这一点做了很好的解释。这是维基上的图片。 enter image description here

有两个重要参数a> 1N。参数a对您的行有影响(在上图中a = s),并且N是大小

当你们根据zipf分布生成数字时,你们必须考虑频率。在您的代码中,您使用了density=True,这意味着您的条高是标准化的,如果不使用此参数,您将看到确切的计数

column_rank = list(range(1,21))
a = 2.
N = 200000
s = np.random.zipf(a, N)
for i in column_rank: 
    print(i, ((len(s[s==i]))/N)*100)

结果是:

1 60.8245
2 15.265500000000001
3 6.7965
4 3.8015
5 2.4250000000000003
6 1.6760000000000002
7 1.2269999999999999
8 0.9535
9 0.72
10 0.6224999999999999
11 0.4775
12 0.42
13 0.357
14 0.29750000000000004
15 0.24849999999999997
16 0.22999999999999998
17 0.218
18 0.19849999999999998
19 0.1595
20 0.149

精确编号的绘图:

count, bins = s[s<21], 21
plt.hist(count, bins, align='left')
plt.xticks(np.arange(1, 21, 1))

结果是:

enter image description here

相关问题 更多 >