Titanic - Machine Learning from Disaster
Titanic - Machine Learning from Disaster
- [Baseline] Logistic Regression with Only Simple Data PreProcessing
- Complete Missing Value
- Drop Feature
- Convert a categorical feature
- Model : Logistic Regression
Score : 0.76555
Rank : 12142 (Top 78%)
- Logistic Regression with Feature Engineering
Score : 0.77272
Rank : 10045 (Top 64%)
- Various Model with Deeper Feature Engineering
Support Vector Machine
Score : 0.76794
K - Nearest Neighbors
Score : 0.76794
Decision Tree
Score : 0.76076
Random Tree
Score : 0.76076
- Ensemble(Voting) RandomForest, ExtraTrees, SVC, AdaBoost, GradientBoosting with GridSearchCV with Deeper Feature Engineering
Score : 0.78229
Rank : 2707 (Top 16%)
์ ์ฉํ๋ Techinque
Analyze by pivoting features
- ๋ฐ์ดํฐ๋ฅผ ํน์ feature๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ทธ๋ฃนํํ์ฌ ๊ด๊ณ๋ฅผ ํ์ ํ๋๋ฐ ์ ์ฉํ๋ค.
- ์ฌ๊ธฐ์๋ ๋ฐ์ดํฐ์ ๊ฐ feature(
Pclass
,Sex
,SibSp
,Parch
)์ target(Survived
)๊ณผ์ ์๊ด์ฑ(์์กด ์ฌ๋ถ์ ์ํฅ์ ์ฃผ๋ feature์ธ์ง)์ ํ์ ํ๋๋ฐ ์ฌ์ฉํ๋ค. - ์ถ๊ฐ๋ก, ์๋ก ๋ง๋
Has_Cabin
feature๊ฐ ๋์์ด ๋ ๋งํ์ง ํ์ธํ๊ธฐ ์ํด ์ฌ์ฉํ๊ธฐ๋ ํ๋ค.
train[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False).mean().sort_values(by='Survived', ascending=False)
FacetGrid
- Grid ํํ๋ก ์ฌ๋ฌ ๊ฐ์ subplot์ ์์ฑํ๊ณ ๊ฐ๊ฐ์ subplot์ ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํ ์ ์๊ฒ ํด์ค, ๋ฐ์ดํฐ์ ์ ์ฌ๋ฌ ์ธก๋ฉด์ ์ดํด๋ณด๊ธฐ์ ์ ์ฉํ๋ค.
- ์ฌ๊ธฐ์๋
Age
feature์ null๊ฐ์ ์ฐ๊ด์๋ feature๋ฅผ ์ด์ฉํด ์ถ์ ํ ๊ฐ์ผ๋ก ๋์ฒดํ๊ธฐ ์ํด,Pclass
์Survived
์ ๋ฐ๋ฅธAge
์ ๋ถํฌ๋ฅผ ํ์ ํ๋๋ฐ ์ฌ์ฉํ๋ค.
grid = sns.FacetGrid(train, col="Survived", row="Pclass", height=2.2, aspect=1.6)
grid.map(plt.hist, 'Age', alpha=.5, bins=20)
grid.add_legend()
Null๊ฐ ์ถ์
Age
feature์ null๊ฐ์ ์ฑ์ฐ๊ธฐ ์ํด, ๋จ์ ๋์น๊ฐ ์๋Pclass
์Survived
์ ๋ฐ๋ฅธAge
๋ถํฌ๋ฅผ ์ด์ฉํ์ฌ,Age
feature์ null๊ฐ์Pclass
์Survived
์ ๊ฐ์ ๋ฐ๋ฅธ ๋ถํฌ์ median๊ฐ์ผ๋ก ๋์ฒดํ์๋ค.
Numerical Feature์ Band feature ์์ฑ
Age
์Fare
์ ๊ฐ์ numerical feature๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํ๋ ๊ฒ์ด ์๋, ๊ฐ ์์ญ๋๋ก ๋ถ๋ฅํ์ฌ ๊ธฐ์กด feature ๋์Age Band
,Fare Band
feature๋ฅผ ๋ง๋ค์ด ์ฌ์ฉํ์๋ค.
๊ธฐ์กด feature๋ค์ ๊ฒฐํฉํ์ฌ ์๋ก์ด feature ์์ฑ
Parch
feature์SibSp
feautre๋ฅผ ์ด์ฉํ์ฌFamilySize
feature๋ฅผ ์์ฑํ๊ณ , ์ด๋ก๋ถํฐIsAlone
feature๋ฅผ ์๋กญ๊ฒ ๋์ถํ์ฌ ์ฌ์ฉํ์๋ค.
์ ๋ณด ์ถ์ถ
- ํ์ต์ ๋์์ด ๋์ง ์์ ๊ฒ ๊ฐ์ ๋ณดํต drop ํ๋
Name
feature์์Title
(ํธ์นญ ex) Mr, Mrs, Dr)์ ๋ณด๋ฅผ ์ถ์ถํ์ฌ ์๋ก์ด feature๋ก ๋ง๋ค์ด ์ฌ์ฉํ์๋ค.
pd.crosstab
- ๋ฐ์ดํฐํ๋ ์์ ์ฌ์ฉํ์ฌ crosstabulation table์ ๋ง๋ค์ด, ๊ฐ feature์ ์กฐํฉ์ ๋ํ ๋น๋์๋ฅผ ํ์ธํ ์ ์์๋ค
- ์ฌ๊ธฐ์๋
Title
feature์ ๊ฐ value์ ๋ํSurvived
์ฌ๋ถ๋ฅผ ํ์ธํด๋ณผ ์ ์์๋ค.
print(pd.crosstab(train['Title'],train['Survived']))
SklearnHelper class ์ ์
- ๊ฐ ๋ชจ๋ธ๋ค์ ํ์ตํ ๋ ๊ณตํต์ผ๋ก ์ฌ์ฉ๋๋ ๋ถ๋ถ์ ํ๋์ class๋ก ์ ์ํ์ฌ ํจ์จ์ ์ผ๋ก ์ฝ๋๋ฅผ ์งค ์ ์์๋ค.
class SklearnHelper(object):
def __init__(self, clf, seed = 0, params=None):
params['random_state'] = seed
self.clf = clf(**params)
def train(self, x_train, y_train):
self.clf.fit(x_train, y_train)
def predict(self, x):
return self.clf.predict(x)
def fit(self, x, y):
return self.clf.fit(x,y)
def feature_importances(self, x, y):
return self.clf.fit(x,y).feature_importances_
Plotly scatterplots
- Plotly package๋ฅผ ์ด์ฉํ์ฌ ๊ฐ ๋ชจ๋ธ์ feature importances ๊ฐ๋ค์ ์๊ฐํํ ์ ์์๋ค.
Plot learning curves
plot_learning_curve
ํจ์๋ฅผ ์ ์ํ์ฌ learning curve๋ฅผ ์๊ฐํํ์ฌ overfitting, underfitting์ ํ์ธํด๋ณผ ์ ์์๋ค.
Reference
Leave a comment