Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<prepare_x_and_y>
plt.rc("font", size=14)
 sns.set(style="dark")
 sns.set(style="darkgrid", color_codes=True)
 RED = "\033[1;31m"
BLUE = "\033[1;34m"
CYAN = "\033[1;36m"
GREEN = "\033[0;32m"
N = test_df.shape[0]
x_test = np.empty(( N, im_size, im_size, 3), dtype=np.uint8)
 try:
for i, image_id in enumerate(test_df['id_code']):
x_test[i, :, :, :] = preprocess_image(
 f'.. /input/aptos2019-blindness-detection/test_images/{image_id}.png',
desired_size=im_size 
)
 print('Test dataset correctly processed')
 except:
print('Test dataset NOT processed' )<choose_model_class>
titanic_df = pd.read_csv(".. /input/titanic/train.csv")
 test_df = pd.read_csv(".. /input/titanic/test.csv")
 titanic_df.head(5)
print(os.listdir(".. /input/kerasefficientnetsmaster/keras-efficientnets-master/keras-efficientnets-master/keras_efficientnets"))
sys.path.append(os.path.abspath('.. /input/kerasefficientnetsmaster/keras-efficientnets-master/keras-efficientnets-master/'))
effnet = EfficientNetB5(input_shape=(im_size,im_size,3),
weights=sys.path.append(os.path.abspath('.. /input/efficientnet-keras-weights-b0b5/efficientnet-b5_imagenet_1000_notop.h5')) ,
include_top=False)
 def build_model() :
model = Sequential()
model.add(effnet)
 model.add(layers.Dropout(0.25))
model.add(layers.Dense(2048))
model.add(layers.LeakyReLU())
 model.add(layers.GlobalAveragePooling2D())
 model.add(layers.Dropout(0.5))
model.add(layers.Dense(1, activation='linear'))
return model
model = build_model()
model.load_weights('.. /input/efficientnet-keras-aptos/model.h5')
 model.summary()<predict_on_test>
train_data = titanic_df
train_data["Age"].fillna(28, inplace=True)
 train_data["Embarked"].fillna("S", inplace=True)
 train_data.drop('Cabin', axis=1, inplace=True )
y_test = model.predict(x_test)
 coef = [0.5, 1.5, 2.5, 3.5]
for i, pred in enumerate(y_test):
if pred < coef[0]:
y_test[i] = 0
elif pred >= coef[0] and pred < coef[1]:
y_test[i] = 1
elif pred >= coef[1] and pred < coef[2]:
y_test[i] = 2
elif pred >= coef[2] and pred < coef[3]:
y_test[i] = 3
else:
y_test[i] = 4
test_df['diagnosis'] = y_test.astype(int)
 test_df.to_csv('submission.csv',index=False)
 print(round(test_df.diagnosis.value_counts() /len(test_df)*100,4))<import_modules>
train_data['TravelBuds']=train_data["SibSp"]+train_data["Parch"]
train_data['TravelAlone']=np.where(train_data['TravelBuds']>0, 0, 1)
 train_data.drop('SibSp', axis=1, inplace=True)
 train_data.drop('Parch', axis=1, inplace=True)
 train_data.drop('TravelBuds', axis=1, inplace=True)
 train2 = pd.get_dummies(train_data, columns=["Pclass"])
 train3 = pd.get_dummies(train2, columns=["Embarked"])
 train4=pd.get_dummies(train3, columns=["Sex"])
 train4.drop('Sex_female', axis=1, inplace=True)
 train4.drop('PassengerId', axis=1, inplace=True)
 train4.drop('Name', axis=1, inplace=True)
 train4.drop('Ticket', axis=1, inplace=True)
 train4.head(5)
 df_final = train4
import pandas as pd
from sklearn.preprocessing import OneHotEncoder,LabelEncoder
from sklearn.model_selection import train_test_split<load_from_csv>
test_df["Age"].fillna(28, inplace=True)
 test_df["Fare"].fillna(14.45, inplace=True)
 test_df.drop('Cabin', axis=1, inplace=True )
train=pd.read_csv(".. /input/train_V2.csv")
<load_from_csv>
test_df['TravelBuds']=test_df["SibSp"]+test_df["Parch"]
test_df['TravelAlone']=np.where(test_df['TravelBuds']>0, 0, 1)
 test_df.drop('SibSp', axis=1, inplace=True)
 test_df.drop('Parch', axis=1, inplace=True)
 test_df.drop('TravelBuds', axis=1, inplace=True)
 test2 = pd.get_dummies(test_df, columns=["Pclass"])
 test3 = pd.get_dummies(test2, columns=["Embarked"])
 test4=pd.get_dummies(test3, columns=["Sex"])
 test4.drop('Sex_female', axis=1, inplace=True)
 test4.drop('PassengerId', axis=1, inplace=True)
 test4.drop('Name', axis=1, inplace=True)
 test4.drop('Ticket', axis=1, inplace=True)
 final_test = test4
test=pd.read_csv(".. /input/test_V2.csv" )<categorify>
df_final['IsMinor']=np.where(train_data['Age']<=16, 1, 0 )
le=LabelEncoder()
enc=OneHotEncoder()
train.loc[(train.matchType!='solo')&(train.matchType!='duo')&(train.matchType!='squad')&(train.matchType!='solo-fpp')&(train.matchType!='duo-fpp')&(train.matchType!='squad-fpp'),'matchType']='other'
train['matchType']=train['matchType'].map({'solo':0 , 'duo':1, 'squad':2, 'solo-fpp':3, 'duo-fpp':4, 'squad-fpp':5,'other':6} )<count_missing_values>
final_test['IsMinor']=np.where(final_test['Age']<=16, 1, 0 )
train.isnull().sum()<count_missing_values>
cols=["Age", "Fare", "TravelAlone", "Pclass_1", "Pclass_2","Embarked_C","Embarked_S","Sex_male","IsMinor"]
X=df_final[cols]
Y=df_final['Survived']
train.isnull().sum()<data_type_conversions>
cols2=["Age", "Pclass_1", "Pclass_2","Embarked_C","Embarked_S","Sex_male"]
X2=df_final[cols2]
Y=df_final['Survived']
logit_model=sm.Logit(Y,X2)
 result=logit_model.fit()
sys.stdout.write(GREEN)
 print(result.summary() )
train.dropna(inplace=True)
 train.isnull().sum()<categorify>
logreg = LogisticRegression()
logreg.fit(X2, Y)
 print("Model Accuracy : {:.2f}%".format(logreg.score(X2, Y)*100))
data=enc.fit(train[['matchType']])
 temp=enc.transform(train[['matchType']] )<create_dataframe>
train, test = train_test_split(df_final, test_size=0.25 )
temp1=pd.DataFrame(temp.toarray() ,columns=["solo", "duo", "squad", "solo-fpp", "duo-fpp", "squad-fpp","other"])
 temp1=temp1.set_index(train.index.values)
 temp1
train=pd.concat([train,temp1],axis=1)
<drop_column>
cols2=["Age", "Pclass_1", "Pclass_2","Embarked_C","Embarked_S","Sex_male"]
X3=train[cols2]
Y3=train['Survived']
logit_model3=sm.Logit(Y3,X3 )
train['killsasist']=train['kills']+train['assists']+train['roadKills']
train['total_distance']=train['swimDistance']+train['rideDistance']+train['walkDistance']
train['external_booster']=train['boosts']+train['weaponsAcquired']+train['heals']
train=train.drop(['assists','kills','swimDistance','rideDistance','walkDistance','boosts','weaponsAcquired','heals','roadKills','rankPoints'],axis=1 )<drop_column>
logreg = LogisticRegression()
logreg.fit(X3, Y3)
 sys.stdout.write(GREEN)
 print("Model Accuracy : {:.2f}%".format(logreg.score(X3, Y3)*100))
train=train.drop(['killPoints','maxPlace','winPoints'],axis=1 )<categorify>
logreg.fit(X3, Y3)
 X3_test = test[cols2]
Y3_test = test['Survived']
Y3test_pred = logreg.predict(X3_test)
 sys.stdout.write(GREEN)
 print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X3_test, Y3_test)*100))
train['Players_all']=train.groupby('matchId')['Id'].transform('count')
 train['players_group']=train.groupby('groupId')['Id'].transform('count' )<prepare_x_and_y>
logreg.fit(X3, Y3)
 Y3_pred = logreg.predict(X3)
 y_true = Y3
y_scores = Y3_pred
sys.stdout.write(GREEN)
 print("Model ROC_AUC : {:.2f}%".format(roc_auc_score(y_true, y_scores)) )
Y=train.winPlacePerc
train = train.drop(["Id", "groupId", "matchId","winPlacePerc"], axis=1)
 del train['matchType']
train.head()<split>
cols=["Age", "Fare", "TravelAlone", "Pclass_1", "Pclass_2","Embarked_C","Embarked_S","Sex_male","IsMinor"]
X=df_final[cols]
Y=df_final['Survived']
random_forest = RandomForestClassifier(n_estimators=100)
 random_forest.fit(X, Y)
 sys.stdout.write(GREEN)
 print('ROC AUC: %0.3f' % random_forest.score(X, Y))
<import_modules>
final_test_RF=final_test[cols]
Y_pred_RF = random_forest.predict(final_test_RF )
<train_model>
submission = pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": Y_pred_RF
})
 submission.to_csv('titanic_RF.csv', index=False )
d_train = lgb.Dataset(train, label=Y)
 params = {}
params['learning_rate'] = 0.05
params['boosting_type'] = 'gbdt'
params['objective'] = 'regression'
params['metric'] = 'mae'
params['sub_feature'] = 0.9
params['num_leaves'] = 511
params['min_data'] = 1
params['max_depth'] = 30
params['min_gain_to_split']= 0.00001
clf = lgb.train(params, d_train,2000)
<train_model>
tree1 = tree.DecisionTreeClassifier(criterion='gini', splitter='best',max_depth=3, min_samples_leaf=20 )
<predict_on_test>
cols=["Age", "Fare", "TravelAlone", "Pclass_1", "Pclass_2","Embarked_C","Embarked_S","Sex_male","IsMinor"]
X_DT=df_final[cols]
Y_DT=df_final['Survived']
tree1.fit(X_DT, Y_DT )
<categorify>
tree1_view = tree.export_graphviz(tree1, out_file=None, feature_names = X_DT.columns.values, rotate=True)
 tree1viz = graphviz.Source(tree1_view)
 tree1viz
test.loc[(test.matchType!='solo')&(test.matchType!='duo')&(test.matchType!='squad')&(test.matchType!='solo-fpp')&(test.matchType!='duo-fpp')&(test.matchType!='squad-fpp'),'matchType']='other'
test['matchType']=test['matchType'].map({'solo':0 , 'duo':1, 'squad':2, 'solo-fpp':3, 'duo-fpp':4, 'squad-fpp':5,'other':6} )<count_missing_values>
final_test_DT=final_test[cols]
Y_pred_DT = tree1.predict(final_test_DT)
 submission = pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": Y_pred_DT
})
 submission.to_csv('titanic_DT.csv', index=False )
test.isnull().sum()<count_missing_values>
submission = pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": Y_pred_RF* 0.8 + Y_pred_DT*0.2
})
 submission.to_csv('titanic_ensemble.csv', index=False )
test.isnull().sum()<categorify>
from sympy import simplify, cos, sin, Symbol, Function, tanh, pprint, init_printing, exp
from sympy.functions import Min,Max
data_test=enc.fit(test[['matchType']])
 temp_test=enc.transform(test[['matchType']])
 temp2=pd.DataFrame(temp_test.toarray() ,columns=["solo", "duo", "squad", "solo-fpp", "duo-fpp", "squad-fpp","other"])
 temp2=temp2.set_index(test.index.values)
 temp2
test=pd.concat([test,temp2],axis=1)
 del test['matchType'] 
<drop_column>
A = 0.058823499828577
B = 0.841127
C = 0.138462007045746
D = 0.31830988618379069
E = 2.810815
F = 0.63661977236758138
G = 5.428569793701172
H = 3.1415926535897931
I = 0.592158
J = 4.869778
K = 0.063467
L = -0.091481
M = 0.0821533
N = 0.720430016517639
O = 0.230145
P = 9.89287
Q = 785
R = 1.07241
S = 281
T = 734
U = 5.3
V = 67.0
W = 2.484848
X = 8.48635
Y = 63
Z = 12.6275
AA = 0.735354
AB = 727
AC = 2.5
AD = 2.6
AE = 0.3
AF = 3.0
AG = 0.226263
AH = 2.0
AI = 12.4148
AJ = 96
AK = 0.130303
AL = 176
AM = 3.2
BIG = [A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,AA,AB,AC,AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM]
test['killsasist']=test['kills']+test['assists']+test['roadKills']
test['total_distance']=test['swimDistance']+test['rideDistance']+test['walkDistance']
test['external_booster']=test['boosts']+test['weaponsAcquired']+test['heals']
test=test.drop(['assists','kills','swimDistance','rideDistance','walkDistance','boosts','weaponsAcquired','heals','roadKills','rankPoints'],axis=1 )<drop_column>
def GeneticFunction(data,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,AA,AB,AC,AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM):
return(( np.minimum(((((A + data["Sex"])- np.cos(( data["Pclass"] / AH)))* AH)) ,(( B)))* AH)+
np.maximum(((data["SibSp"] - AC)) ,(-(np.minimum(( data["Sex"]),(np.sin(data["Parch"])))* data["Pclass"])))+ 
(AG *(( np.minimum(( data["Sex"]),(((data["Parch"] / AH)/ AH)))* data["Age"])- data["Cabin"])) +
np.minimum(((np.sin(( data["Parch"] *(( data["Fare"] - AA)* AH)))* AH)) ,(( data["SibSp"] / AH)))+
np.maximum(( np.minimum(( -np.cos(data["Embarked"])) ,(C))),(np.sin(((data["Cabin"] - data["Fare"])* AH)))) +
-np.minimum(((((data["Age"] * data["Parch"])* data["Embarked"])+ data["Parch"])) ,(np.sin(data["Pclass"])))+
np.minimum(( data["Sex"]),(( np.sin(-(data["Fare"] * np.cos(( data["Fare"] * W)))) / AH)))+
np.minimum(((O)) ,(np.sin(np.minimum(((( V / AH)* np.sin(data["Fare"]))),(D)))))+
np.sin(( np.sin(data["Cabin"])*(np.sin(( Z)) * np.maximum(( data["Age"]),(data["Fare"])))))+
np.sin(((np.minimum(( data["Fare"]),(( data["Cabin"] * data["Embarked"])))/ AH)* -data["Fare"])) +
np.minimum(((( AD * data["SibSp"])* np.sin(((AJ)* np.sin(data["Cabin"]))))),(data["Parch"])) +
np.sin(np.sin(( np.maximum(( np.minimum(( data["Age"]),(data["Cabin"]))),(( data["Fare"] * AK)))* data["Cabin"])))+
np.maximum(( np.sin(((AI)*(data["Age"] / AH)))) ,(np.sin(( -AF * data["Cabin"])))) + 
(np.minimum(( np.sin(((( np.sin(((data["Fare"] * AH)* AH)) * AH)* AH)* AH))),(data["SibSp"])) / AH)+ 
(( data["Sex"] - data["SibSp"])*(np.cos(((data["Embarked"] - AA)+ data["Age"])) / AH)) + 
(( np.sin(data["Cabin"])/ AH)-(np.cos(np.minimum(( data["Age"]),(data["Embarked"])))* np.sin(data["Embarked"])))+
np.minimum(( AE),(( data["Sex"] *(J *(N - np.sin(( data["Age"] * AH)))))))+ 
(np.minimum(( np.cos(data["Fare"])) ,(np.maximum(( np.sin(data["Age"])) ,(data["Parch"])))) * np.cos(( data["Fare"] / AH)))+
np.sin(( data["Parch"] * np.minimum(((data["Age"] - K)) ,(( np.cos(( data["Pclass"] * AH)) / AH)))))+ 
(data["Parch"] *(np.sin(((data["Fare"] *(I * data["Age"])) * AH)) / AH)) + 
(D * np.cos(np.maximum(((0.5 * data["Fare"])) ,(( np.sin(N)* data["Age"])))))+ 
(np.minimum(((data["SibSp"] / AH)) ,(np.sin(((data["Pclass"] - data["Fare"])* data["SibSp"])))) * data["SibSp"])+
np.tanh(( data["Sex"] * np.sin(( U * np.sin(( data["Cabin"] * np.cos(data["Fare"])))))))+ 
(np.minimum(( data["Parch"]),(data["Sex"])) * np.cos(np.maximum(((np.cos(data["Parch"])+ data["Age"])) ,(AM)))) + 
(np.minimum(( np.tanh(((data["Cabin"] / AH)+ data["Parch"]))),(( data["Sex"] + np.cos(data["Age"])))) / AH)+ 
(np.sin(( np.sin(data["Sex"])*(np.sin(( data["Age"] * data["Pclass"])) * data["Pclass"])))/ AH)+ 
(data["Sex"] *(np.cos(((data["Sex"] + data["Fare"])*(( X)*(Y)))) / AH)) +
np.minimum(( data["Sex"]),(( np.cos(( data["Age"] * np.tanh(np.sin(np.cos(data["Fare"])))))/ AH)))+ 
(np.tanh(np.tanh(-np.cos(( np.maximum(( np.cos(data["Fare"])) ,(L)) * data["Age"])))) / AH)+ 
(np.tanh(np.cos(( np.cos(data["Age"])+(data["Age"] + np.minimum(( data["Fare"]),(data["Age"])))))) / AH)+ 
(np.tanh(np.cos(( data["Age"] *(( -AH + np.sin(data["SibSp"])) + data["Fare"])))) / AH)+ 
(np.minimum(((( S)- data["Fare"])) ,(np.sin(( np.maximum(((AL)) ,(data["Fare"])) * data["SibSp"])))) * AH)+
np.sin(((np.maximum(( data["Embarked"]),(data["Age"])) * AH)*(((Q)* H)* data["Age"])))+
np.minimum(( data["Sex"]),(np.sin(-(np.minimum(((data["Cabin"] / AH)) ,(data["SibSp"])) *(data["Fare"] / AH)))))+
np.sin(np.sin(( data["Cabin"] *(data["Embarked"] +(np.tanh(-data["Age"])+ data["Fare"])))))+ 
(np.cos(np.cos(data["Fare"])) *(np.sin(( data["Embarked"] -(( T)* data["Fare"])))/ AH)) + 
(( np.minimum(( data["SibSp"]),(np.cos(data["Fare"])))* np.cos(data["SibSp"])) * np.sin(( data["Age"] / AH)))+ 
(np.sin(( np.sin(( data["SibSp"] * np.cos(( data["Fare"] * AH)))) +(data["Cabin"] * AH)))/ AH)+ 
(((data["Sex"] * data["SibSp"])* np.sin(np.sin(-(data["Fare"] * data["Cabin"])))) * AH)+ 
(np.sin(( data["SibSp"] *(((( G + V)* AH)/ AH)* data["Age"])))/ AH)+ 
(data["Pclass"] *(np.sin(((data["Embarked"] * data["Cabin"])*(data["Age"] -(R)))) / AH)) + 
(np.cos(((((-data["SibSp"] + data["Age"])+ data["Parch"])* data["Embarked"])/ AH)) / AH)+ 
(D * np.sin(((data["Age"] *(( data["Embarked"] * np.sin(data["Fare"])) * AH)) * AH)))+ 
(( np.minimum(((data["Age"] * A)) ,(data["Sex"])) - F)* np.tanh(np.sin(data["Pclass"])))+
-np.minimum(((np.cos(((AB)*(( data["Fare"] + data["Parch"])* AH)))/ AH)) ,(data["Fare"])) + 
(np.minimum(( np.cos(data["Fare"])) ,(data["SibSp"])) * np.minimum(( np.sin(data["Parch"])) ,(np.cos(( data["Embarked"] * AH)))))+ 
(np.minimum(((( data["Fare"] / AH)- E)) ,(C)) * np.sin(( K * data["Age"])))+
np.minimum(((M)) ,(((np.sin(data["Fare"])+ data["Embarked"])- np.cos(( data["Age"] *(P)))))) )
test=test.drop(['killPoints','maxPlace','winPoints'],axis=1 )<categorify>
def CleanData(data):
data.drop(['Ticket', 'Name'], inplace=True, axis=1)
 data.Sex.fillna('0', inplace=True)
 data.loc[data.Sex != 'male', 'Sex'] = 0
data.loc[data.Sex == 'male', 'Sex'] = 1
data.Cabin.fillna('0', inplace=True)
 data.loc[data.Cabin.str[0] == 'A', 'Cabin'] = 1
data.loc[data.Cabin.str[0] == 'B', 'Cabin'] = 2
data.loc[data.Cabin.str[0] == 'C', 'Cabin'] = 3
data.loc[data.Cabin.str[0] == 'D', 'Cabin'] = 4
data.loc[data.Cabin.str[0] == 'E', 'Cabin'] = 5
data.loc[data.Cabin.str[0] == 'F', 'Cabin'] = 6
data.loc[data.Cabin.str[0] == 'G', 'Cabin'] = 7
data.loc[data.Cabin.str[0] == 'T', 'Cabin'] = 8
data.loc[data.Embarked == 'C', 'Embarked'] = 1
data.loc[data.Embarked == 'Q', 'Embarked'] = 2
data.loc[data.Embarked == 'S', 'Embarked'] = 3
data.Embarked.fillna(0, inplace=True)
 data.fillna(-1, inplace=True)
 return data.astype(float)
 def Outputs(data):
return np.round(1.-(1./(1.+np.exp(-data))))
test['Players_all']=test.groupby('matchId')['Id'].transform('count')
 test['players_group']=test.groupby('groupId')['Id'].transform('count' )<drop_column>
raw_train = pd.read_csv('.. /input/titanic/train.csv')
 raw_test = pd.read_csv('.. /input/titanic/test.csv')
 cleanedTrain = CleanData(raw_train)
 cleanedTest = CleanData(raw_test)
 thisArray = BIG.copy()
testPredictions = Outputs(GeneticFunction(cleanedTrain,thisArray[0],thisArray[1],thisArray[2],thisArray[3],thisArray[4],thisArray[5],thisArray[6],thisArray[7],thisArray[8],thisArray[9],thisArray[10],thisArray[11],thisArray[12],thisArray[13],thisArray[14],thisArray[15],thisArray[16],thisArray[17],thisArray[18],thisArray[19],thisArray[20],thisArray[21],thisArray[22],thisArray[23],thisArray[24],thisArray[25],thisArray[26],thisArray[27],thisArray[28],thisArray[29],thisArray[30],thisArray[31],thisArray[32],thisArray[33],thisArray[34],thisArray[35],thisArray[36],thisArray[37],thisArray[38]))
pdcheck = pd.DataFrame({'Survived': testPredictions.astype(int)})
 ret = pdcheck.Survived.where(pdcheck["Survived"].values==cleanedTrain["Survived"].values ).notna()
t,f = ret.value_counts()
score = 100/(t+f)*t
print("Training set score: ",score )
<save_to_csv><EOS> 
testPredictions = Outputs(GeneticFunction(cleanedTest,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,AA,AB,AC,AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM))
pdtest = pd.DataFrame({'PassengerId': cleanedTest.PassengerId.astype(int),
'Survived': testPredictions.astype(int)})
 pdtest.to_csv('submission_GA.csv', index=False)
 pdtest.head()