Book/0005.파이썬으로 배우응 통계학 교과서
[python] 사분위 구하기
잠수함토끼
2020. 5. 3. 07:39
사분위 구하기
목적: Data를 순서대로 늘어놓았을 때 아래부터 지정한 percent의 값에 맞는 값을 산출한다.
from scipy import stats
code
def sample_stats():
import numpy as np
from scipy import stats
fish_datas3 = np.asarray([1,2,3,4,5,6,7,8,9,10])
num = 25
print('{0}% = {1}'.format(num, stats.scoreatpercentile(fish_datas3, num)))
num = 75
print('{0}% = {1}'.format(num, stats.scoreatpercentile(fish_datas3, num)))
print('-'*10)
for i in range(13):
num = i*10
print('{0}% = {1}'.format(num, stats.scoreatpercentile(fish_datas3, num)))
결과 값
범위를 넘는 percent의 경우 아래와 같은 오류가 발생한다.
25% = 3.25
75% = 7.75
----------
0% = 1.0
10% = 1.9
20% = 2.8000000000000003
30% = 3.6999999999999997
40% = 4.6
50% = 5.5
60% = 6.3999999999999995
70% = 7.3
80% = 8.2
90% = 9.1
100% = 10.0
Traceback (most recent call last):
File "C:/Users/mellowlee/Project_Lotto/Sample006/sample_scipy.py", line 26, in <module>
sample_stats()
File "C:/Users/mellowlee/Project_Lotto/Sample006/sample_scipy.py", line 20, in sample_stats
print('{0}% = {1}'.format(num, stats.scoreatpercentile(fish_datas3, num)))
File "C:\Users\mellowlee\python_project\venv\lib\site-packages\scipy\stats\stats.py", line 1820, in scoreatpercentile
return _compute_qth_percentile(sorted_, per, interpolation_method, axis)
File "C:\Users\mellowlee\python_project\venv\lib\site-packages\scipy\stats\stats.py", line 1832, in _compute_qth_percentile
raise ValueError("percentile must be in the range [0, 100]")
ValueError: percentile must be in the range [0, 100]
Process finished with exit code 1