Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<define_search_space>
print(os.listdir(".. /input"))
warnings.filterwarnings("ignore")
lr = slice(2e-05 )<save_model>
ROOT_DIR = ".. /input/"
SUBINT_DIR = "./"
learn.save('stage-1' )<train_model>
print("Lecture des fichiers train.csv")
 train = pd.read_csv(ROOT_DIR+"train.csv",sep=',')
 print(train.shape )
learn.fit_one_cycle(5,lr )<save_model>
print("Lecture des fichiers test.csv")
 test = pd.read_csv(ROOT_DIR+"test.csv",sep=',')
 print(test.shape )
learn.save('stage-2' )<train_model>
print("Lecture des fichiers gender_submission.csv")
 submit = pd.read_csv(ROOT_DIR+"gender_submission.csv",sep=',')
 print(submit.shape )
learn.fit_one_cycle(10,lr )<save_model>
print("Concatenation train + test")
 train['X'] = 'X'
test['X'] = 'Y'
big = pd.concat([train,test] )
learn.save('stage-3' )<load_pretrained>
big['XName'] = big['Name'].apply(lambda x: str(x)[0:str(x ).find(',')] if str(x ).find(',')!= -1 else x)
 big['TName'] = big['Name'].apply(lambda x: str(x)[str(x ).find(',')+2:str(x ).find('.')+1:] if str(x ).find('.')!= -1 else x)
 big['XCabin'] = big['Cabin'].apply(lambda x: 'U' if(x is np.nan or x != x)else str(x)[0])
 big['LTick'] = big['Ticket'].apply(lambda x: str(x)[0:str(x ).find(' ')] if str(x ).find(' ')!= -1 else ' ')
 K = big.groupby(['Ticket'] ).groups
for name,group in K.items() :
if len(group)> 1:
CN = list(set([ str(x)[0] for x in big['Cabin'].iloc[group] ])- set(['n']))
if(len(CN)== 0):
big['XCabin'].iloc[group] = 'U'
else:
big['XCabin'].iloc[group] = CN[0]
learn.load('stage-3' )<load_from_csv>
big['XFam'] = big['SibSp'] + big['Parch'] + 1
big['XFam'] = np.log1p(( big['XFam'] - big['XFam'].mean())/ big['XFam'].std() )
test_csv = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 test_csv.drop('id',axis = 'columns',inplace = True)
 sub_df = pd.DataFrame(columns=['id','label'] )<normalization>
big['Last_Name'] = big['Name'].apply(lambda x: str.split(x, ",")[0])
 big['Fare'].fillna(big['Fare'].mean() , inplace=True)
 DEFAULT_SURVIVAL_VALUE = 0.5
big['Family_Survival'] = DEFAULT_SURVIVAL_VALUE
for grp, grp_df in big[['Survived','Name', 'Last_Name', 'Fare', 'Ticket', 'PassengerId',
'SibSp', 'Parch', 'Age', 'Cabin']].groupby(['Last_Name', 'Fare']):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
big.loc[big['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
big.loc[big['PassengerId'] == passID, 'Family_Survival'] = 0
print("Number of passengers with family survival information:", big.loc[big['Family_Survival']!=0.5].shape[0] )
def get_img(data):
t1 = data.reshape(28,28)/255
t1 = np.stack([t1]*3,axis=0)
 img = Image(FloatTensor(t1))
return img<import_modules>
for _, grp_df in big.groupby('Ticket'):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
if(row['Family_Survival'] == 0)|(row['Family_Survival']== 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
big.loc[big['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
big.loc[big['PassengerId'] == passID, 'Family_Survival'] = 0
print("Number of passenger with family/group survival information: "+str(big[big['Family_Survival']!=0.5].shape[0]))
from fastprogress import progress_bar<save_to_csv>
del big['Ticket'], big['Cabin'], big['Name'], big['XName'], big['Last_Name']
def decr(ido):
return ido-1
sub_df['id'] = sub_df['id'].map(decr)
 sub_df.to_csv('submission.csv',index=False )<data_type_conversions>
print(big['TName'].value_counts())
 big['XWho'] = big['TName']
for i in [ 'Master.', 'Sir.', 'Don.', 'Lady.', 'Dona.', 'the Countess.', 'Mme.' ]:
big['XWho'][big['TName'] == i] = "High."
for i in [ 'Col.', 'Major.', 'Capt.' ]:
big['XWho'][big['TName'] == i] = "Mil."
for i in [ 'Mr.', 'Dr.', 'Rev.' ]:
big['XWho'][big['TName'] == i] = "Mr."
for i in [ 'Mrs.', 'Ms.', 'Mlle.', 'Miss.' ]:
big['XWho'][big['TName'] == i] = "Miss."
big['XWho'][~big['TName'].isin([ 'Sir.', 'Don.', 'Lady.', 'Dona.', 'the Countess.', 'Col.',
'Major.', 'Capt.', 'Mr.', 'Master.', 'Dr.', 'Rev.', 'Mrs.',
'Ms.', 'Mlle.', 'Mme.', 'Miss.' ])] = "Oth."
print(big['XWho'].value_counts() )
def get_images_and_labels(csv,label):
fileraw = pd.read_csv(csv)
 labels = fileraw[label].to_numpy()
data = fileraw.drop([label],axis=1 ).to_numpy(dtype=np.float32)
 data = np.true_divide(data,255.).reshape(( fileraw.shape[0],28,28))
data = np.expand_dims(data, axis=1)
 return data, labels<train_model>
for col in [ 'Sex', 'Pclass', 'XWho', 'Embarked', 'LTick', 'XCabin', 'TName' ]:
dummy = pd.get_dummies(big[col],prefix=str(col),prefix_sep="__")
 big = pd.concat([big, dummy], axis=1)
 big.drop(col, inplace=True, axis=1)
 for col in [ 'XFam' ]:
lbl = LabelEncoder()
lbl.fit(list(big[col].values))
big[col] = lbl.transform(list(big[col].values))
plt.title(f'Training Label: {train_labels[4]}')
 plt.imshow(train_data[4,0],cmap='gray');<define_variables>
CNULL = big.isnull().sum()
print(CNULL[CNULL != 0] )
np.random.seed(42)
 ran_10_pct_idx =(np.random.random_sample(train_labels.shape)) <.1
train_90_labels = train_labels[np.invert(ran_10_pct_idx)]
train_90_data = train_data[np.invert(ran_10_pct_idx)]
valid_10_labels = train_labels[ran_10_pct_idx]
valid_10_data = train_data[ran_10_pct_idx]<create_dataframe>
big['Fare'] = big['Fare'].fillna(big['Fare'].mean())
 big['Age'] = big['Age'].fillna(big['Age'].mean() )
train_ds = ArrayDataset(train_90_data,train_90_labels)
 valid_ds = ArrayDataset(valid_10_data,valid_10_labels)
 other_ds = ArrayDataset(other_data, other_labels)
 test_ds = ArrayDataset(test_data, test_labels )<train_model>
train['Age'] =(train['Age'] - train['Age'].mean())/ train['Age'].std()
test['Age'] =(test['Age'] - test['Age'].mean())/ test['Age'].std()
bs = 128
databunch = DataBunch.create(train_ds, valid_ds, test_ds=test_ds, bs=bs )<choose_model_class>
train['Fare'] = np.log1p(( train['Fare'] - train['Fare'].mean())/ train['Fare'].std())
 test['Fare'] = np.log1p(( test['Fare'] - test['Fare'].mean())/ test['Fare'].std() )
leak = 0.15
best_architecture = nn.Sequential(
 conv_layer(1,32,stride=1,ks=3,leaky=leak),
conv_layer(32,32,stride=1,ks=3,leaky=leak),
conv_layer(32,32,stride=2,ks=5,leaky=leak),
nn.Dropout(0.4),
conv_layer(32,64,stride=1,ks=3,leaky=leak),
conv_layer(64,64,stride=1,ks=3,leaky=leak),
conv_layer(64,64,stride=2,ks=5,leaky=leak),
nn.Dropout(0.4),
Flatten() ,
nn.Linear(3136, 128),
relu(inplace=True),
nn.BatchNorm1d(128),
nn.Dropout(0.4),
nn.Linear(128,10)
 )<choose_model_class>
train['c_mean'] = pd.Series(train.mean(axis=1), index=train.index)
 c_mean_max = train['c_mean'].max()
c_mean_min = train['c_mean'].min()
c_mean_scaled =(train.c_mean-c_mean_min)/ c_mean_max
train['c_mean_s'] = pd.Series(c_mean_scaled, index=train.index)
 del train['c_mean']
train['c_std'] = pd.Series(train.std(axis=1), index=train.index)
 c_std_max = train['c_std'].max()
c_std_min = train['c_std'].min()
c_std_scaled =(train.c_std-c_std_min)/ c_std_max
train['c_std_s'] = np.log1p(pd.Series(c_std_scaled, index=train.index))
del train['c_std']
test['c_mean'] = pd.Series(test.mean(axis=1), index=test.index)
 c_mean_max = test['c_mean'].max()
c_mean_min = test['c_mean'].min()
c_mean_scaled =(test.c_mean-c_mean_min)/ c_mean_max
test['c_mean_s'] = np.log1p(pd.Series(c_mean_scaled, index=test.index))
del test['c_mean']
test['c_std'] = pd.Series(test.std(axis=1), index=test.index)
 c_std_max = test['c_std'].max()
c_std_min = test['c_std'].min()
c_std_scaled =(test.c_std-c_std_min)/ c_std_max
test['c_std_s'] = pd.Series(c_std_scaled, index=test.index)
 del test['c_std']
print(train.shape, test.shape )
learn = Learner(databunch, best_architecture, loss_func = nn.CrossEntropyLoss() , metrics=[accuracy] )<train_model>
print(train.shape)
 train.drop_duplicates(inplace=True)
 print(train.shape )
learn.fit_one_cycle(20 )<predict_on_test>
print(" <*> Debut")
 kDate = time.strftime('%d%m%y_%H%M%S',time.localtime())
 start = time.time()
y_train = train['Survived'].values.astype(np.float64)
 x_train = train.drop(['PassengerId', 'Survived'], axis=1 ).values.astype(np.float64)
 x_test = test.drop(['PassengerId'], axis=1 ).values.astype(np.float64)
 print('Shape train: {}
Shape test: {}
Shape Y: {}'.format(x_train.shape, x_test.shape, y_train.shape))
NSplit = 5
SliceTrain = 0.75
SliceTest = 0.25
models = []
NIter = 0
TScore = 0
print("Entrainement")
 rs = StratifiedShuffleSplit(n_splits=NSplit, random_state=99, test_size=SliceTest)
 for train_index, test_index in rs.split(x_train, y_train):
X_train = x_train[train_index]
Y_train = y_train[train_index]
X_valid = x_train[test_index]
Y_valid = y_train[test_index]
rfc_params = {}
rfc_params['n_estimators'] = 200
rfc_params['learning_rate'] = 0.015
rfc_params['max_depth'] = 250
rfc_params['max_features'] = "auto"
rfc_params['min_samples_split'] = 0.7
rfc_params['min_samples_leaf'] = 0.01
rfc_params['random_state'] = 0
rfc_params['verbose'] = 0
sum_score = 0
score = 0
clf = GradientBoostingClassifier(**rfc_params)
 clf.fit(X_train, Y_train)
 models.append(clf)
 score = clf.score(X_valid, Y_valid)
 print(" <*> Entrainement ",NIter," avec ", SliceTrain, " pour train et ",SliceTest," pour test - Score : ", score)
 TScore += score
NIter += 1
TScore /= NSplit
print(" <*> ---------------- Resultats CV ------------------ ")
 print(" <*> params : ",rfc_params)
 print(" <*> Score Moyenne training : ", TScore)
 print("Verification avec le train")
 score = 0
SCLOG = 0
NIter = 0
for clf in models:
PTrain = clf.predict(x_train)
 score = clf.score(x_train, y_train)
 SCLOG += score
NIter += 1
SCLOG /= NSplit
print(" <*> Score Moyenne Train : ", SCLOG)
 print("Predictions")
 NIter = 0
ctb_pred1 = []
for clf in models:
PTest = clf.predict(x_test)
 ctb_pred1.append(PTest)
 NIter += 1
PTest = [0] * len(ctb_pred1[0])
 for i in range(NSplit):
PTest += ctb_pred1[i]
PTest /= NSplit
print(pd.DataFrame(PTest ).head())
 end = time.time()
print(" <*> Duree : ",end - start)
 print(" <*> Fin" )
preds, ids = learn.get_preds(DatasetType.Test)
 y = torch.argmax(preds, dim=1 )<save_to_csv>
print(" Mise a jour des colonnes submit")
 submit['Survived'] = np.clip(PTest, 0, 1 ).astype(int)
 localtime = time.localtime(time.time())
 WDate = str(localtime.tm_mday ).rjust(2, '0')+str(localtime.tm_mon ).rjust(2, '0')+str(localtime.tm_year)
 SUBFIC = SUBINT_DIR+"Titanic_GBR_"+str(kDate)+".csv"
print(" <*> Ecriture deb CSV/7z : ", SUBFIC)
 submit.to_csv(SUBFIC, index=False)
 print(" <*> Ecriture fin CSV/7z : ", SUBFIC )