欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中np.percentile和df.quantile分位數(shù)詳解

 更新時間:2023年05月09日 16:21:35   作者:小小嘍啰  
分位數(shù)(Quantile)亦稱分位點是指將一個隨機變量的概率分布范圍分為幾個等份的數(shù)值點,下面這篇文章主要給大家介紹了關于Python中np.percentile和df.quantile分位數(shù)的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

np.percentile

numpy.percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False)

參數(shù):

  • a : array,用來算分位數(shù)的對象,可以是多維的數(shù)組
  • q : array_like of float,介于0-100的float,用來計算是幾分位的參數(shù),如四分之一位就是25,如要算兩個位置的數(shù)就(25,75)
  • axis : 坐標軸的方向,一維的就不用考慮了,多維的就用這個調整計算的維度方向,取值范圍0/1,默認值為沿著數(shù)組的展平版本計算百分位數(shù)
  • out : 輸出數(shù)據(jù)的存放對象,參數(shù)要與預期輸出有相同的形狀和緩沖區(qū)長度
  • overwrite_input : bool,默認False,為True時及計算直接在數(shù)組內存計算,計算后原數(shù)組無法保存
  • interpolation : 取值范圍{'linear', 'lower', 'higher', 'midpoint', 'nearest'}
  • 默認liner,比如取中位數(shù),但是中位數(shù)有兩個數(shù)字6和7,選不同參數(shù)來調整輸出
  • keepdims : bool,默認False,為真時取中位數(shù)的那個軸將保留在結果中
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')

參數(shù):

  • q:float or array-like, default 0.5 (50% quantile),0 <= q <= 1之間的值,即要計算的分位數(shù)
  • axis:{0, 1, ‘index’, ‘columns’}, default 0,對于行,等于0或“索引”,對于列,等于1或“列”
  • numeric_only:bool, default True,如果為False,則還將計算日期時間和時間增量數(shù)據(jù)的分位數(shù)
  • interpolation:{‘linear’, ‘lower’, ‘higher’, ‘midpoint’, ‘nearest’},當所需分位數(shù)位于兩個數(shù)據(jù)點i和j之間時,此可選參數(shù)指定要使用的插值方法

返回

Series or DataFrame

  • 如果q是數(shù)組,則將返回DataFrame,其中index為q,列為self的列,值為分位數(shù)。
  • 如果q為float,則在index是self的列,值是分位數(shù)
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
'''

總結 

到此這篇關于Python中np.percentile和df.quantile分位數(shù)詳解的文章就介紹到這了,更多相關np.percentile和df.quantile分位數(shù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論