溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Hive中的中位數(shù)是什么

發(fā)布時間:2021-07-12 09:27:56 來源:億速云 閱讀:154 作者:chen 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“Hive中的中位數(shù)是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“Hive中的中位數(shù)是什么”吧!

關(guān)于求解中位數(shù),我們知道在Python中直接有中位數(shù)處理函數(shù)(mean),比如在Python中求解一個中位數(shù),代碼很簡單。

Python計算中位數(shù)

import numpy as np

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

#均值

np.mean(nums)

#中位數(shù)

np.median(nums)

在hive中沒有直接提供相關(guān)的mean函數(shù),但官方提供了兩個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.

也就是說,真正的中位數(shù)只能用percentile來計算,輸入需要為整數(shù)類型,使用percentile_approx(輸入為浮點型)計算得到的并不是真正的中位數(shù),也就是所說的近似中位數(shù),經(jīng)過大量數(shù)據(jù)驗證,有時候這個近似中位數(shù)和真正的中位數(shù)差別還是很大的。

如何對有小數(shù)的數(shù)據(jù)求取中位數(shù)呢?

可以把小數(shù)轉(zhuǎn)換為整數(shù),然后再求取中位數(shù)(如先??乘10000)

sparksql中也是如此求取中位數(shù)的,趕快去試一試吧!

到此,相信大家對“Hive中的中位數(shù)是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI