Kaggle competition: Digit Recognizer
df[(df.atemp-df.temp ).abs() >10]<train_model>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 print(train.shape)
 print(test.shape)
 n_train_img = train.shape[0]
MAX_LR=3e-3
VALID_PCT=3000/n_train_img
VALID_PCT=.1
VALID_PCT
line_fitter = LinearRegression()
line_fitter.fit(df['temp'].values.reshape(-1,1), df['atemp'].values.reshape(-1,1))<predict_on_test>
class ArrayDataset(Dataset):
"Sample numpy array dataset"
def __init__(self, x, y):
self.x, self.y = x, y
self.c = 10
def __len__(self):
return len(self.x)
 def __getitem__(self, i):
return self.x[i], self.y[i]
def reshape_and_normalize(arr):
return arr.astype(np.float32 ).reshape([-1, 1, 28, 28])/255.0
def conv2(ni,nf):
return conv_layer(ni,nf,stride=2)
 def conv_and_res(ni,nf):
return nn.Sequential(conv2(ni, nf), res_block(nf))
def create_nn(n_channel=1):
model = nn.Sequential(
 conv_and_res(n_channel, 8),
conv_and_res(8, 16),
conv_and_res(16, 32),
conv_and_res(32, 16),
conv2(16, 10),
Flatten() 
)
 return model
def get_preds(learn):
pred, _ = learn.get_preds(ds_type=DatasetType.Test)
 print(len(pred))
pred = pd.Series(np.argmax(pred, 1))
print(pred.unique())
 return pred
def get_callbacks() :
return [partial(EarlyStoppingCallback,
patience=3 
)]
def create_learner(data, model):
learn = Learner(data, model, loss_func = nn.CrossEntropyLoss() ,
metrics=accuracy,
callback_fns=get_callbacks())
 return learn
def create_submission(learn, file='learn.csv'):
pred = get_preds(learn)
 submission = pd.DataFrame({'ImageId': range(1,1+len(pred)) , 'Label': pred})
 submission.to_csv(file, index=False)
 return
pred_result = line_fitter.predict(df[(df.atemp-df.temp ).abs() >10]['temp'].values.reshape(-1,1))<find_best_model_class>
%%time
data = prepare_dataset(train, test)
 learn = create_learner(data, create_nn())
 learn.fit_one_cycle(1, max_lr=MAX_LR)
 learn.lr_find(end_lr=10)
 learn.recorder.plot()
df.columns.to_series().groupby(df.dtypes ).groups
x_train,x_test,y_train,y_test=train_test_split(df.drop('count',axis=1),df['count'],test_size=0.25,random_state=42)
 models=[RandomForestRegressor() ]
model_names=['RandomForestRegressor']
rmsle=[]
d={}
for model in range(len(models)) :
clf=models[model]
clf.fit(x_train,y_train)
 test_pred=clf.predict(x_test)
 rmsle.append(np.sqrt(mean_squared_log_error(test_pred,y_test)))
 d={'Modelling Algo':model_names,'RMSLE':rmsle}
d<train_on_grid>
def prepare_dataset(train, test):
X = reshape_and_normalize(train.drop('label', axis=1 ).values)
 y = train.label.values
train_x, valid_x, train_y, valid_y = train_test_split(X, y,test_size=VALID_PCT)
 test_x = reshape_and_normalize(test.values)
 train_ds, valid_ds = ArrayDataset(train_x, train_y), ArrayDataset(valid_x, valid_y)
 test_ds = ArrayDataset(test_x, [EmptyLabel() ]*len(test_x))
data = ImageDataBunch.create(train_ds, valid_ds,
test_ds,
bs=100, num_workers=1)
 print("Train/Valid/Test sizes:", len(train_ds), len(valid_ds), len(test_ds))
return data
no_of_test=[500]
params_dict={'n_estimators':no_of_test,'n_jobs':[-1],'max_features':["auto",'sqrt','log2']}
clf_rf=GridSearchCV(estimator=RandomForestRegressor() ,param_grid=params_dict,scoring='neg_mean_squared_log_error')
 clf_rf.fit(x_train,y_train)
 pred=clf_rf.predict(x_test)
 print(( np.sqrt(mean_squared_log_error(pred,y_test))))<find_best_params>
tfms = get_transforms(do_flip=False)
 len(tfms)
 tfms
clf_rf.best_params_<save_to_csv>
%%time
data = ImageDataBunch.from_folder('png/train',
bs=100,
ds_tfms=tfms,
valid_pct=VALID_PCT 
)
 data.add_test(ImageList.from_df(test_df, '.'))
data.normalize()
data
pred=clf_rf.predict(test_df.drop('datetime',axis=1))
d={'datetime':test['datetime'],'count':pred}
ans=pd.DataFrame(d)
 ans.to_csv('answer.csv',index=False )<load_from_csv>
data.show_batch(figsize=(7,6))
!unzip.. /input/jigsaw-toxic-comment-classification-challenge/train.csv.zip
!unzip.. /input/jigsaw-toxic-comment-classification-challenge/test.csv.zip
!unzip.. /input/jigsaw-toxic-comment-classification-challenge/test_labels.csv.zip
!unzip.. /input/jigsaw-toxic-comment-classification-challenge/sample_submission.csv.zip<define_variables>
%%time
learn1 = create_learner(data, create_nn(3))
learn1.fit_one_cycle(1, max_lr=MAX_LR)
 learn1.lr_find(end_lr=10)
 learn1.recorder.plot()
TRAIN = './train.csv'
TEST = './test.csv'
TEST_LABEL = './test_labels.csv'
SAMPLE = './sample_submission.csv'
EPOCHS = 2
MAX_TOKEN_COUNT = 128
BATCH_SIZE = 32<set_options>
learn1.fit_one_cycle(100, max_lr=MAX_LR)
 learn1.recorder.plot()
%matplotlib inline
%config InlineBackend.figure_format='retina'
RANDOM_SEED = 42
sns.set(style='whitegrid', palette='muted', font_scale=1.2)
 HAPPY_COLORS_PALETTE = ["
sns.set_palette(sns.color_palette(HAPPY_COLORS_PALETTE))
rcParams['figure.figsize'] = 12, 8
pl.seed_everything(RANDOM_SEED )<load_from_csv>
%%time
learn2 = cnn_learner(data,
models.resnet50,
metrics=[accuracy], callback_fns=get_callbacks())
 learn2.fit_one_cycle(1, max_lr=MAX_LR)
 learn2.lr_find(end_lr=10)
 learn2.recorder.plot()
df = pd.read_csv(TRAIN)
 test_df = pd.read_csv(TEST)
 test_label = pd.read_csv(TEST_LABEL)
 sample_sub = pd.read_csv(SAMPLE)
 df.describe()<split>
%%time
learn2.fit_one_cycle(100, max_lr=MAX_LR)
 learn2.recorder.plot()
<create_dataframe><EOS> 
!rm -Rf png