这是p平方算法的python实现

psquare的Python项目详细描述


简介

这是this paper的python实现, 提出了一种动态计算中值和其他百分位数的启发式算法。 它的优点是在每次迭代时运行o(1),因此在 处理连续(快速)输入的数据。

安装

  1. 来源

    git clone git@gitlab.octo.com:bdalab/psquare.git
    cd psquare
    python setup.py install
    
  2. 使用pip

    pip install psquare
    

使用示例

在下面的例子中,我们将估计n(0,1)分布的95%的值 使用p平方算法。当我们不断从分布中提取数据时,我们会比较我们的估计值, 与numpy.percentile函数给出的真值进行比较。最后,我们将 使用psquare和numpy.percentile计算残差和执行时间。

importnumpyasnpimportmatplotlibimportmatplotlib.pyplotaspltimporttimefrompsquare.psquareimportPSquareNB_ITERATIONS=30000defrandom_generator():returnnp.random.normal(0,1,1)defexact_value_for_quantile(values,quantile):returnnp.percentile(values,quantile)defmain():values=[random_generator()for_inrange(5)]quantile_to_estimate=95psquare=PSquare(quantile_to_estimate)exact_quantiles=[]estimated_quantiles=[]psquare_exec_time=[]numpy_exec_time=[]forvalinvalues:# p square algorithm necessitates 5 values to startpsquare.update(val)for_inrange(NB_ITERATIONS):new_val=random_generator()values.append(new_val)psquare_start=time.time()psquare.update(new_val)estimated_quantiles.append(psquare.p_estimate())psquare_end=time.time()numpy_start=time.time()exact_quantiles.append(exact_value_for_quantile(values,quantile_to_estimate))numpy_end=time.time()psquare_exec_time.append(psquare_end-psquare_start)numpy_exec_time.append(numpy_end-numpy_start)matplotlib.rc('figure',figsize=(10,5))errors=np.abs(np.array(estimated_quantiles)-np.array(exact_quantiles))plt.plot(errors)plt.title('Absolute error between p-square predicted value and exact percentile value')plt.ylabel('Difference between exact percentile value and p-square estimation')plt.xlabel('Size of the dataset')plt.rcParams["figure.figsize"]=(10,5)plt.show()plt.plot(psquare_exec_time[1:],label='p-square')plt.plot(numpy_exec_time[1:],label='numpy percentile')plt.title('Execution time to compute percentile on a growing dataset')plt.ylabel('Execution time (in seconds)')plt.xlabel('Size of the dataset')plt.legend()plt.rcParams["figure.figsize"]=(10,5)plt.show()if__name__=='__main__':main()

估计值与准确值之间的误差

使用前面的示例,我们得到以下图:

  • 精确百分位数和预测百分位数之间的误差

  • p平方估计与numpypercentile函数之间的执行时间:

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java mahout创建带有首选项的基于项目的推荐程序   java Maven:过滤任何资源   swing为什么Java中的侦听器相互依赖?   java在TextView中显示json响应   drjava从txt文件中检索一个随机字,但没有得到任何输出,也没有编译错误   JWindow上的java JPanel,添加组件   安卓使用jcocoa将ios代码转换为java   除非调整帧的大小,否则java动画不起作用   从java代码创建Json文件   java使用jdom向现有xml添加内容   如何在java中设置socket写超时?   java将值拆分为两个随机数