获取 IP 地址数据集的单变量概率密度函数

问题描述 投票:0回答:1

我有两个简单的数据集,其中有 10k IP 地址,编码为 整数(因此数据是离散的,可以采用 1 到 4B 之间的任何数字范围)。

仅供参考:一个数据集是在网络上捕获的真实数据集,而另一个数据集是合成数据集。归根结底,我想看看合成的(通过 AI/ML 生成的)与真实的相比有多好。但我一开始就陷入困境:D

由于数据集的分布未知,但不遵循任何已知的分布,我想计算它们的PDF(然后比较它们的相似程度)。

我的两个数据集被称为

p

q
,都是 IP 地址数组(作为整数)。

我不是概率论专家,所以请耐心听我说:)

由于我最终想比较这两个概率,为了计算它们的 PDF,我采用了

p

q
 中存在的所有可能事件(即 IP 地址)。为此,我使用 
numpy
 在 Python 中执行以下操作:

import numpy as np import pandas as pd q=np.array(real_data_1m.srcip) # p=np.array(syn_data_1m.srcip) #get all possible discrete events from p and q px=np.array(list(set(p))) #use set here to remove duplicates qx=np.array(list(set(q))) #use set here to remove duplicates #concatenate px and qx mx=np.concatenate([px,qx]) mx.sort() #sort them, as they are anyway integers mx=np.array(list(set(mx))) #remove duplicates by creating a set #mx.reshape((len(mx),1)) #reshape from 1D to nD, where n=len(mx)
然后,为了计算 PDF,我创建了一个简单的函数 

create_prob_dist()

 来帮助实现这一目标。

def create_prob_dist(data: np.array, sample_space: np.array): #number of all events sum_of_events = sample_space.size #get the counts of each event via pandas.crosstab() data_counts = pd.crosstab(index='counts', columns=data) #create probabilities for each event prob_dist=dict() for i in sample_space: if i in data_counts: prob_dist[i]=(data_counts[i]['counts'])/sum_of_events else: prob_dist[i]=0 return prob_dist
此函数不返回 PDF 本身。在此阶段,它返回一个 Python 字典,其中键是 

p

q
 中表示的可能 IP 地址,即 
mx
 中。因此,相应的值就是它们每个的概率。比如:dict[2130706433]=0.05,意味着数据集中IP地址127.0.0.1的概率是0.05。

有了这本概率词典后,我尝试绘制它,但随之而来的是我的问题:

#create true PDFs of p and q using mx p_pdf=create_prob_dist(p, mx) q_pdf=create_prob_dist(q, mx) #get the probability values only from the dictionary p_pdf=np.array(list(p_pdf.values())) #already sorted according to mx q_pdf=np.array(list(q_pdf.values())) #already sorted according to mx plt.figure() plt.plot(mx, q_pdf, 'g', label="Q") plt.plot(mx, p_pdf, 'r', label="P") plt.legend(loc="upper right") plt.show()

我知道秤或其他地方应该有问题,但我无法理解它。

我做错了什么?是Python调用错误还是PDF计算错误?

顺便说一句,

p

q
的纯直方图如下所示:

# plot a histogram of the two datasets to have a quick look at them plt.hist(np.array(syn_data_1m.srcip), bins=100) plt.hist(np.array(real_data_1m.srcip),bins=100, alpha=.5) plt.show()

python numpy probability-density probability-distribution probability-theory
1个回答
0
投票
感谢

slothrop,解决方案如下:

import numpy as np import pandas as pd q=np.array(real_data_1m.srcip) # p=np.array(syn_data_1m.srcip) #get all possible discrete events from p and q px=np.array(list(set(p))) #use set here to remove duplicates qx=np.array(list(set(q))) #use set here to remove duplicates #concatenate px and qx mx=np.concatenate([px,qx]) mx=np.array(list(set(mx))) #remove duplicates by creating a set # CALL SORT THE LAST TIME mx.sort() #sort them, as they are anyway integers #mx.reshape((len(mx),1)) #reshape from 1D to nD, where n=len(mx)
PDF 现在已经很好了:

© www.soinside.com 2019 - 2024. All rights reserved.