Kaggle competition: Digit Recognizer
categorical_cols=['season','holiday','workingday','weather']
numerical_cols=['temp','atemp','humidity','windspeed']
label='count'<feature_engineering>
sample_submission = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 train = pd.read_csv(".. /input/digit-recognizer/train.csv" )
train['Month'] = train['datetime'].dt.month
test['Month'] = test['datetime'].dt.month
train['Year'] = train['datetime'].dt.year
test['Year'] = test['datetime'].dt.year
train['WeekDay'] = train['datetime'].dt.weekday
test['WeekDay'] = test['datetime'].dt.weekday
train['Hour'] = train['datetime'].dt.hour
test['Hour'] = test['datetime'].dt.hour<categorify>
x = np.ndarray(shape =(train.values.shape[0], 28, 28, 1))
x_label = np.ndarray(shape = train.values.shape[0])
 i = 0
for img in train.values:
x[i, :, :, 0] = np.reshape(img[1:],(28, 28)) /255
x_label[i] = img[0]
i += 1
def encodetime(train,test,col,label):
d3=train[[col,label]].groupby(col ).mean()
d3.sort_values(by='count',ascending=False)
 plt.scatter(x=d3.index,y=d3['count'])
 plt.xlabel(col)
 d3=d3.sort_values(by='count')
 d3['w']=np.arange(train[col].nunique())
 dic=dict(zip(d3.index,d3['w']))
train[col]=train[col].map(dic)
 test[col]=test[col].map(dic )<categorify>
rot_gen = ImageDataGenerator(rotation_range = 25, width_shift_range = 3, height_shift_range = 3,
brightness_range = [0.65, 1.5], shear_range = 15, zoom_range = 0.15)
 x_aug = rot_gen.flow(x, batch_size=1 )
encodetime(train,test,'Year',label )<categorify>
encodetime(train,test,'Month',label)
<categorify>
x_train, x_test, y_train, y_test = train_test_split(x, x_label, test_size=0.2, random_state=42 )
encodetime(train,test,'Hour',label)
<categorify>
Y_train = ks.utils.to_categorical(y_train)
 Y_test = ks.utils.to_categorical(y_test )
encodetime(train,test,'WeekDay',label)
<feature_engineering>
new = rot_gen.flow(x_train, Y_train, batch_size = 10 )
features=['holiday', 'workingday', 'weather', 'temp', 'atemp', 'Hour']
def lag(data,features, shift):
for feature in features:
data['lag_'+str(shift)+'_'+feature] = data[feature].shift(shift )<create_dataframe>
for i in range(10):
print(np.argmax(lbl[i]))
<count_missing_values>
aug = rot_gen.flow(x_train, Y_train, batch_size = 40)
 x_train_aug = np.ndarray(shape =(x_train.shape[0]*41, 28, 28, 1))
Y_train_aug = np.ndarray(shape =(Y_train.shape[0]*41, 10))
x_train_aug[: x_train.shape[0], :, :, :] = x_train
Y_train_aug[: x_train.shape[0], :] = Y_train
for i in range(x_train.shape[0] - 1):
nn = aug.next()
x_train_aug[x_train.shape[0] + i*40: x_train.shape[0] +(i + 1)*40, :, :, :] = nn[0]/255
Y_train_aug[x_train.shape[0] + i*40: x_train.shape[0] +(i + 1)*40, :] = nn[1]
train.isnull().sum()<count_missing_values>
class makestats(Callback):
def on_epoch_end(self, batch, logs=None):
if batch % 5 == 0 and batch != 0:
YY_test = np.argmax(Y_test, axis=1)
 y_pred = model.predict(x_test)
 print(classification_report(YY_test, np.argmax(y_pred, axis = 1)))
 print(confusion_matrix(( YY_test), y_pred.argmax(axis=1)) )
train.isnull().sum()<train_model>
inputA = Input(shape =(28, 28, 1))
x = Conv2D(32, kernel_size=(5, 5), strides=(1),
activation='relu',
padding = 'same' )(inputA)
 x = MaxPooling2D(pool_size=(2, 2), strides=(2))(x)
 x = Dropout(0.3 )(x)
 x = Conv2D(64,(5, 5), activation='relu' )(x)
 x = MaxPooling2D(pool_size=(2, 2))(x)
 x = Dropout(0.3 )(x)
 x = Flatten()(x)
 x = Dense(1000, activation='relu' )(x)
 x = Dropout(0.3 )(x)
 z = Dense(500, activation="relu" )(x)
 x = Dropout(0.3 )(x)
 z = Dense(10, activation="softmax" )(z)
 model = Model(inputs=inputA, outputs=z)
 epochs = 40
lrate = 0.0003
decay = lrate/epochs
sgd = SGD(lr=lrate, momentum=0.9, decay=decay, nesterov=False)
 model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'] )
train.fillna(-1, inplace=True)
 test.fillna(-1, inplace=True )<split>
model.fit(x_train_aug, Y_train_aug, validation_data=(x_test, Y_test), verbose = 1,
epochs=epochs, batch_size=32, callbacks = [history, makestats() ] )
X = train.drop(['count','datetime'],axis=1)
 xtest = test.drop(['datetime'],axis=1)
 y = train['count']
tss = TimeSeriesSplit(n_splits = 5)
 for train_index, test_index in tss.split(X):
X_train, X_valid = X.iloc[train_index, :], X.iloc[test_index,:]
y_train, y_valid = y.iloc[train_index], y.iloc[test_index]<split>
TEST = np.ndarray(shape =(test.values.shape[0], 28, 28, 1))
i = 0
for img in test.values:
TEST[i, :, :, 0] = np.reshape(img,(28, 28)) /255
i += 1
xt,xv,yt,yv=train_test_split(X,y,test_size=0.2,random_state=0 )<train_model>
y_pred = model.predict(TEST )
def mk_model_xgb(xt,xv,yt,yv,func1,func2,lr=1,min_child_weight =25,colsample_bytree = 0.8,md=None):
model =XGBRegressor(colsample_bytree = colsample_bytree, learning_rate = lr,min_child_weight =min_child_weight, max_depth=md)
 ytt=yt.apply(func1)
 model.fit(xt,ytt)
 ypt=np.apply_along_axis(func2,arr=model.predict(xt),axis=0)
 ypv=np.apply_along_axis(func2,arr=model.predict(xv),axis=0)
 print('training r2:',r2_score(yt,ypt))
print('Validation r2:',r2_score(yv,ypv))
print('training rmsle:',np.sqrt(msle(yt,ypt)))
 print('validation rmsle:',np.sqrt(msle(yv,ypv)))
 return model<train_model>
preds = []
for yy in y_pred:
preds.append(np.argmax(yy))
_=mk_model_xgb(X_train,X_valid,y_train,y_valid,func1=trans,func2=rev_trans,lr=0.2,min_child_weight =20,colsample_bytree = 0.8,md=20 )<split>
out_file = open("predictions.csv", "w")
 out_file.write("ImageId,Label
")
 for i in range(len(preds)) :
out_file.write(str(i+1)+ "," + str(int(preds[i])) + "
")
 out_file.close()