Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<data_type_conversions>
for var in categoricalFeatureNames:
data[var] = data[var].astype("category" )<prepare_x_and_y>
from fastai.vision.all import *
dataTrain = data[pd.notnull(data['count'])].sort_values(by=["datetime"])
 dataTest = data[~pd.notnull(data['count'])].sort_values(by = ["datetime"])
 datetimecol = dataTest["datetime"]
yLabels = dataTrain["count"]
yLabelsRegistered = dataTrain["registered"]
yLabelsCasual = dataTrain["casual"]<split>
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)
X_train, X_validate, y_train, y_validate = train_test_split(dataTrain, yLabels, test_size=0.3, random_state=42)
 dateTimeColValidate = X_validate["datetime"]<drop_column>
def get_x(row): return row['image_id']
def get_y(row): return row['label']
dataTrain = dataTrain.drop(dropFeatures,axis=1)
 dataTest = dataTest.drop(dropFeatures,axis=1)
 X_train = X_train.drop(dropFeatures,axis=1)
 X_validate = X_validate.drop(dropFeatures,axis=1 )<compute_test_metric>
def rmsle(y, y_,convertExp=True):
if convertExp:
y = np.exp(y),
y_ = np.exp(y_)
 log1 = np.nan_to_num(np.array([np.log(v + 1)for v in y]))
log2 = np.nan_to_num(np.array([np.log(v + 1)for v in y_]))
calc =(log1 - log2)** 2
return np.sqrt(np.mean(calc))<set_options>
learn=load_learner(".. /input/resnext50/baseline_rsnx",cpu=False )
pd.options.mode.chained_assignment = None
warnings.filterwarnings("ignore", category=DeprecationWarning )<predict_on_test>
learn = learn.to_native_fp32()
lModel = LinearRegression()
lModel.fit(X = X_train,y = np.log1p(y_train))
preds = lModel.predict(X= X_validate)
 print("RMSLE Value For Linear Regression In Validation: ",rmsle(np.exp(np.log1p(y_validate)) ,np.exp(preds),False))<compute_test_metric>
data_path=".. /input/cassava-leaf-disease-classification/"
<train_on_grid>
sample_df = pd.read_csv(data_path+'sample_submission.csv')
 sample_df.head()
ridge_m_ = Ridge()
ridge_params_ = { 'max_iter':[3000],'alpha':[0.01,0.05,0.1, 1, 2, 3, 4, 10, 30,100,200,300,400,800,900,1000]}
rmsle_scorer = metrics.make_scorer(rmsle, greater_is_better = False)
 grid_ridge_m = GridSearchCV(ridge_m_,
ridge_params_,
scoring = rmsle_scorer,
cv=5)
 grid_ridge_m.fit(X = X_train, y = np.log1p(y_train))
preds = grid_ridge_m.predict(X = X_validate)
 print(grid_ridge_m.best_params_)
 print("RMSLE Value For Ridge Regression: ",rmsle(np.exp(np.log1p(y_validate)) ,np.exp(preds),False))
fig,ax= plt.subplots()
fig.set_size_inches(20,5)
 df = pd.DataFrame(grid_ridge_m.cv_results_)
 df
df["alpha"] = df["params"].apply(lambda x:x["alpha"])
 df["rmsle"] = df["mean_test_score"].apply(lambda x:-x)
 sn.pointplot(data=df,x="alpha",y="rmsle",ax=ax )<train_on_grid>
sample_copy = sample_df.copy()
sample_copy['image_id'] = sample_copy['image_id'].apply(lambda x: ".. /input/cassava-leaf-disease-classification/test_images/"+x )
lasso_m_ = Lasso()
alpha = [0.001,0.005,0.01,0.3,0.1,0.3,0.5,0.7,1]
lasso_params_ = { 'max_iter':[3000],'alpha':alpha}
grid_lasso_m = GridSearchCV(lasso_m_, lasso_params_, scoring = rmsle_scorer, cv = 5)
 grid_lasso_m.fit(X = X_train,y = np.log1p(y_train))
preds = grid_lasso_m.predict(X= X_validate)
 print(grid_lasso_m.best_params_)
 print("RMSLE Value: ",rmsle(np.exp(np.log1p(y_validate)) ,np.exp(preds), False))
fig,ax= plt.subplots()
fig.set_size_inches(20,5)
 df = pd.DataFrame(grid_lasso_m.cv_results_)
 df["alpha"] = df["params"].apply(lambda x:x["alpha"])
 df["rmsle"] = df["mean_test_score"].apply(lambda x:-x)
 sn.pointplot(data=df,x="alpha",y="rmsle",ax=ax )<train_model>
test_dl = learn.dls.test_dl(sample_copy )
rfModel = RandomForestRegressor(n_estimators=100)
 rfModel.fit(X = X_train,y = np.log1p(y_train))
preds = rfModel.predict(X= X_validate)
 print("RMSLE Value: ",rmsle(np.exp(np.log1p(y_validate)) ,np.exp(preds), False))<save_to_csv>
preds, _ = learn.tta(dl=test_dl, n=15, beta=0 )
submission = pd.DataFrame({
"datetime": datetimecol,
"count": [max(0, x)for x in np.exp(predsTest)]
})
 submission.to_csv('bike_predictions_gbm_separate_without_fe.csv', index=False )<load_from_csv>
sample_df['label'] = preds.argmax(dim=-1 ).numpy()
train = pd.read_csv("/kaggle/input/bike-sharing-demand/train.csv", parse_dates=["datetime"])
 test = pd.read_csv("/kaggle/input/bike-sharing-demand/test.csv", parse_dates=["datetime"] )<feature_engineering>
sample_df.to_csv('submission.csv',index=False )
<feature_engineering><EOS> 
pd.read_csv("./submission.csv" )