Kaggle competition: Digit Recognizer
new_tr['year'] = new_tr['datetime'].dt.year
new_tr['month'] = new_tr['datetime'].dt.month
new_tr['day'] = new_tr['datetime'].dt.day
new_tr['hour'] = new_tr['datetime'].dt.hour
new_tr['minute'] = new_tr['datetime'].dt.minute
new_tr['second'] = new_tr['datetime'].dt.second
new_tr['dayofweek'] = new_tr['datetime'].dt.dayofweek<feature_engineering>
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from tqdm import tqdm
import cv2 as cv
import random
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import train_test_split
import tensorflow.keras
from tensorflow.keras.models import Sequential, load_model, Model
from tensorflow.keras import layers
from tensorflow.keras.optimizers import Adam, SGD, RMSprop
from tensorflow.keras.metrics import Precision, Recall, AUC
from tensorflow.keras.callbacks import ModelCheckpoint, Callback, EarlyStopping
new_test['year'] = new_test['datetime'].dt.year
new_test['month'] = new_test['datetime'].dt.month
new_test['day'] = new_test['datetime'].dt.day
new_test['hour'] = new_test['datetime'].dt.hour
new_test['minute'] = new_test['datetime'].dt.minute
new_test['second'] = new_test['datetime'].dt.second
new_test['dayofweek'] = new_test['datetime'].dt.dayofweek<define_variables>
train_df = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test_df = pd.read_csv('.. /input/digit-recognizer/test.csv')
 train_df.head()
feature_names = [ 'season', 'holiday', 'workingday', 'weather',
'temp', 'atemp', 'humidity', 'windspeed',
"year", "hour", "dayofweek"]<import_modules>
%%time
samples, columns = train_df.shape
X = np.zeros(( samples,28,28,1))
y_true = np.zeros(( samples,1))
for sample in tqdm(range(samples)) :
X[sample,:,:,:] = train_df.iloc[sample,1:columns].values.reshape(28,28,1 ).astype('float32')
 y_true[sample,0] = train_df.iloc[sample,0]
from lightgbm import LGBMRegressor
from sklearn.model_selection import train_test_split<feature_engineering>
values = train_df.label
label_encoder = LabelEncoder()
integer_encoded = label_encoder.fit_transform(values)
 print('The original output labels', values)
 onehot_encoder = OneHotEncoder(sparse=False)
 integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
 onehot_encoded = onehot_encoder.fit_transform(integer_encoded)
 samples, classes = onehot_encoded.shape
print("Number of vectors:", samples, "
Number of neurons / length of vector:", classes )
new_tr['log_count'] = np.log1p(new_tr['count'] )<split>
y = onehot_encoded
X = new_tr[feature_names]
y = new_tr['log_count']
X_test_last = new_test[feature_names]
X_train, X_test, y_train, y_test = train_test_split(X, y )<choose_model_class>
%%time
def standard_norm(img):
return(img - np.mean(img)) /np.std(img)
 norm_X = np.zeros(( samples,28,28,1))
for sample in tqdm(range(samples)) :
norm_X[sample,:,:,:] = standard_norm(X[sample,:,:,:] ).reshape(28,28,1)
model_lgbm = LGBMRegressor()<train_model>
def METRICS() :
metrics = ['accuracy',
Precision(name='precision'),
Recall(name='recall'),
AUC(name='AUC')]
return metrics
model = Sequential()
model.add(layers.Input(shape=(28, 28, 1)))
 model.add(layers.Conv2D(32,(3,3), padding = 'same', activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.MaxPooling2D(pool_size =(2, 2)))
 model.add(layers.Conv2D(64,(3,3), padding = 'same', activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.MaxPooling2D(pool_size =(2, 2)))
 model.add(layers.Conv2D(128,(3,3), padding = 'same', activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.GlobalAveragePooling2D())
 model.add(layers.Dense(classes,activation='softmax', name = 'output_layer'))
model.compile(Adam(lr = 0.00100005134), metrics= METRICS() , loss = 'categorical_crossentropy')
 model.summary()
model_lgbm.fit(X_train, y_train )<compute_test_metric>
def split_data(X,Y):
return train_test_split(X, Y, test_size=0.2, random_state=42)
 def train_model(model, X, Y, epochs, bs):
X_train, X_val, y_train, y_val = split_data(X,Y)
 STEP_SIZE_TRAIN = X_train.shape[0]//bs + 1
STEP_SIZE_VAL = X_val.shape[0]//bs + 1
es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=5)
 train_history = model.fit(X_train, y_train,
steps_per_epoch = STEP_SIZE_TRAIN,
validation_data =(X_val,y_val),
validation_steps = STEP_SIZE_VAL,
epochs = epochs, shuffle = True, 
)
 return train_history, model
model_lgbm.score(X_test, y_test )<choose_model_class>
epochs, bs = 20, 32
train_hist, final_model = train_model(model, norm_X, y, epochs, bs )
model_random = RandomForestRegressor()<train_model>
%%time
samples, columns = test_df.shape
X_test = np.zeros(( samples,28,28,1))
X_norm_test = np.zeros(( samples,28,28,1))
for sample in tqdm(range(samples)) :
X_test[sample,:,:,:] = test_df.iloc[sample,:].values.reshape(28,28,1 ).astype('float32')
 X_norm_test[sample,:,:,:] = standard_norm(X_test[sample,:,:].reshape(28,28,1))
model_random.fit(X_train, y_train )<compute_test_metric>

 class MNIST_CAM:
def __init__(self, img):
self.resize_width, self.resize_height, _ = img.shape
def standard_norm(self, img):
return(( img - np.mean(img)) /np.std(img))
def feature_model(self, model):
return Model(inputs = model.layers[0].input, outputs = model.layers[-3].output)
 def weight_tensor(self, model):
final_outputs = model.layers[-1]
return final_outputs.get_weights() [0]
def predict_class(self, model, X):
prob_vec = model.predict(X)
 return np.argmax(prob_vec[0])
 def generate_CAM(self, model, img):
norm_img = self.standard_norm(img)
 Fmap_model = self.feature_model(model)
 Wtensor = self.weight_tensor(model)
 feature_map = Fmap_model.predict(norm_img.reshape(1,28,28,1))
label = self.predict_class(model, norm_img.reshape(1,28,28,1))
CAM = feature_map.dot(Wtensor[:,label])[0,:,:]
return cv.resize(CAM, 
(self.resize_width, self.resize_height),
interpolation = cv.INTER_CUBIC), label
def generate_probvec(self, model, img):
X = self.standard_norm(img)
 prob_vec = model.predict(X.reshape(1,28,28,1))
return prob_vec
model_random.score(X_test, y_test )<compute_train_metric>
final_model.predict(X_norm_test[0,:,:,:].reshape(1,28,28,1))
scores = cross_val_score(model_random, X_test, y_test)
 print("{} 교차 검증 점수 : {}".format(model_random,scores))<import_modules>
y_test,test_Ids = np.zeros(( samples,1)) , np.zeros(( samples,1))
for sample in tqdm(range(samples)) :
y_test[sample,0] = np.argmax(final_model.predict(X_norm_test[sample,:,:,:].reshape(1,28,28,1)))
 test_Ids[sample,0] = int(sample+1 )
from sklearn.model_selection import LeaveOneOut, cross_val_score, KFold<choose_model_class>
label_df, pred_df = pd.DataFrame(test_Ids), pd.DataFrame(y_test)
 sub_df = pd.concat([label_df, pred_df], axis = 1)
 sub_df.iloc[:,:] = sub_df.iloc[:,:].astype('int')
 sub_df.columns = ['ImageId', 'Label']
sub_df.head()
<import_modules><EOS> 
sub_df.to_csv('sample_submission.csv', index=False )