Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<choose_model_class>
warnings.filterwarnings('ignore')
 warnings.filterwarnings('ignore', category=DeprecationWarning)
 %matplotlib inline
mpl.style.use('ggplot')
 sns.set_style('white')
 params = {
'axes.labelsize': "large",
'xtick.labelsize': 'x-large',
'legend.fontsize': 20,
'figure.dpi': 150,
'figure.figsize': [25, 7]
}
plt.rcParams.update(params )
learn = cnn_learner(train_img, models.densenet161, metrics=[error_rate, accuracy] )<train_model>
HTML() ;
lr = 3e-02
learn.fit_one_cycle(5, slice(lr))<predict_on_test>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 test_df = test.copy()
abc,_ = learn.get_preds(ds_type=DatasetType.Test)
<set_options>
test_df.has_cactus = abc.numpy() [:, 0]<save_to_csv>
train.Survived.value_counts()
test_df.to_csv('submission.csv', index=False )<import_modules>
train.Survived.value_counts(normalize=True )
from pathlib import Path
from fastai import *
from fastai.vision import *
import torch<define_variables>
train[['Pclass' , 'Survived']].groupby('Pclass' ).mean()
data_folder = Path(".. /input" )<load_from_csv>
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/sample_submission.csv" )<define_variables>
print(train.Age.count())
 train['Age'].fillna(train.Age.median() , inplace=True )
train_img.show_batch(rows=3, figsize=(7,6))<choose_model_class>
print(train.Age.count() )
learn = cnn_learner(train_img, models.densenet161, metrics=[error_rate, accuracy] )<train_model>
lr = 3e-02
learn.fit_one_cycle(5, slice(lr))<save_to_csv>
print(train.Embarked.count())
 train['Embarked'].fillna(train['Embarked'].mode() [0] ,inplace=True )
preds,_ = learn.get_preds(ds_type=DatasetType.Test)
 test_df.has_cactus = preds.numpy() [:, 0]
test_df.to_csv('submission.csv', index=False )<install_modules>
train.Embarked.count()
!pip install albumentations > /dev/null 2>&1
!pip install pretrainedmodels > /dev/null 2>&1
!pip install catalyst > /dev/null 2>&1<set_options>
train[['SibSp' , 'Survived']].groupby('SibSp' ).mean()
%matplotlib inline
train_on_gpu = True 
<import_modules>
train[['Parch','Survived']].groupby('Parch' ).mean()
from catalyst.dl.utils import UtilsFactory
from catalyst.dl.experiments import SupervisedRunner
from catalyst.dl.callbacks import EarlyStoppingCallback, OneCycleLR, InferCallback<categorify>
train['Alone']=0
train.loc[(train['SibSp']==0)&(train['Parch']==0), 'Alone'] = 1
test['Alone']=0
test.loc[(test['SibSp']==0)&(test['Parch']==0), 'Alone'] = 1
data_transforms = albumentations.Compose([
albumentations.HorizontalFlip() ,
albumentations.VerticalFlip() ,
albumentations.RandomBrightness() ,
albumentations.Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225], 
),
ToTensor()
])
 data_transforms_test = albumentations.Compose([
albumentations.Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225], 
),
ToTensor()
] )<load_from_csv>
drop_features = ['PassengerId' , 'Name' , 'SibSp' , 'Parch' , 'Ticket' , 'Cabin']
train.drop(drop_features , axis=1, inplace = True)
 test.drop(drop_features , axis=1 , inplace = True )
train_df = pd.read_csv('.. /input/train.csv')
 train, valid = train_test_split(train_df.has_cactus, stratify=train_df.has_cactus, test_size=0.1)
 img_class_dict = {k:v for k, v in zip(train_df.id, train_df.has_cactus)}<categorify>
def map_all(frame):
frame['Sex'] = frame.Sex.map({'female': 0 , 'male': 1} ).astype(int)
 frame['Embarked'] = frame.Embarked.map({'S' : 0 , 'C': 1 , 'Q':2} ).astype(int)
 frame.loc[frame.Age <= 16 , 'Age'] = 0
frame.loc[(frame.Age >16)&(frame.Age<=32), 'Age'] = 1
frame.loc[(frame.Age >32)&(frame.Age<=48), 'Age'] = 2
frame.loc[(frame.Age >48)&(frame.Age<=64), 'Age'] = 3
frame.loc[(frame.Age >64)&(frame.Age<=80), 'Age'] = 4
frame.loc[(frame.Fare <= 7.91), 'Fare'] = 0
frame.loc[(frame.Fare > 7.91)&(frame.Fare <= 14.454), 'Fare'] = 1
frame.loc[(frame.Fare > 14.454)&(frame.Fare <= 31), 'Fare'] = 2
frame.loc[(frame.Fare > 31), 'Fare'] = 3
class CactusDataset(Dataset):
def __init__(self, datafolder, datatype='train', transform = transforms.Compose([transforms.CenterCrop(32),transforms.ToTensor() ]), labels_dict={}):
self.datafolder = datafolder
self.datatype = datatype
self.image_files_list = [s for s in os.listdir(datafolder)]
self.transform = transform
self.labels_dict = labels_dict
if self.datatype == 'train':
self.labels = [np.float32(labels_dict[i])for i in self.image_files_list]
else:
self.labels = [np.float32(0.0)for _ in range(len(self.image_files_list)) ]
def __len__(self):
return len(self.image_files_list)
 def __getitem__(self, idx):
img_name = os.path.join(self.datafolder, self.image_files_list[idx])
 img = cv2.imread(img_name)[:,:,::-1]
image = self.transform(image=img)
 image = image['image']
label = self.labels[idx]
return image, label<create_dataframe>
map_all(train)
 train.head()
dataset = CactusDataset(datafolder='.. /input/train/train', datatype='train', transform=data_transforms, labels_dict=img_class_dict)
 test_set = CactusDataset(datafolder='.. /input/test/test', datatype='test', transform=data_transforms_test )<load_pretrained>
map_all(test)
 test.head()
loaders = collections.OrderedDict()
train_sampler = SubsetRandomSampler(list(train.index))
valid_sampler = SubsetRandomSampler(list(valid.index))
batch_size = 512
num_workers = 0
train_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, sampler=train_sampler, num_workers=num_workers)
 valid_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, sampler=valid_sampler, num_workers=num_workers)
 test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, num_workers=num_workers)
 loaders["train"] = train_loader
loaders["valid"] = valid_loader
loaders["test"] = test_loader<init_hyperparams>
x_train,x_test,y_train,y_test=train_test_split(train.drop('Survived',axis=1),train.Survived,test_size=0.20,random_state=66 )
class Flatten(nn.Module):
def forward(self, input):
return input.view(input.size(0), -1)
 class Net(nn.Module):
def __init__(
 self,
num_classes: int,
p: float = 0.2,
pooling_size: int = 2,
last_conv_size: int = 1664,
arch: str = "densenet169",
pretrained: str = "imagenet")-> None:

 super().__init__()
net = pretrainedmodels.__dict__[arch](pretrained=pretrained)
 modules = list(net.children())[:-1]
modules += [nn.Sequential(
 Flatten() ,
nn.BatchNorm1d(1664),
nn.Dropout(p),
nn.Linear(1664, num_classes)
)]
self.net = nn.Sequential(*modules)
 def forward(self, x):
logits = self.net(x)
 return torch.squeeze(logits )<choose_model_class>
models = [LogisticRegression() ,LinearSVC() ,SVC(kernel='rbf'),KNeighborsClassifier() ,RandomForestClassifier() ,
DecisionTreeClassifier() ,GradientBoostingClassifier() ,GaussianNB() , LinearDiscriminantAnalysis() ,
QuadraticDiscriminantAnalysis() ]
model_names=['LogisticRegression','LinearSVM','rbfSVM','KNearestNeighbors','RandomForestClassifier','DecisionTree',
'GradientBoostingClassifier','GaussianNB', 'LinearDiscriminantAnalysis','QuadraticDiscriminantAnalysis']
accuracy = []
for model in range(len(models)) :
clf = models[model]
clf.fit(x_train,y_train)
 pred = clf.predict(x_test)
 accuracy.append(accuracy_score(pred , y_test))
compare = pd.DataFrame({'Algorithm' : model_names , 'Accuracy' : accuracy})
 compare
num_epochs = 10
logdir = "./logs/simple"
model = Net(num_classes=1)
 criterion = nn.BCEWithLogitsLoss()
optimizer = torch.optim.SGD(model.parameters() , momentum=0.99, lr=1e-2)
 scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor=0.1, patience=2 )<train_model>
params_dict={'criterion':['gini','entropy'],'max_depth':[5.21,5.22,5.23,5.24,5.25,5.26,5.27,5.28,5.29,5.3]}
clf_dt=GridSearchCV(estimator=DecisionTreeClassifier() ,param_grid=params_dict,scoring='accuracy', cv=5)
 clf_dt.fit(x_train,y_train)
 pred=clf_dt.predict(x_test)
 print(accuracy_score(pred,y_test))
print(clf_dt.best_params_ )
runner = SupervisedRunner()
runner.train(
 model=model,
criterion=criterion,
optimizer=optimizer,
scheduler=scheduler,
loaders=loaders,
logdir=logdir,
callbacks=[
OneCycleLR(
 cycle_len=num_epochs,
div_factor=3,
increase_fraction=0.3,
momentum_range=(0.95, 0.85))
],
num_epochs=num_epochs,
verbose=False
)<choose_model_class>
no_of_test=[i+1 for i in range(50)]
params_dict={'n_neighbors':no_of_test}
clf_knn=GridSearchCV(estimator=KNeighborsClassifier() ,param_grid=params_dict,scoring='accuracy')
 clf_knn.fit(x_train,y_train)
 pred=clf_knn.predict(x_test)
 print(accuracy_score(pred,y_test))
print(clf_knn.best_params_ )
<predict_on_test><EOS> 
pred = clf_dt.predict(test)
 d = {'PassengerId' : test_df.PassengerId , 'Survived' : pred}
answer = pd.DataFrame(d)
 answer.to_csv('Prediction.csv' , index=False )