np.quantile()详解

numpy.quantile

numpy.quantile(a, q, axis=None, out=None, overwrite_input=False, interpolation=‘linear’, keepdims=False)

import numpy as np

arr = np.array([[0.77395605, 0.43887844, 0.85859792, 0.69736803 ],
                [0.09417735, 0.97562235, 0.7611397 , 0.78606431 ],
                [0.12811363, 0.45038594, 0.37079802, 0.92676499 ]])

axis=0

axis=0,先按列进行升序排列

[[0.09417735, 0.43887844, 0.37079802, 0.69736803 ],
 [0.12811363, 0.45038594, 0.7611397 , 0.78606431 ],
 [0.77395605, 0.97562235, 0.85859792, 0.92676499 ]]
np.quantile(arr, q=0.2, axis=0)
array([0.10775186, 0.44348144, 0.52693469, 0.73284654])
# n=3,q=0.2,n*p = 3*0.2 = 0.6, 0.6不足1按1看,那么在第(1,2)中间进行插值
# 0.4 为 2q
print(0.09417735 + 0.4*(0.12811363-0.09417735))
print(0.43887844 + 0.4*(0.45038594-0.43887844))
print(0.37079802 + 0.4*(0.7611397-0.37079802))
print(0.69736803 + 0.4*(0.78606431-0.69736803))
0.107751862
0.44348144
0.526934692
0.732846542
np.quantile(arr, q=0.4, axis=0)
array([0.12132637, 0.44808444, 0.68307136, 0.76832505])
# n=3,q=0.3,n*p = 3*0.4 = 1.2, 那么在第(1,2)中间进行插值
# 0.8 为 2q
print(0.09417735 + 0.8*(0.12811363-0.09417735))
print(0.43887844 + 0.8*(0.45038594-0.43887844))
print(0.37079802 + 0.8*(0.7611397-0.37079802))
print(0.69736803 + 0.8*(0.78606431-0.69736803))
0.12132637400000001
0.44808444
0.683071364
0.768325054

axis=1

# axis=1,先按行进行升序排列
arr.sort()
arr
array([[0.43887844, 0.69736803, 0.77395605, 0.85859792],
       [0.09417735, 0.7611397 , 0.78606431, 0.97562235],
       [0.12811363, 0.37079802, 0.45038594, 0.92676499]])
np.quantile(arr, q=0.75, axis=1, keepdims=True)
array([[0.79511652],
       [0.83345382],
       [0.5694807 ]])
# n=4,q=0.75,n*p = 4*0.75 = 3, 那么在第(3,4)中间进行插值
# 0.25 为 1-q
print(0.77395605+0.25*(0.85859792-0.77395605))
print(0.78606431+0.25*(0.97562235-0.78606431))
print(0.45038594+0.25*(0.92676499-0.45038594))
0.7951165175
0.83345382
0.5694807024999999

你可能感兴趣的:(巨硬的Numpy,python,numpy,机器学习)