Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<create_dataframe>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
X_train=X.copy()
X_test=test_cat_transformed.copy()<create_dataframe>
warnings.simplefilter('ignore' )
X_train_cat = X_train.copy()
X_test_cat = X_test.copy()<choose_model_class>
from fastai import *
from fastai.vision import *
import torchvision.models
xgb_v = XGBRegressor(
 objective='count:poisson',random_state=42)
 xgb= XGBRegressor(
 objective='count:poisson',random_state=42,
subsample= 0.83, n_estimators= 3000,
min_child_weight= 16, max_depth= 5,
learning_rate= 0.055, gamma= 1, colsample_bytree= 0.5)
 catboost_v=CatBoostRegressor(objective='Poisson',iterations=4000,l2_leaf_reg=1,learning_rate=0.054 , silent = True)
 catboosttrial=CatBoostRegressor(objective='Poisson',iterations=4000,l2_leaf_reg=1,learning_rate=0.054,silent=True)
 xgb_models={
"XGB_vanella":xgb_v,
"XGB_mod" : xgb }
cat_models ={
"Catbbost_mod" :catboosttrial,
"Catboost_Vanilla" :catboost_v}<compute_train_metric>
import os
from pathlib import Path
import pandas as pd
columns = ['Classifier', 'RMSLE']
df_scores_cat = pd.DataFrame(columns=columns)
 for name, model in cat_models.items() :
model.fit(X_train_cat, y)
 Y_predicted = model.predict(X_train_cat)
 rmsle = np.sqrt(mean_squared_log_error(y, Y_predicted))
df_row = pd.DataFrame([[name, rmsle]], columns=columns)
 df_scores_cat = df_scores_cat.append(df_row, ignore_index=True )<compute_train_metric>
train_df = pd.read_csv(INPUT/"train.csv")
 train_df.head(3 )
columns = ['Classifier', 'RMSLE']
df_scores_xgb = pd.DataFrame(columns=columns)
 for name, model in xgb_models.items() :
model.fit(X_train, y)
 Y_predicted = model.predict(X_train)
 rmsle = np.sqrt(mean_squared_log_error(y, Y_predicted))
df_row = pd.DataFrame([[name, rmsle]], columns=columns)
 df_scores_xgb = df_scores_xgb.append(df_row, ignore_index=True )<sort_values>
test_df = pd.read_csv(INPUT/"test.csv")
 test_df.head(3 )
df_scores_cat.sort_values(by=['RMSLE'] )<sort_values>
TRAIN = Path(".. /train")
 TEST = Path(".. /test" )
df_scores_xgb.sort_values(by=['RMSLE'] )<predict_on_test>
def saveDigit(digit, filepath):
digit = digit.reshape(28,28)
 digit = digit.astype(np.uint8)
 img = Image.fromarray(digit)
 img.save(filepath )
y_cat_pred=catboosttrial.predict(X_test_cat )<predict_on_test>
for index, row in train_df.iterrows() :
label,digit = row[0], row[1:]
folder = TRAIN/str(label)
 filename = f"{index}.jpg"
filepath = folder/filename
digit = digit.values
saveDigit(digit, filepath )
y_xgb_pred=xgb.predict(X_test )<data_type_conversions>
for index, digit in test_df.iterrows() :
folder = TEST
filename = f"{index}.jpg"
filepath = folder/filename
digit = digit.values
saveDigit(digit, filepath )
<data_type_conversions>
tfms = get_transforms(do_flip=False )
test_df_copy['count'] = y_cat_pred.astype('int' )<filter>
data = ImageDataBunch.from_folder(
 path = TRAIN,
test = TEST,
valid_pct = 0.2,
bs = 256,
size = 28,
num_workers = 5,
ds_tfms = tfms
).normalize(mnist_stats )
test_df_copy[test_df_copy["workingday"]=='No']<save_to_csv>
resnet_learn = Learner(data, torchvision.models.resnet50(pretrained=True), metrics=[error_rate, accuracy, top_k_accuracy], model_dir="/tmp/models", callback_fns=ShowGraph)
 googlenet_learn = Learner(data, torchvision.models.googlenet(pretrained=True), metrics=[error_rate, accuracy, top_k_accuracy], model_dir="/tmp/models", callback_fns=ShowGraph)
 resnext_learn = Learner(data, torchvision.models.resnext50_32x4d(pretrained=True), metrics=[error_rate, accuracy, top_k_accuracy], model_dir="/tmp/models", callback_fns=ShowGraph)
 wideres_learn = Learner(data, torchvision.models.wide_resnet50_2(pretrained=True), metrics=[error_rate, accuracy, top_k_accuracy], model_dir="/tmp/models", callback_fns=ShowGraph)
 mobilenet_learn = Learner(data, torchvision.models.mobilenet_v2(pretrained=True), metrics=[error_rate, accuracy, top_k_accuracy], model_dir="/tmp/models", callback_fns=ShowGraph )
test_df_copy[['datetime','count']].to_csv("submissions1.csv",index=False )<load_from_csv>
model = [resnet_learn, googlenet_learn, resnext_learn, wideres_learn, mobilenet_learn]
data_path = '/kaggle/input/bike-sharing-demand/'
train = pd.read_csv(data_path + 'train.csv')
 test = pd.read_csv(data_path + 'test.csv')
 submission = pd.read_csv(data_path + 'sampleSubmission.csv' )<filter>
for learn in model:
learn.lr_find()
learn.recorder.plot(suggestion=True )
train = train[train['weather'] != 4]<concatenate>
resnet_learn.fit_one_cycle(10 )
all_data = pd.concat([train, test], ignore_index=True)
 all_data<feature_engineering>
googlenet_learn.fit_one_cycle(10 )
all_data['date'] = all_data['datetime'].apply(lambda x: x.split() [0])
 all_data['year'] = all_data['datetime'].apply(lambda x: x.split() [0].split('-')[0])
 all_data['month'] = all_data['datetime'].apply(lambda x: x.split() [0].split('-')[1])
 all_data['hour'] = all_data['datetime'].apply(lambda x: x.split() [1].split(':')[0])
 all_data["weekday"] = all_data['date'].apply(lambda dateString : datetime.strptime(dateString,"%Y-%m-%d" ).weekday() )<drop_column>
resnext_learn.fit_one_cycle(10 )
drop_features = ['casual', 'registered', 'datetime', 'date', 'windspeed', 'month']
all_data = all_data.drop(drop_features, axis=1 )<prepare_x_and_y>
wideres_learn.fit_one_cycle(10 )
X_train = all_data[~pd.isnull(all_data['count'])]
X_test = all_data[pd.isnull(all_data['count'])]
X_train = X_train.drop(['count'], axis=1)
 X_test = X_test.drop(['count'], axis=1)
 y = train['count']<compute_test_metric>
mobilenet_learn.fit_one_cycle(10 )
def rmsle(y_true, y_pred, convertExp=True):
if convertExp:
y_true = np.exp(y_true)
 y_pred = np.exp(y_pred)
 log_true = np.nan_to_num(np.array([np.log(y+1)for y in y_true]))
log_pred = np.nan_to_num(np.array([np.log(y+1)for y in y_pred]))
output = np.sqrt(np.mean(( log_true - log_pred)**2))
return output<choose_model_class>
ImageId = [int(os.path.splitext(path)[0])+1 for path in os.listdir(TEST)]
<train_on_grid><EOS> 
model_name = ['resnet', 'googlenet', 'resnext', 'wideres', 'mobilenet']
i = 0
for learn in model:
class_score, y = learn.get_preds(DatasetType.Test)
 class_score = np.argmax(class_score, axis=1)
 submission = pd.DataFrame({"ImageId": ImageId,"Label": class_score})
 submission.to_csv("submission_"+str(model_name[i])+".csv", index=False)
 i += 1