Kaggle competition: Tabular Playground Series - Feb 2021
model.load_weights(file_path )<create_dataframe>
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.ensemble import RandomForestRegressor
import lightgbm as lgb
test_gen=ImageDataGenerator(
 rescale=1./255, 
)
 test_generator=test_gen.flow_from_dataframe(
 x_col='id',
y_col='has_cactus',
dataframe=test_df,
directory=test_dir,
class_mode='other',
color_mode='rgb',
batch_size=1,
target_size=IMAGE_SIZE,
shuffle=False
)<predict_on_test>
train = pd.read_csv('.. /input/tabular-playground-series-feb-2021/train.csv')
 test = pd.read_csv('.. /input/tabular-playground-series-feb-2021/test.csv')
 sample_sub = pd.read_csv('.. /input/tabular-playground-series-feb-2021/sample_submission.csv' )
test_generator.reset()
pred = model.predict_generator(test_generator, verbose=1, steps=test_generator.n)
 pred[pred>0.99]=1
pred[pred<0.01]=0<save_to_csv>
delete_columns = ['id']
train.drop(delete_columns, axis=1, inplace=True)
 test.drop(delete_columns, axis=1, inplace=True )
test_df['has_cactus'] = pred
test_df.to_csv('submission.csv', index = False )<import_modules>
categorical_features = ['cat0', 'cat1', 'cat2', 'cat3', 'cat4', 'cat5', 'cat6','cat7', 'cat8', 'cat9']
<import_modules>
for c in train.columns:
if train[c].dtype == 'object':
lbl = LabelEncoder()
lbl.fit(list(train[c].values)+list(test[c].values))
train[c] = lbl.transform(train[c].values)
 test[c] = lbl.transform(test[c].values)
 display(train.head())
import numpy as np
import pandas as pd
import torch
from fastai import *
from fastai.vision import *<load_from_csv>
y_train = train['target']
X_train = train.drop('target', axis = 1)
 X_test = test
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/sample_submission.csv" )<prepare_x_and_y>
y_preds = []
models = []
oof_train = np.zeros(len(X_train))
cv = KFold(n_splits=5, shuffle=True, random_state=0)
 params = {
'random_state':42,
'metric': 'rmse',
'n_jobs': -1,
'cat_feature': [x for x in range(len(categorical_features)) ],
'bagging_seed':42,
'feature_fraction_seed':42,
'learning_rate': 0.0011992715138089741,
'max_depth': 101,
'num_leaves': 86,
'reg_alpha': 7.504329214783163,
'reg_lambda': 1.5631184517427836,
'colsample_bytree': 0.22354989226986266,
'min_child_samples': 149,
'subsample_freq': 4,
'subsample': 0.5143496951794435,
'max_bin': 720,
'min_data_per_group': 55,
'cat_smooth': 78,
'cat_l2': 7
}
y_preds = 0
for fold_id,(train_index, valid_index)in enumerate(cv.split(X_train, y_train)) :
X_tr = X_train.loc[train_index, :]
X_val = X_train.loc[valid_index, :]
y_tr = y_train[train_index]
y_val = y_train[valid_index]
lgb_train = lgb.Dataset(X_tr, y_tr, categorical_feature=categorical_features)
 lgb_eval = lgb.Dataset(X_val, y_val, reference=lgb_train, categorical_feature=categorical_features)
 model = lgb.train(params, lgb_train,
valid_sets=[lgb_train, lgb_eval],
verbose_eval = 1000,
num_boost_round = 30000,
early_stopping_rounds=1000)
 oof_train[valid_index] = model.predict(X_val, num_iteration=model.best_iteration)
 y_pred = model.predict(X_test, num_iteration=model.best_iteration)
 y_preds += y_pred/5
models.append(model )
test_img = ImageList.from_df(test_df, path='.. /input/test', folder='test' )<choose_model_class>
pd.DataFrame(oof_train ).to_csv('oof_train_kfold.csv', index=False )
learn2 = cnn_learner(train_img2, models.densenet161, metrics=[error_rate, accuracy] )<train_model>
y_preds = pd.DataFrame(y_preds )
learn2.fit_one_cycle(5, slice(3e-02))<predict_on_test>
y_subs = y_preds
preds,_ = learn2.get_preds(ds_type=DatasetType.Test )<define_variables>
sample_sub['target'] = y_subs
sample_sub.to_csv('submission_CV.csv', index=False )