Kaggle competition: Costa Rican Household Poverty Level Prediction
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>
%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.4)
 submission.to_csv("submission.csv", index=False )<load_from_csv>
from IPython.display import clear_output as clear
import sklearn
%matplotlib inline
train = pd.read_csv(".. /input/train.csv")
 testfile = pd.read_csv(".. /input/test.csv")
 train.head(6 )<filter>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 train_df.head()
train = train[(train.trip_duration < 10000)&(train.trip_duration > 100)]<drop_column>
train_df = train_df.fillna(0)
 test_df = test_df.fillna(0 )
train.drop(['id'], axis=1, inplace=True)
 train.drop(['store_and_fwd_flag'], axis=1, inplace=True)
 testfile.drop(['store_and_fwd_flag'], axis=1, inplace=True )<data_type_conversions>
train_label = np.array(train_df_sorted.Target.tolist() )
train['pickup_datetime'] = pd.to_datetime(train.pickup_datetime)
 testfile['pickup_datetime'] = pd.to_datetime(testfile.pickup_datetime)
 train.info()<feature_engineering>
train_data = []
for i in range(len(train_df_sorted)) :
li = train_df_sorted.iloc[i]
li = li.drop('Target')
 li = li.drop('Id' ).tolist()
for _ in range(len(li)) :
try:
float(li[_])
 except ValueError:
if li[_] == 'yes':
li[_] = np.exp(1)
 else:
li[_] = 0
li = np.array(li, dtype='float64')
 li = np.log1p(np.abs(li))
train_data.append(li)
 train_data = np.array(train_data)
 print('done' )
train['week'] = train.pickup_datetime.dt.week
train['weekday'] = train.pickup_datetime.dt.weekday
train['hour'] = train.pickup_datetime.dt.hour
train.drop(['pickup_datetime'], axis=1, inplace=True)
 train.drop(['dropoff_datetime'], axis=1, inplace=True)
 testfile['week'] = testfile.pickup_datetime.dt.week
testfile['weekday'] = testfile.pickup_datetime.dt.weekday
testfile['hour'] = testfile.pickup_datetime.dt.hour
testfile.drop(['pickup_datetime'], axis=1, inplace=True )<prepare_x_and_y>
test_Id = test_df['Id'].tolist()
test_data = []
for i in range(len(test_df)) :
li = test_df.iloc[i]
li = li.drop('Id' ).tolist()
for _ in range(len(li)) :
try:
float(li[_])
 except ValueError:
if li[_] == 'yes':
li[_] = np.exp(1)
 else:
li[_] = 0
li = np.array(li, dtype='float64')
 li = np.log1p(np.abs(li))
test_data.append(li)
 test_data = np.array(test_data)
 print('done' )
y = train["trip_duration"]
train.drop(["trip_duration"], axis=1, inplace=True)
 X = train
X.shape, y.shape<split>
clf_svc = svm.SVC(C=15.0)
 clf_svr = svm.SVR(C=15.0 )
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
 X_train.shape, y_train.shape, X_test.shape, y_test.shape<split>
clf_svc.fit(train_data.clip(0,100000000)[0:8000],train_label[0:8000] )
X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, random_state=42)
 X_train.shape, y_train.shape, X_valid.shape, y_valid.shape<train_model>
clf_svr.fit(train_data.clip(0,100000000)[0:8000],train_label[0:8000] )
m1 = RandomForestRegressor(n_estimators=19, min_samples_split=2, min_samples_leaf=4, max_features='auto', max_depth=80, bootstrap=True)
 m1.fit(X_train, y_train)
 m1.score(X_valid, y_valid )<compute_test_metric>
f1_score(clf_svc.predict(train_data.clip(0,100000000)) , train_label,average='macro' )
print(np.sqrt(MSE(y_valid, m1.predict(X_valid)))) 
<find_best_params>
f1_score(np.round(clf_svr.predict(train_data.clip(0,100000000)) ).clip(1,4), train_label,average='macro' )
<predict_on_test>
pre_svc = clf_svc.predict(test_data.clip(0,100000000))
pre_svr = np.array(np.round(clf_svr.predict(test_data.clip(0,100000000)) ).clip(1,4),dtype='int64' )
test_columns = X_train.columns
predictions = m1.predict(testfile[test_columns] )<prepare_output>
pre_svc_df = pd.DataFrame({'Id':test_Id, 'Target':pre_svc})
 pre_svr_df = pd.DataFrame({'Id':test_Id, 'Target':pre_svr} )
<save_to_csv><EOS> 
pre_svc_df.to_csv('submission_svc.csv',index=False)
 pre_svr_df.to_csv('submission_svr.csv',index=False )