Kaggle competition: Human Protein Atlas Image Classification
<SOS> metric: MacroFScore Kaggle data source: human-protein-atlas-image-classification<split>
from fastai.conv_learner import *
from fastai.dataset import *
import pandas as pd
import numpy as np
import os
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
X_test=df_all.iloc[ntrain:]
X_train_full=df_all.iloc[:ntrain]
X_train, X_valid, y_train, y_valid = train_test_split(pd.get_dummies(X_train_full), y_train_full )<train_on_grid>
PATH = './'
TRAIN = '.. /input/human-protein-atlas-image-classification/train/'
TEST = '.. /input/human-protein-atlas-image-classification/test/'
LABELS = '.. /input/human-protein-atlas-image-classification/train.csv'
SAMPLE = '.. /input/human-protein-atlas-image-classification/sample_submission.csv'
print('ElasticNet')
 def elastic_score(X,y):
elastic = ElasticNet(random_state=1)
 param = {'l1_ratio' : [0],
'alpha' : [0.017]}
elastic = GridSearchCV(elastic, param, cv=5, scoring='neg_mean_squared_error')
 elastic.fit(X,y)
 print('ElasticNet:', np.sqrt(elastic.best_score_*-1))
return elastic
elastic_score(X_train, y_train)
 print('XGBoost')
 m_xgb = XGBRegressor(n_estimators=160, learning_rate=0.05)
 m_xgb.fit(X_train, y_train)
 print_score(m_xgb,X_train, X_valid, y_train, y_valid)
 print('Random Forest')
 m_rf = RandomForestRegressor(n_estimators=160, min_samples_leaf=1, max_features=0.5, n_jobs=-1, oob_score=True)
 m_rf.fit(X_train, y_train)
 print_score(m_rf,X_train, X_valid, y_train, y_valid)
<train_on_grid>
name_label_dict = {
0: 'Nucleoplasm',
1: 'Nuclear membrane',
2: 'Nucleoli',
3: 'Nucleoli fibrillar center',
4: 'Nuclear speckles',
5: 'Nuclear bodies',
6: 'Endoplasmic reticulum',
7: 'Golgi apparatus',
8: 'Peroxisomes',
9: 'Endosomes',
10: 'Lysosomes',
11: 'Intermediate filaments',
12: 'Actin filaments',
13: 'Focal adhesion sites',
14: 'Microtubules',
15: 'Microtubule ends',
16: 'Cytokinetic bridge',
17: 'Mitotic spindle',
18: 'Microtubule organizing center',
19: 'Centrosome',
20: 'Lipid droplets',
21: 'Plasma membrane',
22: 'Cell junctions',
23: 'Mitochondria',
24: 'Aggresome',
25: 'Cytosol',
26: 'Cytoplasmic bodies',
27: 'Rods & rings' }
def cv_train() :
elastic = ElasticNet(random_state=1)
 param = {'l1_ratio' : [0],
'alpha' : [0.017]}
elastic = GridSearchCV(elastic, param, cv=5, scoring='neg_mean_squared_error')
 elastic.fit(X_train_full, y_train_full)
 print('Elastic:', np.sqrt(elastic.best_score_*-1))
return elastic
elastic = cv_train()<predict_on_test>
nw = 2
arch = resnet34
y_pred=np.expm1(elastic.predict(X_test)) ; y_pred<save_to_csv>
train_names = list({f[:36] for f in os.listdir(TRAIN)})
 test_names = list({f[:36] for f in os.listdir(TEST)})
 tr_n, val_n = train_test_split(train_names, test_size=0.1, random_state=42 )
sub = pd.DataFrame()
sub['Id'] = test_ID
sub['SalePrice'] = y_pred
sub.to_csv('submission_31Aug19.csv',index=False )<import_modules>
def open_rgby(path,id):
colors = ['red','green','blue','yellow']
flags = cv2.IMREAD_GRAYSCALE
img = [cv2.imread(os.path.join(path, id+'_'+color+'.png'), flags ).astype(np.float32)/255
for color in colors]
return np.stack(img, axis=-1 )
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import randint as randint
from scipy.stats import uniform
import lightgbm as lgbm
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.model_selection import train_test_split, RandomizedSearchCV, GridSearchCV
from sklearn.metrics import accuracy_score<load_from_csv>
class pdFilesDataset(FilesDataset):
def __init__(self, fnames, path, transform):
self.labels = pd.read_csv(LABELS ).set_index('Id')
 self.labels['Target'] = [[int(i)for i in s.split() ] for s in self.labels['Target']]
super().__init__(fnames, transform, path)
 def get_x(self, i):
img = open_rgby(self.path,self.fnames[i])
 if self.sz == 512: return img
else: return cv2.resize(img,(self.sz, self.sz),cv2.INTER_AREA)
 def get_y(self, i):
if(self.path == TEST): return np.zeros(len(name_label_dict),dtype=np.int)
 else:
labels = self.labels.loc[self.fnames[i]]['Target']
return np.eye(len(name_label_dict),dtype=np.float)[labels].sum(axis=0)
 @property
def is_multi(self): return True
@property
def is_reg(self):return True
def get_c(self): return len(name_label_dict )
def load_data(data_path, **kwargs):
return pd.read_csv(data_path, header=0, delimiter=kwargs['delimiter'])
 def clean_titanic_dataset(base_data_org, new_data_org, create_dummy=False):

 base_data = base_data_org.copy(deep=True)
 new_data = new_data_org.copy(deep=True)
 if(type(base_data)!= type(pd.DataFrame())) or(type(new_data)!= type(pd.DataFrame())) :
print("Input data should be pandas dataframe.")
 return None
if('PassengerId' not in base_data)or('PassengerId' not in new_data):
print("PassengerId attribute is missing.")
 return None
if(base_data.PassengerId.isnull().sum() != 0)or(new_data.PassengerId.isnull().sum() != 0):
print("PassengerId cannot have missing value.")
 return None
for item in new_data.loc[new_data.Pclass.isnull() ,:].iterrows() :
fare = item[1]["Fare"]
PassengerId = item[1]["PassengerId"]
if(fare != fare):
new_data.loc[new_data["PassengerId"]==PassengerId,"Pclass"] = base_data.Pclass.mode()
new_data.loc[new_data["PassengerId"]==PassengerId,"Fare"] = base_data.Fare.median()
else:
pclass_impute =(base_data.groupby('Pclass')['Fare'].median() - fare ).abs().idxmin()
new_data.loc[new_data["PassengerId"]==PassengerId,"Pclass"] = pclass_impute
new_data["Pclass"] = new_data["Pclass"].astype(int)
 new_data.loc[new_data.SibSp != new_data.SibSp, "SibSp"] = base_data.SibSp.mode() [0]
new_data.loc[new_data.Parch != new_data.Parch, "Parch"] = base_data.Parch.mode() [0]
new_data["Family_size"] = new_data["SibSp"] + new_data["Parch"] +1
new_data["Family_type"] = "Alone"
new_data.loc[new_data["Family_size"] > 1, "Family_type"] = "Small"
new_data.loc[new_data["Family_size"] > 4, "Family_type"] = "Big"
new_data.loc[new_data.Fare != new_data.Fare, "Fare"] = base_data.Fare.median()
new_data['Adj_fare'] = new_data['Fare'] / new_data['Family_size']
new_data["Cabin_assigned"] = 1
new_data.loc[new_data.Cabin != new_data.Cabin,"Cabin_assigned"] = 0
new_data.loc[new_data.Embarked != new_data.Embarked,"Embarked"] = base_data.Embarked.mode() [0]
for my_data in [base_data,new_data]:
my_data["Title"] = my_data.Name.str.extract('\w+,\s+([a-zA-z\s]+)\s*\.. *')
 my_data["Last_Name"] = my_data.Name.str.extract('(\w+),\s+[a-zA-z\s]+\s*\.. *')
 title_set = set(base_data.Title.unique())
 title_set.update(['Other','Special'])
 mrs = ["Mrs","Mme"]
miss = ["Mlle","Miss","Ns"]
other = ["Capt","Col","Major","Dr","Rev","Mlle","Mme","Ms","Ms",]
special = ["Lady", "Don", "Dona", "Sir", "the Countess", "Jonkheer"]
for my_data in [base_data,new_data]:
my_data["Title"].replace(mrs,"Mrs", inplace=True)
 my_data["Title"].replace(miss,"Miss", inplace=True)
 my_data["Title"].replace(other,"Other", inplace=True)
 my_data["Title"].replace(special,"Special", inplace=True)
 Titles = ["Mr","Mrs","Miss","Master","Other","Special"]
new_data.loc[new_data["Title"].apply(lambda x: x not in title_set),"Title"] = "Other"
new_data["Sex_numerical"] = new_data["Sex"].map({"male": 1, "female": 0} ).astype(int)
 new_data["Age_missing"] = 0
new_data.loc[new_data.Age.isnull() ,"Age_missing"] = 1
for title in Titles:
for pclass in range(1,4):
try:
agg_df = base_data.groupby(['Title','Pclass'])['Age'].median()
age_to_impute = np.asscalar(agg_df.loc[[(title, pclass)]].values)
 new_data.loc[(new_data['Age'].isnull())&(new_data['Title'] == title)&(new_data['Pclass'] == pclass), 'Age'] = age_to_impute
except:
agg_df = base_data.groupby(['Title'])['Age'].median()
age_to_impute = np.asscalar(agg_df.loc[[(title)]].values)
 new_data.loc[(new_data['Age'].isnull())&(new_data['Title'] == title), 'Age'] = age_to_impute
new_data["Child"] = 0
new_data.loc[new_data["Age"]<18,"Child"] = 1
new_data['Fsize_age'] = new_data['Family_size'] + new_data['Age']/70
new_data['Adj_fare'] = new_data['Fare'] / new_data['Family_size']
if create_dummy:
new_data = pd.concat([new_data, pd.get_dummies(new_data["Embarked"], prefix='Embarked')], axis=1)
 new_data = pd.concat([new_data, pd.get_dummies(new_data["Title"], prefix='Title')], axis=1)
 new_data = pd.concat([new_data, pd.get_dummies(new_data["Pclass"], prefix='Pclass')], axis=1)
 new_data = pd.concat([new_data, pd.get_dummies(new_data["Family_type"], prefix='Ftype')], axis=1)
 return new_data
def plot_feature_importance(model,X_train,model_name,num_features):
fig, axes = plt.subplots(nrows = 1, ncols = 1, sharex="all", figsize=(10,3))
indices = np.argsort(model.feature_importances_)[::-1][:num_features]
g = sns.barplot(y = X_train.columns[indices][:num_features],
x = model.feature_importances_[indices][:num_features] , orient='h')
 g.set_xlabel("Relative importance",fontsize=12)
 g.set_ylabel("Features",fontsize=12)
 g.tick_params(labelsize=9)
 g.set_title(model_name + " feature importance");
def evaluate_model_with_test_data(test_set,features,model):
Y_test = test_set["Survived"]
X_test = test_set[features]
return accuracy_score(Y_test, model.predict(X_test))
def retrain_with_whole_data(data,model,features):
Y_train = data["Survived"]
X_train = data[features]
return model.fit(X_train,Y_train)
 def predict_test_data(model, data, features):
X_test_prediction = data[features]
return pd.Series(model.predict(X_test_prediction ).astype(int), name="Survived")
<load_from_csv>
def get_data(sz,bs):
aug_tfms = [RandomRotate(30, tfm_y=TfmType.NO),
RandomDihedral(tfm_y=TfmType.NO)]
stats = A([0.00505, 0.00331, 0.00344, 0.00519], [0.10038, 0.08131, 0.08284, 0.10179])
 tfms = tfms_from_stats(stats, sz, crop_type=CropType.NO, tfm_y=TfmType.NO,
aug_tfms=aug_tfms)
 ds = ImageData.get_ds(pdFilesDataset,(tr_n[:-(len(tr_n)%bs)],TRAIN), 
(val_n,TRAIN), tfms, test=(test_names,TEST))
md = ImageData(PATH, ds, bs, num_workers=nw, classes=None)
 return md
train_data = load_data('.. /input/titanic/train.csv', delimiter = ',')
 test_data = load_data('.. /input/titanic/test.csv', delimiter = ',' )<drop_column>
class FocalLoss(nn.Module):
def __init__(self, gamma=2):
super().__init__()
self.gamma = gamma
def forward(self, input, target):
if not(target.size() == input.size()):
raise ValueError("Target size({})must be the same as input size({})" 
.format(target.size() , input.size()))
max_val =(-input ).clamp(min=0)
 loss = input - input * target + max_val + \ 
(( -max_val ).exp() +(-input - max_val ).exp() ).log()
invprobs = F.logsigmoid(-input *(target * 2.0 - 1.0))
loss =(invprobs * self.gamma ).exp() * loss
return loss.sum(dim=1 ).mean()
tr_data_c = clean_titanic_dataset(train_data,train_data, create_dummy=True)
 ts_data_c = clean_titanic_dataset(train_data,test_data, create_dummy=True)
 IDtest = ts_data_c["PassengerId"]<prepare_output>
def acc(preds,targs,thresh=0.0):
preds =(preds > thresh ).int()
targs = targs.int()
return(preds==targs ).float().mean()
tr_data_c.set_index("PassengerId",inplace=True)
 ts_data_c.set_index("PassengerId",inplace=True )<define_variables>
class ConvnetBuilder_custom() :
def __init__(self, f, c, is_multi, is_reg, ps=None, xtra_fc=None, xtra_cut=0,
custom_head=None, pretrained=True):
self.f,self.c,self.is_multi,self.is_reg,self.xtra_cut = f,c,is_multi,is_reg,xtra_cut
if xtra_fc is None: xtra_fc = [512]
if ps is None: ps = [0.25]*len(xtra_fc)+ [0.5]
self.ps,self.xtra_fc = ps,xtra_fc
if f in model_meta: cut,self.lr_cut = model_meta[f]
else: cut,self.lr_cut = 0,0
cut-=xtra_cut
layers = cut_model(f(pretrained), cut)
 w = layers[0].weight
layers[0] = nn.Conv2d(4,64,kernel_size=(7,7),stride=(2,2),padding=(3, 3), bias=False)
 layers[0].weight = torch.nn.Parameter(torch.cat(( w,torch.zeros(64,1,7,7)) ,dim=1))
self.nf = model_features[f] if f in model_features else(num_features(layers)*2)
 if not custom_head: layers += [AdaptiveConcatPool2d() , Flatten() ]
self.top_model = nn.Sequential(*layers)
 n_fc = len(self.xtra_fc)+1
if not isinstance(self.ps, list): self.ps = [self.ps]*n_fc
if custom_head: fc_layers = [custom_head]
else: fc_layers = self.get_fc_layers()
self.n_fc = len(fc_layers)
 self.fc_model = to_gpu(nn.Sequential(*fc_layers))
if not custom_head: apply_init(self.fc_model, kaiming_normal)
 self.model = to_gpu(nn.Sequential(*(layers+fc_layers)))
 @property
def name(self): return f'{self.f.__name__}_{self.xtra_cut}'
def create_fc_layer(self, ni, nf, p, actn=None):
res=[nn.BatchNorm1d(num_features=ni)]
if p: res.append(nn.Dropout(p=p))
res.append(nn.Linear(in_features=ni, out_features=nf))
if actn: res.append(actn)
 return res
def get_fc_layers(self):
res=[]
ni=self.nf
for i,nf in enumerate(self.xtra_fc):
res += self.create_fc_layer(ni, nf, p=self.ps[i], actn=nn.ReLU())
 ni=nf
final_actn = nn.Sigmoid() if self.is_multi else nn.LogSoftmax()
if self.is_reg: final_actn = None
res += self.create_fc_layer(ni, self.c, p=self.ps[-1], actn=final_actn)
 return res
def get_layer_groups(self, do_fc=False):
if do_fc:
return [self.fc_model]
idxs = [self.lr_cut]
c = children(self.top_model)
 if len(c)==3: c = children(c[0])+c[1:]
lgs = list(split_by_idxs(c,idxs))
return lgs+[self.fc_model]
class ConvLearner(Learner):
def __init__(self, data, models, precompute=False, **kwargs):
self.precompute = False
super().__init__(data, models, **kwargs)
 if hasattr(data, 'is_multi')and not data.is_reg and self.metrics is None:
self.metrics = [accuracy_thresh(0.5)] if self.data.is_multi else [accuracy]
if precompute: self.save_fc1()
self.freeze()
self.precompute = precompute
def _get_crit(self, data):
if not hasattr(data, 'is_multi'): return super()._get_crit(data)
 return F.l1_loss if data.is_reg else F.binary_cross_entropy if data.is_multi else F.nll_loss
@classmethod
def pretrained(cls, f, data, ps=None, xtra_fc=None, xtra_cut=0, custom_head=None, precompute=False,
pretrained=True, **kwargs):
models = ConvnetBuilder_custom(f, data.c, data.is_multi, data.is_reg,
ps=ps, xtra_fc=xtra_fc, xtra_cut=xtra_cut, custom_head=custom_head, pretrained=pretrained)
 return cls(data, models, precompute, **kwargs)
 @classmethod
def lsuv_learner(cls, f, data, ps=None, xtra_fc=None, xtra_cut=0, custom_head=None, precompute=False,
needed_std=1.0, std_tol=0.1, max_attempts=10, do_orthonorm=False, **kwargs):
models = ConvnetBuilder(f, data.c, data.is_multi, data.is_reg,
ps=ps, xtra_fc=xtra_fc, xtra_cut=xtra_cut, custom_head=custom_head, pretrained=False)
 convlearn=cls(data, models, precompute, **kwargs)
 convlearn.lsuv_init()
return convlearn
@property
def model(self): return self.models.fc_model if self.precompute else self.models.model
def half(self):
if self.fp16: return
self.fp16 = True
if type(self.model)!= FP16: self.models.model = FP16(self.model)
 if not isinstance(self.models.fc_model, FP16): self.models.fc_model = FP16(self.models.fc_model)
 def float(self):
if not self.fp16: return
self.fp16 = False
if type(self.models.model)== FP16: self.models.model = self.model.module.float()
if type(self.models.fc_model)== FP16: self.models.fc_model = self.models.fc_model.module.float()
@property
def data(self): return self.fc_data if self.precompute else self.data_
def create_empty_bcolz(self, n, name):
return bcolz.carray(np.zeros(( 0,n), np.float32), chunklen=1, mode='w', rootdir=name)
 def set_data(self, data, precompute=False):
super().set_data(data)
 if precompute:
self.unfreeze()
self.save_fc1()
self.freeze()
self.precompute = True
else:
self.freeze()
def get_layer_groups(self):
return self.models.get_layer_groups(self.precompute)
 def summary(self):
precompute = self.precompute
self.precompute = False
res = super().summary()
self.precompute = precompute
return res
def get_activations(self, force=False):
tmpl = f'_{self.models.name}_{self.data.sz}.bc'
names = [os.path.join(self.tmp_path, p+tmpl)for p in('x_act', 'x_act_val', 'x_act_test')]
if os.path.exists(names[0])and not force:
self.activations = [bcolz.open(p)for p in names]
else:
self.activations = [self.create_empty_bcolz(self.models.nf,n)for n in names]
def save_fc1(self):
self.get_activations()
act, val_act, test_act = self.activations
m=self.models.top_model
if len(self.activations[0])!=len(self.data.trn_ds):
predict_to_bcolz(m, self.data.fix_dl, act)
 if len(self.activations[1])!=len(self.data.val_ds):
predict_to_bcolz(m, self.data.val_dl, val_act)
 if self.data.test_dl and(len(self.activations[2])!=len(self.data.test_ds)) :
if self.data.test_dl: predict_to_bcolz(m, self.data.test_dl, test_act)
 self.fc_data = ImageClassifierData.from_arrays(self.data.path, 
(act, self.data.trn_y),(val_act, self.data.val_y), self.data.bs, classes=self.data.classes,
test = test_act if self.data.test_dl else None, num_workers=8)
 def freeze(self):
self.freeze_to(-1)
 def unfreeze(self):
self.freeze_to(0)
 self.precompute = False
def predict_array(self, arr):
precompute = self.precompute
self.precompute = False
pred = super().predict_array(arr)
 self.precompute = precompute
return pred
train_data_cleaned, test_data_cleaned = df.loc[tr_data_c.index], df.loc[ts_data_c.index] 
<split>
sz = 256
bs = 64
md = get_data(sz,bs)
 learner = ConvLearner.pretrained(arch, md, ps=0.5)
 learner.opt_fn = optim.Adam
learner.crit = FocalLoss()
learner.metrics = [acc]
split = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=914)
 for train_index, test_index in split.split(train_data_cleaned, train_data_cleaned[["Sex"]]):
train_set = train_data_cleaned.iloc[train_index]
test_set = train_data_cleaned.iloc[test_index]<define_variables>
learner.load('ResNet34_256_1' )
lgbm_grid_search = False
lgbm_feature_names = [
'Sex_numerical'
, 'Family_survival_rate'
, 'Is_single_traveler'
]
Y_train = train_set["Survived"]
X_train = train_set[lgbm_feature_names]
if lgbm_grid_search:
fit_params = {"early_stopping_rounds" : 100,
"eval_metric" : 'auc',
"eval_set" : [(X_train,Y_train)],
'eval_names': ['valid'],
'verbose': 0,
'categorical_feature': 'auto'}
param_test = {'learning_rate' : [0.01, 0.02, 0.03, 0.04, 0.05, 0.08, 0.1, 0.2, 0.3, 0.4],
'n_estimators' : [100, 200, 300, 400, 500, 600, 800, 1000, 1500, 2000],
'num_leaves': randint(6, 200),
'min_child_samples': randint(100, 500),
'min_child_weight': [1e-5, 1e-3, 1e-2, 1e-1, 1, 1e1, 1e2, 1e3, 1e4],
'subsample': uniform(loc = 0.2, scale = 0.8),
'max_depth': [-1, 1, 2, 3, 4, 5, 6, 7,8,9,10],
'colsample_bytree': uniform(loc = 0.4, scale = 0.6),
'reg_alpha': [0, 1e-1, 1, 2, 5, 7, 10, 50, 100],
'reg_lambda': [0, 0.01, 0.1, 0.3, 1, 5, 10, 20, 50, 100]}
lgbm_clf = lgbm.LGBMClassifier(random_state = 914, silent = True, metric = 'None', n_jobs = 16)
 grid_search = RandomizedSearchCV(
 estimator = lgbm_clf, param_distributions = param_test,
n_iter = 2000,
scoring = 'accuracy',
cv = 5,
refit = True,
random_state = 914,
verbose = True)
 grid_search.fit(X_train, Y_train, **fit_params)
 opt_parameters = grid_search.best_params_
print(grid_search.best_params_)
 else:
opt_parameters = {
'colsample_bytree': 0.951896848025216,
'learning_rate': 0.2,
'max_depth': 10,
'min_child_samples': 102,
'min_child_weight': 0.01,
'n_estimators': 400,
'num_leaves': 102,
'reg_alpha': 2,
'reg_lambda': 0.5,
'subsample': 0.4194694182848429}
lgbm_best = lgbm.LGBMClassifier(**opt_parameters)
 lgbm_model_best_ = lgbm_best.fit(X_train,Y_train )<compute_test_metric>
lr = 2e-2
learner.fit(lr,1 )
evaluate_model_with_test_data(test_set,lgbm_feature_names,lgbm_model_best_ )<predict_on_test>
learner.unfreeze()
lrs=np.array([lr/10,lr/3,lr] )
lgbm_on_training = retrain_with_whole_data(train_data_cleaned,lgbm_model_best_,lgbm_feature_names)
 test_Survived_lgbm = predict_test_data(lgbm_on_training, test_data_cleaned,lgbm_feature_names )<save_to_csv>
learner.fit(lrs/4,4,cycle_len=2,use_clr=(10,20))
results = pd.concat([IDtest,test_Survived_lgbm],axis=1)
 results.to_csv("submission.csv",index=False )<import_modules>
learner.fit(lrs/4,2,cycle_len=4,use_clr=(10,20))
import pandas as pd
import numpy as np<import_modules>
learner.fit(lrs/8,1,cycle_len=8,use_clr=(5,20))
import pandas as pd
import numpy as np<load_from_csv>
learner.save('ResNet34_256_1' )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
def sigmoid_np(x):
return 1.0/(1.0 + np.exp(-x))
preds,y = learner.TTA()
preds = np.stack(preds, axis=-1)
 preds = sigmoid_np(preds)
 pred = preds.max(axis=-1 )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<define_variables>
th = np.array([0.50,0.56,0.50,0.50,0.50,0.47,0.39,0.56,0.36,0.45,
0.41,0.48,0.45,0.44,0.63,0.50,0.38,0.33,0.45,0.41,
0.33,0.49,0.43,0.52,0.51,0.48,0.43,0.1])
 print(f1_score(y, pred>0.5, average='macro'),f1_score(y, pred>th, average='macro'))
test_key = test.PassengerId<set_options>
preds_t,y_t = learner.TTA(is_test=True)
 preds_t = np.stack(preds_t, axis=-1)
 preds_t = sigmoid_np(preds_t)
 pred_t = preds_t.max(axis=-1 )
sns.set()<feature_engineering>
pred_list = []
for line in pred_t:
s = ' '.join(list([str(i)for i in np.nonzero(line>th_t)[0]]))
pred_list.append(s )
<feature_engineering><EOS> 
sample_df = pd.read_csv(SAMPLE)
 sample_list = list(sample_df.Id)
 pred_dic = dict(( key, value)for(key, value)
 in zip(learner.data.test_ds.fnames,pred_list))
pred_list_cor = [pred_dic[id] for id in sample_list]
df = pd.DataFrame({'Id':sample_list,'Predicted':pred_list_cor})
 df.to_csv('protein_classification.csv', header=True, index=False )