Kaggle competition: Titanic - Machine Learning from Disaster
img_cols=28
img_rows=28<prepare_x_and_y>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.preprocessing import StandardScaler,LabelEncoder
from sklearn.impute import KNNImputer
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier
if k.image_data_format=='channels_first':
train_x = train_x.values.reshape(train_x.shape[0], 1,img_cols,img_rows)
 test = test.values.reshape(test.shape[0], 1,img_cols,img_rows)
 train_x=train_x/255.0
test=test/255.0
input_shape =(1,img_cols,img_rows)
 else:
train_x=train_x.values.reshape(train_x.shape[0],img_cols,img_rows,1)
 test=test.values.reshape(test.shape[0],img_cols,img_rows,1)
 train_x=train_x/255.0
test=test/255.0
input_shape =(img_cols,img_rows,1 )<choose_model_class>
df_train = pd.read_csv(".. /input/titanic/train.csv")
 df_test = pd.read_csv(".. /input/titanic/test.csv")
 df_train.head()
earlystopping = EarlyStopping(monitor ="val_accuracy",
mode = 'auto', patience = 10,
restore_best_weights = True)
 modelacc = []
nfilters = [32, 64, 128,256]
conv_layers = [1, 2, 3,4]
dense_layers = [0, 1, 2,3]
dp=0.5
for filters in nfilters:
for conv_layer in conv_layers:
for dense_layer in dense_layers:
cnnsays = 'No of filters/feature maps: {} conv_layers: {} dense_layers: {} dropout: {}'.format(filters, conv_layer, dense_layer, dp)
 print(cnnsays)
 model = Sequential()
model.add(Conv2D(filters,(3, 3), input_shape = input_shape))
model.add(BatchNormalization())
 model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(dp))
for i in range(conv_layer-1):
model.add(Conv2D(filters,(3, 3),padding="same"))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(dp))
model.add(Flatten())
 for i in range(dense_layer):
model.add(Dense(256))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(dp))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'])
 EPOCHS = 30
history = model.fit(train_x, train_y, epochs=EPOCHS, batch_size=32, validation_split=0.2, callbacks=[earlystopping])
 modelacc.append([round(100*max(history.history['val_accuracy']), 2), cnnsays])
<compute_test_metric>
df_train = df_train.drop(["Name","PassengerId","Ticket","Cabin"],axis=1)
 df_test = df_test.drop(["Name","Ticket","Cabin"],axis=1)
 df_train.info()
print("Highest Validation Accuracy so far : {}%".format(round(100*max(history.history['val_accuracy']), 2)) )<sort_values>
df_test.isna().sum()
modelacc.sort(reverse=True)
 modelacc<predict_on_test>
df_train.isna().sum()
pred=model.predict([test])
 soln=[]
for i in range(len(pred)) :
soln.append(np.argmax(pred[i]))<save_to_csv>
df_train.Age.fillna(df_train.Age.mean() ,inplace = True)
 df_test.Age.fillna(df_test.Age.mean() ,inplace = True)
 df_test.Fare.fillna(df_test["Fare"].mean() ,inplace = True)
 df_train.dropna(axis=0,inplace = True)
 df_train.head()
final = pd.DataFrame()
final['ImageId']=[i+1 for i in test_x.index]
final['Label']=soln
final.to_csv('mnistcnn.csv', index=False )<set_options>
le = LabelEncoder()
df_train.Sex = le.fit_transform(df_train.Sex)
 df_test.Sex = le.fit_transform(df_test.Sex )
%matplotlib inline
if torch.cuda.is_available() :
torch.backends.cudnn.deterministic = True<load_from_csv>
embarked_column_train = pd.get_dummies(df_train["Embarked"])
 embarked_column_test = pd.get_dummies(df_test["Embarked"])
 df_test.drop(["Embarked"],axis=1,inplace = True)
 df_train.drop(["Embarked"],axis=1,inplace = True)
 df_train = pd.concat([df_train,embarked_column_train],axis=1)
 df_test= pd.concat([df_test,embarked_column_test],axis=1 )
train_df = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test_df = pd.read_csv(".. /input/digit-recognizer/test.csv" )<filter>
y = df_train.iloc[:,0]
X = df_train.iloc[:,1:]
train_df.iloc[:,1:]<create_dataframe>
X_train,X_valid,y_train,y_valid = train_test_split(X,y,test_size = 0.3,random_state = 42 )
class MNISTDataset(Dataset):

 def __init__(self, dataframe,
transform = transforms.Compose([transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,)) ])
):
df = dataframe
self.n_pixels = 784
if len(df.columns)== self.n_pixels:
self.X = df.values.reshape(( -1,28,28)).astype(np.uint8)[:,:,:,None]
self.y = None
else:
self.X = df.iloc[:,1:].values.reshape(( -1,28,28)).astype(np.uint8)[:,:,:,None]
self.y = torch.from_numpy(df.iloc[:,0].values)
 self.transform = transform
def __len__(self):
return len(self.X)
 def __getitem__(self, idx):
if self.y is not None:
return self.transform(self.X[idx]), self.y[idx]
else:
return self.transform(self.X[idx] )<import_modules>
name = []
metric = []
def trainModel(alg,X_train,y_train,X_valid,y_valid):
model = alg().fit(X_train,y_train)
 y_pred = model.predict(X_valid)
 name.append(alg.__name__)
 metric.append(accuracy_score(y_valid,y_pred))
print(alg.__name__ , " Accuracy ", accuracy_score(y_valid,y_pred))
class MNISTResNet(ResNet):
def __init__(self):
super().__init__(BasicBlock, [2, 2, 2, 2], num_classes=10)
 self.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=1, padding=3,bias=False)
 model = MNISTResNet()
print(model )<train_on_grid>
models = [RandomForestClassifier,GradientBoostingClassifier,DecisionTreeClassifier,SVC,XGBClassifier]
for i in models:
trainModel(i,X_train,y_train,X_valid,y_valid )
def train(train_loader, model, criterion, optimizer, epoch):
model.train()
loss_train = 0
for batch_idx,(data, target)in enumerate(train_loader):
if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
output = model(data)
 loss = criterion(output, target)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
if(batch_idx + 1)% 100 == 0:
print('Train Epoch: {} [{}/{}({:.0f}%)]\tLoss: {:.6f}'.format(
 epoch,(batch_idx + 1)* len(data), len(train_loader.dataset),
100.*(batch_idx + 1)/ len(train_loader), loss.data.item())) 
<find_best_params>
status = pd.DataFrame({"name" : name,
"training_metric" :metric
})
 status
def validate(val_loader, model, criterion):
model.eval()
loss = 0
correct = 0
for _,(data, target)in enumerate(val_loader):
if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
output = model(data)
 loss += criterion(output, target ).data.item()
pred = output.data.max(1, keepdim=True)[1]
correct += pred.eq(target.data.view_as(pred)).cpu().sum()
loss /= len(val_loader.dataset)
 print('
On Val set Average loss: {:.4f}, Accuracy: {}/{}({:.3f}%)
 '.format(
 loss, correct, len(val_loader.dataset),
100.0 * float(correct)/ len(val_loader.dataset)))
<categorify>
Gradient_boosting = GradientBoostingClassifier().fit(X,y )
train_transforms = transforms.Compose(
 [transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,)) ])
 val_test_transforms = transforms.Compose(
 [transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,)) ] )<choose_model_class>
sub = pd.DataFrame({"PassengerId":df_test.PassengerId, "Survived":Gradient_boosting.predict(df_test.drop("PassengerId", axis = 1)) })
 sub.to_csv("submission.csv", index = None )