Kaggle competition: Titanic - Machine Learning from Disaster
<categorify>
train=pd.read_csv(".. /input/titanic/train.csv")
 test=pd.read_csv(".. /input/titanic/test.csv" )
encoder = preprocessing.LabelEncoder()
encoder.fit(np.load(".. /input/tmpfory/trainY.npy"))<load_pretrained>
train.isnull().sum()
T = np.load(".. /input/inputtestdata40x40/testT.npy" )<choose_model_class>
test.isnull().sum()
model = Sequential()
model.add(Conv2D(64, kernel_size = 3, activation='relu', input_shape =(img_rows, img_cols, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(128, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(128, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(128, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(256, kernel_size = 4, activation='relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dropout(0.4))
model.add(Dense(1000, activation='softmax'))
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
 print(model.summary() )<create_dataframe>
train.drop(columns=['PassengerId','Name','Ticket','Cabin'],inplace=True)
 test.drop(columns=['PassengerId','Name','Ticket','Cabin'],inplace=True)
datagen = ImageDataGenerator(
 rotation_range=8,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1 )<choose_model_class>
train.drop(columns=['SibSp','Parch'],inplace=True)
 test.drop(columns=['SibSp','Parch'],inplace=True)
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x )<categorify>
a=train.groupby('Pclass' ).median() ['Age'].iloc[0]
b=train.groupby('Pclass' ).median() ['Age'].iloc[1]
c=train.groupby('Pclass' ).median() ['Age'].iloc[2]
def fillAge_train(x):
age=x[0]
pclass=x[1]
if pd.isnull(age):
if pclass==1:
return a
elif pclass==2:
return b
else:
return c
else:
return age
a_test=test.groupby('Pclass' ).median() ['Age'].iloc[0]
b_test=test.groupby('Pclass' ).median() ['Age'].iloc[1]
c_test=test.groupby('Pclass' ).median() ['Age'].iloc[2]
def fillAge_test(x):
age=x[0]
pclass=x[1]
if pd.isnull(age):
if pclass==1:
return a_test
elif pclass==2:
return b_test
else:
return c_test
else:
return age
train['Age']=train[['Age','Pclass']].apply(fillAge_train,axis=1)
 test['Age']=test[['Age','Pclass']].apply(fillAge_test,axis=1)
X_train2 = np.load(".. /input/rescalinginputdata/X_train2.npy")
 X_val2 = np.load(".. /input/rescalinginputdata/X_val2.npy")
 Y_train2 = np_utils.to_categorical(encoder.transform(np.load(".. /input/rescalinginputdata/Y_train2.npy")))
 Y_val2 = np_utils.to_categorical(encoder.transform(np.load(".. /input/rescalinginputdata/Y_val2.npy")) )<train_model>
a=test.groupby('Pclass' ).median() ['Fare'].iloc[0]
b=test.groupby('Pclass' ).median() ['Fare'].iloc[1]
c=test.groupby('Pclass' ).median() ['Fare'].iloc[2]
def fillFare(x):
fare=x[0]
pclass=x[1]
if pd.isnull(fare):
if pclass==1:
return a
elif pclass==2:
return b
else:
return c
else:
return fare
test['Fare']=test[['Fare','Pclass']].apply(fillFare,axis=1 )
history = model.fit_generator(datagen.flow(X_train2,Y_train2, batch_size=64),
epochs = 18, steps_per_epoch = X_train2.shape[0]//64,
validation_data =(X_val2,Y_val2), callbacks=[annealer], verbose=0, initial_epoch=0 )<save_to_csv>
train['Embarked'].fillna(train['Embarked'].mode() [0], inplace=True )
history = history.history
file = open('info.csv', 'w')
 file.write(str(history))
file.close()<predict_on_test>
train=pd.get_dummies(train,columns=['Sex','Embarked','TravelAlone'],drop_first=True)
 test=pd.get_dummies(test,columns=['Sex','Embarked','TravelAlone'],drop_first=True)
predictions = model.predict(T)
 predictions = np.argmax(predictions, axis=1)
 res = encoder.inverse_transform(predictions )<save_to_csv>
train['AgeCateg'] = pd.cut(train['Age'], 5)
 train[['AgeCateg', 'Survived']].groupby(['AgeCateg'], as_index=False ).mean().sort_values(by='AgeCateg', ascending=True)
 train['FareCateg'] = pd.qcut(train['Fare'], 4)
 train[['FareCateg', 'Survived']].groupby(['FareCateg'], as_index=False ).mean().sort_values(by='FareCateg', ascending=True)
 def encodeAgeFare(train):
train.loc[train['Age'] <= 16, 'Age'] = 0
train.loc[(train['Age'] > 16)&(train['Age'] <= 32), 'Age'] = 1
train.loc[(train['Age'] > 32)&(train['Age'] <= 48), 'Age'] = 2
train.loc[(train['Age'] > 48)&(train['Age'] <= 64), 'Age'] = 3
train.loc[(train['Age'] > 48)&(train['Age'] <= 80), 'Age'] = 4
train.loc[train['Fare'] <= 7.91, 'Fare'] = 0
train.loc[(train['Fare'] > 7.91)&(train['Fare'] <= 14.454), 'Fare'] = 1
train.loc[(train['Fare'] > 14.454)&(train['Fare'] <= 31.0), 'Fare'] = 2
train.loc[(train['Fare'] > 31.0)&(train['Fare'] <= 512.329), 'Fare'] = 3
encodeAgeFare(train)
 encodeAgeFare(test)
 train.drop(columns=['AgeCateg','FareCateg'],inplace=True)
file = open('ansEpochs.csv', 'w')
 file.write('Id,Category
')
 for i in range(res.size):
file.write('' + str(i + 1)+ ',' + str(res[i])+ '
')
 file.close()<categorify>
X = train.drop('Survived',axis=1)
 y = train['Survived']
def Education_to_numeric(bp):
if bp == 'Bachelors ':
return 4
if bp == 'Partial College':
return 3
if bp == 'High School':
return 2
if bp == 'Graduate Degree':
return 1
if bp == 'Partial High School':
return 0
edu=train_data.Education
edut=test_data.Education
edupp = edu.apply(Education_to_numeric)
 edud = edut.apply(Education_to_numeric)
 train_data.Education=edupp
test_data.Education=edud
def occupation_to_numeric(b):
if b == 'Clerical':
return 1
if b == 'Professional':
return 1
if b == 'Manual':
return 0
if b == 'Skilled Manual':
return 0
if b == 'Management':
return 1
p=train_data.Occupation
pt=test_data.Occupation
d = p.apply(occupation_to_numeric)
 dt = pt.apply(occupation_to_numeric)
 train_data.Occupation=d
test_data.Occupation=dt
def COUNTRY_to_numeric(d):
if d == 'Canada':
return 3
if d == 'France':
return 4
if d == 'Australia':
return 1
if d == 'United Kingdom':
return 2
if d == 'United States':
return 5
if d == 'Germany':
return 6
pon=train_data.CountryRegionName
ptu=test_data.CountryRegionName
qa = pon.apply(COUNTRY_to_numeric)
 qaw= ptu.apply(COUNTRY_to_numeric)
 train_data.CountryRegionName=qa
test_data.CountryRegionName=qaw
def marital_to_numeric(ip):
if ip == 'M':
return 0
if ip == 'S':
return 1
pl= train_data.MaritalStatus
plt=test_data.MaritalStatus
ml = pl.apply(marital_to_numeric)
 mlt = plt.apply(marital_to_numeric)
 train_data.MaritalStatus=ml
test_data.MaritalStatus=mlt
def region_to_numeric(a):
if a == 'M':
return 2
if a == 'F':
return 1
z=train_data.Gender
zt=test_data.Gender
c = z.apply(region_to_numeric)
 ct = zt.apply(region_to_numeric)
 train_data.Gender=c
test_data.Gender=ct
yy=train_data.BirthDate
now=pd.Timestamp(DT.datetime.now())
 nuu = pd.to_datetime(yy)
 nuu=(now-nuu)/365
ny=nuu.dt.days
train_data.BirthDate=ny
yyt=test_data.BirthDate
nowt=pd.Timestamp(DT.datetime.now())
 nuut = pd.to_datetime(yyt)
 nuut=(nowt-nuut)/365
nyt=nuut.dt.days
test_data.BirthDate=nyt
train_data.PostalCode=train_data.TotalChildren.subtract(train_data.NumberChildrenAtHome, fill_value=0)
 test_data.PostalCode=test_data.TotalChildren.subtract(test_data.NumberChildrenAtHome, fill_value=0)
 gh=train_data.YearlyIncome-train_data.TotalChildren
def age_to_numeric(qa):
if qa <=37:
return 3
if 51>qa >=44:
return 4
if 58>qa >=51:
return 1
if 65>qa >=58:
return 2
if qa >=65:
return 5
train_data.BirthDate=100/train_data.BirthDate**3.9
test_data.BirthDate=100/test_data.BirthDate**3.9
def ave_to_numeric(qa):
if qa <40:
return 1
if 50>qa >=40:
return 2
if 59>qa >=50:
return 3
if 65>qa >=59:
return 4
if qa >=65:
return 5
train_data.AveMonthSpend=(np.log(train_data.AveMonthSpend)) *12
test_data.AveMonthSpend=(np.log(test_data.AveMonthSpend)) *12
train_data.Suffix=(train_data.YearlyIncome**3.3)-(train_data.AveMonthSpend**3)
 test_data.Suffix=(test_data.YearlyIncome**3.3)-(test_data.AveMonthSpend**3)
 train_data.YearlyIncome=(train_data.YearlyIncome**3.3)
 test_data.YearlyIncome=(test_data.YearlyIncome**3.3)
 train_data.TotalChildren=(train_data.TotalChildren*2)
 test_data.TotalChildren=(test_data.TotalChildren*2)
 train_data.City=train_data.AveMonthSpend*30
test_data.City=test_data.AveMonthSpend*30
train_data.PostalCode=train_data.TotalChildren.subtract(train_data.NumberChildrenAtHome, fill_value=0)
 test_data.PostalCode=test_data.TotalChildren.subtract(test_data.NumberChildrenAtHome, fill_value=0)
 train_data.PostalCode=1/train_data.CustomerID**1/2
test_data.PostalCode=1/test_data.CustomerID**1/2
y = train_data.BikeBuyer
features = ['TotalChildren','YearlyIncome',
'HomeOwnerFlag','NumberChildrenAtHome','Gender','MaritalStatus','BirthDate','CountryRegionName',
'Occupation','PostalCode','Suffix','Education','PostalCode'
]
train_data.AveMonthSpend.plot.hist(color='blue',bins=50)
 plt.show()
X= train_data[features]
textX=test_data[features]
corr= train_data.corr()
f, ax = plt.subplots(figsize=(10, 5))
sns.heatmap(corr,cmap='coolwarm',linewidths=2.0, annot=True)
 my_imputer = SimpleImputer()
X = my_imputer.fit_transform(X)
 textX = my_imputer.fit_transform(textX)
<train_model>
np.random.seed(0)
 X_train,X_test,y_train,y_test = train_test_split(X,y, test_size=0.3 )
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)
 xgb = XGBClassifier(n_estimators=380, learning_rate=0.049, random_state=1,min_child_weight=6)
 training_start = time.perf_counter()
xgb.fit(train_X, train_y)
 training_end = time.perf_counter()
prediction_start = time.perf_counter()
preds = xgb.predict(val_X)
 prediction_end = time.perf_counter()
acc_xgb =(preds == val_y ).sum().astype(float)/ len(preds)*100
xgb_train_time = training_end-training_start
xgb_prediction_time = prediction_end-prediction_start
print("XGBoost's prediction accuracy is: %3.2f" %(acc_xgb))
print("Time consumed for training: %4.3f" %(xgb_train_time))
print("Time consumed for prediction: %6.5f seconds" %(xgb_prediction_time))
pre=xgb.predict(textX)
 final=pre.astype(int)
 output = pd.DataFrame({'CustomerID': test_data.CustomerID,
'BikeBuyer': final})
 output.to_csv('Sample Submission file clf.csv', index=False )<install_modules>
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
from catboost import CatBoostClassifier
from sklearn.metrics import accuracy_score,confusion_matrix
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import RandomizedSearchCV
!pip install --upgrade efficientnet tensorflow_addons tensorflow<import_modules>
xgbclassifier=XGBClassifier()
xgbclassifier.fit(X_train,y_train)
 pred_xgb=xgbclassifier.predict(X_test)
import math
import re
import random
import os
import tensorflow as tf
import tensorflow_addons as tfa
import numpy as np
import tensorflow.keras.backend as K
import efficientnet.tfkeras as efn
import efficientnet
import itertools
import matplotlib
import scipy
import pandas as pd
import sklearn
from matplotlib import pyplot as plt
from datetime import datetime<import_modules>
randomfc = RandomForestClassifier(n_estimators=100)
 randomfc.fit(X_train,y_train)
 pred_rf=randomfc.predict(X_test)
print(f'Numpy version : {np.__version__}')
 print(f'Tensorflow version : {tf.__version__}')
 print(f'Tensorflow Addons version : {tfa.__version__}')
 print(f'EfficientNet(library)version : {efficientnet.__version__}')
 print(f'Matplotlib version : {matplotlib.__version__}')
 print(f'Scipy version : {scipy.__version__}')
 print(f'Pandas version : {pd.__version__}')
 print(f'Scikit-Learn version : {sklearn.__version__}' )<install_modules>
lightgb=LGBMClassifier()
lightgb.fit(X_train,y_train)
 pred_lgb=lightgb.predict(X_test)
!pip freeze > requirements.txt<feature_engineering>
ada=AdaBoostClassifier(n_estimators=50,learning_rate=1)
 ada.fit(X_train,y_train)
 pred_ada=ada.predict(X_test)
PRE_TRAINING_TIME_START = datetime.now()
AUTO = tf.data.experimental.AUTOTUNE<set_options>
cbc=CatBoostClassifier(verbose=0, n_estimators=100)
 cbc.fit(X_train,y_train)
 pred_cbc=cbc.predict(X_test)
SEED = 42
os.environ['PYTHONHASHSEED']=str(SEED)
 random.seed(SEED)
 np.random.seed(SEED)
 os.environ['TF_DETERMINISTIC_OPS']=str(SEED)
 tf.random.set_seed(SEED)
<set_options>
log=LogisticRegression(max_iter=1000)
 log.fit(X_train,y_train)
 pred_log=log.predict(X_test)
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )<define_variables>
print('XGBoost:', round(xgbclassifier.score(X_train, y_train)* 100, 2), '%.\t\t\t RandomForest:', round(randomfc.score(X_train, y_train)* 100, 2), '%.')
 print('LightGBM:', round(lightgb.score(X_train, y_train)* 100, 2), '%.\t\t\t AdaBoost:', round(ada.score(X_train, y_train)* 100, 2), '%.')
 print('CatBoost:', round(cbc.score(X_train, y_train)* 100, 2), '%.\t\t\t LogisticRegression:', round(log.score(X_train, y_train)* 100, 2), '%.' )
IMAGE_SIZE =(512, 512)
 GCS_TRAIN_PATHS = [
KaggleDatasets().get_gcs_path('tfrecords'),
KaggleDatasets().get_gcs_path('tfrecords-2')
 ]
TRAINING_FILENAMES = []
for i in GCS_TRAIN_PATHS:
TRAINING_FILENAMES.append(tf.io.gfile.glob(i + '/*.tfrecords'))
TRAINING_FILENAMES = list(itertools.chain.from_iterable(TRAINING_FILENAMES))
GCS_TEST_PATH = KaggleDatasets().get_gcs_path('tfrecords-3')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_TEST_PATH + '/*.tfrecords')
 print(len(TRAINING_FILENAMES))
print(len(TEST_FILENAMES))<define_variables>
print('XGBoost:', round(accuracy_score(y_test,pred_xgb)* 100, 2), '%.\t\t\t RandomForest:', round(accuracy_score(y_test,pred_rf)* 100, 2), '%.')
 print('LightGBM:', round(accuracy_score(y_test,pred_lgb)* 100, 2), '%.\t\t\t AdaBoost:', round(accuracy_score(y_test,pred_ada)* 100, 2), '%.')
 print('CatBoost:', round(accuracy_score(y_test,pred_cbc)* 100, 2), '%.\t\t\t LogisticRegression:', round(accuracy_score(y_test,pred_log)* 100, 2), '%.' )
EPOCHS = 12
DO_AUG = True
BATCH_SIZE = 256
current_epoch = 0
chance = 0
NUM_TRAINING_IMAGES = 105390
NUM_TEST_IMAGES = 12186
STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE<define_variables>
X_train=train.drop('Survived',axis=1)
 X_test=test.copy()
y_train = train['Survived']
model_params = {
'n_estimators': randint(4,200),
'max_features': truncnorm(a=0, b=1, loc=0.25, scale=0.1),
'min_samples_split': uniform(0.01, 0.199)
 }
rf_model = RandomForestClassifier()
clf = RandomizedSearchCV(rf_model, model_params, n_iter=100, cv=5, random_state=1)
 model = clf.fit(X_train, y_train)
 pprint(model.best_estimator_.get_params() )
CLASSES = [str(c ).zfill(2)for c in range(0, 42)]<categorify>
testData=test.copy()
predictions_rf=model.predict(testData )
def decode_image(image_data):
image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"words": tf.io.FixedLenFeature([6633], tf.float32),
"label": tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 words = example['words']
label = tf.cast(example['label'], tf.int32)
 return(( image, words), label)
 def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"words": tf.io.FixedLenFeature([6633], tf.float32),
"filename": tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 words = example['words']
filename = example['filename']
return(( image, words), filename)
 def load_dataset(filenames, labeled=True, ordered=False):
ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 return dataset
def get_training_dataset(do_aug=True):
dataset = load_dataset(TRAINING_FILENAMES, labeled=True)
 if do_aug:
dataset = dataset.map(image_augmentation, num_parallel_calls=AUTO)
 dataset = dataset.repeat()
dataset = dataset.shuffle(2048)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def get_test_dataset(ordered=False, tta=None):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 if tta == 0:
dataset = dataset.map(image_augmentation_tta_0, num_parallel_calls=AUTO)
 elif tta == 1:
dataset = dataset.map(image_augmentation_tta_1, num_parallel_calls=AUTO)
 elif tta == 2:
dataset = dataset.map(image_augmentation_tta_2, num_parallel_calls=AUTO)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset<normalization>
subm=pd.read_csv('.. /input/titanic/gender_submission.csv')
 Survived=pd.Series(predictions_rf)
 subm.drop(columns=['Survived'],inplace=True)
 subm['Survived']=pd.Series(predictions_rf)
 subm.to_csv(r'RandomForestSubmission.csv', index = False)
 print("Submitted" )