这是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 JavaFX TableView更新单元格,不更新对象值   在扫描器中使用分隔符的java   java OkHttp 4.9.2,连接无法重用,导致端口耗尽   eclipse中的c JNI:运行Java代码   java是否在出厂的所有硬件设备中都有/mnt/sdcard/Android/data文件夹(或等效文件夹)?   Java,在eclipse中访问资源文件夹中的图像   java为什么Bluemix dashDB操作抛出SqlSyntaxErrorException,SQLCODE=1667?   JavaHtmlUnitWebClient。getPage不处理javascript   Google API认证的java问题   java如何将JSON数组反序列化为Apache beam PCollection<javaObject>   ServerSocket停止接收命令,java/安卓   来自Java类的安卓 Toast消息   java如何自动重新加载应用程序引擎开发服务器?   java是否可以尝试/捕获一些东西来检查是否抛出了异常?   java如何做到这一点当我按下load game时,它不仅会加载信息,还会将您带到游戏中?   Java选项Xmx代表什么?   Java映射,它在插入时打印值   设置“ulimit c unlimited”后,java无法生成系统核心转储