np.percentile
numpy.percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False)
參數:
a : array,用來算分位數的對象,可以是多維的數組
q : array_like of float,介於0-100的float,用來計算是幾分位的參數,如四分之一位就是25,如要算兩個位置的數就(25,75)
axis : 坐標軸的方向,一維的就不用考慮了,多維的就用這個調整計算的維度方向,取值范圍0/1,默認值為沿着數組的展平版本計算百分位數
out : 輸出數據的存放對象,參數要與預期輸出有相同的形狀和緩沖區長度
overwrite_input : bool,默認False,為True時及計算直接在數組內存計算,計算后原數組無法保存
interpolation : 取值范圍{'linear', 'lower', 'higher', 'midpoint', 'nearest'}
默認liner,比如取中位數,但是中位數有兩個數字6和7,選不同參數來調整輸出
keepdims : bool,默認False,為真時取中位數的那個軸將保留在結果中
a = np.array([[10, 7, 4], [3, 2, 1]]) a ''' array([[10, 7, 4], [ 3, 2, 1]]) ''' np.percentile(a, 50) #3.5 np.percentile(a, 50, axis=0) #array([[ 6.5, 4.5, 2.5]]) np.percentile(a, 50, axis=1) #array([ 7., 2.]) np.percentile(a, 50, axis=1, keepdims=True) ''' array([[ 7.], [ 2.]]) '''
pandas.DataFrame.quantile
DataFrame.quantile(q=0.5, axis=0, numeric_only=True, interpolation='linear')
參數:
- q:float or array-like, default 0.5 (50% quantile),0 <= q <= 1之間的值,即要計算的分位數
- axis:{0, 1, ‘index’, ‘columns’}, default 0,對於行,等於0或“索引”,對於列,等於1或“列”
- numeric_only:bool, default True,如果為False,則還將計算日期時間和時間增量數據的分位數
- interpolation:{‘linear’, ‘lower’, ‘higher’, ‘midpoint’, ‘nearest’},當所需分位數位於兩個數據點i和j之間時,此可選參數指定要使用的插值方法
返回
Series or DataFrame
- 如果
q
是數組,則將返回DataFrame,其中index為q
,列為self的列,值為分位數。 - 如果
q
為float,則在index是self的列,值是分位數
df = pd.DataFrame(np.array([[1, 1], [2, 10], [3, 100], [4, 100]]), columns=['a', 'b'])
df.quantile(.1) ''' a 1.3 b 3.7 Name: 0.1, dtype: float64 ''' df.quantile([.1, .5]) ''' a b 0.1 1.3 3.7 0.5 2.5 55.0 '''