网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Hive中的中位数是什么

本篇内容主要讲解“Hive中的中位数是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Hive中的中位数是什么”吧!

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册雅安服务器托管、营销软件、网站建设、忠县网站维护、网站推广。

关于求解中位数,我们知道在Python中直接有中位数处理函数(mean),比如在Python中求解一个中位数,代码很简单。

Python计算中位数

import numpy as np

nums = [1.1,2.2,3.3,4.4,5.5,6.6]

#均值

np.mean(nums)

#中位数

np.median(nums)

在hive中没有直接提供相关的mean函数,但官方提供了两个UDAF,percentile和percentile_approx。

我们看下官方是怎么说的

DOUBLEpercentile(BIGINT col, p)Returns the exact pthpercentile of a  column in the group (does not work with floating point types). p must be  between 0 and 1. NOTE: A true percentile can only be computed for integer  values. Use PERCENTILE_APPROX if your input is non-integral.

arraypercentile(BIGINT col, array(p1[, p2]...))Returns the exact percentiles p1, p2,  ... of a column in the group (does not work with floating point types). pimust  be between 0 and 1. NOTE: A true percentile can only be computed for integer  values. Use PERCENTILE_APPROX if your input is non-integral.

DOUBLEpercentile_approx(DOUBLE col, p [, B])Returns an approximate pthpercentile of  a numeric column (including floating point types) in the group. The B  parameter controls approximation accuracy at the cost of memory. Higher  values yield better approximations, and the default is 10,000. When the  number of distinct values in col is smaller than B, this gives an exact  percentile value.

arraypercentile_approx(DOUBLE col, array(p1[,  p2]...) [, B])Same as above, but accepts and returns an array of  percentile values instead of a single one.

请注意,官方文档上说了一句话:NOTE: A true percentile can only be computed for integer values. Use

PERCENTILE_APPROX if your input is non-integral.

也就是说,真正的中位数只能用percentile来计算,输入需要为整数类型,使用percentile_approx(输入为浮点型)计算得到的并不是真正的中位数,也就是所说的近似中位数,经过大量数据验证,有时候这个近似中位数和真正的中位数差别还是很大的。

如何对有小数的数据求取中位数呢?

可以把小数转换为整数,然后再求取中位数(如先✖️乘10000)

sparksql中也是如此求取中位数的,赶快去试一试吧!

到此,相信大家对“Hive中的中位数是什么”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


文章标题:Hive中的中位数是什么
本文链接:http://bjjierui.cn/article/iiggip.html

其他资讯