Kaggle competition: Titanic - Machine Learning from Disaster
test = pd.read_csv('.. /input/test.csv',
dtype={'srch_destination_id':np.int32},
usecols=['srch_destination_id'], )<merge>
train=pd.read_csv(".. /input/train.csv")
 test=pd.read_csv(".. /input/test.csv" )
test = test.merge(most_pop, how='left',left_on='srch_destination_id',right_index=True)
 test.head()<count_missing_values>
train.columns[train.isnull().any() ], test.columns[test.isnull().any() ]
test.hotel_cluster.isnull().sum()<groupby>
total = train.isnull().sum().sort_values(ascending=False)
 percent =(train.isnull().sum() /train.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data
most_pop_all = agg.groupby('hotel_cluster')['relevance'].sum().nlargest(5 ).index
most_pop_all = np.array_str(most_pop_all)[1:-1]
most_pop_all<categorify>
total=test.isnull().sum().sort_values(ascending=False)
 percent=(test.isnull().sum() /test.isnull().count() ).sort_values(ascending=False)
 missing_data=pd.concat([total,percent],axis=1,keys=['Total','Percent'])
 missing_data
test.hotel_cluster.fillna(most_pop_all,inplace=True )<save_to_csv>
train=train.drop(['Ticket','Cabin'],axis=1)
 test=test.drop(['Ticket','Cabin'],axis=1 )
test.hotel_cluster.to_csv('predicted_with_pandas.csv',header=True, index_label='id' )<load_from_csv>
for dataset in combine:
dataset['Title']=dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 train[['Title','Survived']].groupby(['Title'],as_index=False ).mean()
train = pd.read_csv('.. /input/train.csv',
dtype={'is_booking':bool,'srch_destination_id':np.int32, 'hotel_cluster':np.int32},
usecols=['srch_destination_id','is_booking','hotel_cluster'],
chunksize=1000000)
 aggs = []
print('-'*38)
 for chunk in train:
agg = chunk.groupby(['srch_destination_id',
'hotel_cluster'])['is_booking'].agg(['sum','count'])
 agg.reset_index(inplace=True)
 aggs.append(agg)
 print('.',end='')
 print('')
 aggs = pd.concat(aggs, axis=0)
 aggs.head()<count_unique_values>
title_mapping={'Mr':1,'Miss':2,'Mrs':3,'Master':4,'Rare':5}
for dataset in combine:
dataset['Title']=dataset['Title'].map(title_mapping)
 dataset['Title']=dataset['Title'].fillna(0)
 train.head(6)
def most_popular(group, n_max=5):
relevance = group['relevance'].values
hotel_cluster = group['hotel_cluster'].values
most_popular = hotel_cluster[np.argsort(relevance)[::-1]][:n_max]
return np.array_str(most_popular)[1:-1]<prepare_output>
train['Age']=train['Age'].fillna(train['Age'].median(skipna=True))
test['Age']=test['Age'].fillna(test['Age'].median(skipna=True))
most_pop = agg.groupby(['srch_destination_id'] ).apply(most_popular)
 most_pop = pd.DataFrame(most_pop ).rename(columns={0:'hotel_cluster'})
 most_pop.head()<load_from_csv>
train['Age']=train['Age'].astype(int)
 test['Age']=test['Age'].astype(int )
test = pd.read_csv('.. /input/test.csv',
dtype={'srch_destination_id':np.int32},
usecols=['srch_destination_id'], )<merge>
train['AgeBand']=pd.cut(train['Age'],5)
 train[['AgeBand','Survived']].groupby(['AgeBand'],as_index=False ).mean().sort_values(by='AgeBand',ascending=True )
test = test.merge(most_pop, how='left',left_on='srch_destination_id',right_index=True)
 test.head()<count_missing_values>
combine=[train,test]
for dataset in combine:
dataset.loc[dataset['Age']<=16,'Age']=0
dataset.loc[(dataset['Age']>16)&(dataset['Age']<=32),'Age']=1
dataset.loc[(dataset['Age']>32)&(dataset['Age']<=48),'Age']=2
dataset.loc[(dataset['Age']>48)&(dataset['Age']<=64),'Age']=3
dataset.loc[(dataset['Age']>64),'Age']=4
train.head(6 )
test.hotel_cluster.isnull().sum()<groupby>
train=train.drop(['AgeBand'],axis=1)
 combine=[train,test]
most_pop_all = agg.groupby('hotel_cluster')['relevance'].sum().nlargest(5 ).index
most_pop_all = np.array_str(most_pop_all)[1:-1]
most_pop_all<categorify>
for dataset in combine:
dataset['FamilySize']=dataset['SibSp']+dataset['Parch']+1
train[['FamilySize','Survived']].groupby(['FamilySize'],as_index=False ).mean().sort_values(by='Survived',ascending=False)
test.hotel_cluster.fillna(most_pop_all,inplace=True )<save_to_csv>
for dataset in combine:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
train[['IsAlone', 'Survived']].groupby(['IsAlone'], as_index=False ).mean()
test.hotel_cluster.to_csv('predicted_with_pandas.csv',header=True, index_label='id' )<set_options>
train = train.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
 test = test.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
 combine = [train, test]
train.head()
warnings.filterwarnings('ignore')
 pd.options.display.float_format = '{:.3f}'.format
%matplotlib inline
color = sns.color_palette()
py.init_notebook_mode(connected=True)
 print(os.listdir(".. /input")) 
<load_from_csv>
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].fillna(train['Embarked'].dropna().mode() [0])
 train[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
df_train = reduce_mem_usage(pd.read_csv('.. /input/train_V2.csv'))
df_test = reduce_mem_usage(pd.read_csv('.. /input/test_V2.csv'))<train_model>
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 train.head()
print('train : {}'.format(df_train.shape))
print('test : {}'.format(df_test.shape))<sort_values>
test['Fare'].fillna(test['Fare'].dropna().median() , inplace=True)
 test.head()
total = df_train.isnull().sum().sort_values(ascending=False)
 percent =(df_train.isnull().sum() / df_train.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head()<sort_values>
for dataset in combine:
dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[ dataset['Fare'] > 31, 'Fare'] = 3
dataset['Fare'] = dataset['Fare'].astype(int)
 train = train.drop(['FareBand'], axis=1)
 combine = [train, test]
train.head(10 )
total = df_test.isnull().sum().sort_values(ascending=False)
 percent =(df_test.isnull().sum() /df_test.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head()<count_missing_values>
for dataset in combine:
dataset['Sex']=dataset['Sex'].map({'female':1,'male':0} ).astype(int)
 train.head(10 )
df_train.dropna(axis=0, inplace=True)
 df_train.isnull().sum()<feature_engineering>
train=train.drop(['Name','PassengerId'],axis=1)
 test=test.drop(['Name'],axis=1 )
headshot = df_train[['kills', 'winPlacePerc', 'headshotKills']]
headshot['headshotrate'] = headshot['headshotKills'] / headshot['kills']
headshot.corr()<feature_engineering>
X_train=train.drop(['Survived'],axis=1)
 Y_train=train.Survived
X_test=test.drop('PassengerId',axis=1 ).copy()
df_train['headshotrate'] = df_train['headshotKills']/df_train['kills']
df_test['headshotrate'] = df_test['headshotKills']/df_test['kills']
del headshot<feature_engineering>
k_fold=KFold(len(Y_train),n_folds=10,shuffle=True,random_state=0 )
killStreak = df_train[['kills','winPlacePerc','killStreaks']]
killStreak['killStreakrate'] = killStreak['killStreaks']/killStreak['kills']
killStreak.corr()<feature_engineering>
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC,LinearSVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import SGDClassifier
df_train['killStreakrate'] = -(df_train['killStreaks'] / df_train['kills'])
 df_test['killStreakrate'] = -(df_test['killStreaks'] / df_test['kills'])
 del killStreak<feature_engineering>
svc=SVC()
svc.fit(X_train, Y_train)
 Y_pred = svc.predict(X_test)
 acc_svc = round(svc.score(X_train, Y_train)* 100, 2)
 acc_svc
df_train['hacker_pt'] = 0
df_test['hacker_pt'] = 0<set_options>
knn = KNeighborsClassifier(n_neighbors = 3)
 knn.fit(X_train, Y_train)
 Y_pred = knn.predict(X_test)
 acc_knn = round(knn.score(X_train, Y_train)* 100, 2)
 acc_knn
pd.set_option('display.max_columns', 50 )<feature_engineering>
gaussian = GaussianNB()
gaussian.fit(X_train, Y_train)
 Y_pred = gaussian.predict(X_test)
 acc_gaussian = round(gaussian.score(X_train, Y_train)* 100, 2)
 acc_gaussian
df_train['total_Distance'] = df_train['rideDistance'] +df_train['walkDistance'] + df_train['swimDistance']
df_test['total_Distance'] = df_test['rideDistance'] + df_test['walkDistance'] + df_test['swimDistance']
df_train[(df_train['winPlacePerc'] == 1)&(df_train['total_Distance'] < 100)].head()<data_type_conversions>
linear_svc = LinearSVC()
linear_svc.fit(X_train, Y_train)
 Y_pred = linear_svc.predict(X_test)
 acc_linear_svc = round(linear_svc.score(X_train, Y_train)* 100, 2)
 acc_linear_svc
df_train['headshotrate'] = df_train['headshotrate'].fillna(0)
 df_train['killStreakrate'] = df_train['killStreakrate'].fillna(0)
 df_test['headshotrate'] = df_test['headshotrate'].fillna(0)
 df_test['killStreakrate'] = df_test['killStreakrate'].fillna(0)
<feature_engineering>
sgd = SGDClassifier()
sgd.fit(X_train, Y_train)
 Y_pred = sgd.predict(X_test)
 acc_sgd = round(sgd.score(X_train, Y_train)* 100, 2)
 acc_sgd
df_train['hacker_pt'][(df_train['heals'] + df_train['boosts'] < 1)&(df_train['total_Distance'] < 100)&(df_train['kills'] > 20)] = 1
df_test['hacker_pt'][(df_test['heals'] + df_test['boosts'] < 1)&(df_test['total_Distance'] < 100)&(df_test['kills'] > 20)] = 1<filter>
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, Y_train)
 Y_pred = decision_tree.predict(X_test)
 acc_decision_tree = round(decision_tree.score(X_train, Y_train)* 100, 2)
 acc_decision_tree
df_train['hacker_pt'][(df_train['kills'] > 10)&(df_train['weaponsAcquired'] >= 10)&(df_train['total_Distance'] == 0)] += 1
df_test['hacker_pt'][(df_test['kills'] > 10)&(df_test['weaponsAcquired'] >= 10)&(df_test['total_Distance'] == 0)] += 1<filter>
random_forest = RandomForestClassifier(n_estimators=100)
 random_forest.fit(X_train, Y_train)
 Y_pred1 = random_forest.predict(X_test)
 random_forest.score(X_train, Y_train)
 acc_random_forest = round(random_forest.score(X_train, Y_train)* 100, 2)
 acc_random_forest
df_train['hacker_pt'][df_train['longestKill'] >= 1000] += 1
df_test['hacker_pt'][df_train['longestKill'] >= 1000] += 1<feature_engineering>
models = pd.DataFrame({
'Model': ['Support Vector Machines', 'KNN','Random Forest', 'Naive Bayes','Stochastic Gradient Decent', 'Linear SVC','Decision Tree'],
'Score': [acc_svc, acc_knn,acc_random_forest, acc_gaussian, acc_sgd, acc_linear_svc, acc_decision_tree]})
 models.sort_values(by='Score', ascending=False )
kills = df_train[['assists','winPlacePerc','kills']]
kills['kills_assists'] =(kills['kills'] + kills['assists'])
 kills.corr()<drop_column>
submission=pd.read_csv('.. /input/gender_submission.csv' )
df_train['kills_assists'] = df_train['kills'] + df_train['assists']
df_test['kills_assists'] = df_test['kills'] + df_test['assists']
del kills<drop_column>
submission['Survived']= Y_pred1
submission['PassengerId']=test['PassengerId']
pd.DataFrame(submission,columns=['PassengerId','Survived'] ).to_csv('randomforest.csv',index=False)
df_train = reduce_mem_usage(df_train)
 df_test = reduce_mem_usage(df_test)
 gc.collect()<drop_column>
scoring = 'accuracy'
results = cross_val_score(random_forest, X_train, Y_train, cv=k_fold, n_jobs=1, scoring=scoring)
 results
del missing_data
del percent
del total
gc.collect()<groupby>
param_grid = {"n_estimators": [40,50,60],
"max_depth": [3, 5],
"min_samples_split": [5, 10],
"min_samples_leaf": [5, 6],
"max_leaf_nodes": [10, 15],
"min_weight_fraction_leaf": [0.1]}
param_grid
df_train_size = df_train.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_test_size = df_test.groupby(['matchId','groupId'] ).size().reset_index(name='group_size' )<groupby>
grid_search = GridSearchCV(random_forest, param_grid=param_grid,scoring='accuracy',cv=k_fold,n_jobs=-1)
 grid_search.fit(X_train, Y_train )
df_train_mean = df_train.groupby(['matchId','groupId'] ).mean().reset_index()
df_test_mean = df_test.groupby(['matchId','groupId'] ).mean().reset_index()<groupby>
print(grid_search.best_score_)
 print(grid_search.best_params_ )
<merge><EOS> 
xgb=XGBClassifier(max_depth=5, n_estimators=900, learning_rate=1,gamma=0,min_child_weight=2, reg_alpha=0.1,subsample=0.8,cv=k_fold)
 xgb.fit(X_train,Y_train)
 Y_pred = xgb.predict(X_test)
 acc_xgb = round(xgb.score(X_train, Y_train)* 100, 2)
 acc_xgb