Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<predict_on_test>
print("Tensorflow DL Version: " + tf.__version__)

print("Setup Completed" )
pred_registered = best_HistGradientAlgo_registered.predict(X_valid)
 print(rmsle(pred_registered,y_valid))<split>
train_file = ".. /input/digit-recognizer/train.csv"
predict_file = ".. /input/digit-recognizer/test.csv"
submission_file = ".. /input/digit-recognizer/sample_submission.csv"
train_data = pd.read_csv(train_file, sep=',')
 predict_data = pd.read_csv(predict_file, sep=',')
 print("Files Preparation Completed" )
Y_casual=casual_df.casual
casual_df.drop(columns=['casual','atemp'],inplace = True)
 X_train, X_valid, y_train, y_valid = train_test_split(casual_df,Y_casual, train_size=0.8, test_size=0.2,random_state=0 )<train_model>
print('**************************Train File Preliminary Investigation**************************')
 print('1.Train File Shape:', train_data.shape)
 missing_val_count_by_column =(train_data.isnull().sum())
 print('2.Train File Missing Valu:', missing_val_count_by_column[missing_val_count_by_column > 0])
 print(" Missing Values in Train File: {}".format(train_data.isna().any().any()))
print('3.Train File Variables Information:')
 print(train_data.info())
 print('4.Train File Variables Unique Number:')
 print(train_data.nunique())
 print(' ')
 print('**************************Prediction File Preliminary Investigation**************************')
 print('1.Test File Shape:', predict_data.shape)
 missing_val_count_by_column =(predict_data.isnull().sum())
 print('2.Test File Missing Value:', missing_val_count_by_column[missing_val_count_by_column > 0])
 print(" Missing Values in Test File: {}".format(predict_data.isna().any().any()))
print('3.Test File Variables Information:')
 print(predict_data.info())
 print('4.Test File Variables Unique Number:')
 print(predict_data.nunique() )
get_best_model(X_train, X_valid, y_train, y_valid )<train_on_grid>
def label_conversion(data, num_classes = 10):
label_array = np.array(data, dtype='uint8')
 label_array = to_categorical(label_array,num_classes=num_classes)
 return label_array
def images_conversion(data, img_row = 28, imag_col = 28):
image_array = np.array(data, dtype='uint8')
 image_array = np.reshape(image_array,(len(image_array), img_row, imag_col, 1))
image_array = image_array *(1./ 255)- 0.5
image_array = np.reshape(image_array,(len(image_array), img_row, imag_col, 1))
return image_array
print("Function Established Completed" )
HistGradientAlgo_casual = HistGradientBoostingRegressor()
param = {
'max_iter':[i for i in range(115,118)],
'max_depth' : [i for i in range(13,18)],
'max_leaf_nodes':[25]
}
gridSearch_HistGradientAlgo_casual=GridSearchCV(HistGradientAlgo_casual,param,scoring=myScorer,cv=5,verbose=3)
 gridSearch_HistGradientAlgo_casual.fit(X_train, y_train)
 best_HistGradientAlgo_casual=gridSearch_HistGradientAlgo_casual.best_estimator_
bestHistGradientAlgo_testScore_casual=best_HistGradientAlgo_casual.score(X_train, y_train )<predict_on_test>
y = label_conversion(train_data[output_list[0]], num_classes = num_classes)
 X = images_conversion(train_data.drop(output_list[0], axis=1), img_row = img_rows, imag_col = img_cols)
 X_predict = images_conversion(predict_data, img_row = img_rows, imag_col = img_cols)
 print("Conversion Completed" )
pred_casual = best_HistGradientAlgo_casual.predict(X_valid)
 print(rmsle(pred_casual,y_valid))<predict_on_test>
print("Training Data: {}
Labels: {}".format(X, y))
test_df['windspeed']=np.log1p(test_df['windspeed'])
 test_df['rushHours'] = test_df['Hour'].isin([8,17,18])
 test_df['rushHours'] = pd.factorize(test_df['rushHours'])[0].reshape(-1, 1)
 test_df['weekEnd'] = pd.factorize(test_df['weekEnd'])[0].reshape(-1, 1)
 test_df.drop(columns=['atemp'],inplace = True)
 pred_casual = np.round(np.expm1(best_HistGradientAlgo_casual.predict(test_df.drop('rushHours',axis = 1)))).astype(int)
 pred_registered = np.round(np.expm1(best_HistGradientAlgo_registered.predict(test_df)) ).astype(int)
<create_dataframe>
print("Prediction Data: {}
".format(X_predict))
pred_registered = pd.DataFrame(pred_registered,columns = ['count'])
 pred_registered<create_dataframe>
data_aug = 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)
 print("Data Augmentation Setting Completed" )
pred_casual = pd.DataFrame(pred_casual,columns = ['count'])
 pred_casual<prepare_output>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=0)
 print("Data Preparation Completed" )
predictions = pd.DataFrame({'datetime':test_df_ID})
 predictions['count'] = pred_registered['count'] + pred_casual['count']<save_to_csv>
l_model_name = 'lenet5_model'
v_model_name = 'vgg_model'
batch_size = 64
num_classes = 10
epoch_num = 6
dropout_rate = 0.5
oz = keras.optimizers.Adam(lr=0.001,
beta_1=0.9,
beta_2=0.999,
epsilon=1e-08,
decay=1e-4,
amsgrad=False)
 sgd = keras.optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
 print("Parameters Setting Completed" )
predictions.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!" )<import_modules>
lenet5_model_history = lenet5_model.fit_generator(data_aug.flow(X_train, y_train, batch_size=batch_size),
epochs = epoch_num,
validation_data =(X_test, y_test),
verbose = 2,
steps_per_epoch=X_train.shape[0],
callbacks=[l_tb_callback,
l_lr_callback,
l_ck_callback,
l_best_callback])
 lenet5_model.save(l_model_name+'.h5')
 del lenet5_model
print("Training Completed!" )
from IPython.display import Image<define_variables>
vgg_model_history = vgg_model.fit_generator(data_aug.flow(X_train, y_train, batch_size=batch_size),
epochs = epoch_num,
validation_data =(X_test, y_test),
verbose = 2,
steps_per_epoch=X_train.shape[0],
callbacks=[v_tb_callback,
v_lr_callback,
v_ck_callback,
v_best_callback])
 vgg_model.save(v_model_name+'.h5')
 del vgg_model
print("Training Completed!" )
Image(filename=".. /input/sf-picture/sf1.jpg" )<set_options>
def best_model(histories, key='categorical_crossentropy'):
ini_flag = bool(1)
 best_model = None
best_score = 0
for name, history in histories:
val_score_record = history['val_'+key]
for score in val_score_record:
if ini_flag:
best_score = score
best_model = name
ini_flag = bool(0)
 if best_score > score:
best_score = score
best_model = name
return best_model
best_model_name = best_model([(l_model_name, lenet5_model_history.history), 
(v_model_name, vgg_model_history.history)])
 print(best_model_name )
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
sns.set_style("white")
 rcParams['figure.figsize'] =(8,4)
<load_from_csv>
df = pd.read_csv(".. /input/sf-crime/train.csv.zip",dtype={"X":np.float32,"Y":np.float32} )<count_duplicates>
load_model = keras.models.load_model(best_model_name+'.h5')
 print("Check Model:")
 load_model.evaluate(X_test, y_test)
 sample_submission = pd.read_csv(submission_file)
 submission_id = sample_submission["ImageId"]
submission = pd.DataFrame({
"ImageId": submission_id,
"Label": np.argmax(load_model.predict(X_predict), axis=1)
 })
 submission.to_csv('submission.csv', index=False)
 print("Submission File Produced Completed" )