hive 中位数_Hive的中位数

在Hive中,虽然没有直接的中位数函数,但可以通过percentile和percentile_approx这两个UDAF来计算。对于整数,可以使用percentile函数获取精确的中位数,而对于非整数(浮点类型),percentile_approx则提供近似值,且当B参数值增加时,近似精度提高。值得注意的是,percentile_approx计算的中位数在大数据集上可能与真实值有较大偏差。要对包含小数的数据求中位数,可以将其转换为整数类型后再进行计算。
摘要由CSDN通过智能技术生成

关于求解中位数,我们知道在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.

DOUBLEpercentil

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值