Kaggle competition: Costa Rican Household Poverty Level Prediction
<SOS> metric: MacroFScore Kaggle data source: costa-rican-household-poverty-level-prediction<define_variables>
init_notebook_mode(connected=True )
data.show_batch(rows=3,figsize=(7,6))<choose_model_class>
data=pd.read_csv(".. /input/costa-rican-household-poverty-prediction/train.csv")
 test=pd.read_csv(".. /input/costa-rican-household-poverty-prediction/test.csv")

label_dict={1:"extreme poverty",2:"moderate poverty",3:"vulnerable households",4:"non-vulnerable",}

 features=data.columns
info={}
x=open(".. /input/description/description.txt")
 for x,line in enumerate(x):
info[line.split(",",1)[0]]= line.split(",",1)[1].replace("
","")
 info
learn = create_cnn(data,models.resnet50,metrics=error_rate )<find_best_params>
data=data.drop(columns=["area2","female"])
 data=data.drop(columns=["estadocivil1","estadocivil2","estadocivil3","estadocivil4","estadocivil5"])
 test=test.drop(columns=["area2","female"])
 test=test.drop(columns=["estadocivil1","estadocivil2","estadocivil3","estadocivil4","estadocivil5"] )
learn.lr_find()<train_model>
data["depend"] =(data["hogar_nin"]+data["hogar_mayor"]+1)/(1+data["hogar_adul"])
 test["depend"] =(test["hogar_nin"]+test["hogar_mayor"]+1)/(1+test["hogar_adul"])
 set(data["depend"] )
learn.fit_one_cycle(6,slice(1e-2))<save_model>
data[["depend","Target"]].corr(method="spearman")
 info["depend"]=info["dependency"]
learn.save('stg-1' )<find_best_params>
data=data.drop(columns="dependency")
 test=test.drop(columns="dependency" )
learn.lr_find()<train_model>
id_ = ['Id', 'idhogar', 'Target']
ind_bool = ['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', 'rez_esc']
ind_ordered = ['rez_esc', 'escolari', 'age']
hh_bool = ['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', 'area1', 'area2', 'v2a1']
hh_ordered = [ 'rooms', 'r4h1', 'r4h2', 'r4h3', 'r4m1','r4m2','r4m3', 'r4t1', 'r4t2',
'r4t3', 'v18q1', 'tamhog','tamviv','hhsize','hogar_nin',
'hogar_adul','hogar_mayor','hogar_total', 'bedrooms', 'qmobilephone']
hh_cont = ['v2a1', 'dependency', 'edjefe', 'edjefa', 'meaneduc', 'overcrowding']
learn.unfreeze()
learn.fit_one_cycle(4,max_lr=slice(1e-4,1e-3))<load_pretrained>
all_equal = data.groupby('idhogar')['Target'].apply(lambda x: x.nunique() == 1)
 not_equal = all_equal[all_equal != True]
print('There are {} households where the family members do not all have the same target.'.format(len(not_equal)) )
data = ImageDataBunch.from_folder(path,test='.. /test', ds_tfms=get_transforms() ,valid_pct=0.25,size=350,bs=32,num_workers=0)
 data.normalize(imagenet_stats )<compute_train_metric>
households_leader = data.groupby('idhogar')['parentesco1'].sum()
households_no_head = households_leader[households_leader==0]
print('There are {} households without a head.'.format(len(households_leader[households_leader==0])) )
interp = ClassificationInterpretation.from_learner(learn)
 print(metrics.classification_report(interp.y_true.numpy() , interp.pred_class.numpy() ,target_names =data.classes))<save_model>
for household in not_equal.index:
true_target = int(data[(data['idhogar'] == household)&(data['parentesco1'] == 1.0)]['Target'])
 data.loc[data['idhogar'] == household, 'Target'] = true_target
all_equal = data.groupby('idhogar')['Target'].apply(lambda x: x.nunique() == 1)
 not_equal = all_equal[all_equal != True]
print('There are {} households where the family members do not all have the same target.'.format(len(not_equal)) )
learn.save('stg-2' )<prepare_output>
data=data.drop(columns=[i for i in data.columns if i.startswith("SQ")==True])
 data=data.drop(columns=["agesq"] )
learn.data=data<train_model>
test=test.drop(columns=[i for i in test.columns if i.startswith("SQ")==True])
 test=test.drop(columns=["agesq"] )
learn.unfreeze()
learn.fit_one_cycle(4,max_lr=slice(1e-5,1e-4))<predict_on_test>
null=data.isnull().any()
null[null==True]
preds,y=learn.get_preds(ds_type=DatasetType.Test )<prepare_output>
data["v2a1"]=data["v2a1"].fillna(0)
 data["v18q1"]=data["v18q1"].fillna(0)
 test["v2a1"]=test["v2a1"].fillna(0)
 test["v18q1"]=test["v18q1"].fillna(0 )
preds = np.argmax(preds, axis = 1)
 preds_classes = [data.classes[i] for i in preds]<save_to_csv>
data["Target"][data["meaneduc"].isnull() ==True]
submission = pd.DataFrame({ 'file': os.listdir('input/test'), 'species': preds_classes })
 submission.to_csv('test_classification_results.csv', index=False )<import_modules>
mode_edu=statistics.mode(data["meaneduc"][data["Target"]==4][data["meaneduc"].isnull() ==False])
 data["meaneduc"]=data["meaneduc"].fillna(mode_edu)
 test["meaneduc"]=test["meaneduc"].fillna(mode_edu)
scale = 70
seed = 7<train_model>
set(data["rez_esc"].fillna(0))
data=data.drop(columns=["rez_esc"])
 test=test.drop(columns=["rez_esc"] )
path_to_images = '.. /input/plant-seedlings-classification/train/*/*.png'
images = glob(path_to_images)
 trainingset = []
traininglabels = []
num = len(images)
 count = 1
for i in images:
print(str(count)+'/'+str(num),end='\r')
 trainingset.append(cv2.resize(cv2.imread(i),(scale,scale)))
 traininglabels.append(i.split('/')[-2])
 count=count+1
trainingset = np.asarray(trainingset)
 traininglabels = pd.DataFrame(traininglabels )<categorify>
features=["escolari","meaneduc","instlevel1","instlevel2","instlevel3","instlevel4","instlevel5","instlevel6",
"instlevel7","instlevel8","instlevel9"]
labels = preprocessing.LabelEncoder()
labels.fit(traininglabels[0])
 print('Classes'+str(labels.classes_))
encodedlabels = labels.transform(traininglabels[0])
 clearalllabels = np_utils.to_categorical(encodedlabels)
 classes = clearalllabels.shape[1]
print(str(classes))
traininglabels[0].value_counts().plot(kind='pie' )<split>
encoding={i:int(i[-1])for i in [j for j in features if "inst" in j]}
insti=["instlevel1","instlevel2","instlevel3","instlevel4","instlevel5","instlevel6",
"instlevel7","instlevel8","instlevel9"]
for i in range(len(data)) :
for j in insti:
if data.loc[i,j]==1:
data.loc[i,j]=encoding[j]
for i in range(len(test)) :
for j in insti:
if test.loc[i,j]==1:
test.loc[i,j]=encoding[j]
data["qualified"]=data[insti].sum(axis=1)
 test["qualified"]=test[insti].sum(axis=1)
 info["qualified"]="sum of all instlevels"
data=data.drop(columns=insti)
 test=test.drop(columns=insti )
new_train = new_train/255
x_train,x_test,y_train,y_test = train_test_split(new_train,clearalllabels,test_size=0.1,random_state=seed,stratify=clearalllabels )<train_model>
avg_qual=data.groupby("idhogar")["qualified"].apply(lambda x: x.mean())
 avg_qual_test=test.groupby("idhogar")["qualified"].apply(lambda x: x.mean())
 info["avg_qual"]="average academic qualification level of a household"
data["avg_qual"]=""
for i in range(len(data)) :
data.loc[i,"avg_qual"]=avg_qual[data.loc[i,"idhogar"]]
data["avg_qual"]=data["avg_qual"].astype(float)
 for i in range(len(test)) :
test.loc[i,"avg_qual"]=avg_qual_test[test.loc[i,"idhogar"]]
test["avg_qual"]=test["avg_qual"].astype(float )
generator = ImageDataGenerator(rotation_range = 180,zoom_range = 0.1,width_shift_range = 0.1,height_shift_range = 0.1,horizontal_flip = True,vertical_flip = True)
 generator.fit(x_train )<choose_model_class>
data=data.drop(columns=["escolari","meaneduc"])
 test=test.drop(columns=["escolari","meaneduc"] )
np.random.seed(seed)
 model = Sequential()
model.add(Conv2D(filters=64, kernel_size=(5, 5), input_shape=(scale, scale, 3), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=64, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Conv2D(filters=128, kernel_size=(5, 5), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=128, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Conv2D(filters=256, kernel_size=(5, 5), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=256, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 model.summary() 
<predict_on_test>
walls={"epared1":1,"epared2":2,"epared3":3}
roof={"etecho1":1,"etecho2":2,"etecho3":3}
floor={"eviv1":1,"eviv2":2,"eviv3":3}
data["walls"]=""
data["roof"]=""
data["floor"]=""
test["walls"]=""
test["roof"]=""
test["floor"]=""
for i in range(len(data)) :
for j in walls.keys() :
if data.loc[i,j]==1:
data.loc[i,"walls"]=walls[j]
for i in range(len(data)) :
for j in roof.keys() :
if data.loc[i,j]==1:
data.loc[i,"roof"]=roof[j]
for i in range(len(data)) :
for j in floor.keys() :
if data.loc[i,j]==1:
data.loc[i,"floor"]=floor[j]
for i in range(len(test)) :
for j in walls.keys() :
if test.loc[i,j]==1:
test.loc[i,"walls"]=walls[j]
for i in range(len(test)) :
for j in roof.keys() :
if test.loc[i,j]==1:
test.loc[i,"roof"]=roof[j]
for i in range(len(test)) :
for j in floor.keys() :
if test.loc[i,j]==1:
test.loc[i,"floor"]=floor[j]
y_pred = model.predict(x_test)
 y_class = np.argmax(y_pred, axis = 1)
 y_check = np.argmax(y_test, axis = 1)
 cmatrix = confusion_matrix(y_check, y_class)
 print(cmatrix )<save_to_csv>
data["housing"]=data["walls"]+data["floor"]+data["roof"]
test["housing"]=test["walls"]+test["floor"]+test["roof"]
newtestimages=newtestimages/255
prediction = model.predict(newtestimages)
 pred = np.argmax(prediction,axis=1)
 predStr = labels.classes_[pred]
result = {'file':tests,'species':predStr}
result = pd.DataFrame(result)
 result.to_csv("Prediction.csv",index=False )<set_options>
data=data.drop(columns=["walls","floor","roof"])
 test=test.drop(columns=["walls","floor","roof"] )
%matplotlib inline 
<define_variables>
features1=["mobilephone","computer","television","refrig","v18q"]
species = ['Black-grass', 'Charlock', 'Cleavers', 'Common Chickweed', 'Common wheat', 'Fat Hen',
'Loose Silky-bent', 'Maize','Scentless Mayweed', 'Shepherds Purse',
'Small-flowered Cranesbill', 'Sugar beet']
data_dir = '.. /input/plant-seedlings-classification/'
train_dir = os.path.join(data_dir, 'train')
 test_dir = os.path.join(data_dir, 'test' )<prepare_output>
data["equipped"] = data[features1].sum(axis=1)
 test["equipped"] = test[features1].sum(axis=1)
 info["equipped"]="how tech savvy a family is"
test_data = []
for file in os.listdir(test_dir):
test_data.append(['test/{}'.format(file), file])
 test = pd.DataFrame(test_data, columns=['Filepath', 'File'])
 test.head()<train_on_grid>
data=data.drop(columns=features1)
 test=test.drop(columns=features1 )
IMAGE_SIZE = 66
def read_image(filepath):
return cv2.imread(os.path.join(data_dir, filepath))
def resize_image(image, image_size):
return cv2.resize(image.copy() , image_size, interpolation=cv2.INTER_AREA )<statistical_test>
data["tab_cap"]=(data["v18q1"]/data["hogar_total"] ).fillna(0)
 data["phone_cap"]=(data["qmobilephone"]/data["hogar_total"] ).fillna(0)
 data["tech_cap"]=data["tab_cap"]+data["phone_cap"]
test["tab_cap"]=(test["v18q1"]/test["hogar_total"] ).fillna(0)
 test["phone_cap"]=(test["qmobilephone"]/test["hogar_total"] ).fillna(0)
 test["tech_cap"]=test["tab_cap"]+test["phone_cap"]
def create_mask(image):
image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
 lower_green = np.array([30, 100, 50])
 upper_green = np.array([85, 255, 255])
 mask = cv2.inRange(image_hsv, lower_green, upper_green)
 kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(15, 15))
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
 return mask
def segment_image(image):
mask = create_mask(image)
 res = cv2.bitwise_and(image, image, mask=mask)
 return res<normalization>
data=data.drop(columns=["tab_cap","phone_cap","qmobilephone","v18q1"])
 test=test.drop(columns=["tab_cap","phone_cap","qmobilephone","v18q1"] )
X_train = np.zeros(( train.shape[0], IMAGE_SIZE, IMAGE_SIZE, 3))
for i, file in tqdm(enumerate(train['File'].values)) :
image = read_image(file)
 image_segmented = segment_image(image)
 X_train[i] = resize_image(image_segmented,(IMAGE_SIZE, IMAGE_SIZE))
X_train = X_train / 255.
 print('Train Shape: {}'.format(X_train.shape))<prepare_x_and_y>
data=data.drop(columns=["tamhog","tamviv","hhsize"])
 test=test.drop(columns=["tamhog","tamviv","hhsize"] )
Y_train = train['SpeciesId'].values
Y_train = to_categorical(Y_train, num_classes=12 )<split>
features=["bedrooms","overcrowding","hacdor","rooms","hacapo"]
BATCH_SIZE = 16
EPOCHS = 75
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size=0.1, random_state=SEED )<choose_model_class>
data=data.drop(columns=["hacapo","hacdor","bedrooms"])
 test=test.drop(columns=["hacapo","hacdor","bedrooms"] )
def construct_model() :
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3),
activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dense(12, activation='softmax'))
optimizer = Adam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=0.1, decay=0.0)
 model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
 model.summary()
return model<choose_model_class>
features=['tipovivi1', 'tipovivi2', 'tipovivi3', 'tipovivi4',"tipovivi5","v2a1"]
def train_model() :
model = construct_model()
annealer = ReduceLROnPlateau(monitor='val_acc', factor=0.5, patience=5, verbose=1, min_lr=1e-5)
 checkpoint = ModelCheckpoint('model.h5', verbose=1, save_best_only=True)
 datagen = ImageDataGenerator(rotation_range=360,
width_shift_range=0.2,
height_shift_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
vertical_flip=True)
 datagen.fit(X_train)
 hist = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=BATCH_SIZE),
steps_per_epoch=X_train.shape[0] // BATCH_SIZE,
epochs=EPOCHS,
verbose=2,
callbacks=[annealer, checkpoint],
validation_data=(X_val, Y_val)) 
<train_model>
data=data.drop(columns="v2a1")
 test=test.drop(columns="v2a1" )
final_model = load_model('.. /input/plant-seedling-models/model.h5')
 final_loss, final_accuracy = final_model.evaluate(X_val, Y_val)
 print('Final Loss: {}, Final Accuracy: {}'.format(final_loss, final_accuracy))<normalization>
data=data.drop(columns=["r4h1","r4h2","r4m1","r4m2","r4m3","r4t2","r4t3"])
 test=test.drop(columns=["r4h1","r4h2","r4m1","r4m2","r4m3","r4t2","r4t3"] )
X_test = np.zeros(( test.shape[0], IMAGE_SIZE, IMAGE_SIZE, 3))
for i, file in tqdm(enumerate(test['Filepath'].values)) :
image = read_image(file)
 image_segmented = segment_image(image)
 X_test[i] = resize_image(image_segmented,(IMAGE_SIZE, IMAGE_SIZE))
X_test = X_test / 255 .<save_to_csv>
data=data.drop(columns=["lugar1","lugar2","lugar3","lugar4","lugar5","lugar6"])
 test=test.drop(columns=["lugar1","lugar2","lugar3","lugar4","lugar5","lugar6"] )
predictions = final_model.predict(X_test)
 predictions = np.argmax(predictions, axis=1)
 df = pd.DataFrame({'file': [file for file in test['File'].values], 'species': [species[i] for i in predictions]})
 df.to_csv('submission.csv', index=False )<set_options>
data=data[data["parentesco1"]==1]
pd.options.display.max_rows = 500
pd.options.display.max_columns = 500<feature_engineering>
from imblearn.over_sampling import RandomOverSampler
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>
y=data["Target"]
x=data.drop(columns=["Target","Id"])
 test_labels=test["Id"]
test=test.drop(columns=["Id"])
 x=x.drop(columns=["edjefe","edjefa"])
 test=test.drop(columns=["edjefe","edjefa"])
 x=x.drop(columns=["hogar_nin","hogar_adul","hogar_mayor"])
 test=test.drop(columns=["hogar_nin","hogar_adul","hogar_mayor"])
 x=x.drop(columns=["idhogar"])
 test=test.drop(columns=["idhogar"])
set_seed(1213 )<define_variables>
ros = RandomOverSampler(random_state=0)
 x_res, y_res = ros.fit_resample(x, y)
 Counter(y_res )
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>
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import RobustScaler
from sklearn.base import BaseEstimator, TransformerMixin, RegressorMixin, clone
from sklearn.model_selection import KFold, cross_val_score, train_test_split
from sklearn.metrics import mean_squared_error, accuracy_score
import xgboost as xgb
import lightgbm as lgb
train = pd.read_csv(RAW_DATA / "train.csv" )<load_from_csv>
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import f1_score, make_scorer,accuracy_score
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import Imputer
from sklearn.preprocessing import MinMaxScaler
from sklearn.pipeline import Pipeline
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>
scorer = make_scorer(f1_score, greater_is_better=True, average = 'macro')
 scorer2 = make_scorer(accuracy_score, greater_is_better=True )
sub = pd.read_csv(".. /input/birdsong-recognition/sample_submission.csv")
 sub.to_csv("submission.csv", index=False )<load_pretrained>
pipeline = Pipeline([('imputer', Imputer(strategy = 'median')) , 
('scaler', MinMaxScaler())] )
TARGET_SR = 32000
model_config_0 = {
"base_model_name": "resnest50_fast_1s1x64d",
"pretrained": False,
"num_classes": 264,
"trained_weights": ".. /input/cornellfold0/best_model.pth"
}
model_config_1 = {
"base_model_name": "resnest50_fast_1s1x64d",
"pretrained": False,
"num_classes": 264,
"trained_weights": ".. /input/cbsweights/best_model.pth"
}
model_config_2 = {
"base_model_name": "resnest50_fast_1s1x64d",
"pretrained": False,
"num_classes": 264,
"trained_weights": ".. /input/training-birdsong-baseline-resnest50-fast/best_model.pth"
}
model_config_3 = {
"base_model_name": "resnest50_fast_1s1x64d",
"pretrained": False,
"num_classes": 264,
"trained_weights": ".. /input/training-birdsong-baseline-resnest50-fast-f3/best_model.pth"
}
model_config_4 = {
"base_model_name": "resnest50_fast_1s1x64d",
"pretrained": False,
"num_classes": 264,
"trained_weights": ".. /input/training-birdsong-baseline-resnest50-fast-f4/best_model.pth"
}
model_list=[model_config_0,model_config_1,model_config_2,model_config_3,model_config_4]
melspectrogram_parameters = {
"n_mels": 128,
"fmin": 20,
"fmax": 16000
}<define_variables>
x_res=pipeline.fit_transform(x_res)
 test=pipeline.transform(test )
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>
model = RandomForestClassifier(n_estimators=500, random_state=10, n_jobs = -1)
 cv_score = cross_val_score(model, x_res, y_res, cv = 10, scoring = scorer)
 cv2_score = cross_val_score(model, x_res, y_res, cv = 10, scoring = scorer2 )
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>
rf_model = RandomForestClassifier(n_estimators=500, random_state=10, n_jobs = -1 )
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>
rf_trained=rf_model.fit(x_res,y_res)
 predicted=rf_model.predict(test )
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"]))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 model = model.to(device)
 state_dict = torch.load(args["trained_weights"], map_location=device)
 model.load_state_dict(state_dict)
 model.eval()
return model<create_dataframe>
subm=pd.DataFrame()
subm["Id"]=test_labels
subm["Target"]=predicted
def prediction_for_clip(test_df: pd.DataFrame,
clip: np.ndarray,
model_list: [ResNet],
mel_params: dict,
threshold=0.5,
maxpreds=3, 
):

 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")
 for model in model_list:
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() :
proba=np.zeros(( 264,))
for model in model_list:
prediction = F.sigmoid(model(image))
proba= proba + prediction.detach().cpu().numpy().reshape(-1)
 proba = proba / len(model_list)
 events = proba >= threshold
labels = np.argsort(-proba)[:events.sum() ].tolist()
else:
model=model_list[0]
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()
probas = []
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() :
proba=np.zeros(( 264,))
for model in model_list:
prediction = F.sigmoid(model(batch))
proba= proba + prediction.detach().cpu().numpy()
proba = proba / len(model_list)
 probas.append(proba)
 probas = np.vstack(probas)
 probas = probas.max(0)
 events =(probas>=threshold)
 labels = np.argsort(-probas)[:events.sum() ].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[:maxpreds])
 prediction_dict[row_id] = label_string
return prediction_dict<predict_on_test>
subm.to_csv("results.csv",index=False )
def prediction(test_df: pd.DataFrame,
test_audio: Path,
model_list: list,
mel_params: dict,
target_sr: int,
threshold=0.5,
maxpreds = 3, 
):
models=[]
for m in model_list:
models.append(get_model(m))
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_list=models,
mel_params=mel_params,
threshold=threshold,
maxpreds = maxpreds, 
)
 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<predict_on_test>
from xgboost import XGBClassifier
from sklearn.model_selection import GridSearchCV as grid_search
submission = prediction(test_df=test,
test_audio=TEST_AUDIO_DIR,
model_list=model_list,
mel_params=melspectrogram_parameters,
target_sr=TARGET_SR,
threshold=0.56,
maxpreds=2, 
)
 submission.to_csv("submission.csv", index=False )<set_options>
xg_model=XGBClassifier(gamma=0.1,max_depth=5,objective='multi:softmax',n_estimators=1200 )
pd.options.display.max_rows = 500
pd.options.display.max_columns = 500<feature_engineering>
param_grid={"max_depth":[3,4,5,6],"gamma":[0.001,0.01,0.1,0.2,0,3],"n_estimators":[100,150,200,250,300,350,400,450,500,550,600,650], "objective":['multi:softmax']}
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>
xg_model.fit(x_res,y_res)
set_seed(1213 )<define_variables>
subm=pd.DataFrame()
subm["Id"]=test_labels
subm["Target"]=xg_model.predict(test )
<load_from_csv><EOS> 
subm.to_csv("results.csv",index=False )