Kaggle competition: Digit Recognizer
del test['id'], test['timestamp'], test['sub_area']
test_mean_life_sq = test['life_sq'].mean()
test = test.fillna(value={'life_sq':test_mean_life_sq} )<prepare_x_and_y>
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import matplotlib.cm as cm
x_test = test.iloc[:, 0:5].copy()<choose_model_class>
train_data_raw = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test_data_raw = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 print('train_data_shape: ' + str(train_data_raw.shape))
print('test_data_shape: ' + str(test_data_raw.shape))
model = BaggingRegressor(xgb.XGBRegressor(colsample_bytree=0.4,
learning_rate=0.03,
max_depth=6,
min_child_weight=1.5,
n_estimators=390,
reg_alpha=0.75,
reg_lambda=0.45,
subsample=0.6,
seed=42,
tree_method='gpu_hist', gpu_id=0 
), n_estimators=50 )<train_model>
def pre_process_data(dataset, has_label=True):
if has_label:
data = dataset.iloc[:,1:].values
else:
data = dataset.iloc[:,:].values
data = data.astype(np.float)
 data = np.multiply(data, 1.0 / 255.0)
 data = data.reshape(data.shape[:1] +(28, 28, 1))
return data
train_images = pre_process_data(train_data_raw)
 print(train_images.shape)
 print(train_images )
k = model.fit(x_train, y_train )<predict_on_test>
def pre_process_labels(data):
labels_flat = data.iloc[:,0].values.ravel()
num_classes = np.unique(labels_flat ).shape[0]
num_labels = labels_flat.shape[0]
index_offset = np.arange(num_labels)* num_classes
labels_one_hot = np.zeros(( num_labels, num_classes))
labels_one_hot.flat[index_offset + labels_flat.ravel() ] = 1
return labels_one_hot.astype(np.uint8)
 train_labels = pre_process_labels(train_data_raw)
 print(train_labels.shape)
 print(train_labels )
preds = model.predict(x_test )<save_to_csv>
def fit(train_images, train_labels, config):
NUM_EPOCHS = config['NUM_EPOCHS']
BATCH_SIZE = config['BATCH_SIZE']
LEARNING_RATE = config['LEARNING_RATE']
DROPOUT_RATE = config['DROPOUT_RATE']
NETWORK_WIDTH = config['NETWORK_WIDTH']
initializer = tf.keras.initializers.GlorotNormal()
model = keras.models.Sequential([
keras.layers.Conv2D(32,kernel_size=(3, 3),activation='swish',kernel_initializer=initializer, input_shape=(28,28,1), use_bias=True),
keras.layers.Conv2D(32,kernel_size=(3,3), activation='swish',kernel_initializer=initializer, use_bias=True),
keras.layers.MaxPooling2D(pool_size=(2,2)) ,
keras.layers.Dropout(DROPOUT_RATE),
keras.layers.Conv2D(64,(3, 3),activation='swish', padding='same', use_bias=True),
keras.layers.MaxPooling2D(pool_size=(2,2)) ,
keras.layers.Dropout(DROPOUT_RATE),
keras.layers.Conv2D(512,(3, 3),activation='swish', padding='same', use_bias=True),
keras.layers.MaxPooling2D(pool_size=(2,2)) ,
keras.layers.Dropout(DROPOUT_RATE),
keras.layers.Conv2D(256,(2, 2),activation='swish', padding='same', use_bias=True),
keras.layers.MaxPooling2D(pool_size=(2,2)) ,
keras.layers.Dropout(DROPOUT_RATE),
keras.layers.Flatten() ,
keras.layers.Dense(128,activation='swish', kernel_regularizer =tf.keras.regularizers.l1(l=0.01)) ,
keras.layers.Dropout(DROPOUT_RATE),
keras.layers.Dense(10, activation=tf.nn.softmax),
])
 opt = tf.keras.optimizers.Adam(learning_rate=LEARNING_RATE)
 loss = tf.keras.losses.CategoricalCrossentropy()
model.compile(
 optimizer=opt,
loss=loss,
metrics=['accuracy'] 
)
 val_size = int(train_images.shape[0] * 0.2)
 val_images = train_images[:val_size,:, :]
val_labels = train_labels[:val_size,:]
train_images = train_images[val_size:,:, :]
train_labels = train_labels[val_size:,:]
hist = model.fit(
 x=train_images, y=train_labels,
epochs=NUM_EPOCHS,
batch_size=BATCH_SIZE,
validation_steps=10,
validation_data=(val_images, val_labels),
verbose=1 
)
 show_history(hist)
 loss, acc = model.evaluate(val_images, val_labels)
 return model, loss, acc
submission = pd.DataFrame({'id':test1['id'],'price_doc':preds})
 submission.to_csv('./submission.csv', index=False )<import_modules>
config = {
'BATCH_SIZE': 128,
'LEARNING_RATE': 0.001,
'DROPOUT_RATE': 0.4,
'NUM_EPOCHS': 50,
'NETWORK_WIDTH': 512
}
model, loss, acc = fit(train_images, train_labels, config)
 print('Accuracy: ' + str(acc * 100)+ '%' )
import os
import datetime
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt<load_from_csv>
predictions = model.predict(test_data )
train = pd.read_csv('/kaggle/input/bike-sharing-demand/train.csv')
 test = pd.read_csv('/kaggle/input/bike-sharing-demand/test.csv')
 train.head()<data_type_conversions>
submissions=pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)) ,
"Label": np.argmax(predictions, axis=1)})
 submissions.to_csv("my_submissions.csv", index=False, header=True )