평균, 절사 평균, 중간값¶
1단계 : 데이터 로드하기¶
In [ ]:
from google.colab import drive
drive.mount('/content/drive')
Mounted at /content/drive
2단계: 데이터 확인하기¶
In [ ]:
STATE_CSV = '/content/drive/MyDrive/통계공부/PSDS/data/state.csv'
In [ ]:
import pandas as pd
state = pd.read_csv(STATE_CSV)
print(state.head(8))
State Population Murder.Rate Abbreviation 0 Alabama 4779736 5.7 AL 1 Alaska 710231 5.6 AK 2 Arizona 6392017 4.7 AZ 3 Arkansas 2915918 5.6 AR 4 California 37253956 4.4 CA 5 Colorado 5029196 2.8 CO 6 Connecticut 3574097 2.4 CT 7 Delaware 897934 5.8 DE
3단계: 계산하기¶
In [ ]:
#평균
state['Population'].mean()
Out[ ]:
6162876.3
In [ ]:
#절사 평균
from scipy.stats import trim_mean
trim_mean(state['Population'], 0.1)
Out[ ]:
4783697.125
In [ ]:
#중앙값
state['Population'].median()
Out[ ]:
4436369.5
In [ ]:
#가중 평균
import numpy as np
np.average(state['Population'], weights=state['Population'])
Out[ ]:
13620491.638786951
In [ ]:
#가중 중간값
In [ ]:
pip install wquantiles
Collecting wquantiles Downloading wquantiles-0.6-py3-none-any.whl (3.3 kB) Requirement already satisfied: numpy>=1.18 in /usr/local/lib/python3.10/dist-packages (from wquantiles) (1.23.5) Installing collected packages: wquantiles Successfully installed wquantiles-0.6
In [ ]:
import wquantiles
wquantiles.median(state['Murder.Rate'], weights=state['Population'])
Out[ ]:
4.4
가장 기본적인 위치 추정값은 평균 but. 극단값(outlier)에 민감할 수 있음
중간값, 절사평균과 같은 다른 방법들이 outlier나 이상데이터에 robust
'수학 및 통계 > PSDS' 카테고리의 다른 글
| [PSDS] 데이터 분포 탐색하기 실습 (0) | 2023.09.22 |
|---|---|
| [PSDS] 데이터 분포 탐색하기 (0) | 2023.09.21 |
| [PSDS] 위치 추정 (0) | 2023.09.17 |
| [PSDS] 테이블 데이터 (0) | 2023.09.16 |
| [PSDS] 정형화된 데이터의 요소 (0) | 2023.09.15 |