前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >你知道Hive中的中位数吗

你知道Hive中的中位数吗

作者头像
数据社
发布2020-05-25 10:34:31
1.8K0
发布2020-05-25 10:34:31
举报
文章被收录于专栏:数据社数据社

关于求解中位数,我们知道在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中也是如此求取中位数的,赶快去试一试吧!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-03-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据社 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档