Kaggle competition: Cassava Leaf Disease Classification
train['id_15'] = train['id_15'].str.lower().astype('category')
 test['id_15'] = test['id_15'].str.lower().astype('category' )<data_type_conversions>
from fastai.vision.all import *
def SplitEmailDomain(df):
df['P_emaildomain_prefix'] = df['P_emaildomain'].str.split('.',1, expand=True)[0].astype('category')
 df['P_emaildomain_sufix'] = df['P_emaildomain'].str.split('.',1, expand=True)[1].fillna('unknown' ).astype('category')
 df['R_emaildomain_prefix'] = df['R_emaildomain'].str.split('.',1, expand=True)[0].astype('category')
 df['R_emaildomain_sufix'] = df['R_emaildomain'].str.split('.',1, expand=True)[1].fillna('unknown' ).astype('category')
 return df<feature_engineering>
class AlbumentationsTransform(RandTransform):
"A transform handler for multiple `Albumentation` transforms"
split_idx,order=None,2
def __init__(self, train_aug, valid_aug): store_attr()
def before_call(self, b, split_idx):
self.idx = split_idx
def encodes(self, img: PILImage):
if self.idx == 0:
aug_img = self.train_aug(image=np.array(img)) ['image']
else:
aug_img = self.valid_aug(image=np.array(img)) ['image']
return PILImage.create(aug_img )
train = SplitEmailDomain(train)
 test = SplitEmailDomain(test)
 train.head()<feature_engineering>
def get_x(row): return data_path/row['image_id']
def get_y(row): return row['label']
for i in range(1,16):
if i not in [2,6,7]:
train['D'+str(i)] = train['D'+str(i)] - train.TransactionDT/np.float32(24*60*60)
 test['D'+str(i)] = test['D'+str(i)] - test.TransactionDT/np.float32(24*60*60 )<categorify>
class LeafModel(Module):
def __init__(self, num_classes):
self.effnet = EfficientNet.from_pretrained("efficientnet-b3")
 self.dropout = nn.Dropout(0.1)
 self.out = nn.Linear(1536, num_classes)
 def forward(self, image):
batch_size, _, _, _ = image.shape
x = self.effnet.extract_features(image)
 x = F.adaptive_avg_pool2d(x, 1 ).reshape(batch_size, -1)
 outputs = self.out(self.dropout(x))
return outputs
cat_cols = cat_cols + ['device_name','id_30_word1','P_emaildomain_prefix','P_emaildomain_sufix',\
'R_emaildomain_prefix','R_emaildomain_sufix']
train_copy = train.copy()
test_copy = test.copy()
for col in cat_cols:
le = LabelEncoder()
le.fit(list(train_copy[col].astype(str ).values)+ list(test_copy[col].astype(str ).values))
train_copy[col] = le.transform(list(train_copy[col].astype(str ).values))
test_copy[col] = le.transform(list(test_copy[col].astype(str ).values))
first_day = train_copy['TransactionDT'].min()
five_months = first_day + 5*30*24*3600
X_train = train_copy.loc[train_copy['TransactionDT'] <= five_months,cat_cols + num_cols]
y_train = train_copy.loc[train_copy['TransactionDT'] <= five_months,'isFraud']
X_valid = train_copy.loc[train_copy['TransactionDT'] > five_months,cat_cols + num_cols]
y_valid = train_copy.loc[train_copy['TransactionDT'] > five_months,'isFraud']
X_test = test_copy[cat_cols + num_cols]
del train_copy
del test_copy
gc.collect()<choose_model_class>
path = Path(".. /input")
 data_path = path/'cassava-leaf-disease-classification'
params = {'num_leaves': 256,
'min_child_samples': 79,
'objective': 'binary',
'max_depth': 8,
'learning_rate': 0.02,
"boosting_type": "gbdt",
"subsample_freq": 3,
"subsample": 0.9,
"bagging_seed": 11,
"metric": 'auc',
'reg_alpha': 0.3,
'reg_lambda': 0.3,
'colsample_bytree': 0.9,
}
model = lgb.LGBMClassifier(**params, n_estimators=2000, n_jobs = -1)
 model.fit(X_train, y_train,eval_set=[(X_train, y_train),(X_valid, y_valid)], eval_metric='auc', early_stopping_rounds=100,verbose=50)
 y_pred_valid = model.predict_proba(X_valid)[:, 1]
y_pred = model.predict_proba(X_test)[:, 1]
score = metrics.roc_auc_score(y_valid, y_pred_valid)
 feat_imp = pd.Series(model.feature_importances_, index=X_train.columns)
 print('AUC score: {:.3f}'.format(score))
fig,ax = plt.subplots(figsize=(10,10))
_ = feat_imp.nlargest(20 ).sort_values().plot(kind='barh',ax=ax)
 _ = ax.set_xlabel('Average importance(a.u.) ')
 _ = ax.set_ylabel('Features' )<save_to_csv>
learn = load_learner(Path('.. /input/abishektez/baseline'), cpu=False )
output = pd.DataFrame({'TransactionID': test.TransactionID, 'isFraud': y_pred})
 output.to_csv('lightgbm_submission.csv', index=False)
 print("Submission successfully saved!" )<load_from_csv>
learn.to_native_fp32()
submission = pd.read_csv("/kaggle/input/cat-in-the-dat/sample_submission.csv")
 train = pd.read_csv("/kaggle/input/cat-in-the-dat/train.csv")
 test = pd.read_csv("/kaggle/input/cat-in-the-dat/test.csv" )<drop_column>
sample_df = pd.read_csv(data_path/'sample_submission.csv')
 sample_df.head()
test_id = test["id"]
test.drop(['id'],inplace=True, axis = 1)
 test.head()<prepare_x_and_y>
sample_copy = sample_df.copy()
sample_copy['image_id'] = sample_copy['image_id'].apply(lambda x: f'test_images/{x}' )
y = train['target']
X = train.drop(['target', 'id'], axis = 1)
 X.head()<define_variables>
test_dl = learn.dls.test_dl(sample_copy )
cat_columns = [cols for cols in train.columns if train[cols].dtype == 'object']
print(cat_columns )<categorify>
preds, _ = learn.tta(dl=test_dl, n=15, beta=0 )
encoder = LabelEncoder()
for col in cat_columns:
X[col] = pd.DataFrame(encoder.fit_transform(X[col]))
test[col] = pd.DataFrame(encoder.fit_transform(test[col])) 
<split>
sample_df['label'] = preds.argmax(dim=-1 ).numpy()
<train_on_grid><EOS> 
sample_df.to_csv('submission.csv',index=False )