Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<import_modules>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import LabelEncoder
from itertools import product
from sklearn.feature_extraction.text import TfidfVectorizer
import re
from xgboost import XGBRegressor
from sklearn.ensemble import RandomForestClassifier
from xgboost import plot_importance<load_from_csv>
train = pd.read_csv(".. /input/titanic/train.csv")
 test = pd.read_csv(".. /input/titanic/test.csv")
 data_cleaner = [train, test]
train.describe(include="all")
 sub = test['PassengerId']
train = pd.read_csv(".. /input/competitive-data-science-predict-future-sales/sales_train.csv")
 test = pd.read_csv(".. /input/competitive-data-science-predict-future-sales/test.csv")
 item_category = pd.read_csv(".. /input/competitive-data-science-predict-future-sales/item_categories.csv")
 item = pd.read_csv(".. /input/competitive-data-science-predict-future-sales/items.csv")
 shop = pd.read_csv(".. /input/competitive-data-science-predict-future-sales/shops.csv" )<merge>
print(pd.isnull(train ).sum())
<import_modules>
print(pd.isnull(test ).sum())
<choose_model_class>
for data in data_cleaner:
print(data.isnull().sum())
 print('
')
<save_to_csv>
age_ref = pd.DataFrame(data=[train.groupby('Pclass')['Age'].mean() ],columns=train['Pclass'].unique())
 age_ref
<data_type_conversions>
def fill_age(pclass,age):
if pd.isnull(age):
return float(age_ref[pclass])
 else:
return age
for data in data_cleaner:
data['Age'] = train.apply(lambda x: fill_age(x['Pclass'],x['Age']), axis=1)
train['date'] = pd.to_datetime(train['date'],format = '%d.%m.%Y' )<filter>
def fill_fare(fare):
if pd.isnull(fare):
return train['Fare'].mean()
else:
return fare
def fill_embark(embark):
if pd.isnull(embark):
return train['Embarked'].mode().iloc[0]
else:
return embark
for data in data_cleaner:
data['Fare'] = train.apply(lambda x: fill_fare(x['Fare']), axis=1)
 data['Embarked'] = train.apply(lambda x: fill_embark(x['Embarked']), axis=1 )
train[train.item_price > 100000]<filter>
for data in data_cleaner:
data.drop(['Cabin'],axis=1,inplace=True )
train[train.item_price < 0]<filter>
for data in data_cleaner:
print(data.isnull().sum())
 print('
' )
train[train.item_cnt_day >= 1000]<filter>
title_list = list()
for data in data_cleaner:
for title in data['Name']:
title = title.split('.')[0].split(',')[1]
title_list.append(title)
 data['Title'] = title_list
title_list = list()
train[train.item_cnt_day < 0]<feature_engineering>
train['Title'] = train['Title'].replace([ ' Don', ' Rev', ' Dr', ' Mme',' Ms', ' Major', ' Lady', ' Sir', ' Mlle', ' Col', ' Capt',
' the Countess', ' Jonkheer'], 'Others')
 train['Title'].value_counts()
shop["city"] = shop.shop_name.str.split(" " ).map(lambda x: x[0])
 shop["type"] = shop.shop_name.str.split(" " ).map(lambda x: x[1] )<categorify>
test['Title'] = test['Title'].replace([ ' Don', ' Rev', ' Dr', ' Mme',' Ms', ' Major', ' Lady', ' Sir', ' Mlle', ' Col', ' Capt',
' the Countess', ' Jonkheer',' Dona'], 'Others')
 test['Title'].value_counts()
shop["shop_type"] = LabelEncoder().fit_transform(shop.type)
 shop["shop_city"] = LabelEncoder().fit_transform(shop.city)
 shop.head()<feature_engineering>
def get_size(df):
if df['SibSp'] + df['Parch'] + 1 == 1:
return 'Single'
if df['SibSp'] + df['Parch'] + 1 > 1:
return 'Small'
if df['SibSp'] + df['Parch'] + 1 > 4:
return 'Big'
for data in data_cleaner:
data['FamilySize'] = data.apply(get_size,axis=1)
 for data in data_cleaner:
data['IsAlone'] = 1
data['IsAlone'].loc[data['FamilySize'] != 'Single'] = 0
item_category['item_type'] = item_category['item_category_name'].str.split('-' ).map(lambda x: x[0])
 item_category['item_name'] = item_category['item_category_name'].str.split('-' ).map(lambda x: x[1].strip() if len(x)> 1 else x[0].strip())
 item_category.head()<categorify>
sex = pd.get_dummies(train['Sex'],drop_first=True)
 embark = pd.get_dummies(train['Embarked'],drop_first=True)
 title = pd.get_dummies(train['Title'],drop_first=True)
 Pclass = pd.get_dummies(train['Pclass'],drop_first=True)
 FamilySize = pd.get_dummies(train['FamilySize'],drop_first=True)
 sex2 = pd.get_dummies(test['Sex'],drop_first=True)
 embark2 = pd.get_dummies(test['Embarked'],drop_first=True)
 title2 = pd.get_dummies(test['Title'],drop_first=True)
 Pclass2 = pd.get_dummies(test['Pclass'],drop_first=True)
 FamilySize2 = pd.get_dummies(test['FamilySize'],drop_first=True)
 for data in data_cleaner:
data.drop(['Sex','Embarked','Name','Ticket','PassengerId','Title','Pclass','FamilySize'],axis=1,inplace=True)
 train = pd.concat([sex,embark,train,title,FamilySize,Pclass],axis=1)
 test = pd.concat([sex2,embark2,test,title2,FamilySize2,Pclass2],axis=1 )
item_category['item_type_code'] = LabelEncoder().fit_transform(item_category.item_type)
 item_category['item_name_code'] = LabelEncoder().fit_transform(item_category.item_name)
 item_category.head()<drop_column>
X = train.drop('Survived',axis=1)
 y = train['Survived']
x_test = test
item_category = item_category[['item_category_id', 'item_type_code', 'item_name_code']]
item_category.head()<string_transform>
warnings.simplefilter("ignore" )
def clean_text(item_name):
item_name = item_name.lower()
item_name = re.sub(r'[^\w\s]', '', item_name)
 item_name = re.sub(r'\d+', '', item_name)
 item_name = re.sub(' +', ' ', item_name)
 item_name = item_name.strip()
return item_name<feature_engineering>
logreg = LogisticRegression(class_weight='balanced')
 param = {'C':[0.001,0.003,0.005,0.01,0.03,0.05,0.1,0.3,0.5,1,2,3,3,4,5,10,20]}
clf = GridSearchCV(logreg,param,scoring='roc_auc',refit=True,cv=10)
 clf.fit(X,y)
 print('Best roc_auc: {:.4}, with best C: {}'.format(clf.best_score_, clf.best_params_))
item['clean_item_name'] = item['item_name'].apply(clean_text)
 item.head()<feature_engineering>
seed = 60
kf = StratifiedKFold(n_splits=5,shuffle=True,random_state=seed)
 pred_test_full =0
cv_score =[]
i=1
for train_index,test_index in kf.split(X,y):
print('{} of KFold {}'.format(i,kf.n_splits))
xtr,xvl = X.loc[train_index],X.loc[test_index]
ytr,yvl = y.loc[train_index],y.loc[test_index]
lr = LogisticRegression(C=20)
 lr.fit(xtr,ytr)
 score = roc_auc_score(yvl,lr.predict(xvl))
print('ROC AUC score:',score)
 cv_score.append(score)
 pred_test = lr.predict_proba(x_test)[:,1]
pred_test_full +=pred_test
i+=1
item['sub_name1'] = item['clean_item_name'].str.split(' ' ).map(lambda x: x[0])
 item['sub_name2'] = item['clean_item_name'].str.split(' ' ).map(lambda x: x[1].strip() if len(x)> 1 else x[0].strip())

item.head()<count_unique_values>
print('Confusion matrix
',confusion_matrix(yvl,lr.predict(xvl)))
 print('Cv',cv_score,'
Mean cv Score',np.mean(cv_score))
<count_unique_values><EOS> 
y_pred = pred_test_full/5
submit = pd.DataFrame({'PassengerId':sub,'Survived':y_pred})
 submit['Survived'] = submit['Survived'].apply(lambda x: 1 if x>0.5 else 0)
 submit.to_csv('lr_titanic.csv',index=False)
 submit