์ธ์์ ๋ชจ๋ ํด๋์ค, ํด๋์ค101
NO.1 ์จ๋ผ์ธ ํด๋์ค ํ๋ซํผ - ๋ํ๋ฏผ๊ตญ 1๋ฑ ์จ๋ผ์ธ ํด๋์ค ํ๋ซํผ ์ค๋น๋ฌผ๊น์ง ์ฑ๊ฒจ์ฃผ๋ ํด๋์ค101๊ณผ ์ทจ๋ฏธ๋ถํฐ ๋ถ์ , ์๋ก์ด ์ ์ฑ๊น์ง ์ฐพ์๋ณด์ธ์!
class101.net
๋ฌด๋ฃ๊ณต๊ฐ๋ก ์ฌ๋ ค์ฃผ์ จ๋ค..! ํด๊ทผํ๋ด์ง๋ ์งฑ
์ด๋ถ์ด ๋ฑ ์ด์ ๋ ์ฝ๋๋ง ์ฐ๊ณ 40์ ๋ง์ ์ ๋ฐ์ผ์ จ๋ค๊ณ ํ๋ค.
์ฝ๋์ฑ์ ์์ด ์ ์ถ๋ ํ์ผ๋ง ์ ์๋งค๊ธฐ๋๊น, ์ฌ๋ฌ ๋ฐฉ๋ฒ๋ก ์ ๋ํ ๋ถ๋ด์ด ์ข ๋์ด์ก๋ค.
๋ฒ ์ด์ค๋ผ์ธ๋๋ก๋ง ์ค์ ์์ด ํ๊ธธ..!
๋ฌธ์ : ์ฌํ๋ณดํ์ ๊ตฌ๋งค์ํจ(0), ๊ตฌ๋งค(1)์ผ๋ก ๋๋ ๋, ๊ตฌ๋งค์ผ ํ๋ฅ ์ ์์ธก
3ํ๊ธฐ์ถ์ 2ํ๊ธฐ์ถ๊ณผ ๋ฌ๋ฆฌ X์ y๋ฅผ ๋๋ ์ฃผ์ง ์์๋ค. Train๊ณผ Test๋ง ์ฃผ์ด์ก๊ณ Train์๋ง ์๋ ์ข ์๋ณ์๋ฅผ ๋์ค์ ๋ผ์ด์ฃผ๋ฉด ๋๋ค. ์ด๋ฒ์๋ ์ด์ง๋ถ๋ฅ predict_proba๊ฐ ์ถ์ ๋๋ค. ๊ณต์์์ ๊น์ง ํ๋ฉด ์ธ๊ฐ๊ฐ ๋ค ๋ถ๋ฅ๋ชจ๋ธ์ธ๋ฐ... ๋ด์ผ ์ํ์๋ง ์ฒ์์ผ๋ก ํ๊ท๋ชจ๋ธ์ด ๋์ค๋ฉด ์ด๋กํ๋ ๊ดํ ๋ถ์....
๋ ์ ์ถํ์ผ ์ฝ๋์์๋ฅผ ๋ช ํํ ์ฃผ์ง์์ ์ด๊ฑธ๋ก ์ด์์ ๊ธฐ๊ฐ ๋ง์๋ค๊ณ ํ๋ค. ๋ด๋ผ๋ ๋ชจ์ ์ ๋๋ก ๋ณด๊ณ to_csv ์ ํํ ํ์
import numpy as np
import pandas as pd
train = pd.read_csv('../input/jakuphyung23rdtest/train.csv') # 1490 * 10
test = pd.read_csv('../input/jakuphyung23rdtest/test.csv') # 497 * 9
pd.set_option('display.max_columns',None)
pd.set_option('display.max_rows', 20)
# 0. EDA
print(train)
print(train.info())
print('-'*50)
print(test.info())
# 1. id drop๊ณผ pop
train.drop(columns='Unnamed: 0', inplace=True)
unnamed = test.pop('Unnamed: 0')
print(train.columns, test.columns)
# 2. ๊ฒฐ์ธก์น ์ฒ๋ฆฌ-์์
๊ฒฐ์ธก์น ์๋ค๋! ํ์ ์์ ๊ทธ๋ด ๋ฆฌ๊ฐ ์๋๋ฐ!
isnull()sum()์ ์ฌ์ค์ ์์ฐ๊ฒ ๋๋ค info()์ ๋ค ์๋๋ฐ! shape๋ ๋ง์ฐฌ๊ฐ์ง
์ปฌ๋ผ๋ช ์ ์ด๋์ []ํ๊ณ ์ด๋์ ''๋ก ํด์ Syntax Error๊ฐ ์๊พธ ๋๋ค... ์คํ์ค์๋ ํ๊ณ ...
์ด๋ฒ์ unnamed๋ ๋ค์ ์ธ ํ์ ์์์ง๋ง ๊ทธ๋๋ ํ์์ฒ๋ผ pop()ํด๋ดค๋ค
# 3. ์ธ์ฝ๋ฉ:
print(train.describe(include='object'))
print(test.describe(include='object'))
# ์ ๋ถ nunique๊ฐ 2๊ฐ๋ก ๊ฐ๋จํจ
cols = ['Employment Type', 'GraduateOrNot', 'FrequentFlyer', 'EverTravelledAbroad']
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
for col in cols:
train[col] = le.fit_transform(train[col])
test[col] = le.transform(test[col])
print(train.head().T)
print(test.head().T)
describe()๋ฅผ EDA๊ฐ ์๋ ์ฌ๊ธฐ์ ํด๋ณธ๋ค ํํ
ํ๋จ๊ณ ํ๋จ๊ณ ํ๊ณ ๋์ ์ ์ฉ๋๋์ง ๊ผญ printํ๋ ์ต๊ด์ ๋ค์ด์
ํ๋ฐ๋ฐ ์ฝ๋์ฐ๊ณ ํ๋ฒ์ ๋๋ฆฌ๋ฉด ์ด๋์๋ถํฐ ์๋๋์ง ๊ท์ฐฎ์์ง๋ค
# 4. ์ค์ผ์ผ๋ง
print(train.describe())
print(test.describe())
## ์๊น ๋ฒ๋ฆฐ unnamed์ ์ข
์๋ณ์์ธ ๋ณดํ๋ฃ๋ฅผ ์ ์ธํ๋ฉด ์์นํ ์ปฌ๋ผ์ 4๊ฐ
## ๋ณดํต age๋ ๋ถํฌ ๋ณด๊ณ ๋์ด๋ ๋ฒ์ฃผํํ๋๊ฒ ํํ์ง๋ง ์ด๋ฒ์ ๋ฒ์๊ฐ ์งง์ ํจ์ค
## FamilyMembers 2~9, ChronicDiseases 0~1๋ก ๊ท๋ชจ๊ฐ ์์ ์ค์ผ์ผ๋ง ํจ์ค
## Age์ AnnualIncome์ผ๋ก๋ง ์ค์ผ์ผ๋ง ์งํ
from sklearn.preprocessing import RobustScaler
cols = ['Age', 'AnnualIncome']
ro = RobustScaler()
for col in cols:
train[col] = le.fit_transform(train[col])
test[col] = le.transform(test[col])
print(train.head().T)
print(test.head().T)
๋ด ๋๋ฆ์ ๋ ผ๋ฆฌ๋ก ์ค์ผ์ผ๋งํ ์ปฌ๋ผ์ ์ ๋ณํ๋ค
์ด์ฐจํผ ์ปฌ๋ผ ๊ฐ ๊ท๋ชจ ๋ง์ถ๋ ค๊ณ ์ค์ผ์ผ๋งํ๋๊ฑฐ ์๋๊ฐ..?! ์์นํ์ด๋ผ๊ณ ๋ค ํ ํ์ ์๋ค ใ ..
RobustScaler๋ ์ด์์น๋ฅผ ๊ณ ๋ คํด์ฃผ๋ ๋ฐฉ๋ฒ์ด๋ผํ๋ค ์์ธํ๊ฑด๋ชจ๋ฅด๊ฒ ๋ค
๊ทธ๋์ ์ํ ๋๋ ๋ฏผ๋งฅ์ค๋ ์คํ ๋ค๋๋ณด๋จ ๋ก๋ฒ์คํธ ์ฐ๋ ค ํ๋ค
# 5. ๊ฒ์ฆ๋ฐ์ดํฐ ๋ถ๋ฆฌ
from sklearn.model_selection import train_test_split
xx_train, x_val, yy_train, y_val = train_test_split(train.drop(columns='TravelInsurance'),
train['TravelInsurance'],
train_size=0.8,
random_state=42)
print(xx_train.shape, x_val.shape, yy_train.shape, y_val.shape)
# 5-1. ๊ฒ์ฆ๋ฐ์ดํฐ๋ก ํ์ต
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=500, max_depth=9, random_state=42)
model.fit(xx_train, yy_train)
# 5-2. ๊ฒ์ฆ๋ฐ์ดํฐ๋ก ์์ธก
pred_val = model.predict_proba(x_val)[:,1] # 1์ผ ํ๋ฅ
# 5-3. ๊ฒ์ฆ๋ฐ์ดํฐ๋ก ์ ์
from sklearn.metrics import roc_auc_score
score = roc_auc_score(y_val, pred_val)
# print(score)
# 500, 5, 0.792
# 1000, 5, 0.791
# 500, 7, 0.804
# 500, 9, 0.805
# 700, 9, 0.804
๋ด๊ฐ ์ด๋ ค์ํ๋ ๋ถ๋ถ ์์..!
train_test_split()ํ ๋ x_train๊ณผ y_train์ ๋ฃ๊ณ , (์ฌ๊ธฐ์ ๋ฐ๋ก ์ ๊ณต ์ํ์)
fit()ํ ๋๋ x_train๊ณผ y_train์ ๋ฃ๊ณ ,
์์ธก์ ๋น์ฐํ x_test๋ง,
์ ์๋ y_test์ x_test์ ์์ธก๊ฐ์ ๋ฃ๊ธฐ... (๋ฌผ๋ก y_test๋ฅผ ๊ฐ์ง ์ฑ์ ๊ด ์ ์ฅ)
ํ์ดํผํ๋ผ๋ฏธํฐ ์กฐ์ ํด๊ฐ๋ฉฐ 0.805์์ ๋ง์กฑํ๋ค
# 6. ์ค์ test๋ฐ์ดํฐ๋ก ์์ธก
pred = model.predict_proba(test)[:,1]
# 7. ์ ์ถ: index์ปฌ๋ผ, y_pred์ปฌ๋ผ, 0~496์ธ๋ฑ์ค, index=False ์ฃผ์
output = pd.DataFrame({'index':test.index,
'y_pred':pred}).to_csv('์ํ๋ฒํธ.csv', index=False)
# _____________์ฑ์ _____________________
y_test = pd.read_csv('../input/jakuphyung23rdtest/y_test.csv')
score = roc_auc_score(y_test, pred)
print(score) # 0.78
์ ๊ณตํด์ฃผ์ y_test๋ก ํด๋ณด๋ 0.78์ ์ผ๋ก ์ด์ง ๋ฎ์์ก๋ค.
๋
๋๊ธ