Kaggle competition: Costa Rican Household Poverty Level Prediction
<SOS> metric: MacroFScore Kaggle data source: costa-rican-household-poverty-level-prediction<predict_on_test>
prefix = ".. /input/costa-rican-household-poverty-prediction/"
def prediction_for_clip(
 test_df: pd.DataFrame,
clip: np.ndarray,
model: nn.Module,
mel_params: tp.Dict[str, int],
threshold: float = 0.5, 
)-> tp.Dict[str, str]:

 assert clip.ndim == 1
pd.testing.assert_index_equal(
 test_df.columns, pd.Index(["site", "row_id", "seconds", "audio_id"])
)
 dataset = TestDataset(
 df=test_df, clip=clip, img_size=224, melspectrogram_parameters=mel_params 
)
 loader = data.DataLoader(dataset, batch_size=1, shuffle=False)
 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 model.eval()
prediction_dict = {}
for image, row_id, site in progress_bar(loader):
assert image.ndim == 4
batch_size, channels, frequency, time = image.shape
assert(batch_size, channels, frequency)==(1, 3, 224)
 assert len(row_id)== 1
assert len(site)== 1 
(site,)= site 
(row_id,)= row_id
image = image.to(device)
 with torch.no_grad() :
prediction = torch.sigmoid(model(image))
proba = prediction.detach().cpu().numpy().reshape(-1)
 assert proba.shape ==(264,)
 events = proba >= threshold
labels = np.argwhere(events ).reshape(-1 ).tolist()
if len(labels)== 0:
prediction_dict[row_id] = "nocall"
else:
labels_str_list = list(map(lambda x: INV_BIRD_CODE[x], labels))
label_string = " ".join(labels_str_list)
 prediction_dict[row_id] = label_string
return prediction_dict<predict_on_test>
warnings.filterwarnings('ignore' )
def prediction(
 test_df: pd.DataFrame,
test_audio: Path,
model_config: tp.Dict[str, tp.Union[str, int, bool]],
mel_params: tp.Dict[str, int],
target_sr: int,
threshold: float = 0.5, 
)-> pd.DataFrame:

 model = get_model(model_config)
 unique_audio_id = test_df.audio_id.unique()
prediction_dfs = []
for audio_id in unique_audio_id:
with timer(f"Loading {audio_id}"), warnings.catch_warnings() :
warnings.simplefilter("ignore", UserWarning)
 clip, _ = librosa.load(
 test_audio /(audio_id + ".mp3"),
sr=target_sr,
mono=True,
res_type="kaiser_fast", 
)
 test_df_for_audio_id = test_df.query(f"audio_id == '{audio_id}'")
 test_df_for_audio_id = test_df_for_audio_id.reset_index(drop=True)
 with timer(f"Prediction on {audio_id}"):
prediction_dict = prediction_for_clip(
 test_df_for_audio_id,
clip=clip,
model=model,
mel_params=mel_params,
threshold=threshold, 
)
 row_id = list(prediction_dict.keys())
 birds = list(prediction_dict.values())
 prediction_df = pd.DataFrame({"row_id": row_id, "birds": birds})
 prediction_dfs.append(prediction_df)
 prediction_df = pd.concat(prediction_dfs, axis=0, sort=False ).reset_index(drop=True)
 assert len(prediction_df)== len(test_df)
 return prediction_df<save_to_csv>
init_notebook_mode(connected=True)
 cf.set_config_file(offline=True, world_readable=True, theme='ggplot')
 %matplotlib inline
submission = prediction(
 test_df=test,
test_audio=TEST_AUDIO_DIR,
model_config=model_config,
mel_params=melspectrogram_parameters,
target_sr=TARGET_SR,
threshold=0.6, 
)
 submission.to_csv("submission.csv", index=False )<set_options>
def search_optimised_k(n_clusters_list, random_state, X):
silhouette_score_list = []
result_dict = {}
for n_clusters in n_clusters_list:
km_predict = KMeans(random_state=random_state, n_jobs=-1, n_clusters=n_clusters ).fit_predict(X)
 silhouette_score_list.append(silhouette_score(X=X, labels=km_predict, random_state=16446054))
result_dict["silhouette_score"] = silhouette_score_list
result_dict["n_clusters"] = n_clusters_list
return result_dict
pd.options.display.max_rows = 500
pd.options.display.max_columns = 500<feature_engineering>
class knn_agent:
def __init__(self):
self.uniform_neighbours_num = None
self.distance_neighbours_num = None
self.uniform_knn = None
self.distance_knn = None
self.f1_df = None
def optimise_params(self, X_train, y_train, X_dev, y_dev, max_k_num):
uniform_f1_test_list = []
distance_f1_test_list = []
for i in range(1, max_k_num):
uniform_knn = KNeighborsClassifier(n_neighbors=i, weights="uniform")
 distance_knn = KNeighborsClassifier(n_neighbors=i, weights="distance")
 uniform_prediction = uniform_knn.fit(X_train, y_train ).predict(X_dev)
 distance_prediction = distance_knn.fit(X_train, y_train ).predict(X_dev)
 uniform_f1_test_list.append(f1_score(y_true=y_dev, y_pred=uniform_prediction, average='macro'))
distance_f1_test_list.append(f1_score(y_true=y_dev, y_pred=distance_prediction, average='macro'))
self.f1_df = pd.DataFrame(data={"F1(uniform)": uniform_f1_test_list,
"F1(distance)": distance_f1_test_list},
index=range(1, max_k_num))
self.uniform_neighbours_num = self.f1_df.sort_values("F1(uniform)", ascending=False ).head(1 ).index.values.item(0)
 self.distance_neighbours_num = self.f1_df.sort_values("F1(distance)", ascending=False ).head(1 ).index.values.item(0)
 self.uniform_knn = KNeighborsClassifier(n_neighbors=self.uniform_neighbours_num, weights="uniform")
 self.distance_knn = KNeighborsClassifier(n_neighbors=self.distance_neighbours_num, weights="distance")
 self.plot_validation_result()
def plot_validation_result(self):
self.f1_df.iplot(kind='scatter', yTitle="F1 Score", xTitle="K number", title="Optimised K Number Searching")
 def predict(self, X_train, y_train, X_predict):
return(self.uniform_knn.fit(X_train, y_train ).predict(X_predict), self.distance_knn.fit(X_train, y_train ).predict(X_predict))
def evaluate(self, X_train, y_train, X_test, y_test):
uniform_prediction = self.uniform_knn.fit(X_train, y_train ).predict(X_test)
 distance_prediction = self.distance_knn.fit(X_train, y_train ).predict(X_test)
 return(f1_score(y_true=y_test, y_pred=uniform_prediction, average="macro"),
f1_score(y_true=y_test, y_pred=distance_prediction, average="macro"))
def set_seed(seed: int = 42):
random.seed(seed)
 np.random.seed(seed)
 os.environ["PYTHONHASHSEED"] = str(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 @contextmanager
def timer(name: str)-> None:

 t0 = time.time()
print("[{}] start".format(name))
yield
print("[{}] done in {:.0f} s".format(name, time.time() - t0))<define_variables>
def generate_final_predict_df(input_prediction):
input_prediction = input_prediction.astype(int)
 X_predict["Target"] = input_prediction
tmp_household_predict_df = X_predict.set_index("idhogar")
 tmp_X_predict = all_predict_df.loc[all_predict_df["parentesco1"] == 1, :].set_index("idhogar")
 tmp_X_predict.loc[tmp_household_predict_df.index, "Target"] = tmp_household_predict_df.Target
tmp_X_predict = tmp_X_predict.set_index("Id")
 tmp_all_predict_df = all_predict_df.set_index("Id")
 tmp_all_predict_df.loc[tmp_X_predict.index, "Target"] = tmp_X_predict.Target
tmp_all_predict_df["Target"] = tmp_all_predict_df["Target"].fillna(1 ).astype(int)
 tmp_all_predict_df
tmp_all_predict_df["Target"] = tmp_all_predict_df["Target"].fillna(1 ).astype(int)
 final_predict_df = tmp_all_predict_df.reset_index() [["Id", "Target"]]
return final_predict_df
set_seed(1213 )<define_variables>
def report(results, n_top=3):
for i in range(1, n_top + 1):
candidates = np.flatnonzero(results['rank_test_score'] == i)
 for candidate in candidates:
print("Model with rank: {0}".format(i))
print("Mean validation score: {0:.3f}(std: {1:.3f})".format(
 results['mean_test_score'][candidate],
results['std_test_score'][candidate]))
print("Parameters: {0}".format(results['params'][candidate]))
print("" )
ROOT = Path.cwd().parent
INPUT_ROOT = ROOT / "input"
RAW_DATA = INPUT_ROOT / "birdsong-recognition"
TRAIN_AUDIO_DIR = RAW_DATA / "train_audio"
TEST_AUDIO_DIR = RAW_DATA / "test_audio"<load_from_csv>
def ada_report(ada_models, X_train, y_train, X_test, y_test):
ada_tree_real, ada_tree_discrete = ada_models
real_test_f1 = []
real_train_f1 = []
discrete_test_f1 = []
discrete_train_f1 = []
trees_discrete_num = len(ada_tree_discrete)
 trees_real_num = len(ada_tree_real)
 for real_test_predict, real_train_predict, discrete_test_predict, discrete_train_predict in zip(
 ada_tree_real.staged_predict(X_test),
ada_tree_real.staged_predict(X_train),
ada_tree_discrete.staged_predict(X_test),
ada_tree_discrete.staged_predict(X_train)
):
real_test_f1.append(
 f1_score(y_pred=real_test_predict, y_true=y_test, average='macro')
)
 real_train_f1.append(
 f1_score(y_pred=real_train_predict, y_true=y_train, average='macro')
)
 discrete_test_f1.append(
 f1_score(y_pred=discrete_test_predict, y_true=y_test, average='macro')
)
 discrete_train_f1.append(
 f1_score(y_pred=discrete_train_predict, y_true=y_train, average='macro')
)
 f1s = [
real_test_f1,
real_train_f1,
discrete_test_f1,
discrete_train_f1,
]
labels = ["SAMME.R: Test", "SAMME.R: Train", "SAMME: Test", "SAMME: Train"]
colours = ['rgb(22, 96, 167)', 'rgb(22, 96, 167)', 'rgb(205, 12, 24)', 'rgb(205, 12, 24)']
styles = [None, "dash", None, "dash"]
fig = tools.make_subplots(cols=1, rows=3, print_grid=False, shared_xaxes=True)
 for f1, label, colour, style in zip(f1s, labels, colours, styles):
trace = go.Scatter(
 x=np.arange(1, trees_real_num, 1),
y=f1,
name= label,
line=dict(
 color=colour,
dash=style 
)
)
 fig.append_trace(trace, 1, 1)
 fig["layout"]["yaxis1"].update(title="F1 Macro Score")
 trace_real_error = go.Scatter(
 x=np.arange(1, trees_real_num, 1),
y=ada_tree_real.estimator_errors_,
name="SAMME.R: Error", 
)
 trace_discrete_error = go.Scatter(
 x=np.arange(1, trees_real_num, 1),
y=ada_tree_discrete.estimator_errors_,
name="SAMME: Error", 
)
 fig.append_trace(trace_real_error, 2, 1)
 fig.append_trace(trace_discrete_error, 2, 1)
 fig["layout"]["yaxis2"].update(title="Error")
 trace_real_weights = go.Scatter(
 x=np.arange(1, trees_real_num, 1),
y=ada_tree_real.estimator_weights_,
name="SAMME.R: Weights", 
)
 trace_discrete_weights = go.Scatter(
 x=np.arange(1, trees_real_num, 1),
y=ada_tree_discrete.estimator_weights_,
name="SAMME: Weights", 
)
 fig.append_trace(trace_real_weights, 3, 1)
 fig.append_trace(trace_discrete_weights, 3, 1)
 fig["layout"]["yaxis3"].update(title="Weights")
 fig["layout"].update(title="Adaboost Discrete and Real Algorithm Comparison")
 iplot(fig )
train = pd.read_csv(RAW_DATA / "train.csv" )<load_from_csv>
poverty_map = {
1:"extreme poverty",
2 : "moderate poverty" ,
3 : "vulnerable households",
4 : "non vulnerable households"
}
if not TEST_AUDIO_DIR.exists() :
TEST_AUDIO_DIR = INPUT_ROOT / "birdcall-check" / "test_audio"
test = pd.read_csv(INPUT_ROOT / "birdcall-check" / "test.csv")
 else:
test = pd.read_csv(RAW_DATA / "test.csv" )<save_to_csv>
raw_train_df = pd.read_csv(prefix + "train.csv" )
sub = pd.read_csv(".. /input/birdsong-recognition/sample_submission.csv")
 sub.to_csv("submission.csv", index=False )<init_hyperparams>
raw_predict_df = pd.read_csv(prefix + "test.csv" )
TARGET_SR = 32000
model_config = {
"base_model_name": "resnest50_fast_1s1x64d",
"pretrained": False,
"num_classes": 264,
"trained_weights": ".. /input/myweigh/best_model.pth"
}
melspectrogram_parameters = {
"n_mels": 128,
"fmin": 20,
"fmax": 16000
}<define_variables>
example_predict_df = pd.read_csv(prefix + "sample_submission.csv" )
BIRD_CODE = {
'aldfly': 0, 'ameavo': 1, 'amebit': 2, 'amecro': 3, 'amegfi': 4,
'amekes': 5, 'amepip': 6, 'amered': 7, 'amerob': 8, 'amewig': 9,
'amewoo': 10, 'amtspa': 11, 'annhum': 12, 'astfly': 13, 'baisan': 14,
'baleag': 15, 'balori': 16, 'banswa': 17, 'barswa': 18, 'bawwar': 19,
'belkin1': 20, 'belspa2': 21, 'bewwre': 22, 'bkbcuc': 23, 'bkbmag1': 24,
'bkbwar': 25, 'bkcchi': 26, 'bkchum': 27, 'bkhgro': 28, 'bkpwar': 29,
'bktspa': 30, 'blkpho': 31, 'blugrb1': 32, 'blujay': 33, 'bnhcow': 34,
'boboli': 35, 'bongul': 36, 'brdowl': 37, 'brebla': 38, 'brespa': 39,
'brncre': 40, 'brnthr': 41, 'brthum': 42, 'brwhaw': 43, 'btbwar': 44,
'btnwar': 45, 'btywar': 46, 'buffle': 47, 'buggna': 48, 'buhvir': 49,
'bulori': 50, 'bushti': 51, 'buwtea': 52, 'buwwar': 53, 'cacwre': 54,
'calgul': 55, 'calqua': 56, 'camwar': 57, 'cangoo': 58, 'canwar': 59,
'canwre': 60, 'carwre': 61, 'casfin': 62, 'caster1': 63, 'casvir': 64,
'cedwax': 65, 'chispa': 66, 'chiswi': 67, 'chswar': 68, 'chukar': 69,
'clanut': 70, 'cliswa': 71, 'comgol': 72, 'comgra': 73, 'comloo': 74,
'commer': 75, 'comnig': 76, 'comrav': 77, 'comred': 78, 'comter': 79,
'comyel': 80, 'coohaw': 81, 'coshum': 82, 'cowscj1': 83, 'daejun': 84,
'doccor': 85, 'dowwoo': 86, 'dusfly': 87, 'eargre': 88, 'easblu': 89,
'easkin': 90, 'easmea': 91, 'easpho': 92, 'eastow': 93, 'eawpew': 94,
'eucdov': 95, 'eursta': 96, 'evegro': 97, 'fiespa': 98, 'fiscro': 99,
'foxspa': 100, 'gadwal': 101, 'gcrfin': 102, 'gnttow': 103, 'gnwtea': 104,
'gockin': 105, 'gocspa': 106, 'goleag': 107, 'grbher3': 108, 'grcfly': 109,
'greegr': 110, 'greroa': 111, 'greyel': 112, 'grhowl': 113, 'grnher': 114,
'grtgra': 115, 'grycat': 116, 'gryfly': 117, 'haiwoo': 118, 'hamfly': 119,
'hergul': 120, 'herthr': 121, 'hoomer': 122, 'hoowar': 123, 'horgre': 124,
'horlar': 125, 'houfin': 126, 'houspa': 127, 'houwre': 128, 'indbun': 129,
'juntit1': 130, 'killde': 131, 'labwoo': 132, 'larspa': 133, 'lazbun': 134,
'leabit': 135, 'leafly': 136, 'leasan': 137, 'lecthr': 138, 'lesgol': 139,
'lesnig': 140, 'lesyel': 141, 'lewwoo': 142, 'linspa': 143, 'lobcur': 144,
'lobdow': 145, 'logshr': 146, 'lotduc': 147, 'louwat': 148, 'macwar': 149,
'magwar': 150, 'mallar3': 151, 'marwre': 152, 'merlin': 153, 'moublu': 154,
'mouchi': 155, 'moudov': 156, 'norcar': 157, 'norfli': 158, 'norhar2': 159,
'normoc': 160, 'norpar': 161, 'norpin': 162, 'norsho': 163, 'norwat': 164,
'nrwswa': 165, 'nutwoo': 166, 'olsfly': 167, 'orcwar': 168, 'osprey': 169,
'ovenbi1': 170, 'palwar': 171, 'pasfly': 172, 'pecsan': 173, 'perfal': 174,
'phaino': 175, 'pibgre': 176, 'pilwoo': 177, 'pingro': 178, 'pinjay': 179,
'pinsis': 180, 'pinwar': 181, 'plsvir': 182, 'prawar': 183, 'purfin': 184,
'pygnut': 185, 'rebmer': 186, 'rebnut': 187, 'rebsap': 188, 'rebwoo': 189,
'redcro': 190, 'redhea': 191, 'reevir1': 192, 'renpha': 193, 'reshaw': 194,
'rethaw': 195, 'rewbla': 196, 'ribgul': 197, 'rinduc': 198, 'robgro': 199,
'rocpig': 200, 'rocwre': 201, 'rthhum': 202, 'ruckin': 203, 'rudduc': 204,
'rufgro': 205, 'rufhum': 206, 'rusbla': 207, 'sagspa1': 208, 'sagthr': 209,
'savspa': 210, 'saypho': 211, 'scatan': 212, 'scoori': 213, 'semplo': 214,
'semsan': 215, 'sheowl': 216, 'shshaw': 217, 'snobun': 218, 'snogoo': 219,
'solsan': 220, 'sonspa': 221, 'sora': 222, 'sposan': 223, 'spotow': 224,
'stejay': 225, 'swahaw': 226, 'swaspa': 227, 'swathr': 228, 'treswa': 229,
'truswa': 230, 'tuftit': 231, 'tunswa': 232, 'veery': 233, 'vesspa': 234,
'vigswa': 235, 'warvir': 236, 'wesblu': 237, 'wesgre': 238, 'weskin': 239,
'wesmea': 240, 'wessan': 241, 'westan': 242, 'wewpew': 243, 'whbnut': 244,
'whcspa': 245, 'whfibi': 246, 'whtspa': 247, 'whtswi': 248, 'wilfly': 249,
'wilsni1': 250, 'wiltur': 251, 'winwre3': 252, 'wlswar': 253, 'wooduc': 254,
'wooscj2': 255, 'woothr': 256, 'y00475': 257, 'yebfly': 258, 'yebsap': 259,
'yehbla': 260, 'yelwar': 261, 'yerwar': 262, 'yetvir': 263
}
INV_BIRD_CODE = {v: k for k, v in BIRD_CODE.items() }<define_search_model>
raw_predict_df = pd.concat([raw_predict_df, pd.Series(np.nan, name='Target')], axis=1 )
class SplAtConv2d(Module):

 def __init__(self, in_channels, channels, kernel_size, stride=(1, 1), padding=(0, 0),
dilation=(1, 1), groups=1, bias=True,
radix=2, reduction_factor=4,
rectify=False, rectify_avg=False, norm_layer=None,
dropblock_prob=0.0, **kwargs):
super(SplAtConv2d, self ).__init__()
padding = _pair(padding)
 self.rectify = rectify and(padding[0] > 0 or padding[1] > 0)
 self.rectify_avg = rectify_avg
inter_channels = max(in_channels*radix//reduction_factor, 32)
 self.radix = radix
self.cardinality = groups
self.channels = channels
self.dropblock_prob = dropblock_prob
if self.rectify:
self.conv = RFConv2d(in_channels, channels*radix, kernel_size, stride, padding, dilation,
groups=groups*radix, bias=bias, average_mode=rectify_avg, **kwargs)
 else:
self.conv = Conv2d(in_channels, channels*radix, kernel_size, stride, padding, dilation,
groups=groups*radix, bias=bias, **kwargs)
 self.use_bn = norm_layer is not None
if self.use_bn:
self.bn0 = norm_layer(channels*radix)
 self.relu = ReLU(inplace=True)
 self.fc1 = Conv2d(channels, inter_channels, 1, groups=self.cardinality)
 if self.use_bn:
self.bn1 = norm_layer(inter_channels)
 self.fc2 = Conv2d(inter_channels, channels*radix, 1, groups=self.cardinality)
 if dropblock_prob > 0.0:
self.dropblock = DropBlock2D(dropblock_prob, 3)
 self.rsoftmax = rSoftMax(radix, groups)
 def forward(self, x):
x = self.conv(x)
 if self.use_bn:
x = self.bn0(x)
 if self.dropblock_prob > 0.0:
x = self.dropblock(x)
 x = self.relu(x)
 batch, rchannel = x.shape[:2]
if self.radix > 1:
if torch.__version__ < '1.5':
splited = torch.split(x, int(rchannel//self.radix), dim=1)
 else:
splited = torch.split(x, rchannel//self.radix, dim=1)
 gap = sum(splited)
 else:
gap = x
gap = F.adaptive_avg_pool2d(gap, 1)
 gap = self.fc1(gap)
 if self.use_bn:
gap = self.bn1(gap)
 gap = self.relu(gap)
 atten = self.fc2(gap)
 atten = self.rsoftmax(atten ).view(batch, -1, 1, 1)
 if self.radix > 1:
if torch.__version__ < '1.5':
attens = torch.split(atten, int(rchannel//self.radix), dim=1)
 else:
attens = torch.split(atten, rchannel//self.radix, dim=1)
 out = sum([att*split for(att, split)in zip(attens, splited)])
 else:
out = atten * x
return out.contiguous()
class rSoftMax(nn.Module):
def __init__(self, radix, cardinality):
super().__init__()
self.radix = radix
self.cardinality = cardinality
def forward(self, x):
batch = x.size(0)
 if self.radix > 1:
x = x.view(batch, self.cardinality, self.radix, -1 ).transpose(1, 2)
 x = F.softmax(x, dim=1)
 x = x.reshape(batch, -1)
 else:
x = torch.sigmoid(x)
 return x<import_modules>
raw_all_df = pd.concat([raw_train_df, raw_predict_df], ignore_index=True )
class DropBlock2D(object):
def __init__(self, *args, **kwargs):
raise NotImplementedError
class GlobalAvgPool2d(nn.Module):
def __init__(self):

 super(GlobalAvgPool2d, self ).__init__()
def forward(self, inputs):
return nn.functional.adaptive_avg_pool2d(inputs, 1 ).view(inputs.size(0), -1)
 class Bottleneck(nn.Module):

 expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None,
radix=1, cardinality=1, bottleneck_width=64,
avd=False, avd_first=False, dilation=1, is_first=False,
rectified_conv=False, rectify_avg=False,
norm_layer=None, dropblock_prob=0.0, last_gamma=False):
super(Bottleneck, self ).__init__()
group_width = int(planes *(bottleneck_width / 64.))* cardinality
self.conv1 = nn.Conv2d(inplanes, group_width, kernel_size=1, bias=False)
 self.bn1 = norm_layer(group_width)
 self.dropblock_prob = dropblock_prob
self.radix = radix
self.avd = avd and(stride > 1 or is_first)
 self.avd_first = avd_first
if self.avd:
self.avd_layer = nn.AvgPool2d(3, stride, padding=1)
 stride = 1
if dropblock_prob > 0.0:
self.dropblock1 = DropBlock2D(dropblock_prob, 3)
 if radix == 1:
self.dropblock2 = DropBlock2D(dropblock_prob, 3)
 self.dropblock3 = DropBlock2D(dropblock_prob, 3)
 if radix >= 1:
self.conv2 = SplAtConv2d(
 group_width, group_width, kernel_size=3,
stride=stride, padding=dilation,
dilation=dilation, groups=cardinality, bias=False,
radix=radix, rectify=rectified_conv,
rectify_avg=rectify_avg,
norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 elif rectified_conv:
self.conv2 = RFConv2d(
 group_width, group_width, kernel_size=3, stride=stride,
padding=dilation, dilation=dilation,
groups=cardinality, bias=False,
average_mode=rectify_avg)
 self.bn2 = norm_layer(group_width)
 else:
self.conv2 = nn.Conv2d(
 group_width, group_width, kernel_size=3, stride=stride,
padding=dilation, dilation=dilation,
groups=cardinality, bias=False)
 self.bn2 = norm_layer(group_width)
 self.conv3 = nn.Conv2d(
 group_width, planes * 4, kernel_size=1, bias=False)
 self.bn3 = norm_layer(planes*4)
 if last_gamma:
zeros_(self.bn3.weight)
 self.relu = nn.ReLU(inplace=True)
 self.downsample = downsample
self.dilation = dilation
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
 out = self.bn1(out)
 if self.dropblock_prob > 0.0:
out = self.dropblock1(out)
 out = self.relu(out)
 if self.avd and self.avd_first:
out = self.avd_layer(out)
 out = self.conv2(out)
 if self.radix == 0:
out = self.bn2(out)
 if self.dropblock_prob > 0.0:
out = self.dropblock2(out)
 out = self.relu(out)
 if self.avd and not self.avd_first:
out = self.avd_layer(out)
 out = self.conv3(out)
 out = self.bn3(out)
 if self.dropblock_prob > 0.0:
out = self.dropblock3(out)
 if self.downsample is not None:
residual = self.downsample(x)
 out += residual
out = self.relu(out)
 return out
class ResNet(nn.Module):

 def __init__(self, block, layers, radix=1, groups=1, bottleneck_width=64,
num_classes=1000, dilated=False, dilation=1,
deep_stem=False, stem_width=64, avg_down=False,
rectified_conv=False, rectify_avg=False,
avd=False, avd_first=False,
final_drop=0.0, dropblock_prob=0,
last_gamma=False, norm_layer=nn.BatchNorm2d):
self.cardinality = groups
self.bottleneck_width = bottleneck_width
self.inplanes = stem_width*2 if deep_stem else 64
self.avg_down = avg_down
self.last_gamma = last_gamma
self.radix = radix
self.avd = avd
self.avd_first = avd_first
super(ResNet, self ).__init__()
self.rectified_conv = rectified_conv
self.rectify_avg = rectify_avg
if rectified_conv:
conv_layer = RFConv2d
else:
conv_layer = nn.Conv2d
conv_kwargs = {'average_mode': rectify_avg} if rectified_conv else {}
if deep_stem:
self.conv1 = nn.Sequential(
 conv_layer(3, stem_width, kernel_size=3, stride=2, padding=1, bias=False, **conv_kwargs),
norm_layer(stem_width),
nn.ReLU(inplace=True),
conv_layer(stem_width, stem_width, kernel_size=3, stride=1, padding=1, bias=False, **conv_kwargs),
norm_layer(stem_width),
nn.ReLU(inplace=True),
conv_layer(stem_width, stem_width*2, kernel_size=3, stride=1, padding=1, bias=False, **conv_kwargs), 
)
 else:
self.conv1 = conv_layer(3, 64, kernel_size=7, stride=2, padding=3,
bias=False, **conv_kwargs)
 self.bn1 = norm_layer(self.inplanes)
 self.relu = nn.ReLU(inplace=True)
 self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
 self.layer1 = self._make_layer(block, 64, layers[0], norm_layer=norm_layer, is_first=False)
 self.layer2 = self._make_layer(block, 128, layers[1], stride=2, norm_layer=norm_layer)
 if dilated or dilation == 4:
self.layer3 = self._make_layer(block, 256, layers[2], stride=1,
dilation=2, norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 self.layer4 = self._make_layer(block, 512, layers[3], stride=1,
dilation=4, norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 elif dilation==2:
self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
dilation=1, norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 self.layer4 = self._make_layer(block, 512, layers[3], stride=1,
dilation=2, norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 else:
self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 self.layer4 = self._make_layer(block, 512, layers[3], stride=2,
norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 self.avgpool = GlobalAvgPool2d()
self.drop = nn.Dropout(final_drop)if final_drop > 0.0 else None
self.fc = nn.Linear(512 * block.expansion, num_classes)
 for m in self.modules() :
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2./ n))
elif isinstance(m, norm_layer):
m.weight.data.fill_(1)
 m.bias.data.zero_()
def _make_layer(self, block, planes, blocks, stride=1, dilation=1, norm_layer=None,
dropblock_prob=0.0, is_first=True):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
down_layers = []
if self.avg_down:
if dilation == 1:
down_layers.append(nn.AvgPool2d(kernel_size=stride, stride=stride,
ceil_mode=True, count_include_pad=False))
else:
down_layers.append(nn.AvgPool2d(kernel_size=1, stride=1,
ceil_mode=True, count_include_pad=False))
down_layers.append(nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=1, bias=False))
else:
down_layers.append(nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False))
down_layers.append(norm_layer(planes * block.expansion))
downsample = nn.Sequential(*down_layers)
 layers = []
if dilation == 1 or dilation == 2:
layers.append(block(self.inplanes, planes, stride, downsample=downsample,
radix=self.radix, cardinality=self.cardinality,
bottleneck_width=self.bottleneck_width,
avd=self.avd, avd_first=self.avd_first,
dilation=1, is_first=is_first, rectified_conv=self.rectified_conv,
rectify_avg=self.rectify_avg,
norm_layer=norm_layer, dropblock_prob=dropblock_prob,
last_gamma=self.last_gamma))
elif dilation == 4:
layers.append(block(self.inplanes, planes, stride, downsample=downsample,
radix=self.radix, cardinality=self.cardinality,
bottleneck_width=self.bottleneck_width,
avd=self.avd, avd_first=self.avd_first,
dilation=2, is_first=is_first, rectified_conv=self.rectified_conv,
rectify_avg=self.rectify_avg,
norm_layer=norm_layer, dropblock_prob=dropblock_prob,
last_gamma=self.last_gamma))
else:
raise RuntimeError("=> unknown dilation size: {}".format(dilation))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes,
radix=self.radix, cardinality=self.cardinality,
bottleneck_width=self.bottleneck_width,
avd=self.avd, avd_first=self.avd_first,
dilation=dilation, rectified_conv=self.rectified_conv,
rectify_avg=self.rectify_avg,
norm_layer=norm_layer, dropblock_prob=dropblock_prob,
last_gamma=self.last_gamma))
return nn.Sequential(*layers)
 def forward(self, x):
x = self.conv1(x)
 x = self.bn1(x)
 x = self.relu(x)
 x = self.maxpool(x)
 x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x)
 x = self.layer4(x)
 x = self.avgpool(x)
 x = torch.flatten(x, 1)
 if self.drop:
x = self.drop(x)
 x = self.fc(x)
 return x<choose_model_class>
raw_all_df["edjefe"] = raw_all_df["edjefe"].apply(lambda x: 1 if x == "yes" else x ).apply(lambda x: 0 if x == "no" else x)
 raw_all_df["edjefa"] = raw_all_df["edjefa"].apply(lambda x: 1 if x == "yes" else x ).apply(lambda x: 0 if x == "no" else x)
 raw_all_df["dependency"] = raw_all_df["dependency"].apply(lambda x: 1 if x == "yes" else x ).apply(lambda x: 0 if x == "no" else x)
 raw_all_df["edjefe"] = pd.to_numeric(raw_all_df["edjefe"])
 raw_all_df["edjefa"] = pd.to_numeric(raw_all_df["edjefa"])
 raw_all_df["dependency"] = pd.to_numeric(raw_all_df["dependency"] )
def get_model(args: tp.Dict):
model = ResNet(
 Bottleneck, [3, 4, 6, 3],
radix=1, groups=1, bottleneck_width=64,
deep_stem=True, stem_width=32, avg_down=True,
avd=True, avd_first=True)
 del model.fc
model.fc = nn.Sequential(
 nn.Linear(2048, 1024), nn.ReLU() , nn.Dropout(p=0.2),
nn.Linear(1024, 1024), nn.ReLU() , nn.Dropout(p=0.2),
nn.Linear(1024, args["num_classes"]))
state_dict = torch.load(args["trained_weights"],map_location=torch.device('cpu'))
model.load_state_dict(state_dict)
 device = torch.device("cpu")
 model.to(device)
 model.eval()
return model<create_dataframe>
raw_all_df.isnull().sum() [raw_all_df.isnull().sum() > 0]
def prediction_for_clip(test_df: pd.DataFrame,
clip: np.ndarray,
model: ResNet,
mel_params: dict,
threshold=0.5):
dataset = TestDataset(df=test_df,
clip=clip,
img_size=224,
melspectrogram_parameters=mel_params)
 loader = data.DataLoader(dataset, batch_size=1, shuffle=False)
 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 model.eval()
prediction_dict = {}
for image, row_id, site in progress_bar(loader):
site = site[0]
row_id = row_id[0]
if site in {"site_1", "site_2"}:
image = image.to(device)
 with torch.no_grad() :
prediction = F.sigmoid(model(image))
proba = prediction.detach().cpu().numpy().reshape(-1)
 events = proba >= threshold
labels = np.argwhere(events ).reshape(-1 ).tolist()
else:
image = image.squeeze(0)
 batch_size = 16
whole_size = image.size(0)
 if whole_size % batch_size == 0:
n_iter = whole_size // batch_size
else:
n_iter = whole_size // batch_size + 1
all_events = set()
for batch_i in range(n_iter):
batch = image[batch_i * batch_size:(batch_i + 1)* batch_size]
if batch.ndim == 3:
batch = batch.unsqueeze(0)
 batch = batch.to(device)
 with torch.no_grad() :
prediction = F.sigmoid(model(batch))
proba = prediction.detach().cpu().numpy()
events = proba >= threshold
for i in range(len(events)) :
event = events[i, :]
labels = np.argwhere(event ).reshape(-1 ).tolist()
for label in labels:
all_events.add(label)
 labels = list(all_events)
 if len(labels)== 0:
prediction_dict[row_id] = "nocall"
else:
labels_str_list = list(map(lambda x: INV_BIRD_CODE[x], labels))
label_string = " ".join(labels_str_list)
 prediction_dict[row_id] = label_string
return prediction_dict<categorify>
clean_all_df = raw_all_df.drop(["v2a1", "rez_esc"], axis=1)
 clean_all_df["v18q1"].fillna(0, inplace=True )
def prediction(test_df: pd.DataFrame,
test_audio: Path,
model_config: dict,
mel_params: dict,
target_sr: int,
threshold=0.5):
model = get_model(model_config)
 unique_audio_id = test_df.audio_id.unique()
warnings.filterwarnings("ignore")
 prediction_dfs = []
for audio_id in unique_audio_id:
with timer(f"Loading {audio_id}"):
clip, _ = librosa.load(test_audio /(audio_id + ".mp3"),
sr=target_sr,
mono=True,
res_type="kaiser_fast")
 test_df_for_audio_id = test_df.query(
 f"audio_id == '{audio_id}'" ).reset_index(drop=True)
 with timer(f"Prediction on {audio_id}"):
prediction_dict = prediction_for_clip(test_df_for_audio_id,
clip=clip,
model=model,
mel_params=mel_params,
threshold=threshold)
 row_id = list(prediction_dict.keys())
 birds = list(prediction_dict.values())
 prediction_df = pd.DataFrame({
"row_id": row_id,
"birds": birds
})
 prediction_dfs.append(prediction_df)
 prediction_df = pd.concat(prediction_dfs, axis=0, sort=False ).reset_index(drop=True)
 return prediction_df<save_to_csv>
clean_all_df.isnull().sum() [clean_all_df.isnull().sum() > 0]
submission = prediction(test_df=test,
test_audio=TEST_AUDIO_DIR,
model_config=model_config,
mel_params=melspectrogram_parameters,
target_sr=TARGET_SR,
threshold=0.8)
 submission.to_csv("submission.csv", index=False )<set_options>
id_col = ['Id', 'idhogar', 'Target']
%matplotlib inline 
<import_modules>
ind_bool_col = ['v18q', 'dis', 'male', 'female', 'estadocivil1', 'estadocivil2', 'estadocivil3',
'estadocivil4', 'estadocivil5', 'estadocivil6', 'estadocivil7',
'parentesco1', 'parentesco2', 'parentesco3', 'parentesco4', 'parentesco5',
'parentesco6', 'parentesco7', 'parentesco8', 'parentesco9', 'parentesco10',
'parentesco11', 'parentesco12', 'instlevel1', 'instlevel2', 'instlevel3',
'instlevel4', 'instlevel5', 'instlevel6', 'instlevel7', 'instlevel8',
'instlevel9', 'mobilephone',]
ind_ordered_col = ['escolari', 'age']
import os<load_from_csv>
hh_bool_col = ['hacdor', 'hacapo', 'v14a', 'refrig', 'paredblolad', 'paredzocalo',
'paredpreb','pisocemento', 'pareddes', 'paredmad',
'paredzinc', 'paredfibras', 'paredother', 'pisomoscer', 'pisoother',
'pisonatur', 'pisonotiene', 'pisomadera',
'techozinc', 'techoentrepiso', 'techocane', 'techootro', 'cielorazo',
'abastaguadentro', 'abastaguafuera', 'abastaguano',
'public', 'planpri', 'noelec', 'coopele', 'sanitario1',
'sanitario2', 'sanitario3', 'sanitario5', 'sanitario6',
'energcocinar1', 'energcocinar2', 'energcocinar3', 'energcocinar4',
'elimbasu1', 'elimbasu2', 'elimbasu3', 'elimbasu4',
'elimbasu5', 'elimbasu6', 'epared1', 'epared2', 'epared3',
'etecho1', 'etecho2', 'etecho3', 'eviv1', 'eviv2', 'eviv3',
'tipovivi1', 'tipovivi2', 'tipovivi3', 'tipovivi4', 'tipovivi5',
'computer', 'television', 'lugar1', 'lugar2', 'lugar3',
'lugar4', 'lugar5', 'lugar6', 'area2' , 'area1']
hh_ordered_col = [ 'rooms', 'r4h1', 'r4h2', 'r4h3', 'r4m1','r4m2','r4m3', 'r4t1', 'r4t2',
'v18q1', 'hogar_nin', 'tamhog','tamviv','hhsize', 'r4t3', 'hogar_total',
'hogar_adul','hogar_mayor', 'bedrooms', 'qmobilephone']
hh_cont_col = ['dependency', 'edjefe', 'edjefa', 'meaneduc', 'overcrowding']
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 submission = pd.read_csv('.. /input/sample_submission.csv' )<import_modules>
sqr_col = ['SQBescolari', 'SQBage', 'SQBhogar_total', 'SQBedjefe',
'SQBhogar_nin', 'SQBovercrowding', 'SQBdependency', 'SQBmeaned', 'agesq']
import datetime as dt<feature_engineering>
clean_all_df.drop(sqr_col, axis=1, inplace=True )
train['pickup_datetime']= pd.to_datetime(train['pickup_datetime'])
 train['month'] = train['pickup_datetime'].dt.month
train['weekday'] = train['pickup_datetime'].dt.weekday
train['hours'] = train['pickup_datetime'].dt.hour
test['pickup_datetime']= pd.to_datetime(test['pickup_datetime'])
 test['month'] = test['pickup_datetime'].dt.month
test['weekday'] = test['pickup_datetime'].dt.weekday
test['hours'] = test['pickup_datetime'].dt.hour 
<filter>
cols_list = id_col + ind_bool_col + ind_ordered_col + hh_bool_col + hh_ordered_col + hh_cont_col
train = train[(train.trip_duration < 5000)]<filter>
clean_all_df = clean_all_df[cols_list]
train = train[(train.pickup_longitude > -100)]
train = train[(train.pickup_latitude < 50)] 
<filter>
clean_all_df.isnull().sum() [clean_all_df.isnull().sum() > 0]
train = train[(train.passenger_count > 0)]<prepare_x_and_y>
temp_all_df = pd.concat([clean_all_df, pd.Series(np.argmax(clean_all_df.loc[:, "instlevel1" : "instlevel9"].values, axis=1), name="eduindex")], axis=1)
 edu_median_dict = temp_all_df.groupby("eduindex")["meaneduc"].median().to_dict()
X_train = train.loc[:,['month','weekday','hours','passenger_count','pickup_longitude','pickup_latitude','dropoff_longitude','dropoff_latitude']]
X_test = test.loc[:,['month','weekday','hours','passenger_count','pickup_longitude','pickup_latitude','dropoff_longitude','dropoff_latitude']]
y_train = train['trip_duration']
X_train.shape, y_train.shape 
<import_modules>
def fill_na(x):
if pd.isnull(x["meaneduc"]):
x.loc["meaneduc"] = edu_median_dict[x.loc["eduindex"]]
return x
import xgboost as xgb
from xgboost.sklearn import XGBRegressor<train_model>
temp_all_df = temp_all_df.apply(fill_na, axis=1 )
xgb_model = xgb.XGBRegressor(max_depth=15, learning_rate=0.1, n_estimators=700, silent=True, objective='reg:linear', booster='gbtree', n_jobs=1, nthread=None, gamma=0, min_child_weight=1, max_delta_step=0, subsample=1, colsample_bytree=1, colsample_bylevel=1, reg_alpha=0, reg_lambda=1, scale_pos_weight=1, base_score=0.5, random_state=0, seed=None, missing=None, importance_type='gain',)
 xgb_model.fit(X_train, y_train)
<predict_on_test>
clean_all_df = temp_all_df.drop("eduindex", axis=1 )
y_test_pred = xgb_model.predict(X_test )<prepare_output>
heads_all_df = clean_all_df[clean_all_df.parentesco1 == 1]
submission['trip_duration'] = y_test_pred
submission.head()<save_to_csv>
rm_cols = ["tamhog", "r4t3", "hhsize", "hogar_total", "male", "area1"]
submission.to_csv("submition.csv", index=False )<import_modules>
clean_all_df.drop(rm_cols, axis=1, inplace=True )
import os
import math
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
from datetime import datetime<set_options>
for col in rm_cols:
for _list in [ind_bool_col, ind_ordered_col, hh_bool_col, hh_cont_col, hh_ordered_col]:
if col in _list:
_list.remove(col )
%matplotlib inline<define_variables>
roof_list = ["techozinc", "techoentrepiso", "techocane", "techootro", "cielorazo"]
clean_all_df["cielorazo"] = clean_all_df[roof_list[:-1]].sum(axis=1 ).map({1:0, 0:1} )
FILEPATH = os.path.join(".. ", "input")
 TRAINPATH = os.path.join(FILEPATH, "train.csv")
 TESTPATH = os.path.join(FILEPATH, "test.csv" )<load_from_csv>
no_head_household_ids = clean_all_df.groupby("idhogar" ).sum() [clean_all_df.groupby("idhogar" ).sum() ["parentesco1"] == 0].index
df = pd.read_csv(TRAINPATH, index_col=0)
 df.head()<load_from_csv>
clean_drop_no_head_df = clean_all_df.copy()
for x in no_head_household_ids:
clean_drop_no_head_df.drop(clean_drop_no_head_df.loc[clean_drop_no_head_df.idhogar == x, :].index, axis=0, inplace=True )
df_test = pd.read_csv(TESTPATH)
 df_test.head()<filter>
clean_heads_df = clean_drop_no_head_df.loc[clean_drop_no_head_df.parentesco1 == 1]
heads_dict = clean_heads_df[["idhogar", "Target"]].set_index("idhogar" ).to_dict() ["Target"]
df = df[df.passenger_count != 0]<count_duplicates>
def synchronise_diff_target(x):
x.Target = heads_dict[x.idhogar]
return x
clean_drop_no_head_df = clean_drop_no_head_df.apply(synchronise_diff_target, axis=1 )
df.duplicated().sum()<count_duplicates>
aggregation_list = ["max", "sum", "min", "mean", "std"]
df = df.drop_duplicates()
df.duplicated().sum()<count_missing_values>
hh_agg_features = clean_drop_no_head_df[["idhogar"] + ind_bool_col + ind_ordered_col].groupby("idhogar" ).agg(aggregation_list)
 hh_agg_features.columns = ["_".join(x)for x in hh_agg_features.columns.ravel() ]
hh_agg_features = hh_agg_features.fillna(0 )
df.isna().sum()<filter>
clean_heads_df = clean_drop_no_head_df[clean_drop_no_head_df.parentesco1 == 1].loc[:, id_col + hh_bool_col + hh_cont_col + hh_ordered_col].set_index("idhogar")
 clean_heads_df = pd.concat([clean_heads_df, hh_agg_features], axis=1, sort=False ).reset_index().rename(columns={"index":'idhogar'} )
df = df[(df['trip_duration'] > 60)&(df['trip_duration'] < 3600 * 6)]<data_type_conversions>
X = clean_heads_df[clean_heads_df.Target.notna() ].reset_index(drop=True)
 y = X.Target
X_predict = clean_heads_df[clean_heads_df.Target.isna() ].reset_index(drop=True )
df['store_and_fwd_flag'] = df['store_and_fwd_flag'].astype('category' ).cat.codes
df_test['store_and_fwd_flag'] = df_test['store_and_fwd_flag'].astype('category' ).cat.codes<import_modules>
all_predict_df = clean_all_df[clean_all_df.Target.isnull() ]
from sklearn.decomposition import PCA<categorify>
from sklearn.preprocessing import MinMaxScaler
coords = np.vstack(( df[['pickup_latitude', 'pickup_longitude']].values,
df[['dropoff_latitude', 'dropoff_longitude']].values,
df_test[['pickup_latitude', 'pickup_longitude']].values,
df_test[['dropoff_latitude', 'dropoff_longitude']].values))
pca = PCA().fit(coords)
 df['pickup_pca0'] = pca.transform(df[['pickup_latitude', 'pickup_longitude']])[:, 0]
df['pickup_pca1'] = pca.transform(df[['pickup_latitude', 'pickup_longitude']])[:, 1]
df['dropoff_pca0'] = pca.transform(df[['dropoff_latitude', 'dropoff_longitude']])[:, 0]
df['dropoff_pca1'] = pca.transform(df[['dropoff_latitude', 'dropoff_longitude']])[:, 1]
df_test['pickup_pca0'] = pca.transform(df_test[['pickup_latitude', 'pickup_longitude']])[:, 0]
df_test['pickup_pca1'] = pca.transform(df_test[['pickup_latitude', 'pickup_longitude']])[:, 1]
df_test['dropoff_pca0'] = pca.transform(df_test[['dropoff_latitude', 'dropoff_longitude']])[:, 0]
df_test['dropoff_pca1'] = pca.transform(df_test[['dropoff_latitude', 'dropoff_longitude']])[:, 1]<data_type_conversions>
scaler = MinMaxScaler()
X.iloc[:, 3:], X_predict.iloc[:, 3:] = scaler.fit_transform(X.iloc[:, 3:]), scaler.fit_transform(X_predict.iloc[:, 3:] )
df['pickup_datetime'] = pd.to_datetime(df['pickup_datetime'])
 df['dropoff_datetime'] = pd.to_datetime(df['dropoff_datetime'])
 df_test['pickup_datetime'] = pd.to_datetime(df_test['pickup_datetime'] )<feature_engineering>
from sklearn.decomposition import PCA
from sklearn.feature_selection import SelectKBest, chi2
df['hour'] = df.pickup_datetime.dt.hour
df['day'] = df.pickup_datetime.dt.dayofweek
df['month'] = df.pickup_datetime.dt.month
df_test['hour'] = df_test.pickup_datetime.dt.hour
df_test['day'] = df_test.pickup_datetime.dt.dayofweek
df_test['month'] = df_test.pickup_datetime.dt.month<feature_engineering>
numeric_train_df = X.iloc[:, 3:]
label_series = X.Target.reset_index(drop=True )
df['distance2'] = np.sqrt(( df['pickup_pca0']-df['dropoff_pca0'])**2
+(df['pickup_pca1']-df['dropoff_pca1'])**2)
 df_test['distance2'] = np.sqrt(( df_test['pickup_pca0']-df_test['dropoff_pca0'])**2
+(df_test['pickup_pca1']-df_test['dropoff_pca1'])**2 )<feature_engineering>
pca = PCA(n_components=3, whiten=True, svd_solver='auto', random_state=166446054 )
df['log_trip_duration'] = np.log(df['trip_duration'] )<define_variables>
pca_train_array = pca.fit_transform(numeric_train_df )
NUM_VARS = ['pickup_pca0', 'pickup_pca1', 'dropoff_pca0', 'dropoff_pca1', 'month', 'hour', 'day', 'distance2']
TARGET = 'log_trip_duration'<define_variables>
pca_train_df = pd.DataFrame(pca_train_array)
 pca_train_df = pd.concat([pca_train_df, label_series], axis=1 )
num_features = NUM_VARS<prepare_x_and_y>
from sklearn.cluster import KMeans
from tempfile import mkdtemp
from shutil import rmtree
from sklearn.pipeline import Pipeline
X_train = df.loc[:, num_features]
y_train = df[TARGET]
X_test = df_test.loc[:, num_features]
X_train.shape, y_train.shape, X_test.shape<import_modules>
result_dict = search_optimised_k([4, 8, 16, 32, 64, 128, 259], random_state=16446054, X=X.iloc[:, 3:] )
from sklearn.ensemble import RandomForestRegressor<import_modules>
km_predict = KMeans(n_jobs=-1, n_clusters=4, random_state=16446054 ).fit_predict(pca_train_df)
 km_X = pd.concat([pd.Series(km_predict, name="k_means_predict" ).map({0:"centroid1",
1:"centroid2",
2: "centroid3",
3: "centroid4",
4: "centroid5",
5: "centroid6",
6: "centroid7",
7: "centroid8"
}), X.iloc[:, 2:]], axis=1)
 plot_hist(km_X, "k_means_predict", x_label="KMeans Prediction", title="K Means Clustering Prediction" )
from sklearn.ensemble import RandomForestRegressor<train_model>
from sklearn.model_selection import train_test_split
m = RandomForestRegressor(n_estimators=16, min_samples_leaf=10, min_samples_split=15, max_features='auto', max_depth=80, bootstrap=True)
 m.fit(X_train, y_train )<import_modules>
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score<compute_train_metric>
my_knn_agent = knn_agent()
cv_scores = cross_val_score(m, X_train, y_train, cv=5, scoring='neg_mean_squared_log_error')
 cv_scores<predict_on_test>
X_train, X_test, y_train, y_test = train_test_split(X.iloc[:, 3:], y, test_size=0.2, random_state=16446054, stratify=y )
y_test_pred = m.predict(X_test)
 y_test_pred[:5]<save_to_csv>
my_knn_agent.optimise_params(X_train=X_train, y_train=y_train, X_dev=X_test, y_dev=y_test, max_k_num=50 )
my_submission = pd.DataFrame({'id': df_test.id, 'trip_duration': np.exp(y_test_pred)})
 my_submission.to_csv('submission.csv', index=False )<install_modules>
uniform_f1, distance_f1 = my_knn_agent.evaluate(X_train=X_train, y_train=y_train, X_test=X_test, y_test=y_test)
 print("Unifrom test set f1 score: ", uniform_f1, " Distance test set f1 score: ", distance_f1 )
!pip install -q git+https://github.com/fastai/fastai2
!pip install -q git+https://github.com/fastai/fastcore<import_modules>
uniform_prediction, distance_prediction = my_knn_agent.predict(X_train=X_train, y_train=y_train, X_predict=X_predict.iloc[:, 3:] )
from fastai2.vision.all import *<define_variables>
from sklearn.model_selection import GridSearchCV
path = Path("/kaggle/input/plant-pathology-2020-fgvc7" )<load_from_csv>
from sklearn.model_selection import GridSearchCV
train_df = pd.read_csv(path/"train.csv")
 train_df.head()<filter>
cachedir = mkdtemp()
knn_pipe = Pipeline([('reduce_dim', PCA(random_state=16446054)) ,('classify', my_knn_agent.uniform_knn)], memory=cachedir )
train_df.iloc[0, 1:][train_df.iloc[0, 1:] == 1].index[0]<define_variables>
num_features_list = np.arange(10, 200, 20 )
BS = 8<prepare_x_and_y>
knn_param_grid = [
{
'reduce_dim': [PCA(random_state=16446054)],
'reduce_dim__n_components': num_features_list,
},
{
'reduce_dim': [SelectKBest(chi2)],
'reduce_dim__k': num_features_list,
}
]
reducer_labels = ['PCA', 'KBest(chi2)']
def get_data(size=224):
return DataBlock(blocks =(ImageBlock, CategoryBlock),
get_x=ColReader(0, pref=path/"images", suff=".jpg"),
get_y=lambda o:o.iloc[1:][o.iloc[1:] == 1].index[0],
splitter=RandomSplitter(seed=42),
item_tfms=Resize(size),
batch_tfms=aug_transforms(flip_vert=True),
).dataloaders(train_df, bs=BS )<define_search_space>
knn_grid = GridSearchCV(estimator=knn_pipe, cv=5, n_jobs=4, param_grid=knn_param_grid, scoring='f1_macro' )
dls = get_data(( 450, 800))<compute_train_metric>
_ = knn_grid.fit(X.iloc[:, 3:], y )
def roc_auc(preds, targs, labels=range(4)) :
targs = np.eye(4)[targs]
return np.mean([roc_auc_score(targs[:,i], preds[:,i])for i in labels])
 def healthy_roc_auc(*args):
return roc_auc(*args, labels=[0])
 def multiple_diseases_roc_auc(*args):
return roc_auc(*args, labels=[1])
 def rust_roc_auc(*args):
return roc_auc(*args, labels=[2])
 def scab_roc_auc(*args):
return roc_auc(*args, labels=[3] )<compute_train_metric>
knn_params_results = pd.DataFrame(knn_grid.cv_results_ )
metric = partial(AccumMetric, flatten=False)
 def get_learner(size=224):
dls = get_data(size)
 return cnn_learner(dls, resnet152, metrics=[
error_rate,
metric(healthy_roc_auc),
metric(multiple_diseases_roc_auc),
metric(rust_roc_auc),
metric(scab_roc_auc),
metric(roc_auc)],
).to_fp16()<choose_model_class>
knn_params_results = pd.concat([knn_params_results.param_reduce_dim__n_components.fillna(knn_params_results.param_reduce_dim__k ).rename("num_features"),
knn_params_results.drop(["param_reduce_dim__k", "param_reduce_dim__n_components"], axis=1)], axis=1 )
learn = get_learner(( 450, 800))<define_search_space>
knn_params_results.loc[:, "param_reduce_dim"] = knn_params_results.param_reduce_dim.apply(lambda x: "PCA" if str(x ).startswith("PCA")else "SelectKBest" ).rename("algorithm" )
lr = 3e-3<choose_model_class>
rmtree(cachedir )
m = "multiple_diseases_roc_auc"
d = 0.005
learn.fine_tune(50, lr, freeze_epochs=1, cbs=[EarlyStoppingCallback(monitor=m, min_delta=d, patience=10),
SaveModelCallback(monitor=m, min_delta=d),
ReduceLROnPlateau(monitor=m, min_delta=d, patience=4)] )<choose_model_class>
from sklearn.svm import SVC
from sklearn.utils.class_weight import compute_class_weight
interp = ClassificationInterpretation.from_learner(learn )<load_from_csv>
cachedir = mkdtemp()
svc_pipe = Pipeline([('reduce_dim', PCA(random_state=166446054)) ,('classify', SVC(random_state=16446054, class_weight="balanced")) ], memory=cachedir )
test_df = pd.read_csv(path/"test.csv")
 test_df.head()<train_model>
svc_param_grid = [
{
'reduce_dim__n_components': [10, 70, 140, 200],
'classify__kernel': ['rbf', 'poly'],
'classify__C': [0.1, 1, 10, 50],
}
]
tst_dl = learn.dls.test_dl(test_df )<predict_on_test>
svc_grid = GridSearchCV(svc_pipe, cv=5, n_jobs=4, param_grid=svc_param_grid, scoring='f1_macro', refit=True)
 _ = svc_grid.fit(X.iloc[:, 3:], y )
preds, _ = learn.get_preds(dl=tst_dl )<load_from_csv>
svc_params_results = pd.DataFrame(svc_grid.cv_results_ )
subm = pd.read_csv(path/"sample_submission.csv" )<prepare_output>
rmtree(cachedir )
subm.iloc[:, 1:] = preds<save_to_csv>
from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import StratifiedKFold
subm.to_csv("submission.csv", index=False )<load_from_csv>
ada_tree_real = AdaBoostClassifier(
 DecisionTreeClassifier(max_depth=2),
n_estimators=600,
learning_rate=1.5 
)
 ada_tree_discrete = AdaBoostClassifier(
 DecisionTreeClassifier(max_depth=2),
n_estimators=600,
learning_rate=1.5,
algorithm="SAMME"
)
pd.read_csv("submission.csv" )<import_modules>
ada_tree_real.fit(X_train, y_train)
 ada_tree_discrete.fit(X_train, y_train )
import numpy as np
import pandas as pd
import os 
<import_modules>
ada_report(ada_models=(ada_tree_real, ada_tree_discrete), X_train=X_train, y_train=y_train, X_test=X_test, y_test=y_test )
import matplotlib.pyplot as plt 
<load_from_csv>
cachedir = mkdtemp()
ada_pipe = Pipeline([('reduce_dim', PCA(random_state=166446054)) , 
('classify', AdaBoostClassifier(random_state=16446054)) ], memory=cachedir )
df = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/train.csv')
<load_from_csv>
ada_param_grid = [
{
'reduce_dim__n_components': [10, 70, 140, 259],
'classify__algorithm': ['SAMME.R', 'SAMME'],
'classify__learning_rate': [1],
'classify__n_estimators': [300],
'classify__base_estimator': [DecisionTreeClassifier(max_depth=2)]
}
]
test_df=pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/test.csv')
<load_pretrained>
ada_grid = GridSearchCV(ada_pipe, cv=5,
n_jobs=4, param_grid=ada_param_grid,
scoring='f1_macro', refit=True,)
 _ = ada_grid.fit(X.iloc[:, 3:], y )
ImageFile.LOAD_TRUNCATED_IMAGES = True 
<install_modules>
ada_param_results = pd.DataFrame(ada_grid.cv_results_ )
!pip install --upgrade efficientnet-pytorch<import_modules>
import lightgbm as lgb
from sklearn.model_selection import StratifiedKFold
from efficientnet_pytorch import EfficientNet<split>
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split 
<drop_column>
from sklearn import preprocessing
train_df=df<drop_column>
from sklearn.model_selection import RandomizedSearchCV
train_df.reset_index(drop=True,inplace=True)
 test_df.reset_index(drop=True,inplace=True )<normalization>
lgb_clf = lgb.LGBMClassifier(learning_rate=0.01, n_estimators=15000,
objective='multiclass', matric='logloss', boosting_type='dart',
num_leaves=700, max_depth=10,
class_weight='balanced', silent=True, n_jobs=-1,
colsample_bytree = 0.93, min_child_samples = 95, subsample = 0.96 )
class CustomDataset(Dataset):
def __init__(self,df,root_dir,transform=None,iftest=False):
self.df=df
self.root_dir=root_dir
self.transform=transform
self.iftest=iftest
def __len__(self):
return len(self.df)
 def __getitem__(self,idx):
if torch.is_tensor(idx):
idx=idx.tolist()
img_name=self.root_dir+self.df.iloc[idx,0]+'.jpg'
image= cv2.imread(img_name,cv2.IMREAD_COLOR)
 image= cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
 if self.transform:
image=self.transform(image=image)['image']
if self.iftest:
return image
labels=torch.tensor(np.argmax(self.df.iloc[idx,1:].values))
return(image,labels)
<set_options>
kfold = 10
kf = StratifiedKFold(n_splits=kfold, shuffle=True)
 predicts_result = []
test_result = []
for train_index, test_index in kf.split(X.iloc[:, 3:], X.Target):
print("
X_train, X_val = X.iloc[:, 3:].iloc[train_index], X.iloc[:, 3:].iloc[test_index]
y_train, y_val = X.Target.iloc[train_index], X.Target.iloc[test_index]
lgb_clf.fit(X_train, y_train, eval_set=[(X_val, y_val)], early_stopping_rounds=1000, verbose=10000)
 test_result.append(f1_score(y_pred=lgb_clf.predict(X_val), y_true=y_val, average="macro"))
predicts_result.append(lgb_clf.predict(X_predict.iloc[:, 3:]))
IMSIZE=545
IMSIZE=EfficientNet.get_image_size('efficientnet-b5' )<create_dataframe>
for result in test_result:
print(result)
 print("Mean test f1: ", np.mean(test_result))
train_dataset=CustomDataset(df=train_df,root_dir='/kaggle/input/plant-pathology-2020-fgvc7/images/',
transform=Compose([augmentations.transforms.Resize(height=IMSIZE,width=IMSIZE,always_apply=True),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
ShiftScaleRotate(rotate_limit=25.0,p=0.7),
OneOf([IAAEmboss(p=1),IAASharpen(p=1),Blur(p=1)],p=0.5),
IAAPiecewiseAffine(p=0.5),
Normalize(( 0.485,0.456,0.406), 
(0.229,0.224,0.225),always_apply=True),
ToTensor()
]))<create_dataframe>
lgb_train_predict = lgb_clf.predict(X.iloc[:, 3:])
 lgb_test_f1 = f1_score(y_pred=lgb_train_predict, y_true=X.Target, average="macro")
 print("LGBM train f1: ", lgb_test_f1 )
<define_variables><EOS> 
submission_history = pd.read_excel(".. /input/competitionjournal/costa_rican_competition_result.xlsx")
 submission_history.set_index("sequence", inplace=True)
 plot_submission(submission_history )