Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<train_model>
import numpy as np
import pandas as pd
import seaborn as seaborn
from sklearn import preprocessing
from sklearn import tree
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
def print_num_params(model, display_all_modules=False):
total_num_params = 0
for n, p in model.named_parameters() :
num_params = 1
for s in p.shape:
num_params *= s
if display_all_modules: print("{}: {}".format(n, num_params))
total_num_params += num_params
print("-" * 50)
 print("Total number of parameters: {:.2e}".format(total_num_params))
print_num_params(model )<import_modules>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 train.info()
test.info()
from torchvision.models.resnet import resnet18, resnet34, resnet50<find_best_params>
def dummies(col,train,test):
train_dum = pd.get_dummies(train[col])
 test_dum = pd.get_dummies(test[col])
 train = pd.concat([train, train_dum], axis=1)
 test = pd.concat([test,test_dum],axis=1)
 train.drop(col,axis=1,inplace=True)
 test.drop(col,axis=1,inplace=True)
 return train, test
dropping = ['PassengerId', 'Name', 'Ticket']
train.drop(dropping,axis=1, inplace=True)
 test.drop(dropping,axis=1, inplace=True)
print_num_params(resnet18(pretrained=False, num_classes=1000))
print_num_params(resnet34(pretrained=False, num_classes=1000))
print_num_params(resnet50(pretrained=False, num_classes=1000))<load_pretrained>
print(train.Pclass.value_counts())
 seaborn.factorplot("Pclass",'Survived',data=train,order=[1,2,3])
 train, test = dummies('Pclass',train,test )
model_state = torch.load("/kaggle/input/efficientnet-pytorch/efficientnet-b4-e116e8b3.pth")
 mapping = {
k: v for k, v in zip(model_state.keys() , model.state_dict().keys())
 }
mapped_model_state = OrderedDict([ 
(mapping[k], v)for k, v in model_state.items()
])
 model.load_state_dict(mapped_model_state, strict=False )<load_pretrained>
print(train.Sex.value_counts(dropna=False))
seaborn.factorplot('Sex','Survived',data=train)
 train,test = dummies('Sex',train,test)
 train.drop('male',axis=1,inplace=True)
 test.drop('male',axis=1,inplace=True)
with open("/kaggle/input/efficientnet-pytorch/efficientnet-pytorch/EfficientNet-PyTorch-master/examples/simple/labels_map.txt", "r")as h:
labels = json.load(h)
 img = Image.open("/kaggle/input/efficientnet-pytorch/efficientnet-pytorch/EfficientNet-PyTorch-master/examples/simple/img.jpg")
 tfms = transforms.Compose([transforms.Resize([resolution]*2),
transforms.ToTensor() ,
transforms.Normalize(*img_stats),])
 x = tfms(img ).unsqueeze(0)
 print(x.shape)
 _ = plt.imshow(img, shape=[resolution]*2 )<predict_on_test>
print(train.Cabin.isnull().sum())
 print(test.Cabin.isnull().sum())
 train.drop('Cabin',axis=1,inplace=True)
 test.drop('Cabin',axis=1,inplace=True )
model.eval()
with torch.no_grad() :
y_pred = model(x)
 print('-----')
 for idx in torch.topk(y_pred, k=5)[1].squeeze(0 ).tolist() :
prob = torch.softmax(y_pred, dim=1)[0, idx].item()
print('{label:<75}({p:.2f}%)'.format(label=labels[str(idx)], p=prob*100))<import_modules>
def modeling(clf,ft,target):
acc = cross_val_score(clf,ft,target,cv=kf)
 acc_lst.append(acc.mean())
 return
accuracy = []
def ml(ft,target,time):
accuracy.append(acc_lst)
 logreg = LogisticRegression()
modeling(logreg,ft,target)
 rf = RandomForestClassifier(n_estimators=50,min_samples_split=4,min_samples_leaf=2)
 modeling(rf,ft,target)
 svc1 = SVC(kernel="rbf")
 modeling(svc1,ft,target)
 svc2 = SVC(kernel="linear")
 modeling(svc2,ft,target)
 knn = KNeighborsClassifier(n_neighbors = 3)
 modeling(knn,ft,target)
 mlp = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(15,3),random_state=10)
 modeling(mlp, ft, target)
 logreg.fit(ft,target)
 feature = pd.DataFrame(ft.columns)
 feature.columns = ['Features']
feature["Coefficient Estimate"] = pd.Series(logreg.coef_[0])
 print(feature)
 return
from torchvision.transforms import *
from torch.utils.data import Subset
import torchvision.utils as vutils
import pandas as pd
from sklearn.utils import shuffle<load_from_csv>
train_ft = train.drop('Survived',axis=1)
 train_y = train['Survived']
kf = KFold(n_splits=3,random_state=1)
 acc_lst = []
ml(train_ft,train_y,'test_1')
df_train = pd.read_csv('.. /input/aptos2019-blindness-detection/train.csv')
 df_test = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 x = df_train['id_code']
y = df_train['diagnosis']
x, y = shuffle(x, y)
 _ = y.hist()
n_classes = int(y.max() +1)
 class_weights = len(y)/ df_train.groupby('diagnosis' ).count().values.ravel()
class_weights *= n_classes / class_weights.sum()
print('class_weights:', class_weights.tolist() )<split>
train_ft_2=train.drop(['Survived','young'],axis=1)
 test_2 = test.drop('young',axis=1)
 train_ft.head()
kf = KFold(n_splits=3,random_state=1)
 acc_lst=[]
ml(train_ft_2,train_y,'test_2')
train_x, valid_x, train_y, valid_y = train_test_split(x.values, y.values, test_size=0.10, stratify=y, random_state=42)
 test_x = df_test.id_code.values
if debug:
train_x, train_y = train_x[:128], train_y[:128]
valid_x, valid_y = valid_x[:64], valid_y[:64]
print(train_x.shape)
 print(train_y.shape)
 print(valid_x.shape)
 print(valid_y.shape)
 print(test_x.shape )<categorify>
train_ft_3=train.drop(['Survived','young','C'],axis=1)
 test_3 = test.drop(['young','C'],axis=1)
 train_ft.head()
kf = KFold(n_splits=3,random_state=1)
 acc_lst = []
ml(train_ft_3,train_y,'test_3')
class ImageDataset(torch.utils.data.Dataset):
def __init__(self, root, path_list, targets=None, transform=None, extension='.png'):
super().__init__()
self.root = root
self.path_list = path_list
self.targets = targets
self.transform = transform
self.extension = extension
if targets is not None:
assert len(self.path_list)== len(self.targets)
 self.targets = torch.LongTensor(targets)
 def __getitem__(self, index):

 path = self.path_list[index]
sample = Image.open(os.path.join(self.root, path+self.extension))
if self.transform is not None:
sample = self.transform(sample)
 if self.targets is not None:
return sample, self.targets[index]
else:
return sample, torch.LongTensor([])
 def __len__(self):
return len(self.path_list )<create_dataframe>
train_ft_4=train.drop(['Survived','Fare'],axis=1)
 test_4 = test.drop(['Fare'],axis=1)
 train_ft.head()
kf = KFold(n_splits=3,random_state=1)
 acc_lst = []
ml(train_ft_4,train_y,'test_4')
train_transform = Compose([
Resize([resolution]*2, BICUBIC),
ColorJitter(brightness=0.05, contrast=0.05, saturation=0.01, hue=0),
RandomAffine(degrees=15, translate=(0.01, 0.01), scale=(1.0, 1.25), fillcolor=(0,0,0), resample=BICUBIC),
RandomHorizontalFlip() ,
ToTensor() ,
Normalize(*img_stats)
 ])
 test_transform = Compose([
Resize([resolution]*2, BICUBIC),
ToTensor() ,
Normalize(*img_stats)
 ])
 train_dataset = ImageDataset(root='.. /input/aptos2019-blindness-detection/train_images',
path_list=train_x, targets=train_y, transform=train_transform)
 train_eval_dataset = ImageDataset(root='.. /input/aptos2019-blindness-detection/train_images',
path_list=valid_x, targets=valid_y, transform=test_transform)
 test_dataset = ImageDataset(root='.. /input/aptos2019-blindness-detection/test_images',
path_list=test_x, transform=test_transform)
 print(len(train_dataset), len(test_dataset), len(train_eval_dataset))<load_pretrained>
accuracy_df=pd.DataFrame(data=accuracy,
index=['test1','test2','test3','test4'],
columns=['logistic','rf','svc1','svc2','knn', 'mlp'])
 accuracy_df
train_batch_size = 32
eval_batch_size = 16
num_workers = os.cpu_count()
print('num_workers:', num_workers)
 train_loader = DataLoader(train_dataset, batch_size=train_batch_size, num_workers=num_workers,
shuffle=True, drop_last=True, pin_memory=True)
 test_loader = DataLoader(test_dataset, batch_size=eval_batch_size, num_workers=num_workers,
shuffle=False, drop_last=False, pin_memory=True)
 eval_train_loader = DataLoader(train_eval_dataset, batch_size=eval_batch_size, num_workers=num_workers,
shuffle=False, drop_last=False, pin_memory=True )<find_best_params>
svc = SVC()
svc.fit(train_ft_4,train_y)
 svc_pred = svc.predict(test_4)
 print(svc.score(train_ft_4,train_y))
submission_test = pd.read_csv(".. /input/test.csv")
 submission = pd.DataFrame({"PassengerId":submission_test['PassengerId'],
"Survived":svc_pred})
 submission.to_csv("kaggle_SVC.csv",index=False )