Kaggle competition: Digit Recognizer
def rmsle(y, pred):
log_y = np.log1p(y)
 log_pred = np.log1p(pred)
 squared_error =(log_y - log_pred)** 2
rmsle = np.sqrt(np.mean(squared_error))
return rmsle<import_modules>
%matplotlib inline
np.random.seed(2)
from sklearn.linear_model import LinearRegression, Ridge, Lasso
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor
from sklearn.model_selection import GridSearchCV<train_on_grid>
trainv = pd.read_csv(".. /input/digit-recognizer/train.csv")
 testv = pd.read_csv(".. /input/digit-recognizer/test.csv" )
def evaluate(reg_cls, params=None):
reg = reg_cls()
if params:
reg = GridSearchCV(reg, param_grid=params, refit=True)
 reg.fit(X_train, y_train)
 pred = reg.predict(X_test)
 y_test_exp = np.expm1(y_test)
 pred_exp = np.expm1(pred)
 print('
', reg_cls)
 if params:
print(reg.best_params_)
 reg = reg.best_estimator_
print(rmsle(y_test_exp, pred_exp))
return reg, pred_exp<find_best_params>
Y_train = trainv["label"]
X_train = trainv.drop(labels = ["label"],axis = 1)
lr_reg, pred_lr = evaluate(LinearRegression)
 rg_reg, pred_rg = evaluate(Ridge)
 ls_reg, pred_ls = evaluate(Lasso)
 rf_reg, pred_rf = evaluate(RandomForestRegressor)
 gb_reg, pred_gb = evaluate(GradientBoostingRegressor)
 xg_reg, pred_xg = evaluate(XGBRegressor)
 lg_reg, pred_lg = evaluate(LGBMRegressor)
 params = {'n_estimators': [100*i for i in range(1, 6)]}
xg_reg, pred_xg = evaluate(XGBRegressor, params)
 lg_reg, pred_lg = evaluate(LGBMRegressor, params )<compute_test_metric>
X_train = X_train / 255.0
testv = testv / 255.0
feature_importances(xg_reg )<load_from_csv>
Y_train = to_categorical(Y_train, num_classes = 10 )
submission = pd.read_csv('.. /input/bike-sharing-demand/sampleSubmission.csv')
 submission<predict_on_test>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=3 )
pred = lg_reg.predict(test)
 pred_exp = np.expm1(pred)
 print(pred_exp )<feature_engineering>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
submission.loc[:, 'count'] = pred_exp
submission<save_to_csv>
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )
submission.to_csv('submission.csv', index=False )<save_to_csv>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
 epochs = 25
batch_size = 86
submission.to_csv('submission.csv', index=False )<import_modules>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns<load_from_csv>
history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction] )
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'])
 sub = pd.read_csv('/kaggle/input/bike-sharing-demand/sampleSubmission.csv' )<create_dataframe>
results = model.predict(testv)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
new_tr = train.copy()
new_test = test.copy()<feature_engineering>
sample_submission = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv')
 sample_submission["Label"]=results
sample_submission.to_csv("submission.csv", index=False )