第三四分位数 と
python の
結果を
四分位範囲の 計算が できる ライブラリに ついて
調べた
* numpy
- scipy.stats.iqr
これは直接、 四分位範囲を 計算できます。
numpy を 使う
numpy を
numpy.percentile を
- 四分位範囲の
計算
import numpy as np x = [57,59,60,100,59,58,57,58,300,61,62,60,62,58,57] # 75パーセント点、25パーセント点を取得 q75, q25 = np.percentile(x, [75 ,25]) iqr = q75 - q25 print("25パーセント点", q25) print("75パーセント点", q75) print("四分位範囲", iqr)
25パーセント点 58.0 75パーセント点 61.5 四分位範囲 3.5
- 外れ値の
計算
import numpy as np def identify_outliers(ys): quartile_1, quartile_3 = np.percentile(ys, [25, 75]) iqr = quartile_3 - quartile_1 # 下限 lower_bound = quartile_1 - (iqr * 1.5) # 上限 upper_bound = quartile_3 + (iqr * 1.5) return np.array(ys)[((ys > upper_bound) | (ys < lower_bound))] x = [57,59,60,100,59,58,57,58,300,61,62,60,62,58,57] identify_outliers(x)
array([100, 300])
scipy.stats.iqr を 使う
scipy.stats import iqr で
x = [57,59,60,100,59,58,57,58,300,61,62,60,62,58,57] from scipy.stats import iqr # 1次元配列の四分位範囲を計算 print("iqr:", iqr(x)) from numpy import array x = array([x,[i for i in reversed(x)]]) # 2次元配列の四分位範囲を計算 縦方向 print("iqr axis=0:", iqr(x, axis=0)) # 次元数を維持する print("iqr axis=0 keepdims=True:", iqr(x, axis=0, keepdims=True)) # 2次元配列の四分位範囲を計算 横方向 print("iqr axis=1:", iqr(x, axis=1)) # 次元数を維持する print("iqr axis=1 keepdims=True:", iqr(x, axis=1, keepdims=True))
iqr: 3.5 iqr axis=0: [ 0. 0.5 1. 20. 1.5 1.5 121.5 0. 121.5 1.5 1.5 20. 1. 0.5 0. ] iqr axis=0 keepdims=True: [[ 0. 0.5 1. 20. 1.5 1.5 121.5 0. 121.5 1.5 1.5 20. 1. 0.5 0. ]] iqr axis=1: [ 3.5 3.5] iqr axis=1 keepdims=True: [[ 3.5] [ 3.5]]
使い方が わからなかったが、 外れ値の 計算が できそうな ライブラリ
bumps は、
bumps.dream.outliers.identify_outliers と
pip を
python3 -m pip install bumps
参考
プログラム作成に、
以上です。
コメント