Kaggle competition: Kannada MNIST
def kfold_train(train, test, num_folds, CLF, stratified = False, debug= False):
train_df, test_df = train.copy() , test.copy()
print("Start Training.Train shape: {}, test shape: {}".format(train_df.shape, test_df.shape))
gc.collect()
if stratified:
folds = StratifiedKFold(n_splits= num_folds, shuffle=True, random_state=47)
 else:
folds = KFold(n_splits= num_folds, shuffle=True, random_state=47)
 oof_preds = np.zeros(train_df.shape[0])
 sub_preds = np.zeros(test_df.shape[0])
 feature_importance_df = pd.DataFrame()
feats = [f for f in train_df.columns if f not in [TARGET, ID]]
for n_fold,(train_idx, valid_idx)in enumerate(folds.split(train_df[feats], train_df[TARGET])) :
train_x, train_y = train_df[feats].iloc[train_idx], train_df[TARGET].iloc[train_idx]
valid_x, valid_y = train_df[feats].iloc[valid_idx], train_df[TARGET].iloc[valid_idx]
clf = CLF['class'](**CLF['params'])
 if CLF['eval_set']:
clf.fit(train_x, train_y, eval_set=[(train_x, train_y),(valid_x, valid_y)],
eval_metric= 'auc', verbose= 1000, early_stopping_rounds= 200)
 oof_preds[valid_idx] = clf.predict_proba(valid_x, num_iteration=clf.best_iteration_)[:, 1]
sub_preds += clf.predict_proba(test_df[feats], num_iteration=clf.best_iteration_)[:, 1] / folds.n_splits
elif 'keras' in CLF:
clf.fit(CLF['fit_params'])
 oof_preds[valid_idx] = clf.predict_proba(valid_x)[:, 1]
sub_preds += clf.predict_proba(test_df[feats])[:, 1] / folds.n_splits
else:
clf.fit(train_x, train_y)
 oof_preds[valid_idx] = clf.predict_proba(valid_x)[:, 1]
sub_preds += clf.predict_proba(test_df[feats])[:, 1] / folds.n_splits
if CLF['feature_importance']:
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = feats
fold_importance_df["importance"] = clf.feature_importances_
fold_importance_df["fold"] = n_fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 print('Fold %2d AUC : %.6f' %(n_fold + 1, roc_auc_score(valid_y, oof_preds[valid_idx])))
 del clf, train_x, train_y, valid_x, valid_y
gc.collect()
print('Full AUC score %.6f' % roc_auc_score(train_df[TARGET], oof_preds))
if not debug:
test_df[TARGET] = sub_preds
test_df[[ID, TARGET]].to_csv(CLF['submission'], index= False)
 if CLF['feature_importance']:
display_importances(feature_importance_df)
 return feature_importance_df<choose_model_class>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
CLF = {
'class':GaussianNB, 'params':{},'submission':'sub_naive.csv', 'eval_set':None, 'feature_importance':False
}<concatenate>
from fastai.vision import *
from fastai.metrics import error_rate
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from tqdm import tqdm
kfold_train(train, test, 5, CLF )<import_modules>
PATH = ".. /input/Kannada-MNIST/"
from sklearn.preprocessing import PolynomialFeatures<concatenate>
train = pd.read_csv(PATH+"train.csv")
 train.head()
high_low = list(correlations.head(5 ).index)+ list(correlations.tail(6 ).index)
 high_low.remove('target')
 poly = PolynomialFeatures(2, interaction_only=True)
 poly_train = poly.fit_transform(train[high_low])
 poly_test = poly.fit_transform(test[high_low])
 new_train = pd.concat([train, pd.DataFrame(poly_train)], axis=1)
 new_test = pd.concat([test, pd.DataFrame(poly_test)], axis=1 )<train_model>
test = pd.read_csv(PATH+"test.csv")
 test.head()
CLF = {
'class':GaussianNB, 'params':{},'submission':'sub_naive_new.csv', 'eval_set':None, 'feature_importance':False
}
kfold_train(new_train, new_test, 5, CLF )<train_model>
sample = pd.read_csv(PATH+"sample_submission.csv" )
CLF = {
'class':DecisionTreeClassifier, 'params':{},'submission':'sub_dt.csv', 'eval_set':None, 'feature_importance':True
}
feat_importance = kfold_train(train, test, 5, CLF )<train_model>
image = train.iloc[:,1:]
label = train.iloc[:,0:1]
CLF = {
'class':DecisionTreeClassifier, 'params':{},'submission':'sub_dt_new.csv', 'eval_set':None, 'feature_importance':True
}
feat_importance = kfold_train(new_train, new_test, 5, CLF )<train_model>
test_image = test.iloc[:,1:]
CLF = {
'class':LGBMClassifier, 'params':{'n_estimators':10000, 'learning_rate':0.01, 'max_depth':10},'submission':'sub_lgbm.csv', 'eval_set':True, 'feature_importance':True
}
feat_importance = kfold_train(train, test, 5, CLF )<load_from_csv>
test_id = test.iloc[:,0:1]
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 print("Number of rows and columns in train set : ",train_df.shape)
 print("Number of rows and columns in test set : ",test_df.shape )<filter>
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from PIL import Image
corr_df.loc[corr_df.target>0.05].index[1:]<split>
train['file_name'] = np.NaN
train_df_out_rem=train_df.iloc[:,1:]<drop_column>
for i in tqdm(range(int(len(image)))) :
code = label.iloc[i,:].astype('int')[0]
serial = i
save_path = f'train/{serial}_train_{code}.png'
train.iloc[i,785] = f'{serial}_train_{code}.png'
temp_image = Image.fromarray(image.iloc[i,:].values.astype('uint8' ).reshape(28,28))
temp_image.save(save_path)
train_df_out_rem=train_df_out_rem.reset_index(drop=True )<import_modules>
test['file_name'] = np.NaN
from sklearn.ensemble import RandomForestClassifier<choose_model_class>
for i in tqdm(range(int(len(test_image)))) :
serial = i
save_path = f'test/{serial}_test.png'
test.iloc[i,785] = f'{serial}_test.png'
temp_image = Image.fromarray(test_image.iloc[i,:].values.astype('uint8' ).reshape(28,28))
temp_image.save(save_path )
clf = RandomForestClassifier(n_estimators=100, max_depth=8,random_state=0 )<train_model>
train_dict = {'name':train['file_name'] , 'label': train['label']}
df = pd.DataFrame(train_dict)
 df.head()
clf.fit(train_df_out_rem.iloc[:,1:], train_df_out_rem.iloc[:,0] )<define_variables>
test_dict = {'name':test['file_name']}
df_test = pd.DataFrame(test_dict)
 df_test.head()
importance_dict=dict(zip(list(train_df_out_rem.columns[1:]),list(clf.feature_importances_)) )<import_modules>
tfms = get_transforms(do_flip=False )
from sklearn.preprocessing import PolynomialFeatures<choose_model_class>
src =(ImageList.from_df(path='train', df=df)
.split_by_rand_pct() 
.label_from_df(cols='label')
 )
poly = PolynomialFeatures(interaction_only=True )<feature_engineering>
data =(src.transform(tfms, size=28)
.databunch(bs=64 ).normalize(imagenet_stats)
 )
poly_features=poly.fit_transform(train_df_out_rem[features_names_upd] )<prepare_output>
data.show_batch(rows=3 )
poly_features_df=pd.DataFrame(poly_features)
 poly_features_df.columns=['Imp_feature_'+str(i)for i in range(poly_features.shape[1])]
poly_features_df.head()<filter>
!mkdir -p /tmp/.cache/torch/checkpoints
!cp /kaggle/input/fastai-pretrained-models/resnet50-19c8e357.pth /tmp/.cache/torch/checkpoints/resnet50-19c8e357.pth
learn = cnn_learner(data, models.resnet50, metrics=[error_rate, accuracy], model_dir = Path('.. /kaggle/working'),path = Path("."))
other_cols=train_df_out_rem.loc[:, ~train_df_out_rem.columns.isin(features_names_upd)]<concatenate>
learn.lr_find()
train_final_df=pd.concat([poly_features_df,other_cols],axis=1 )<count_values>
lr = 0.01
learn.fit_one_cycle(4, lr )
train_final_df.target.value_counts(dropna=False )<prepare_x_and_y>
learn.save('kannada-mnist-stage-1' )
train_x=train_final_df.loc[:, train_final_df.columns != 'target']
train_y=train_final_df.target<categorify>
learn.fit_one_cycle(5, slice(1e-4, lr/5))
poly_features_test=poly.transform(test_df[features_names_upd] )<prepare_output>
learn.save('kanada-mnist-stage-2' )
poly_features_test_df=pd.DataFrame(poly_features_test)
 poly_features_test_df.columns=['Imp_feature_'+str(i)for i in range(poly_features_test.shape[1])]
poly_features_test_df.head()<features_selection>
dataframes = []
test = ImageList.from_df(path='test', df=df_test)
 learn = load_learner(path="/kaggle/working/", test=test)
 preds, _ = learn.get_preds(ds_type=DatasetType.Test )
other_cols_test=test_df.loc[:, ~test_df.columns.isin(features_names_upd)]<define_variables>
output= preds.argmax(dim=1 )
id_code=other_cols_test.ID_code<drop_column>
df_sub = test_id
other_cols_test=other_cols_test.drop(columns=['ID_code'] )<concatenate>
df_sub['label'] = pd.Series(output )
final_test_df=pd.concat([poly_features_test_df,other_cols_test],axis=1 )<normalization>
!rm -rf train
train_x=reduce_mem_usage(train_x)
 final_test_df=reduce_mem_usage(final_test_df )<set_options>
df_sub.to_csv(path_or_buf ="submission.csv", index=False )