Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<load_from_csv>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import seaborn as sns
from sklearn.model_selection import train_test_split
import tensorflow as tf
import tensorflow.keras as keras
import tensorflow.keras.layers as layers
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import tensorflow.keras.layers.experimental.preprocessing as preprocessing
SEED = 1111
np.random.seed(SEED)
 train = pd.read_csv('.. /input/jane-street-market-prediction/train.csv')
 train = train.query('date > 85' ).reset_index(drop = True)
 train = train[train['weight'] != 0]
train.fillna(train.mean() ,inplace=True)
 train['action'] =(( train['resp'].values)> 0 ).astype(int)
 features = [c for c in train.columns if "feature" in c]
f_mean = np.mean(train[features[1:]].values,axis=0)
 resp_cols = ['resp_1', 'resp_2', 'resp_3', 'resp', 'resp_4']
X_train = train.loc[:, train.columns.str.contains('feature')]
y_train = np.stack([(train[c] > 0 ).astype('int')for c in resp_cols] ).T
def create_mlp(
 num_columns, num_labels, hidden_units, dropout_rates, label_smoothing, learning_rate 
):
inp = tf.keras.layers.Input(shape=(num_columns,))
x = tf.keras.layers.BatchNormalization()(inp)
 x = tf.keras.layers.Dropout(dropout_rates[0] )(x)
 for i in range(len(hidden_units)) :
x = tf.keras.layers.Dense(hidden_units[i] )(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Activation(tf.keras.activations.swish )(x)
 x = tf.keras.layers.Dropout(dropout_rates[i + 1] )(x)
 x = tf.keras.layers.Dense(num_labels )(x)
 out = tf.keras.layers.Activation("sigmoid" )(x)
 model = tf.keras.models.Model(inputs=inp, outputs=out)
 model.compile(
 optimizer=tfa.optimizers.RectifiedAdam(learning_rate=learning_rate),
loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=label_smoothing),
metrics=tf.keras.metrics.AUC(name="AUC"), 
)
 return model
epochs = 200
batch_size = 4096
hidden_units = [160, 160, 160]
dropout_rates = [0.2, 0.2, 0.2, 0.2]
label_smoothing = 1e-2
learning_rate = 1e-3
tf.keras.backend.clear_session()
tf.random.set_seed(SEED)
 clf = create_mlp(
 len(features), 5, hidden_units, dropout_rates, label_smoothing, learning_rate 
)
 clf.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, verbose=2)
 clf.save(f'model.h5')
 th = 0.502
models = [clf]
f = np.median
env = janestreet.make_env()
for(test_df, pred_df)in tqdm(env.iter_test()):
if test_df['weight'].item() > 0:
x_tt = test_df.loc[:, features].values
if np.isnan(x_tt[:, 1:].sum()):
x_tt[:, 1:] = np.nan_to_num(x_tt[:, 1:])+ np.isnan(x_tt[:, 1:])* f_mean
pred = np.mean([model(x_tt, training = False ).numpy() for model in models],axis=0)
 pred = f(pred)
 pred_df.action = np.where(pred >= th, 1, 0 ).astype(int)
 else:
pred_df.action = 0
env.predict(pred_df )<install_modules>
%matplotlib inline
plt.style.use('bmh')
 np.random.seed(42 )
%%bash
python -m pip install py7zr
mkdir data<load_pretrained>
data = pd.read_csv(".. /input/digit-recognizer/train.csv")
 holdout = pd.read_csv(".. /input/digit-recognizer/test.csv" )
for f in ["train.tsv.7z", "test.tsv.7z"]:
archive = py7zr.SevenZipFile(".. /input/mercari-price-suggestion-challenge/{}".format(f), mode='r')
 archive.extractall(path="./data")
 archive.close()
with zipfile.ZipFile(".. /input/mercari-price-suggestion-challenge/test_stg2.tsv.zip", 'r')as zip_ref:
zip_ref.extractall("./data" )<install_modules>
X = data
y = X.pop('label')
 del data
%%bash
git clone https://github.com/joren015/MercariProject.git
cp -r MercariProject/*./
python -m pip install -r requirements.txt<set_options>
X = X / 255.0
holdout = holdout / 255.0
%%bash
python main.py neural_network predict<set_options>
X = X.values.reshape(-1,28,28,1)
 holdout = holdout.values.reshape(-1,28,28,1)
 y = keras.utils.to_categorical(y, num_classes = 10 )
pd.set_option('display.max_rows', 100)
 pd.set_option('display.max_columns', 500)
 pd.set_option('display.max_colwidth', None)
 pd.set_option('display.float_format', lambda x: '%.5f' % x)
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_pretrained>
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size =.1,
stratify = y,
random_state = 42 )
!apt-get install p7zip
!p7zip -d -f -k /kaggle/input/mercari-price-suggestion-challenge/train.tsv.7z
!unzip -o /kaggle/input/mercari-price-suggestion-challenge/sample_submission_stg2.csv.zip
!unzip -o /kaggle/input/mercari-price-suggestion-challenge/test_stg2.tsv.zip<load_from_csv>
model = keras.Sequential([
layers.InputLayer(input_shape=[28, 28, 1]),
preprocessing.RandomRotation(factor = 0.10),
preprocessing.RandomTranslation(height_factor = 0.1, width_factor = 0.1),
preprocessing.RandomZoom(height_factor = 0.1, width_factor = 0.1),
layers.BatchNormalization(renorm = True),
layers.Conv2D(filters = 32, kernel_size = 5, activation = 'relu', padding = 'same'),
layers.Conv2D(filters = 32, kernel_size = 5, activation = 'relu', padding = 'same'),
layers.Dropout(0.25),
layers.Conv2D(filters = 128, kernel_size = 3, activation = 'relu', padding = 'same'),
layers.MaxPool2D() ,
layers.Conv2D(filters = 64, kernel_size =(3, 3), activation = 'relu', padding = 'same'),
layers.Conv2D(filters = 64, kernel_size =(3, 3), activation = 'relu', padding = 'same'),
layers.MaxPool2D(pool_size =(2, 2), strides =(2, 2)) ,
layers.Flatten() ,
layers.Dense(256, activation = 'relu'),
layers.Dense(10, activation = 'softmax'),
] )
train = pd.read_csv("train.tsv", low_memory=False, sep='\t')
 test = pd.read_csv("test_stg2.tsv", low_memory=False, sep='\t')
 train.info(memory_usage="deep" )<count_unique_values>
optimizer = keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )
df = pd.concat([train.drop(["train_id", "price"], axis=1 ).nunique() , test.drop(["test_id"], axis=1 ).nunique() ], axis=1)
 df.columns = ["Train", "Test"]
df.T<count_values>
epochs = 300
batch_size = 64
train["name"].value_counts() [:5], test["name"].value_counts() [:5]<count_values>
early_stopping = keras.callbacks.EarlyStopping(
 min_delta=0.001,
patience=10,
restore_best_weights=True,
)
train["brand_name"].value_counts() [:5], test["brand_name"].value_counts() [:5]<count_values>
optimizer = tf.keras.optimizers.Adam(epsilon=0.01)
 model.compile(
 optimizer=optimizer,
loss='categorical_crossentropy',
metrics=['accuracy'], 
)
 history = model.fit(X_train, y_train,
batch_size = batch_size,
epochs = epochs,
validation_data =(X_test, y_test),
callbacks = [early_stopping],
verbose = 2)
train["category_name"].value_counts() [:5], test["category_name"].value_counts() [:5]<count_values>
results = model.predict(holdout)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results, name = "Label" )
train["item_description"].value_counts() [:5], test["item_description"].value_counts() [:5]<drop_column>
submission = pd.concat(
 [pd.Series(range(1, 28001), name = "ImageId"),
results],
axis = 1)
 submission.to_csv("digits_submission.csv", index = False )