Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<categorify>
import pandas as pd
from fastai.vision.all import *
import os
print("shape before encoding: ", identity_data.shape, "
")
 print("columns to encode: ", low_card_id_cols, "
")
 low_card_id_encoded = pd.get_dummies(identity_data[low_card_id_cols], dummy_na = False)
 identity_data.drop(columns = low_card_id_cols, inplace = True)
 print("shape after encoding: ", identity_data.shape, "
")
 print("shape of new dataframe: ", low_card_id_encoded.shape, "
")
 print("newly generated columns: ", low_card_id_encoded.columns, "
")
 print("low_card_id_encoded.info() : ", low_card_id_encoded.info() )<count_missing_values>
print(torch.cuda.is_available() , torch.backends.cudnn.enabled)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
print(transaction_data.isnull().sum().sum() , "
")
 print(low_card_trans_encoded.isnull().sum().sum() )<count_missing_values>
path = Path('.. /input/cassava-leaf-disease-classification' )
print(identity_data.isnull().sum().sum() , "
")
 print(low_card_id_encoded.isnull().sum().sum() )<concatenate>
sample_sub = pd.read_csv(path/'sample_submission.csv')
 sample_sub
print("train_transaction.shape before concatting: ", train_transaction.shape, "
")
 print("train_identity.shape before concatting: ", train_identity.shape, "
")
 train_data = pd.concat([train_transaction, train_identity], axis = 1)
 print("train_data.shape: ", train_data.shape )<count_missing_values>
train = pd.read_csv(path/'train.csv')
 print(train.shape)
 print(train.label.value_counts())
 print(train.label.value_counts(1))
counter = 0
for i in train_data.columns:
summ = train_data[i].isnull().sum()
print(i, summ)
 if summ > 0:
counter += 1
print("
number of columns with missing values: ", counter )<count_missing_values>
train = train[~train['image_id'].isin(['1562043567.jpg', '3551135685.jpg', '2252529694.jpg'])]
counter = 0
for i in test_data.columns:
summ = test_data[i].isnull().sum()
print(i, summ)
 if summ > 0:
counter += 1
print("
number of columns with missing values: ", counter )<init_hyperparams>
def get_x(r):
return path/'train_images'/r['image_id']
def get_y(r):
return r['label']
")
<save_to_csv>
dls = data.dataloaders(train, bs=bs)
 dls.show_batch(max_n=9 )
<set_options>
learn = cnn_learner(dls, resnet50, loss_func=LabelSmoothingCrossEntropy() , metrics=accuracy ).to_native_fp16()
FEATURE = True
warnings.filterwarnings("ignore")
 print("XGBoost version:", xgb.__version__)
 print("Tensorflow version " + tf.__version__)
 AUTO = tf.data.experimental.AUTOTUNE
str_type = ['ProductCD', 'card4', 'card6', 'P_emaildomain', 'R_emaildomain','M1', 'M2', 'M3', 'M4','M5',
'M6', 'M7', 'M8', 'M9', 'id_12', 'id_15', 'id_16', 'id_23', 'id_27', 'id_28', 'id_29', 'id_30',
'id_31', 'id_33', 'id_34', 'id_35', 'id_36', 'id_37', 'id_38', 'DeviceType', 'DeviceInfo']
str_type += ['id-12', 'id-15', 'id-16', 'id-23', 'id-27', 'id-28', 'id-29', 'id-30',
'id-31', 'id-33', 'id-34', 'id-35', 'id-36', 'id-37', 'id-38']
cols = ['TransactionID', 'TransactionDT', 'TransactionAmt',
'ProductCD', 'card1', 'card2', 'card3', 'card4', 'card5', 'card6',
'addr1', 'addr2', 'dist1', 'dist2', 'P_emaildomain', 'R_emaildomain',
'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'C10', 'C11',
'C12', 'C13', 'C14', 'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8',
'D9', 'D10', 'D11', 'D12', 'D13', 'D14', 'D15', 'M1', 'M2', 'M3', 'M4',
'M5', 'M6', 'M7', 'M8', 'M9']
v = [1, 3, 4, 6, 8, 11]
v += [13, 14, 17, 20, 23, 26, 27, 30]
v += [36, 37, 40, 41, 44, 47, 48]
v += [54, 56, 59, 62, 65, 67, 68, 70]
v += [76, 78, 80, 82, 86, 88, 89, 91]
v += [107, 108, 111, 115, 117, 120, 121, 123]
v += [124, 127, 129, 130, 136]
v += [138, 139, 142, 147, 156, 162]
v += [165, 160, 166]
v += [178, 176, 173, 182]
v += [187, 203, 205, 207, 215]
v += [169, 171, 175, 180, 185, 188, 198, 210, 209]
v += [218, 223, 224, 226, 228, 229, 235]
v += [240, 258, 257, 253, 252, 260, 261]
v += [264, 266, 267, 274, 277]
v += [220, 221, 234, 238, 250, 271]
v += [294, 284, 285, 286, 291, 297]
v += [303, 305, 307, 309, 310, 320]
v += [281, 283, 289, 296, 301, 314]
cols += ['V'+str(x)for x in v]
dtypes = {}
for c in cols+['id_0'+str(x)for x in range(1,10)]+['id_'+str(x)for x in range(10,34)]+\
['id-0'+str(x)for x in range(1,10)]+['id-'+str(x)for x in range(10,34)]:
dtypes[c] = 'float32'
for c in str_type: dtypes[c] = 'category'<set_options>
learn.lr_find()
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )<set_options>
cbs1 = [MixUp(alpha = 0.7)]
cbs2 = [MixUp(alpha = 0.3)]
MIXED_PRECISION = False
XLA_ACCELERATE = True
if MIXED_PRECISION:
if tpu: policy = tf.keras.mixed_precision.experimental.Policy('mixed_bfloat16')
 else: policy = tf.keras.mixed_precision.experimental.Policy('mixed_float16')
 mixed_precision.set_policy(policy)
 print('Mixed precision enabled')
 if XLA_ACCELERATE:
tf.config.optimizer.set_jit(True)
 print('Accelerated Linear Algebra enabled' )<load_from_csv>
learn.freeze()
learn.fit_one_cycle(25, slice(3e-2), cbs = cbs1 )
%%time
if FEATURE:
X_train = pd.read_csv('.. /input/ieee-with-feature/IEEE_Train.csv', index_col = 'TransactionID')
 X_test = pd.read_csv('.. /input/ieee-with-feature/IEEE_Test.csv', index_col = 'TransactionID')
 y_train = pd.read_csv('.. /input/ieee-with-feature/IEEE_Target.csv', index_col = 'TransactionID')
 else:
X_train = pd.read_csv('.. /input/ieee-fraud-detection/train_transaction.csv',index_col='TransactionID', dtype=dtypes, usecols=cols+['isFraud'])
 train_id = pd.read_csv('.. /input/ieee-fraud-detection/train_identity.csv',index_col='TransactionID', dtype=dtypes)
 X_train = X_train.merge(train_id, how='left', left_index=True, right_index=True)
 X_test = pd.read_csv('.. /input/ieee-fraud-detection/test_transaction.csv',index_col='TransactionID', dtype=dtypes, usecols=cols)
 test_id = pd.read_csv('.. /input/ieee-fraud-detection/test_identity.csv',index_col='TransactionID', dtype=dtypes)
 fix = {o:n for o, n in zip(test_id.columns, train_id.columns)}
test_id.rename(columns=fix, inplace=True)
 X_test = X_test.merge(test_id, how='left', left_index=True, right_index=True)
 y_train = X_train['isFraud'].copy()
del train_id, test_id, X_train['isFraud']; x = gc.collect()
print('Train shape',X_train.shape,'test shape',X_test.shape )<data_type_conversions>
learn.unfreeze()
learn.lr_find()
<feature_engineering>
learn.fit_one_cycle(5, slice(2e-5/50), cbs = cbs2 )
if not FEATURE:
for i in range(1,16):
if i in [1,2,3,5,9]: continue
X_train['D'+str(i)] = X_train['D'+str(i)] - X_train.TransactionDT/np.float32(24*60*60)
 X_test['D'+str(i)] = X_test['D'+str(i)] - X_test.TransactionDT/np.float32(24*60*60 )<data_type_conversions>
sub = pd.read_csv(path/'sample_submission.csv')
 sub
%%time
if not FEATURE:
for i,f in enumerate(X_train.columns):
if(np.str(X_train[f].dtype)=='category')|(X_train[f].dtype=='object'):
df_comb = pd.concat([X_train[f],X_test[f]],axis=0)
 df_comb,_ = df_comb.factorize(sort=True)
 if df_comb.max() >32000: print(f,'needs int32')
 X_train[f] = df_comb[:len(X_train)].astype('int16')
 X_test[f] = df_comb[len(X_train):].astype('int16')
 elif f not in ['TransactionAmt','TransactionDT']:
mn = np.min(( X_train[f].min() ,X_test[f].min()))
X_train[f] -= np.float32(mn)
 X_test[f] -= np.float32(mn)
 X_train[f].fillna(-1,inplace=True)
 X_test[f].fillna(-1,inplace=True )<categorify>
sub2 = sub.copy()
sub2['image_id'] = sub2['image_id'].apply(lambda x: f'.. /test_images/{x}' )
def encode_FE(df1, df2, cols):
for col in cols:
df = pd.concat([df1[col],df2[col]])
 vc = df.value_counts(dropna=True, normalize=True ).to_dict()
vc[-1] = -1
nm = col+'_FE'
df1[nm] = df1[col].map(vc)
 df1[nm] = df1[nm].astype('float32')
 df2[nm] = df2[col].map(vc)
 df2[nm] = df2[nm].astype('float32')
 print(nm,', ',end='')
 def encode_LE(col,train=X_train,test=X_test,verbose=True):
df_comb = pd.concat([train[col],test[col]],axis=0)
 df_comb,_ = df_comb.factorize(sort=True)
 nm = col
if df_comb.max() >32000:
train[nm] = df_comb[:len(train)].astype('int32')
 test[nm] = df_comb[len(train):].astype('int32')
 else:
train[nm] = df_comb[:len(train)].astype('int16')
 test[nm] = df_comb[len(train):].astype('int16')
 del df_comb; x=gc.collect()
if verbose: print(nm,', ',end='')
 def encode_AG(main_columns, uids, aggregations=['mean'], train_df=X_train, test_df=X_test,
fillna=True, usena=False):
for main_column in main_columns:
for col in uids:
for agg_type in aggregations:
new_col_name = main_column+'_'+col+'_'+agg_type
temp_df = pd.concat([train_df[[col, main_column]], test_df[[col,main_column]]])
 if usena: temp_df.loc[temp_df[main_column]==-1,main_column] = np.nan
temp_df = temp_df.groupby([col])[main_column].agg([agg_type] ).reset_index().rename(
 columns={agg_type: new_col_name})
 temp_df.index = list(temp_df[col])
 temp_df = temp_df[new_col_name].to_dict()
train_df[new_col_name] = train_df[col].map(temp_df ).astype('float32')
 test_df[new_col_name] = test_df[col].map(temp_df ).astype('float32')
 if fillna:
train_df[new_col_name].fillna(-1,inplace=True)
 test_df[new_col_name].fillna(-1,inplace=True)
 print("'"+new_col_name+"'",', ',end='')
 def encode_CB(col1,col2,df1=X_train,df2=X_test):
nm = col1+'_'+col2
df1[nm] = df1[col1].astype(str)+'_'+df1[col2].astype(str)
 df2[nm] = df2[col1].astype(str)+'_'+df2[col2].astype(str)
 encode_LE(nm,verbose=False)
 print(nm,', ',end='')
 def encode_AG2(main_columns, uids, train_df=X_train, test_df=X_test):
for main_column in main_columns:
for col in uids:
comb = pd.concat([train_df[[col]+[main_column]],test_df[[col]+[main_column]]],axis=0)
 mp = comb.groupby(col)[main_column].agg(['nunique'])['nunique'].to_dict()
train_df[col+'_'+main_column+'_ct'] = train_df[col].map(mp ).astype('float32')
 test_df[col+'_'+main_column+'_ct'] = test_df[col].map(mp ).astype('float32')
 print(col+'_'+main_column+'_ct, ',end='' )<categorify>
learn = learn.to_native_fp32()
%%time
if not FEATURE:
X_train['cents'] =(X_train['TransactionAmt'] - np.floor(X_train['TransactionAmt'])).astype('float32')
 X_test['cents'] =(X_test['TransactionAmt'] - np.floor(X_test['TransactionAmt'])).astype('float32')
 print('cents, ', end='')
 encode_FE(X_train,X_test,['addr1','card1','card2','card3','P_emaildomain'])
 encode_CB('card1','addr1')
 encode_CB('card1_addr1','P_emaildomain')
 encode_FE(X_train,X_test,['card1_addr1','card1_addr1_P_emaildomain'])
 encode_AG(['TransactionAmt','D9','D11'],['card1','card1_addr1','card1_addr1_P_emaildomain'],['mean','std'],usena=True )<drop_column>
test_dl = learn.dls.test_dl(sub2 )
if not FEATURE:
cols = list(X_train.columns)
 cols.remove('TransactionDT')
 for c in ['D6','D7','D8','D9','D12','D13','D14']:
cols.remove(c)
 for c in ['C3','M5','id_08','id_33']:
cols.remove(c)
 for c in ['card4','id_07','id_14','id_21','id_30','id_32','id_34']:
cols.remove(c)
 for c in ['id_'+str(x)for x in range(22,28)]:
cols.remove(c )<define_variables>
test_dl.show_batch()
if not FEATURE:
print('NOW USING THE FOLLOWING',len(cols),'FEATURES.')
 np.array(cols )<feature_engineering>
preds, _ = learn.tta(dl=test_dl )
if not FEATURE:
START_DATE = datetime.datetime.strptime('2017-11-30', '%Y-%m-%d')
 X_train['DT_M'] = X_train['TransactionDT'].apply(lambda x:(START_DATE + datetime.timedelta(seconds = x)))
 X_train['DT_M'] =(X_train['DT_M'].dt.year-2017)*12 + X_train['DT_M'].dt.month
X_test['DT_M'] = X_test['TransactionDT'].apply(lambda x:(START_DATE + datetime.timedelta(seconds = x)))
 X_test['DT_M'] =(X_test['DT_M'].dt.year-2017)*12 + X_test['DT_M'].dt.month<data_type_conversions>
sub['label'] = preds.argmax(dim=-1 ).numpy()
sub
<categorify><EOS> 
sub.to_csv('submission.csv',index=False )