Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<drop_column>
import keras
from keras.datasets import mnist
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
cols = ['Store', 'DayOfWeek', 'Promo', 'StateHoliday', 'SchoolHoliday', 'StoreType', 'Assortment', 'DayOfYear', 'Promo2', 'CompetitionDistanceLog10']
X = X[cols]<predict_on_test>
batch_size = 128
num_classes = 10
epochs = 30
X_pred = xgbreg.predict(X )<prepare_output>
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv" )
df_test_open['Sales'] = X_pred<concatenate>
y_train = train["label"]
x_train = train.drop(labels=["label"], axis=1 )
df_test_final = pd.concat([df_test_open, df_test_closed] )<load_from_csv>
x_train, x_test, y_train, y_test = train_test_split(x_train, y_train , random_state = 42, test_size=0.3 )
sample_sub = pd.read_csv('.. /input/rossmann-store-sales/sample_submission.csv')
 sample_sub.head()<categorify>
from keras import backend
temp_dict = {}
temp_dict = pd.Series(df_test_final['Sales'].values, index = df_test_final['Id'] ).to_dict()
sample_sub['Sales'] = sample_sub['Id'].map(temp_dict )<save_to_csv>
x_train = x_train.astype('float')
 x_test = x_test.astype('float')
 test = test.astype('float')
 x_train /= 255
x_test /= 255
test/= 255
sample_sub.to_csv('sample_submission.csv', header = ['Id', 'Sales'], index = False )<load_from_csv>
y_train = keras.utils.to_categorical(y_train, num_classes)
 y_test = keras.utils.to_categorical(y_test, num_classes )
data_path = '/kaggle/input/porto-seguro-safe-driver-prediction/'
train = pd.read_csv(data_path + 'train.csv', index_col='id')
 test = pd.read_csv(data_path + 'test.csv', index_col='id')
 submission = pd.read_csv(data_path + 'sample_submission.csv', index_col='id' )<concatenate>
model = Sequential()
all_data = pd.concat([train, test], ignore_index=True)
 all_data = all_data.drop('target', axis=1)
 all_features = all_data.columns.tolist()<feature_engineering>
from keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense
all_data['num_missing'] =(all_data==-1 ).sum(axis=1)
 remaining_features = [col for col in all_features \
if('cat' not in col and 'calc' not in col)]
remaining_features.append('num_missing' )<categorify>
model.add(Conv2D(32, kernel_size=(5, 5), activation='relu', input_shape=input_shape))
model.add(Conv2D(32, kernel_size=(5, 5), activation='relu', input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
cat_features = [col for col in all_features if 'cat' in col]
onehot_encoder = OneHotEncoder()
encoded_cat_matrix = onehot_encoder.fit_transform(all_data[cat_features] )<data_type_conversions>
model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.RMSprop(lr = 0.001, rho=0.9, epsilon=1e-08, decay = 0.0), metrics=['accuracy'] )
ind_features = [col for col in all_features if 'ind' in col]
first_col=True
for col in ind_features:
if first_col:
all_data['mix_ind'] = all_data[col].astype(str)+'_'
first_col = False
else:
all_data['mix_ind'] += all_data[col].astype(str)+'_'<count_values>
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test))
cat_count_features = []
for col in cat_features+['mix_ind']:
val_counts_dic = all_data[col].value_counts().to_dict()
all_data[f'{col}_count'] = all_data[col].apply(lambda x: val_counts_dic[x])
 cat_count_features.append(f'{col}_count' )<drop_column>
from keras.preprocessing.image import ImageDataGenerator
drop_features = ['ps_ind_14', 'ps_ind_10_bin','ps_ind_11_bin',
'ps_ind_12_bin','ps_ind_13_bin','ps_car_14']
all_data_remaining = all_data[remaining_features+cat_count_features].drop(drop_features, axis=1)
 all_data_sprs = sparse.hstack([sparse.csr_matrix(all_data_remaining),
encoded_cat_matrix],
format='csr' )<prepare_x_and_y>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.3,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(x_train )
num_train = train.shape[0]
X = all_data_sprs[:num_train]
X_test = all_data_sprs[num_train:]
y = train['target'].values<compute_train_metric>
annealer_callback = ReduceLROnPlateau(monitor = 'val_accuracy', patience = 3, verbose = 1, factor = 0.5, min_lr = 0.00001 )
def eval_gini(y_true, y_pred):
assert y_true.shape == y_pred.shape
n_samples = y_true.shape[0]
L_mid = np.linspace(1 / n_samples, 1, n_samples)
 pred_order = y_true[y_pred.argsort() ]
L_pred = np.cumsum(pred_order)/ np.sum(pred_order)
 G_pred = np.sum(L_mid - L_pred)
 true_order = y_true[y_true.argsort() ]
L_true = np.cumsum(true_order)/ np.sum(true_order)
 G_true = np.sum(L_mid - L_true)
 return G_pred / G_true<compute_test_metric>
model.fit_generator(generator=datagen.flow(x_train, y_train, batch_size = batch_size), steps_per_epoch= x_train.shape[0] // batch_size, epochs=epochs, validation_data=(x_test,y_test), verbose = 2,
callbacks = [annealer_callback] )
def gini_lgb(preds, dtrain):
labels = dtrain.get_label()
return 'gini', eval_gini(labels, preds), True<compute_test_metric>
score = model.evaluate(x_test, y_test, verbose=0)
 score
def gini_xgb(preds, dtrain):
labels = dtrain.get_label()
return 'gini', eval_gini(labels, preds )<choose_model_class>
results = model.predict(test)
 results = np.argmax(results,axis = 1 )
folds = StratifiedKFold(n_splits=5, shuffle=True, random_state=1991 )<init_hyperparams>
submission_df = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv" )
max_params_lgb = {'bagging_fraction': 0.8043696643500143,
'feature_fraction': 0.6829323879981047,
'lambda_l1': 0.9264555612104627,
'lambda_l2': 0.9774233689434216,
'min_child_samples': 10,
'min_child_weight': 125.68433948868649,
'num_leaves': 28,
'objective': 'binary',
'learning_rate': 0.01,
'bagging_freq': 1,
'verbosity': 0,
'random_state': 1991}<split>
submission_df["Label"] = results
<init_hyperparams><EOS> 
submission_df.to_csv("submission.csv", index = False )