Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<feature_engineering>
warnings.filterwarnings("ignore" )
<feature_engineering>
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv')
 submission = pd.read_csv('.. /input/submission/final submission.csv')
 submission.to_csv('test_submission.csv', index=False )
<drop_column>
def find_missing(data):
count_missing = data.isnull().sum().values
total = data.shape[0]
ratio_missing = count_missing/total
return pd.DataFrame(data={'missing_count':count_missing, 'missing_ratio':ratio_missing}, index=data.columns.values)
 find_missing(train )
<drop_column>
def find_cabin(cabin_list):
find_cabin = []
nan_find = cabin_list.isnull()
for i in range(len(cabin_list)) :
if nan_find[i]:
temp = cabin_list[i]
else:
temp = cabin_list[i][0]
find_cabin.append(temp)
 return find_cabin
train['Cabin_first_letter'] = find_cabin(train.Cabin )
<set_options>
def feature_engineering(df):
df2 = df.copy()
df2['is_class3'] = [1 if i == 3 else 0 for i in df2.Pclass]
df2['is_child'] = [1 if i <= 10 else 0 for i in df2.Age]
df2['no_parch'] = [1 if i == 0 else 0 for i in df2.Parch]
df2['low_fare'] = [1 if i < 5 else 0 for i in df2.Fare]
df2['Cabin_exist'] = cabin_exist(df2.Cabin)
 df2.Cabin = find_cabin(df2.Cabin)
 df2['safe_cabin']=[1 if i in ['B','C','D','E'] else 0 for i in df2.Cabin]
df2['title']=find_title(df2.Name)
 df2['is_mr']=[1 if i == 'Mr.' else 0 for i in df2.title]
df2['is_miss']=[1 if i == 'Miss.' else 0 for i in df2.title]
df2['Ticket'] = [i.split() [-1][0] for i in df2.Ticket]
df2['Ticket'] = [0 if i=='L' else i for i in df2.Ticket]
df2['Position_3to8'] = [1 if(( int(i)>=3)&(int(i)<=8)) else 0 for i in df2.Ticket]
df2 = df2.drop(['Pclass', 'SibSp', 'Parch', 'Cabin', 'Ticket', 'Name', 'title'], 1)
 df2 = pd.get_dummies(df2 ).drop(['Embarked_Q', 'Embarked_C','Sex_male'],1)
 return df2
train_use = feature_engineering(train)
 test_use = feature_engineering(test)
 print(train_use.shape)
 print(test_use.shape )
%matplotlib inline
%pylab inline
print(os.listdir(".. /input"))
warnings.filterwarnings(action='ignore' )<load_from_csv>
train_use[train_use.columns.tolist() ] = MICEImputer(initial_strategy='median', n_imputations=50, n_nearest_features=20, verbose=False ).fit_transform(train_use)
 test_use[test_use.columns.tolist() ] = MICEImputer(initial_strategy='median', n_imputations=50, n_nearest_features=20, verbose=False ).fit_transform(test_use )
df_train = pd.read_csv('.. /input/train_V2.csv')
 df = reduce_mem_usage(df_train)
 df.info()<import_modules>
X = train_use.iloc[:, 2:]
y = train_use.Survived
X_pred = test_use.iloc[:, 1:]
from pandas_summary import DataFrameSummary
from sklearn.ensemble import RandomForestRegressor
from IPython.display import display
from sklearn import metrics
from scipy.cluster import hierarchy as hc
from pdpbox import pdp
from plotnine import *
from fastai.imports import *
from fastai.structured import *<filter>
params1 = {'learning_rate':np.arange(0.01,0.3, 0.01)}
gdbt = GradientBoostingClassifier(n_estimators=100, min_samples_split=300,
min_samples_leaf=20,max_depth=10, subsample=0.8,random_state=10)
 gridscgdbt = GridSearchCV(gdbt, params1, cv=5)
 gridscgdbt.fit(X, y)
 print(gridscgdbt.best_params_)
 print(gridscgdbt.best_score_ )
df[df['winPlacePerc'].isnull() ]<drop_column>
params2 = {'max_depth':range(2,8,1), 'min_samples_split':range(20,200,20)}
gdbt2 = GradientBoostingClassifier(n_estimators=100, learning_rate=gridscgdbt.best_params_['learning_rate'],
subsample=0.8,random_state=10)
 gridscgdbt2 = GridSearchCV(gdbt2, params2, cv=5)
 gridscgdbt2.fit(X, y)
 print(gridscgdbt2.best_params_)
 print(gridscgdbt2.best_score_ )
df.drop(2744604, inplace=True)
 df[df['winPlacePerc'].isnull() ]<define_variables>
params3 = {'max_features':np.arange(0.5,1,0.1), 'subsample':np.arange(0.5, 1, 0.1)}
gdbt3= GradientBoostingClassifier(n_estimators=100, learning_rate=gridscgdbt.best_params_['learning_rate'],
max_depth=gridscgdbt2.best_params_['max_depth'],
min_samples_split=gridscgdbt2.best_params_['min_samples_split'],
subsample=0.8,random_state=10)
 gridscgdbt3 = GridSearchCV(gdbt3, params3, cv=5)
 gridscgdbt3.fit(X, y)
 print(gridscgdbt3.best_params_)
 print(gridscgdbt3.best_score_ )
train = df<feature_engineering>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
 gbdt_best = GradientBoostingClassifier(n_estimators=100, learning_rate=gridscgdbt.best_params_['learning_rate'],
max_depth=gridscgdbt2.best_params_['max_depth'],
min_samples_split=50,
subsample=gridscgdbt3.best_params_['subsample'],
max_features=gridscgdbt3.best_params_['max_features'],
random_state=3)
 gbdt_best.fit(X_train, y_train)
 print('The validation accuracy is:', str(round(( gbdt_best.predict(X_test)== y_test ).mean() ,3)) )
train['killsNorm'] = train['kills']*(( 100-train['playersJoined'])/100 + 1)
 train['damageDealtNorm'] = train['damageDealt']*(( 100-train['playersJoined'])/100 + 1)
 train['maxPlaceNorm'] = train['maxPlace']*(( 100-train['playersJoined'])/100 + 1)
 train['matchDurationNorm'] = train['matchDuration']*(( 100-train['playersJoined'])/100 + 1)
 to_show = ['Id', 'kills','killsNorm','damageDealt', 'damageDealtNorm', 'maxPlace', 'maxPlaceNorm', 'matchDuration', 'matchDurationNorm']
train[to_show][0:11]<feature_engineering>
pd.DataFrame({'importance':gbdt_best.feature_importances_}, index=X.columns )
train['healsandboosts'] = train['heals'] + train['boosts']
train[['heals', 'boosts', 'healsandboosts']].tail()<feature_engineering>
test_ID = test.PassengerId
y_pred_test = gbdt_best.predict(X_pred)
 final = pd.DataFrame({'PassengerId': test_ID, 'Survived': y_pred_test})
 final.Survived = final.Survived.astype(int)
 final.to_csv('final submission.csv', index=False )