import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sb
#๋น๋จ๋ณ์ ๋ถ๋ฅํ๋ ๋ชจ๋ธ
df
Preg | Plas | Pres | skin | test | mass | pedi | age | class | |
0 | 6 | 148 | 72 | 35 | 0 | 33.6 | 0.627 | 50 | 1 |
1 | 1 | 85 | 66 | 29 | 0 | 26.6 | 0.351 | 31 | 0 |
2 | 8 | 183 | 64 | 0 | 0 | 23.3 | 0.672 | 32 | 1 |
3 | 1 | 89 | 66 | 23 | 94 | 28.1 | 0.167 | 21 | 0 |
4 | 0 | 137 | 40 | 35 | 168 | 43.1 | 2.288 | 33 | 1 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
763 | 10 | 101 | 76 | 48 | 180 | 32.9 | 0.171 | 63 | 0 |
764 | 2 | 122 | 70 | 27 | 0 | 36.8 | 0.340 | 27 | 0 |
765 | 5 | 121 | 72 | 23 | 112 | 26.2 | 0.245 | 30 | 0 |
766 | 1 | 126 | 60 | 0 | 0 | 30.1 | 0.349 | 47 | 1 |
767 | 1 | 93 | 70 | 31 | 0 | 30.4 | 0.315 | 23 | 0 |
df['class']์ ์๋ฏธ ?
(1:๋น๋จ์ด๋ค, 0:๋น๋จ๊ฐ ์๋๋ค)
nan ์ ๋ฆฌํ๊ธฐ
๋น์ด์๋ ๋ฐ์ดํฐ๋ ์์ง๋ง ๋น์ด์๋ ํญ๋ชฉ ๋์ 0์ผ๋ก ์ ํ ํ ๋ฐ์ดํฐ์ด๋ค
๋ฐ๋ผ์, Plas ์ปฌ๋ผ๋ถํฐ mass์ปฌ๋ผ๊น์ง๋ 0์ผ๋ก ์ ํ ๋ ๊ฐ์ nan์ผ๋ก ๋ง๋ค๋ ค๊ณ ํ๋ค.
df.loc[ : , 'Plas' : 'mass' ] = df.loc[ : , 'Plas' : 'mass' ].replace(0,np.nan)
df = df.dropna()
y์ X๋ก ๋๋๊ธฐ
y = df['class']
X = df.loc[ : , 'Plas' : 'age' ]
y.value_counts()
class
0 262
1 130
Name: count, dtype: int64
๋น๋จ๊ฐ ์๋ ์ฌ๋์ 262๋ช , ๋น๋จ์ธ ์ฌ๋์ 130๋ช ์๋ค
์ด ์๋ฅผ ๋ง์ถฐ์ฃผ๊ณ ์ถ๋ค๋ฉด
imblearn ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ด์ฉํ์ฌ ๋ฆฌ์ํ๋งํ๊ธฐ
! pip install imblearn
from imblearn.over_sampling import SMOTE
sm = SMOTE(random_state=5)
X, y = sm.fit_resample(X, y)
y.value_counts()
class
0 262
1 262
Name: count, dtype: int64