Kaggle competition: Costa Rican Household Poverty Level Prediction
!cp.. /input/resnet50imet15fold320/resnet50-1-v4-603.pth./models/
Res_learn1 = cnn_learner(Res_data, base_arch=models.resnet50, loss_func=FocalLoss() , metrics=fbeta,pretrained=False)
 Res_learn1.load('resnet50-1-v4-603')
 Res_test_preds1 = Res_learn1.get_preds(ds_type=DatasetType.Test)
 Res_test_preds1<choose_model_class>
df = pd.read_csv('/kaggle/input/costa-rican-household-poverty-prediction/train.csv')
 test = pd.read_csv('/kaggle/input/costa-rican-household-poverty-prediction/test.csv')
 df.shape, test.shape
!cp.. /input/resnet50imet15fold320/resnet50-2-v4-597.pth./models/
Res_learn2 = cnn_learner(Res_data, base_arch=models.resnet50, loss_func=FocalLoss() , metrics=fbeta,pretrained=False)
 Res_learn2.load('resnet50-2-v4-597')
 Res_test_preds2 = Res_learn2.get_preds(ds_type=DatasetType.Test)
 Res_test_preds2<choose_model_class>
df_all = df.append(test)
 df_all.shape
!cp.. /input/resnet50imet15fold320/resnet50-3-v4-597.pth./models/
Res_learn3 = cnn_learner(Res_data, base_arch=models.resnet50, loss_func=FocalLoss() , metrics=fbeta,pretrained=False)
 Res_learn3.load('resnet50-3-v4-597')
 Res_test_preds3 = Res_learn3.get_preds(ds_type=DatasetType.Test)
 Res_test_preds3<choose_model_class>
df_all['dependency'].value_counts()
!cp.. /input/resnet50imet15fold320/resnet50-7-v4-597.pth./models/
Res_learn7 = cnn_learner(Res_data, base_arch=models.resnet50, loss_func=FocalLoss() , metrics=fbeta,pretrained=False)
 Res_learn7.load('resnet50-7-v4-597')
 Res_test_preds7 = Res_learn7.get_preds(ds_type=DatasetType.Test)
 Res_test_preds7<choose_model_class>
df_all['edjefa'].value_counts()
!cp.. /input/resnet50imet15fold320/resnet50-8-v4-603.pth./models/
Res_learn8 = cnn_learner(Res_data, base_arch=models.resnet50, loss_func=FocalLoss() , metrics=fbeta,pretrained=False)
 Res_learn8.load('resnet50-8-v4-603')
 Res_test_preds8 = Res_learn8.get_preds(ds_type=DatasetType.Test)
 Res_test_preds8<choose_model_class>
df_all['edjefe'].value_counts()
!cp.. /input/resnet50imet15fold320/resnet50-10-v4-599.pth./models/
Res_learn10 = cnn_learner(Res_data, base_arch=models.resnet50, loss_func=FocalLoss() , metrics=fbeta,pretrained=False)
 Res_learn10.load('resnet50-10-v4-599')
 Res_test_preds10 = Res_learn10.get_preds(ds_type=DatasetType.Test)
 Res_test_preds10<prepare_output>
mapeamento = {'yes': 1, 'no': 0}
df_all['edjefa'] = df_all['edjefa'].replace(mapeamento ).astype(int)
 df_all['edjefe'] = df_all['edjefe'].replace(mapeamento ).astype(int )
test_preds =(( Incepres_test_preds0[0]*1+Incepres_test_preds1[0]*1.1
+Incepres_test_preds2[0]*0.9+Incepres_test_preds3[0]*0.9
+Incepres_test_preds7[0]*0.9+Incepres_test_preds8[0]*1.1
+Incepres_test_preds10[0]*1.1)/14+(Res_test_preds0[0]*1+Res_test_preds1[0]*1
+Res_test_preds2[0]*1+Res_test_preds3[0]*1
+Res_test_preds7[0]*1+Res_test_preds8[0]*1
+Res_test_preds10[0]*1)/14,Incepres_test_preds1[1])
<data_type_conversions>
df_all['dependency'].value_counts()
i2c = np.array([[i, c] for c, i in Incepres_learn0.data.train_ds.y.c2i.items() ] ).astype(int)
 def join_preds(preds, thr):
return [' '.join(i2c[np.where(t==1)[0],1].astype(str)) for t in(preds[0].sigmoid() >thr ).long() ]<merge>
df_all['dependency'] = df_all['dependency'].replace(mapeamento ).astype(float )
test_df.attribute_ids = join_preds(test_preds, 0.260)
 test_df.head()<save_to_csv>
total = np.product(df_all.shape)
 miss_values = df_all.isnull().sum().sum()
porcentagem_total =(miss_values/total)* 100
print(f'A porcentagem total de valores nulos do dataset é de: {(porcentagem_total)}' )
test_df.to_csv('submission.csv', index=False )<set_options>
total_num = df_all.isnull().sum().sort_values(ascending = False)
 perc = df_all.isnull().sum() /df_all.isnull().count() *100
perc1 =(round(perc,2 ).sort_values(ascending = False))
df_miss = pd.concat([total_num, perc1], axis = 1, keys = ['Total de Valores Nulos', 'Porcentagem %'] ).sort_values(by = 'Porcentagem %', ascending = False)
 top_miss = df_miss[df_miss['Porcentagem %'] > 0]
top_miss.reset_index(inplace = True)
 top_miss
def seed_all(seed=27):

 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_all(27 )<define_variables>
df_all.rez_esc.value_counts()
BATCH_SIZE = 128
NUM_EPOCHS = 20
PERCENTILE = 99.7
LEARNING_RATE = 0.0001
DISABLE_TQDM = True<set_options>
df_all.loc[(( df_all['age'] > 19)|(df_all['age'] < 7)) &(df_all['rez_esc'].isnull()), 'rez_esc'] = 0
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )<load_from_csv>
df_all['rez_esc-missing'] = df_all['rez_esc'].isnull()
df = pd.read_csv(labels_csv)
 attribute_dict = dict(zip(df.attribute_id,df.attribute_name))
del df,labels_csv<define_variables>
df_all.loc[df_all['rez_esc'] > 5, 'rez_esc'] = 5
tag_count = 0
culture_count = 0
for idx,data in attribute_dict.items() :
if data.split("::")[0] == 'tag':
tag_count+=1
if data.split("::")[0] == 'culture':
culture_count+=1
print('total_categories: {0}
tag_categories: {1}
culture_categories: {2} ' \ 
.format(len(attribute_dict),tag_count,culture_count))
assert tag_count+culture_count == len(attribute_dict)
 output_dim = len(attribute_dict )<load_from_csv>
df_all['rez_esc'].fillna(0, inplace = True )
df = pd.read_csv(train_csv)
 labels_dict = dict(zip(df.id,df.attribute_ids))<train_model>
df_all.v18q1.value_counts()
idx = len(os.listdir(train_dir))
number = randint(0,idx)
 image_name = os.listdir(train_dir)[number]
def imshow(image):
plt.figure(figsize=(6, 6))
plt.imshow(image)
 plt.show()
x = Image.open(os.path.join(train_dir,image_name))
for i in labels_dict[os.listdir(train_dir)[number].split('.')[0]].split() :
print(attribute_dict[int(i)])
 np.array(x ).shape
imshow(x )<categorify>
df_all.v18q.value_counts()
data_transforms = transforms.Compose([
transforms.Resize(( 224,224)) ,
transforms.ToTensor() ,
] )<define_variables>
df_all.v2a1.value_counts()
class ImageData(data.Dataset):
def __init__(self,df,dirpath,transform,test = False):
self.df = df
self.test = test
self.dirpath = dirpath
self.conv_to_tensor = transform
if not self.test:
self.image_arr = np.asarray(str(self.dirpath)+'/'+self.df.iloc[:, 0]+'.png')
 else:
self.image_arr = np.asarray(str(self.dirpath)+'/'+self.df.iloc[:, 0])
 if not self.test:
self.label_df = self.df.iloc[:,1]
self.data_len = len(self.df.index)
 def __len__(self):
return self.data_len
def __getitem__(self, idx):
image_name = self.image_arr[idx]
img = Image.open(image_name)
 img_tensor = self.conv_to_tensor(img)
 if not self.test:
image_labels = self.label_df[idx]
label_tensor = torch.zeros(( 1, output_dim))
for label in image_labels.split() :
label_tensor[0, int(label)] = 1
image_label = torch.tensor(label_tensor,dtype= torch.float32)
 return(img_tensor,image_label.squeeze())
 return(img_tensor )<load_from_csv>
df_all.tipovivi3.value_counts().sort_values()
df = pd.read_csv(train_csv)
 train_df,val_df = train_test_split(df, test_size=0.20)
 train_df = train_df.reset_index(drop=True)
 val_df = val_df.reset_index(drop=True)
 print(f"Validation_Data Length: {len(val_df)}
Train_Data Length: {len(train_df)}" )<create_dataframe>
df_all.tipovivi1.value_counts().sort_values()
train_dataset = ImageData(train_df,train_dir,data_transforms)
 train_loader = data.DataLoader(dataset=train_dataset,batch_size=BATCH_SIZE,shuffle=False)
 val_dataset = ImageData(val_df,train_dir,data_transforms)
 val_loader = data.DataLoader(dataset=val_dataset,batch_size=BATCH_SIZE,shuffle=False)
 test_df = pd.DataFrame(os.listdir(test_dir))
test_dataset = ImageData(test_df,test_dir,data_transforms,test = True)
 test_loader = data.DataLoader(dataset=test_dataset,batch_size=BATCH_SIZE,shuffle=False)
 dataloaders_dict = {'train':train_loader, 'val':val_loader}<define_search_model>
df_all.tipovivi2.value_counts().sort_values()
resnet_cls = models.resnet50()
resnet_cls.load_state_dict(torch.load(resnet_weights_path))
class AvgPool(nn.Module):
def forward(self, x):
return F.avg_pool2d(x, x.shape[2:])
 class ResNet50(nn.Module):
def __init__(self,num_outputs):
super(ResNet50,self ).__init__()
self.resnet = resnet_cls
layer4 = self.resnet.layer4
self.resnet.layer4 = nn.Sequential(
 nn.Dropout(0.5),
layer4 
)
 self.resnet.avgpool = AvgPool()
self.resnet.fc = nn.Linear(2048, num_outputs)
 for param in self.resnet.parameters() :
param.requires_grad = False
for param in self.resnet.layer4.parameters() :
param.requires_grad = True
for param in self.resnet.fc.parameters() :
param.requires_grad = True
def forward(self,x):
out = self.resnet(x)
 return out
NeuralNet = ResNet50(num_outputs = output_dim )<train_model>
df_all.tipovivi4.value_counts()
total_params = sum(p.numel() for p in NeuralNet.parameters())
 print(f'{total_params:,} total parameters.')
 total_trainable_params = sum(p.numel() for p in NeuralNet.parameters() if p.requires_grad)
 print(f'{total_trainable_params:,} training parameters.' )<define_variables>
df_all.tipovivi5.value_counts()
print("TRAINING")
 print("training examples: ",len(train_dataset))
print("batch size: ",BATCH_SIZE)
 print("batches available: ",len(train_loader))
print()
print("TESTING")
 print("validation examples: ",len(val_dataset))
print("batch size: ",BATCH_SIZE)
 print("batches available: ",len(val_loader))
print()
print("VALIDATION")
 print("testing examples: ",len(test_dataset))
print("batch size: ",BATCH_SIZE)
 print("batches available: ",len(test_loader))<choose_model_class>
df_all['SQBmeaned'].fillna(-1, inplace=True)
 df_all['meaneduc'].fillna(-1, inplace=True )
NeuralNet = NeuralNet.to(device)
 optimizer = optim.Adam(NeuralNet.parameters() ,lr = LEARNING_RATE)
 loss_func = torch.nn.BCEWithLogitsLoss()
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer,patience = 2)
 best_loss = np.inf
for epoch in range(NUM_EPOCHS):
for phase in ['train', 'val']:
start_time = time.time()
if phase == 'train':
NeuralNet.train()
else:
NeuralNet.eval()
running_loss = 0.0
for images_batch, labels_batch in tqdm(dataloaders_dict[phase],disable = DISABLE_TQDM):
images_batch = images_batch.to(device)
 labels_batch = labels_batch.to(device)
 optimizer.zero_grad()
with torch.set_grad_enabled(phase == 'train'):
pred_batch = NeuralNet(images_batch)
 loss = loss_func(pred_batch,labels_batch)
 if phase == 'train':
loss.backward()
optimizer.step()
running_loss += loss.item() * images_batch.size(0)
 epoch_loss = running_loss / len(dataloaders_dict[phase].dataset)
 if phase == 'val' and epoch_loss < best_loss:
print("model val_loss Improved from {:.8f} to {:.8f}".format(best_loss,epoch_loss))
best_loss = epoch_loss
best_model_wts = copy.deepcopy(NeuralNet.state_dict())
 if phase == 'val':
scheduler.step(epoch_loss)
 elapsed_time = time.time() -start_time
print("Phase: {} | Epoch: {}/{} | {}_loss:{:.8f} | Time: {:.4f}s".format(phase,
epoch+1,
NUM_EPOCHS,
phase,
epoch_loss,
elapsed_time))
NeuralNet.load_state_dict(best_model_wts )<predict_on_test>
total = np.product(df_all.shape)
 miss_values = df_all.isnull().sum().sum()
porcentagem_total =(miss_values/total)* 100
print(f'A porcentagem total de valores nulos do dataset é de: {(porcentagem_total)}' )
NeuralNet.eval()
predictions = np.zeros(( len(test_dataset), output_dim))
i = 0
for test_batch in tqdm(test_loader,disable = DISABLE_TQDM):
test_batch = test_batch.to(device)
 batch_prediction = NeuralNet(test_batch ).detach().cpu().numpy()
predictions[i * BATCH_SIZE:(i+1)* BATCH_SIZE, :] = batch_prediction
i+=1<define_variables>
df_all.idhogar.nunique()
predicted_class_idx = []
for i in range(len(predictions)) :
idx_list = np.where(predictions[i] > np.percentile(predictions[i],PERCENTILE))
predicted_class_idx.append(idx_list[0] )<feature_engineering>
df_all.parentesco1.value_counts()
test_df['attribute_ids'] = predicted_class_idx
test_df['attribute_ids'] = test_df['attribute_ids'].apply(lambda x : ' '.join(map(str,list(x))))
test_df = test_df.rename(columns={0: 'id'})
 test_df['id'] = test_df['id'].apply(lambda x : x.split('.')[0])
 test_df.head()<save_to_csv>
(
df_all.groupby('r4t3' ).idhogar.nunique() 
.reset_index() 
.rename(columns = {'r4t3':'Quantidade de Pessoas na Casa', 'idhogar':'Quantidade de Casas'})
.sort_values(by = ['Quantidade de Casas'], ascending = True)
.reset_index() 
.drop(['index'], axis = 1)
 )
test_df.to_csv('submission.csv',index = False )<define_variables>
(
df_all.groupby('tamviv' ).idhogar.nunique() 
.reset_index() 
.rename(columns={'tamviv':'Quantidade de Pessoas que vivem na Casa', 'idhogar':'Quantidade de Casas'})
.sort_values(by=['Quantidade de Casas'], ascending=True)
.reset_index() 
.drop(['index'], axis = 1)
 )
beginning_time = time()<set_options>
(
df_all.groupby('Target' ).idhogar.count()
)
tqdm_notebook.pandas()
%matplotlib inline
print(os.listdir(".. /input"))<import_modules>
(
df_all.groupby('r4h1' ).idhogar.nunique() 
.reset_index() 
.rename(columns={'r4h1':'Meninos mais jovens que 12 anos de idade','idhogar':'Quantidade de Casas'})
 )
print("chainercsv:", chainercv.__version__)
 print("chainer:", chainer.__version__ )<define_variables>
(
df_all.groupby('r4h2' ).idhogar.nunique() 
.reset_index() 
.rename(columns={'r4h2':'Homens com 12 anos de idade ou mais velhos','idhogar':'Quantidade de Casas'})
 )
DATA_DIR = ".. /input/imet-2019-fgvc6"
PRETRAINED_MODEL_DIR = ".. /input/chainercv-seresnet"<define_variables>
(
df_all.groupby('r4h3' ).idhogar.nunique() 
.reset_index() 
.rename(columns={'r4h3':'Total de Homens na Casa','idhogar':'Quantidade de Casas'})
 )
img_chmean_train = np.array([164.82181258, 155.93463791, 144.58968491], dtype="f")
<init_hyperparams>
(
df_all.groupby('r4m1' ).idhogar.nunique() 
.reset_index() 
.rename(columns={'r4m1':'Meninas mais jovens que 12 anos de idade','idhogar':'Quantidade de Casas'})
 )
seed = 1086
settings = OrderedDict(
 base_model="SEResNet152",
n_class=1103,
image_size=[128, 128],
max_epoch=20,
batch_size=128,
da_select=[
"random_distort",
"random_lr_flip",
"random_rotate",
"random_expand", "resize_with_random",
"random_crop"
],
learning_schedule="cosine",
epoch_per_cycle=20,
optimizer="NesterovAG",
learning_rate=0.01,
learning_rate_min=0.0001,
momentum=0.9,
weight_decay_rate=1e-04,
loss_func="FocalLoss",
alpha=0.5,
gamma=2, 
)
 settings["pretrained_model_path"] = "{}/{}".format(
 PRETRAINED_MODEL_DIR,
{
"SEResNet50": "se_resnet50_imagenet_converted_2018_06_25.npz",
"SEResNet101": "se_resnet101_imagenet_converted_2018_06_25.npz",
"SEResNet152": "se_resnet152_imagenet_converted_2018_06_25.npz",
}[settings["base_model"]] )<import_modules>
(
df_all.groupby('r4m2' ).idhogar.nunique() 
.reset_index() 
.rename(columns={'r4m2':'Meninas de 12 anos de idade ou mais velhas','idhogar':'Quantidade de Casas'})
 )
base_class = getattr(chainercv.links, settings["base_model"])
 class FeatureExtractor(base_class):

 def __init__(self, pretrained_model_path, extract_layers=["pool5"]):

 super(FeatureExtractor, self ).__init__(pretrained_model=pretrained_model_path)
 self._pick = extract_layers
self.remove_unused()
def __call__(self, x):

 h = x
for name in self.layer_names:
h = self[name](h)
 return h
class Ext2Linear(chainer.Chain):

 def __init__(self, n_class, extractor):

 super(Ext2Linear, self ).__init__()
with self.init_scope() :
self.extractor = extractor
self.fc = links.Linear(
 None, n_class, initialW=initializers.Normal(scale=0.01))
def __call__(self, x):

 return self.fc(self.extractor(x))<compute_train_metric>
(
df_all.groupby('r4m3' ).idhogar.nunique() 
.reset_index() 
.rename(columns={'r4m3':'Total de Mulheres na Casa','idhogar':'Quantidade de Casas'})
 )
class MultiLabelClassifier(links.Classifier):

 def __init__(self, predictor, lossfun):

 super(MultiLabelClassifier, self ).__init__(predictor, lossfun)
 self.compute_accuracy = False
self.f_beta = None
self.metfun = self._fbeta_score
def __call__(self, x, t):

 loss = super().__call__(x, t)
 self.f_beta = None
self.f_beta = self.metfun(self.y, t)
 reporter.report({'f-beta': self.f_beta}, self)
 return loss
def _fbeta_score(self, y_pred, t, beta=2, th=0.2, epsilon=1e-09):

 y_prob = functions.sigmoid(y_pred ).data
t_pred =(y_prob >= th ).astype("i")
 true_pos =(t_pred * t ).sum(axis=1)
 pred_pos = t_pred.sum(axis=1)
 poss_pos = t.sum(axis=1)
 precision = true_pos /(pred_pos + epsilon)
 recall = true_pos /(poss_pos + epsilon)
 f_beta_each_id =(1 + beta ** 2)* precision * recall /(( beta ** 2)* precision + recall + epsilon)
 return functions.mean(f_beta_each_id )<compute_test_metric>
edjefas_renda = pd.pivot_table(
 df_all,
columns = 'Target',
index = 'edjefa',
values = 'Id',
aggfunc = {'Id':'count'} 
)
 edjefas_renda
class FocalLoss:

 def __init__(self, alpha=0.25, gamma=2):

 super(FocalLoss, self ).__init__()
self.alpha = alpha
self.gamma = gamma
def __call__(self, y_pred, t, epsilon=1e-31):

 p_dash = functions.clip(
(1 - t)+(2 * t - 1)* functions.sigmoid(y_pred), epsilon, 1 - epsilon)
 alpha_dash =(1 - t)+(2 * t - 1)* self.alpha
loss_by_sample_x_class = - alpha_dash *(1 - p_dash)** self.gamma * functions.log(p_dash)
 loss_by_sample = functions.sum(loss_by_sample_x_class, axis=1)
 return functions.mean(loss_by_sample )<normalization>
edjefes_renda = pd.pivot_table(
 df_all,
columns = 'Target',
index = 'edjefe',
values = 'Id',
aggfunc = {'Id':'count'} 
)
 edjefes_renda
def resize_pair(pair, size=settings["image_size"]):
img, label = pair
img = transforms.resize(img, size=size)
 return(img, label)
 def scale_and_subtract_mean(pair, mean_value=img_chmean_train):
img, label = pair
img =(img - mean_value[:, None, None])/ 255.
 return(img, label )<set_options>
room_rent =(
 df_all.groupby('rooms' ).v2a1.mean()
).reset_index()
room_rent.rename(columns = {'rooms':'Quantidade de Quartos', 'v2a1':'Média do Aluguel Mensal'} )
class DataAugmentor() :

 def __init__(
 self, image_size, image_mean,
using_methods=["random_ud_flip","random_lr_flip", "random_90_rotate", "random_crop"] 
):

 self.img_size = image_size
self.img_mean = image_mean
self.img_mean_single = int(image_mean.mean())
 self.using_methods = using_methods
self.func_dict = {
"random_ud_flip": self._random_ud_flip,
"random_lr_flip": self._random_lr_flip,
"random_90_rotate": self._random_90_rotate,
"random_rotate": self._random_rotate,
"random_expand": self._random_expand,
"resize_with_random": self._resize_with_random,
"random_crop": self._random_crop,
"random_distort": random_distort,
}
self.da_funcs = [self.func_dict[um] for um in using_methods]
def __call__(self, pair):

 img_arr, label = pair
for func in self.da_funcs:
img_arr = func(img_arr)
 return img_arr, label
def _random_lr_flip(self, img_arr):

 if np.random.randint(2):
img_arr = img_arr[:, :, ::-1]
return img_arr
def _random_ud_flip(self, img_arr):

 if np.random.randint(2):
img_arr = img_arr[:, ::-1, :]
return img_arr
def _random_90_rotate(self, img_arr):

 if np.random.randint(2):
img_arr = img_arr.transpose(0, 2, 1)[:, ::-1, :]
return img_arr
def _random_rotate(self, img_arr, max_angle=10):

 angle = np.random.randint(-max_angle, max_angle + 1)
 if angle == 0:
return img_arr
return transforms.rotate(img_arr, angle, fill=self.img_mean_single)
 def _random_expand(self, img_arr):

 if np.random.randint(2):
return img_arr
return transforms.random_expand(img_arr, fill=self.img_mean)
 def _resize_with_random(self, img_arr):

 if img_arr.shape[-2:] == self.img_size:
return img_arr
return resize_with_random_interpolation(img_arr, self.img_size)
 def _random_crop(self, img_arr, rate=0.5):

 crop_size = self.img_size
resize_size = tuple(map(lambda x: int(x * 256 / 224), self.img_size))
if np.random.randint(2):
top = np.random.randint(0, resize_size[0] - crop_size[0])
 botom = top + crop_size[0]
left = np.random.randint(0, resize_size[1] - crop_size[1])
 right = left + crop_size[1]
img_arr = transforms.resize(img_arr, size=resize_size)[:, top:botom, left: right]
return img_arr<load_pretrained>
df_all = df_all.drop(['hhsize','r4t3'], axis = 1 )
class CosineShift(chainer.training.extension.Extension):

 def __init__(self, attr, value, period, period_mult=1, optimizer=None):
self._attr = attr
self._value = value
self._period = period
self._period_mult = period_mult
self._optimizer = optimizer
if not hasattr(self._value, '__getitem__'):
self._value =(self._value, 0)
 def initialize(self, trainer):
self._update_value(trainer)
 def __call__(self, trainer):
self._update_value(trainer)
 def _update_value(self, trainer):
optimizer = self._optimizer or trainer.updater.get_optimizer('main')
 epoch = trainer.updater.epoch
period_range = self._period
period_start = 0
period_end = period_range
while period_end <= epoch:
period_start = period_end
period_range *= self._period_mult
period_end += period_range
n_max, n_min = self._value
t_cur = epoch - period_start
t_i = period_range
value = n_min + 0.5 *(n_max - n_min)*(1 + cos(( t_cur / t_i)* pi))
setattr(optimizer, self._attr, value )<predict_on_test>
df_all = df_all.drop(['age'], axis = 1 )
def predict(model, val_iter, gpu_device=-1):
val_pred_list = []
val_label_list = []
iter_num = 0
epoch_test_start = time()
while True:
val_batch = val_iter.next()
iter_num += 1
print("\rtmp_valid_iteration: {:0>5}".format(iter_num), end="")
 feature_val, label_val = chainer.dataset.concat_examples(val_batch, gpu_device)
 with chainer.no_backprop_mode() and chainer.using_config("train", False):
prediction_val = model(feature_val)
 val_pred_list.append(prediction_val)
 val_label_list.append(label_val)
 prediction_val.unchain_backward()
if val_iter.is_new_epoch:
print(" => valid end: {:.2f} sec".format(time() - epoch_test_start))
val_iter.epoch = 0
val_iter.current_position = 0
val_iter.is_new_epoch = False
val_iter._pushed_position = None
break
val_pred_all = cuda.to_cpu(functions.concat(val_pred_list, axis=0 ).data)
 val_label_all = cuda.to_cpu(functions.concat(val_label_list, axis=0 ).data)
 return val_pred_all, val_label_all<compute_test_metric>
feats = [c for c in df_all.columns if c not in ['Id', 'idhogar','Target']]
def average_fbeta_score(y_prob, t, th=0.2, beta=2, epsilon=1e-09):
t_pred =(y_prob >= th ).astype(int)
 true_pos =(t_pred * t ).sum(axis=1)
 pred_pos = t_pred.sum(axis=1)
 poss_pos = t.sum(axis=1)
 p_arr = true_pos /(pred_pos + epsilon)
 r_arr = true_pos /(poss_pos + epsilon)
 f_beta =(1 + beta ** 2)* p_arr * r_arr /(( beta ** 2)* p_arr + r_arr + epsilon)
 return f_beta.mean()
def search_best_threshold(y_prob, t, eval_func, search_range=[0.05, 0.95], interval=0.01):
tmp_th = search_range[0]
best_th = 0
best_eval = -(10**9 + 7)
 while tmp_th < search_range[1]:
eval_score = eval_func(y_prob, t, th=tmp_th)
 print(tmp_th, eval_score)
 if eval_score > best_eval:
best_th = tmp_th
best_eval = eval_score
tmp_th += interval
return best_th, best_eval
def make_pred_ids(test_pred, th):
class_array = np.arange(test_pred.shape[1])
 test_cond = test_pred >= th
pred_ids = [" ".join(map(str, class_array[cond])) for cond in test_cond]
return pred_ids<train_model>
rf = RandomForestClassifier(max_depth=None, random_state=42, n_jobs=4, n_estimators=700,
min_impurity_decrease=1e-3, min_samples_leaf=2,
verbose=0, class_weight='balanced' )
print("[end of setup]: {:.3f}".format(time() - beginning_time))<load_from_csv>
rf.fit(train[feats], train['Target'] )
train_df = pd.read_csv("{}/train.csv".format(DATA_DIR))
test_df = pd.read_csv("{}/sample_submission.csv".format(DATA_DIR))
labels_df = pd.read_csv("{}/labels.csv".format(DATA_DIR))<feature_engineering>
test['Target'] = rf.predict(test[feats] ).astype(int )
%%time
train_attr_ohot = np.zeros(( len(train_df), len(labels_df)) , dtype="i")
 for idx, attr_arr in enumerate(
 train_df.attribute_ids.str.split(" " ).apply(lambda l: list(map(int, l)) ).values):
train_attr_ohot[idx, attr_arr] = 1<define_variables>
test['Target'].value_counts(normalize=True )
all_train_dataset = datasets.LabeledImageDataset(
 pairs=list(zip(( train_df.id + ".png" ).tolist() , train_attr_ohot)) ,
root="{}/train".format(DATA_DIR))
test_dataset = datasets.LabeledImageDataset(
 pairs=list(zip(( test_df.id + ".png" ).tolist() , [-1] * len(test_df))),
root="{}/test".format(DATA_DIR))<split>
( 650 + 1202 + 929 + 4664)/ 9557
train_dataset, valid_dataset = datasets.split_dataset_random(
 all_train_dataset, first_size=int(len(all_train_dataset)* 0.8), seed=seed)
 print("train set:", len(train_dataset))
print("valid set:", len(valid_dataset))<categorify>
train_dataset = datasets.TransformDataset(train_dataset, resize_pair)
 train_dataset = datasets.TransformDataset(
 train_dataset, DataAugmentor(
 image_size=settings["image_size"], image_mean=img_chmean_train,
using_methods=settings["da_select"])
)
 train_dataset = datasets.TransformDataset(train_dataset, scale_and_subtract_mean )<categorify>
from xgboost import XGBClassifier
valid_dataset = datasets.TransformDataset(valid_dataset, resize_pair)
 valid_dataset = datasets.TransformDataset(valid_dataset, scale_and_subtract_mean)
 test_dataset = datasets.TransformDataset(test_dataset, resize_pair)
 test_dataset = datasets.TransformDataset(test_dataset, scale_and_subtract_mean )<train_model>
xgb = XGBClassifier(n_estimators = 200, learning_rate = 0.09, random_state = 42, class_weight = 'balanced' )
print("[end of preparing data]: {:.3f}".format(time() - beginning_time))<choose_model_class>
xgb.fit(train[feats], train['Target'] )
model = Ext2Linear(
 settings["n_class"], FeatureExtractor(settings["pretrained_model_path"]))
train_model = MultiLabelClassifier(
 model, lossfun=FocalLoss(settings["alpha"], settings["gamma"]))<choose_model_class>
test['Target'] = xgb.predict(test[feats] ).astype(int )
opt_class = getattr(optimizers, settings["optimizer"])
 if settings["optimizer"] != "Adam":
optimizer = opt_class(lr=settings["learning_rate"], momentum=settings["momentum"])
 optimizer.setup(train_model)
 optimizer.add_hook(chainer.optimizer.WeightDecay(settings["weight_decay_rate"]))
else:
optmizer = opt_class(
 alpha=settings["learning_rate"], weight_decay_rate=settings["weight_decay_rate"])
 optimizer.setup(train_model )<concatenate>
accuracy_score(train['Target'], xgb.predict(train[feats]))
train_iter = iterators.MultiprocessIterator(train_dataset, settings["batch_size"])
 valid_iter = iterators.MultiprocessIterator(
 valid_dataset, settings["batch_size"], repeat=False, shuffle=False )<choose_model_class>
updater = training.StandardUpdater(train_iter, optimizer, device=0)
 trainer = training.trainer.Trainer(updater, stop_trigger=(settings["max_epoch"], "epoch"), out="training_result" )<train_model>
from sklearn.ensemble import AdaBoostClassifier
print("[end of preparing trainer]: {:.3f}".format(time() - beginning_time))<set_options>
abc = AdaBoostClassifier(n_estimators = 200, learning_rate = 1.0, random_state = 42 )
gc.collect()<train_model>
abc.fit(train[feats], train['Target'] )
%%time
trainer.run()<train_model>
accuracy_score(train['Target'], abc.predict(train[feats]))
<predict_on_test><EOS> 
test[['Id','Target']].to_csv('submission.csv', index=False )