Kaggle competition: Predicting Age Groups
<SOS> metric: categorizationaccuracy Kaggle data source: predicting-age-groups<predict_on_test>
import numpy as np
import pandas as pd
def predict_batch(self, df):
dl = self.dls.test_dl(df)
 dl.dataset.conts = dl.dataset.conts.astype(np.float32)
 inp,preds,_,dec_preds = self.get_preds(dl=dl, with_input=True, with_decoded=True)
 return preds.numpy()
setattr(learn, 'predict_batch', predict_batch )<import_modules>
df = pd.read_csv(".. /input/pk-hska/agegroups_train.csv")
 df.head()
import lightgbm as lgb<create_dataframe>
features = ['weekday_number', 'region_number', 'sum_sales_pg1', 'sum_sales_pg2', 'sum_sales_pg3']
target = 'age_group_number'
cat_features = ['tags_encoded', 'tag_1', 'tag_2']
lgb_train = lgb.Dataset(train_df[features], train_df[target], categorical_feature = cat_features, free_raw_data=False)
 lgb_eval = lgb.Dataset(valid_df[features], valid_df[target], categorical_feature = cat_features, reference=lgb_train, free_raw_data=False )<init_hyperparams>
X = df[features]
y = df[target]
METRICS = ['auc']
params = {
'objective': 'binary',
'seed': 42,
'metric': METRICS,
'learning_rate': 0.05,
'max_bin': 800,
'num_leaves': 80
}<train_model>
clf = RandomForestClassifier()
param_grid = [
{'max_depth': range(5, 21, 5),
'n_estimators' : range(50, 101, 50)}
]
search = GridSearchCV(clf, param_grid, cv=3)
 search.fit(X, y)
 print("Best parameter(CV score={:.2f}):{})".format(search.best_score_, search.best_params_))
%%time
NUM_BOOST_ROUNDS = 300
evals_result = {}
lgb_model = lgb.train(
 params,
lgb_train,
valid_sets=[lgb_train, lgb_eval],
verbose_eval=20,
num_boost_round=NUM_BOOST_ROUNDS,
early_stopping_rounds=20,
evals_result=evals_result
)<choose_model_class>
df_test = pd.read_csv(".. /input/pk-hska/agegroups_test.csv" ).set_index('index')
 df_test.head()
clf_tabnet = TabNetClassifier(cat_idxs=[list(train_df[features].columns ).index(x)for x in cat_features],
scheduler_params={"step_size": 50, "gamma": 0.9},
scheduler_fn=torch.optim.lr_scheduler.StepLR)
 clf_tabnet<train_model>
prediction = search.predict(df_test)
 prediction
%%time
clf_tabnet.fit(
 X_train=train_df[features].values, y_train=train_df[target].values,
eval_set=[(valid_df[features].values, valid_df[target].values)],
max_epochs=2,
batch_size=8192 * 4
)<predict_on_test>
df_test['age_group_number'] = prediction
<data_type_conversions><EOS> 
df_submission = df_test[['age_group_number']]
df_submission.to_csv('submission.csv', index=True, index_label='index' )