Kaggle competition: Titanic - Machine Learning from Disaster
t_start = time.time()
warnings.filterwarnings('always')
 warnings.filterwarnings('ignore')
 %matplotlib inline
style.use('fivethirtyeight')
 sns.set(style='whitegrid', color_codes=True)
 !ls.. /input/*<import_modules>
warnings.filterwarnings('ignore')
 %matplotlib inline
fastai.__version__<set_options>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
def is_interactive() :
return 'runtime' in get_ipython().config.IPKernelApp.connection_file
print('Interactive?', is_interactive() )<set_options>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything(42 )<define_variables>
pd.isnull(train ).sum()
Path('/tmp/.cache/torch/checkpoints/' ).mkdir(exist_ok=True, parents=True)
 model_path = '/tmp/.cache/torch/checkpoints/efficientNet.pth'
!cp.. /input/efficientnet*/efficientNet_*.pth {model_path}<load_from_csv>
train["Cabin"] =(train["Cabin"].notnull().astype('int'))
test["Cabin"] =(test["Cabin"].notnull().astype('int'))
sns.barplot(x="Cabin", y="Survived", data=train )
PATH = Path('.. /input/aptos2019-blindness-detection')
 PATH_train = Path('.. /input/drd-newold/drd')
 df_train = pd.read_csv('.. /input/oldandnew/new_train_data.csv')
 df_test = pd.read_csv(PATH/'test.csv')
 _ = df_train.hist()<load_pretrained>
sex_map = {'male':0, 'female':1}
train['Sex'] = train['Sex'].map(sex_map)
 test['Sex'] = test['Sex'].map(sex_map )
aptos19_stats =([0.42, 0.22, 0.075], [0.27, 0.15, 0.081])
 data = ImageDataBunch.from_df(df=df_train,
path=PATH_train, folder='aptos_drd_jpeg', suffix='.jpeg',
valid_pct=0.1,
ds_tfms=get_transforms(flip_vert=True, max_warp=0.05, max_rotate=20.) ,
bs=4,
num_workers=os.cpu_count()
).normalize(aptos19_stats )<import_modules>
train.isnull().sum()
print(f'Classes:
{data.classes}' )<define_variables>
train.isnull().sum()
data.show_batch(rows=3, figsize=(7,6))<define_variables>
test["Fare"] = test["Fare"].fillna(test["Fare"].median())
 test.isnull().sum()
package_path = '.. /input/efficientnet-pytorch/efficientnet-pytorch/EfficientNet-PyTorch-master'
sys.path.append(package_path)
<load_pretrained>
label = train["Survived"]
PassengerId = test["PassengerId"]
train = train.drop(["Survived","PassengerId","Name","Ticket","Embarked","AgeGroup"], axis=1)
 test = test.drop(["PassengerId","Name","Ticket","Embarked","AgeGroup"], axis=1 )
def EfficientNetB4(pretrained=True):

 model = EfficientNet.from_name('efficientnet-b4', override_params={'num_classes': 5 })
 if pretrained:
model_state = torch.load(model_path)
 if '_fc.weight' in model_state.keys() :
model_state.pop('_fc.weight')
 model_state.pop('_fc.bias')
 res = model.load_state_dict(model_state, strict=False)
 assert str(res.missing_keys)== str(['_fc.weight', '_fc.bias']), 'issue loading pretrained weights'
else:
mapping = { i:o for i,o in zip(model_state.keys() , model.state_dict().keys())}
mapped_model_state = OrderedDict([ 
(mapping[k], v)for k,v in model_state.items() if not mapping[k].startswith('_fc')
 ])
 res = model.load_state_dict(mapped_model_state, strict=False)
 print(res)
 return model<choose_model_class>
gbc = GradientBoostingClassifier()
gbc.fit(train,label)
 gbc.score(train,label)
model = EfficientNetB4(pretrained=True)
<compute_train_metric>
pred = gbc.predict(test )
class FocalLoss(nn.Module):
def __init__(self, gamma=3., reduction='mean'):
super().__init__()
self.gamma = gamma
self.reduction = reduction
def forward(self, inputs, targets):
CE_loss = nn.CrossEntropyLoss(reduction='none' )(inputs, targets)
 pt = torch.exp(-CE_loss)
 F_loss =(( 1 - pt)**self.gamma)* CE_loss
if self.reduction == 'sum':
return F_loss.sum()
elif self.reduction == 'mean':
return F_loss.mean()<choose_model_class>
pred_df = pd.DataFrame(pred, columns=['Survived'])
 result = pd.concat([PassengerId,pred_df],axis =1)
<choose_model_class><EOS> 
result.to_csv('result.csv', index=False )