Kaggle competition: Digit Recognizer
train_Y = np.log1p(train["price"])
 validation_Y = np.log1p(validation["price"] )<import_modules>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import itertools
from keras.models import Sequential, load_model
from keras.layers import Flatten, Dense, MaxPool2D, Conv2D, Dropout, BatchNormalization
from keras.optimizers import RMSprop
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint
from keras.utils.np_utils import to_categorical
import tensorflow as tf
from sklearn.linear_model import Ridge<train_model>
train =pd.read_csv('.. /input/digit-recognizer/train.csv')
 test =pd.read_csv('.. /input/digit-recognizer/test.csv' )
model = Ridge(
 alpha=.5,
copy_X=True,
fit_intercept=True,
max_iter=100,
normalize=False,
random_state=101,
solver='auto',
tol=0.01 
)
 model.fit(train_X, train_Y )<compute_test_metric>
y_train = train['label']
X_train = train.drop('label', axis=1 )
def rmsle(y, y0):
assert len(y)== len(y0)
 return np.sqrt(np.mean(np.power(np.log1p(y)-np.log1p(y0), 2)) )<predict_on_test>
train.isnull().sum()
val_pred = model.predict(validation_X)
 val_pred = np.expm1(val_pred)
 print(rmsle(val_pred, validation["price"]))<predict_on_test>
train.isnull().sum()
test_pred = model.predict(test_X)
 submission['price'] = np.expm1(test_pred )<save_to_csv>
X_train = X_train / 255.0
test = test / 255.0
X_train = X_train.values.reshape(-1, 28,28,1)
 test = test.values.reshape(-1, 28,28,1)
 y_train = to_categorical(y_train, num_classes=10 )
submission.to_csv("submission.csv", index=False )<import_modules>
X_train, X_test, y_train, y_test = train_test_split(X_train, y_train)
 print("X_train shape: ", X_train.shape,"
X_test shape: ", X_test.shape,
"
y_train shape: ", y_train.shape,"
y_test shape: ", y_test.shape )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split<load_pretrained>
IMG_SIZE = 28
batch_size= 86
AUTOTUNE = tf.data.experimental.AUTOTUNE
num_classes = 10
!apt-get install p7zip
!p7zip -d -f -k /kaggle/input/mercari-price-suggestion-challenge/train.tsv.7z
!p7zip -d -f -k /kaggle/input/mercari-price-suggestion-challenge/test.tsv.7z
!p7zip -d -f -k /kaggle/input/mercari-price-suggestion-challenge/sample_submission.csv.7z<load_from_csv>
from keras.models import Sequential, load_model
from keras.layers import Flatten, Dense, MaxPool2D, Conv2D, Dropout, BatchNormalization
from keras.optimizers import RMSprop
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import layers
!unzip /kaggle/input/mercari-price-suggestion-challenge/sample_submission_stg2.csv.zip
!unzip /kaggle/input/mercari-price-suggestion-challenge/test_stg2.tsv.zip<load_from_csv>
def create_model(train_ds, val_ds, epochs):
model = tf.keras.Sequential([
layers.Conv2D(32,(3, 3), activation = 'relu', padding = 'same', input_shape=(28,28,1)) ,
layers.Conv2D(32,(3, 3), activation = 'relu', padding = 'same'),
layers.BatchNormalization() ,
layers.MaxPooling2D(2, 2),
layers.Dropout(0.2),
layers.Conv2D(64,(3, 3), activation = 'relu', padding = 'same'),
layers.Conv2D(64,(3, 3), activation = 'relu', padding = 'same'),
layers.BatchNormalization() ,
layers.MaxPooling2D(2, 2),
layers.Dropout(0.2),
layers.Conv2D(128,(3, 3), activation = 'relu', padding = 'same'),
layers.Conv2D(128,(3, 3), activation = 'relu', padding = 'same'),
layers.BatchNormalization() ,
layers.MaxPooling2D(2, 2),
layers.Dropout(0.2),
layers.Flatten() ,
layers.Dense(512, activation = 'relu'),
layers.BatchNormalization() ,
layers.Dropout(0.2),
layers.Dense(256, activation = 'relu'),
layers.BatchNormalization() ,
layers.Dropout(0.35),
layers.Dense(10, activation = 'softmax')
 ])
 model.compile(optimizer='adam',
loss=tf.keras.losses.CategoricalCrossentropy() ,
metrics=['accuracy'])
 print(model.summary())
 return model
train_data = pd.read_csv('train.tsv', sep='\t')
 train_data.head(5 )<count_missing_values>
cnn_model = create_model(X_train, y_train, 100 )
train_data.isnull().sum()<split>
optimizer = RMSprop(lr = 0.001, rho = 0.9, epsilon=1e-08, decay =0.0)
 cnn_model.compile(optimizer = optimizer, loss = 'categorical_crossentropy', metrics = ['accuracy'] )
train_data = train_data[train_data['price'] > 0].reset_index(drop=True)
 train_data,validation_data=train_test_split(train_data,test_size=0.1,random_state=42)
 print(train_data.shape)
 print(validation_data.shape )<count_missing_values>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
train_data.isnull().sum()<count_missing_values>
datagen = ImageDataGenerator(
 featurewise_center=False, samplewise_center=False,
featurewise_std_normalization=False, samplewise_std_normalization=False,
zca_whitening=False, zca_epsilon=1e-06, rotation_range=0, width_shift_range=0.0,
height_shift_range=0.0, brightness_range=None, shear_range=0.0, zoom_range=0.0,
channel_shift_range=0.0, fill_mode='nearest', cval=0.0,
horizontal_flip=False, vertical_flip=False, rescale=None,
preprocessing_function=None, data_format=None, validation_split=0.0, dtype=None 
)
 datagen.fit(X_train )
validation_data.isnull().sum()<create_dataframe>
history = cnn_model.fit(datagen.flow(X_train,y_train, batch_size=batch_size),
epochs = 28, validation_data =(X_test,y_test),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction] )
train = train_data.copy()
valid = validation_data.copy()<feature_engineering>
prediction = cnn_model.predict(X_test ).argmax(axis=1)
def split_categories(category):
try:
sub_category1,sub_category2,sub_category3 = category.split("/")
 return sub_category1,sub_category2,sub_category3
except:
return("No label","No label","No label")
 def create_split_categories(data):
data['sub_category1'],data['sub_category2'],data['sub_category3']=zip(*data['category_name'].\
apply(lambda x: split_categories(x)) )<string_transform>
score = cnn_model.evaluate(X_test, y_test, verbose = 0)
 print("Test Accuracy: ",score[1] )
<categorify><EOS> 
result = cnn_model.predict(test)
 result = np.argmax(result, axis=1)
 result = pd.Series(result, name='Label')
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),result],axis = 1)
 submission.to_csv("submission.csv",index=False )