Kaggle competition: Titanic - Machine Learning from Disaster
model_1.save("model_1_solo.h5")
<choose_model_class>
datrn = pd.read_csv('.. /input/train.csv')
 datst = pd.read_csv('.. /input/test.csv' )
model_2 = keras.models.Sequential()
model_2.add(Dense(32, input_dim=len(list_feat), activation="elu", kernel_initializer="he_normal"))
model_2.add(Dense(64, activation="elu", kernel_initializer="he_normal"))
model_2.add(Dense(128, activation="elu", kernel_initializer="he_normal"))
model_2.add(keras.layers.Dropout(0.25))
model_2.add(Dense(256, activation="elu", kernel_initializer="he_normal"))
model_2.add(Dense(256, activation="elu", kernel_initializer="he_normal"))
model_2.add(keras.layers.Dropout(0.25))
model_2.add(Dense(128, activation="elu", kernel_initializer="he_normal"))
model_2.add(Dense(64, activation="elu", kernel_initializer="he_normal"))
model_2.add(Dense(32, activation="elu", kernel_initializer="he_normal"))
model_2.add(keras.layers.Dropout(0.25))
model_2.add(Dense(1, activation="sigmoid"))
model_2.compile(optimizer="RMSprop", loss='MAE', metrics=["MAE"] )<prepare_x_and_y>
datsub = pd.read_csv('.. /input/gender_submission.csv' )
x_train = train.loc[train.matchType_1 == "duo", list_feat]
y_train = train.loc[train.matchType_1 == "duo", ["winPlacePerc"]]<train_model>
outcome = datrn['Survived']
data = datrn.drop('Survived',axis=1 )
model_2.fit(x=x_train, y=y_train, epochs=50, batch_size=10000, validation_split=0.2, shuffle=True)
 model_2.fit(x=x_train, y=y_train, epochs=40, batch_size=2000, validation_split=0.2, shuffle=True )<train_model>
def accuracy_score(truth, pred):

 if len(truth)== len(pred):
return "Predictions have an accuracy of {:.2f}%.".format(( truth == pred ).mean() *100)
 else:
return "Number of predictions does not match number of outcomes!"
<save_model>
warnings.filterwarnings("ignore", category = UserWarning, module = "matplotlib")
 get_ipython().run_line_magic('matplotlib', 'inline')
 def filter_data(data, condition):

 field, op, value = condition.split(" ")
 try:
value = float(value)
 except:
value = value.strip("'"")
 if op == ">":
matches = data[field] > value
elif op == "<":
matches = data[field] < value
elif op == ">=":
matches = data[field] >= value
elif op == "<=":
matches = data[field] <= value
elif op == "==":
matches = data[field] == value
elif op == "!=":
matches = data[field] != value
else:
raise Exception("Invalid comparison operator.Only >, <, >=, <=, ==, != allowed.")
 data = data[matches].reset_index(drop = True)
 return data
def survival_stats(data, outcomes, key, filters = []):

 if key not in data.columns.values :
print("'{}' is not a feature of the Titanic data.Did you spell something wrong?".format(key))
return False
if(key == 'Cabin' or key == 'PassengerId' or key == 'Ticket'):
print("'{}' has too many unique categories to display! Try a different feature.".format(key))
return False
all_data = pd.concat([data, outcomes.to_frame() ], axis = 1)
 for condition in filters:
all_data = filter_data(all_data, condition)
 all_data = all_data[[key, 'Survived']]
plt.figure(figsize=(8,6))
if(key == 'Age' or key == 'Fare'):
all_data = all_data[~np.isnan(all_data[key])]
min_value = all_data[key].min()
max_value = all_data[key].max()
value_range = max_value - min_value
if(key == 'Fare'):
bins = np.arange(0, all_data['Fare'].max() + 20, 20)
 if(key == 'Age'):
bins = np.arange(0, all_data['Age'].max() + 10, 10)
 nonsurv_vals = all_data[all_data['Survived'] == 0][key].reset_index(drop = True)
 surv_vals = all_data[all_data['Survived'] == 1][key].reset_index(drop = True)
 plt.hist(nonsurv_vals, bins = bins, alpha = 0.6,
color = 'red', label = 'Did not survive')
 plt.hist(surv_vals, bins = bins, alpha = 0.6,
color = 'green', label = 'Survived')
 plt.xlim(0, bins.max())
 plt.legend(framealpha = 0.8)
 else:
if(key == 'Pclass'):
values = np.arange(1,4)
 if(key == 'Parch' or key == 'SibSp'):
values = np.arange(0,np.max(data[key])+ 1)
 if(key == 'Embarked'):
values = ['C', 'Q', 'S']
if(key == 'Sex'):
values = ['male', 'female']
frame = pd.DataFrame(index = np.arange(len(values)) , columns=(key,'Survived','NSurvived'))
for i, value in enumerate(values):
frame.loc[i] = [value, \
len(all_data[(all_data['Survived'] == 1)&(all_data[key] == value)]), \
len(all_data[(all_data['Survived'] == 0)&(all_data[key] == value)])]
bar_width = 0.4
for i in np.arange(len(frame)) :
nonsurv_bar = plt.bar(i-bar_width, frame.loc[i]['NSurvived'], width = bar_width, color = 'r')
 surv_bar = plt.bar(i, frame.loc[i]['Survived'], width = bar_width, color = 'g')
 plt.xticks(np.arange(len(frame)) , values)
 plt.legend(( nonsurv_bar[0], surv_bar[0]),('Did not survive', 'Survived'), framealpha = 0.8)
 plt.xlabel(key)
 plt.ylabel('Number of Passengers')
 plt.title('Passenger Survival Statistics With '%s' Feature'%(key))
plt.show()
if sum(pd.isnull(all_data[key])) :
nan_outcomes = all_data[pd.isnull(all_data[key])]['Survived']
print("Passengers with missing '{}' values: {}({} survived, {} did not survive)".format(\
key, len(nan_outcomes), sum(nan_outcomes == 1), sum(nan_outcomes == 0)) )
model_2.save("model_2_duo.h5")
<choose_model_class>
survival_stats(data, outcome, 'Sex' )
model_3 = keras.models.Sequential()
model_3.add(Dense(32, input_dim=len(list_feat), activation="elu", kernel_initializer="he_normal"))
model_3.add(Dense(64, activation="elu", kernel_initializer="he_normal"))
model_3.add(Dense(128, activation="elu", kernel_initializer="he_normal"))
model_3.add(keras.layers.Dropout(0.25))
model_3.add(Dense(256, activation="elu", kernel_initializer="he_normal"))
model_3.add(Dense(256, activation="elu", kernel_initializer="he_normal"))
model_3.add(keras.layers.Dropout(0.35))
model_3.add(Dense(128, activation="elu", kernel_initializer="he_normal"))
model_3.add(Dense(64, activation="elu", kernel_initializer="he_normal"))
model_3.add(Dense(32, activation="elu", kernel_initializer="he_normal"))
model_3.add(keras.layers.Dropout(0.25))
model_3.add(Dense(1, activation="sigmoid"))
model_3.compile(optimizer="RMSprop", loss='MAE', metrics=["MAE"] )<prepare_x_and_y>
survival_stats(data, outcome, 'Age', ["Sex == 'female'"] )
x_train = train.loc[train.matchType_1 == "squad", list_feat]
y_train = train.loc[train.matchType_1 == "squad", ["winPlacePerc"]]<train_model>
survival_stats(data, outcome, "Age",["Sex == 'male'","Embarked == 'C'"] )
model_3.fit(x=x_train, y=y_train, epochs=60, batch_size=10000, validation_split=0.2, shuffle=True)
 model_3.fit(x=x_train, y=y_train, epochs=50, batch_size=3000, validation_split=0.2, shuffle=True )<train_model>
survival_stats(data, outcome, "Age",["Sex == 'male'","Embarked == 'S'"] )
<save_model>
def predict(data):

 predictions = []
for _, passenger in data.iterrows() :
if passenger['Sex'] == 'female':
if passenger['Embarked']== 'C' and passenger['Pclass'] <=3 :predictions.append(1)
 elif passenger['Embarked']== 'S' and passenger['Pclass'] <3:predictions.append(1)
 else:
if(passenger['SibSp'] <2)and(passenger['Parch']<2):
predictions.append(1)
 else:
predictions.append(0)
 elif passenger['Sex']=='male' and passenger['Age']<15:
if passenger['SibSp'] < 3 and passenger['Embarked']=='S':predictions.append(1)
 elif passenger['Embarked']=='C':predictions.append(1)
 else:predictions.append(0)
 else:predictions.append(0)
 return pd.Series(predictions )
model_3.save("model_3_squad.h5")
<choose_model_class>
pred = predict(data )
model_4 = keras.models.Sequential()
model_4.add(Dense(32, input_dim=len(list_feat), activation="elu", kernel_initializer="he_normal"))
model_4.add(Dense(64, activation="elu", kernel_initializer="he_normal"))
model_4.add(Dense(128, activation="elu", kernel_initializer="he_normal"))
model_4.add(keras.layers.Dropout(0.25))
model_4.add(Dense(128, activation="elu", kernel_initializer="he_normal"))
model_4.add(Dense(64, activation="elu", kernel_initializer="he_normal"))
model_4.add(Dense(32, activation="elu", kernel_initializer="he_normal"))
model_4.add(keras.layers.Dropout(0.25))
model_4.add(Dense(1, activation="sigmoid"))
model_4.compile(optimizer="RMSprop", loss='MAE', metrics=["MAE"] )<prepare_x_and_y>
print(accuracy_score(outcome,pred))
x_train = train.loc[train.matchType_1 == "etc", list_feat]
y_train = train.loc[train.matchType_1 == "etc", ["winPlacePerc"]]<train_model>
predtst = predict(datst )
model_4.fit(x=x_train, y=y_train, epochs=70, batch_size=10000, validation_split=0.2, shuffle=True)
 model_4.fit(x=x_train, y=y_train, epochs=150, batch_size=1000, validation_split=0.2, shuffle=True )<define_variables>
Past = datst.iloc[:,0]
<save_model>
dst = []
i=0
while i<len(predtst):
dst.append(( Past[i],predtst[i]))
i+=1
model_4.save("model_4_etc.h5")
<drop_column>
d = pd.DataFrame(dst,columns=['PassengerId','Survived'] )
<load_from_csv><EOS> 
d.to_csv('gender_submission.csv',index=False )