오늘부터 새로운 파트인 머신러닝과 딥러닝 심화 파트에 들어갔다. 본격적으로 머신러닝과 딥러닝에 대해 공부하기 전에 필요한 통계학에 대해 더 깊게 배운 뒤에 머신러닝과 딥러닝을 배울 것 같다. 이전 파트에서도 가볍게 통계학을 다루긴 했는데 이번에는 조금 더 복잡하고 어려운 내용을 다뤘다.
모집단 정의 → 프레임 확보 → 표본 추출 방법 결정 → 표본 크기 결정 → 표본 추출
| 구분 | 동일 여부(=, ≠) | 순서 있음(<, >) | 간격 일정(+-, 평균) | 절대 0 존재(X, %) |
|---|---|---|---|---|
| 명목척도 | O | X | X | X |
| 서열척도 | O | O | X | X |
| 등간척도 | O | O | O | X |
| 비율척도 | O | O | O | O |
df['Price'].mean()
# np.float64(9690.232941176471)
skipna 속성으로 결측값을 제외한 평균 반환nums = pd.Series([1, np.nan, 2])
nums.mean()
# np.float64(1.5)
stats.trim_mean(df['Price'], proportiontocut=0.1)
# np.float64(9584.380019588638)
df['Price'].median()
# np.float64(9450.0)
df['FuelType'].mode()
# 0 Petrol
# Name: FuelType, dtype: object
df['FuelType'].value_counts()
# FuelType
# Petrol 1129
# Diesel 129
# CNG 17
# Name: count, dtype: int64
df['FuelType'].value_counts(normalize=True)
# FuelType
# Petrol 0.885490
# Diesel 0.101176
# CNG 0.013333
# Name: proportion, dtype: float64
df['Price'].quantile(np.linspace(0, 1, 5))
# 0.00 4350.0
# 0.25 8250.0
# 0.50 9450.0
# 0.75 10950.0
# 1.00 15950.0
# Name: Price, dtype: float64
df['Price'].quantile(np.linspace(0, 1, 11))
# 0.0 4350.0
# 0.1 7250.0
# 0.2 7950.0
# 0.3 8500.0
# 0.4 8950.0
# 0.5 9450.0
# 0.6 9950.0
# 0.7 10500.0
# 0.8 11456.0
# 0.9 12500.0
# 1.0 15950.0
# Name: Price, dtype: float64
df['Price'].min()
# np.int64(4350)
df['Price'].max()
# np.int64(15950)
quantile 사용df['Price'].quantile([0, 1])
# 0.0 4350.0
# 1.0 15950.0
# Name: Price, dtype: float64
agg 사용df['Price'].agg(func=['min', 'max'])
# min 4350
# max 15950
# Name: Price, dtype: int64
df['Price'].max() - df['Price'].min()
# np.int64(11600)
quantile 과 diff 사용diff : 인접한 두 원소 간의 차이를 반환df['Price'].quantile([0, 1]).diff().iloc[-1]
# np.float64(11600.0)
df['Price'].quantile([0.25, 0.75]).diff().iloc[-1]
# np.float64(2700.0)
ddof 는 degree of freedom이며, 1을 지정하면 편차 제곱합을 n-1로 나눔df['Price'].var()
# np.float64(4120265.326386555)
df['Price'].var(ddof=0)
# np.float64(4117033.7457384085)
ddof 기본값 = 0np.var(df['Price'])
# np.float64(4117033.7457384085)
df['Price'].std()
# np.float64(2029.8436704304484)
robust.mad(df['Price'])
# np.float64(2223.903327758403)
df.describe(include=object)
# FuelType MetColor Automatic
# count 1273 1273 1273
# unique 3 2 2
# top Petrol 1 0
# freq 1128 842 1203
df['Age'].cov(df['Price'])
# np.float64(-22136.617857831003)
df.cov(numeric_only=True).round(2)
# Price Age KM HP CC Doors Weight
# Price 4117054.19 -22136.62 -3.747196e+07 5977.21 17623.05 325.35 15746.20
# Age -22136.62 187.56 1.711083e+05 -8.46 -210.17 -1.22 -102.32
# KM -37471955.30 171108.28 1.285867e+09 -156932.02 2588046.91 467.42 359285.14
# HP 5977.21 -8.46 -1.569320e+05 172.01 -53.44 1.58 -41.74
# CC 17623.05 -210.17 2.588047e+06 -53.44 34010.01 23.36 5010.95
# Doors 325.35 -1.22 4.674200e+02 1.58 23.36 0.90 13.10
# Weight 15746.20 -102.32 3.592851e+05 -41.74 5010.95 13.10 1553.06
df['Age'].corr(df['Price'])
# np.float64(-0.7966182791038179)
df.corr(numeric_only=True)
# Price Age KM HP CC Doors Weight
# Price 1.000000 -0.796618 -0.515009 0.224610 0.047096 0.168562 0.196920
# Age -0.796618 1.000000 0.348422 -0.047075 -0.083213 -0.093670 -0.189588
# KM -0.515009 0.348422 1.000000 -0.333686 0.391355 0.013703 0.254242
# HP 0.224610 -0.047075 -0.333686 1.000000 -0.022095 0.126925 -0.080759
# CC 0.047096 -0.083213 0.391355 -0.022095 1.000000 0.133181 0.689482
# Doors 0.168562 -0.093670 0.013703 0.126925 0.133181 1.000000 0.349477
# Weight 0.196920 -0.189588 0.254242 -0.080759 0.689482 0.349477 1.000000
import seaborn as sns
import matplotlib.pyplot as plt
plt.rc(group='font', family='Gowun Batang', size=10)
plt.rc(group='figure', figsize=(8, 4), dpi=120)
plt.rc(group='axes', unicode_minus=False)
plt.rc(group='legend', frameon=True, fc='0.9', ec='0.9')
plt.rc(group='figure', figsize=(4, 4))
df['Price'].agg(func=['min', 'max'])
sns.histplot(
df, x='Price', binrange=[4000, 16000],
binwidth=1000, facecolor='0.8'
)
plt.show()

sns.kdeplot(
df, x='Price',
fill=True, color='0.8'
)
plt.axvline(df['Price'].mean())
plt.axvline(df['Price'].median(), color='red', linestyle='--')
plt.show()

corr = df.corr(numeric_only=True)
sns.heatmap(
corr, annot=True, fmt='.2f',
annot_kws={'size': 8},
cmap='RdYlBu', linewidths=1
)
plt.show()

hds.plot.regline(
df, x='Age', y='Price'
)

hds.plot.regline(
df, x='KM', y='Price'
)

df1 = df.copy()
df1['KM_log'] = np.log(df1['KM'])
hds.plot.regline(
df1, x='KM_log', y='Price'
)

hds.plot.regline(
df, x='Weight', y='Price'
)
plt.axvline(
x=1250, ls='--', lw=1, color='0.2'
)

hds.plot.box_group(
df, x='FuelType', y='Price',
palette='Pastel1'
)

cond1 = df['Doors'].ne(2)
cond2 = df['Weight'].le(1250)
df = df.loc[cond1 & cond2, :]
df = df.reset_index(drop=True)
df.shape
df.to_pickle() : 데이터프레임 하나를 pickle 파일로 저장pd.to_pickle(obj, filepath_or_buffer) : obj 매개변수에 지정한 파이썬 객체를 pickle 파일로 저장주사위 한 번 던지기의 표본 공간
사건 A : 1, 3, 5
사건 B : 2, 4, 6
연속확률분포는 표로 나타낼 수 없음
정상적인 주사위를 한 번 굴렸을 때의 확률은
| 눈금 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| 확률 | 1/6 | 1/6 | 1/6 | 1/6 | 1/6 | 1/6 |
95% 신뢰구간은 같은 방법으로 표본평균의 계산을 100번 반복했을 때,
그중에서 95개의 구간이 실제 모평균을 포함한다는 것을 의미
ADsP를 공부하면서 어느 정도 익숙한 내용들이 많았는데 자격증 공부할 때보다 더 깊게 파고드는 부분들이 꽤 있어서 겨우 따라갔던 것 같다. 지금 파트는 수업 중에 조금이라도 놓치면 너무 많은 정보를 놓치게 되어서 힘들어도 더 열심히 수업을 들어야 될 것 같다.