Kaggle competition: Digit Recognizer
test['target']=preds
test=test[['id','target']]
test.to_csv('submission.csv',index=False )<import_modules>
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.utils.multiclass import unique_labels
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
from lightgbm import LGBMRegressor<load_from_csv>
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.optimizers import RMSprop, Adam
from keras.preprocessing.image import ImageDataGenerator
train = pd.read_csv(".. /input/tabular-playground-series-feb-2021/train.csv")
 test = pd.read_csv(".. /input/tabular-playground-series-feb-2021/test.csv" )<define_variables>
df = pd.read_csv('.. /input/train.csv')
category_features = [
"cat0", "cat1", "cat2", "cat3", "cat4", "cat5", "cat6", "cat7",
"cat8", "cat9"
]
continous_features = [
"cont0", "cont1", "cont2", "cont3", "cont4",
"cont5", "cont6", "cont7", "cont8", "cont9", "cont10",
"cont11", "cont12", "cont13"
]
all_features = category_features + continous_features 
<categorify>
test_df = pd.read_csv('.. /input/test.csv' )
for feature in category_features:
encoder = LabelEncoder()
encoder.fit(train[feature])
 train[feature] = pd.Series(encoder.transform(train[feature]), dtype="category")
 test[feature] = pd.Series(encoder.transform(test[feature]), dtype="category" )<init_hyperparams>
Y = df['label']
X = df.drop(['label'], axis = 1 )
PARAMS = {
'objective': 'regression',
'metric': 'rmse',
'boosting': 'gbdt',
'num_iterations': 5000,
'learning_rate': 0.02,
'num_leaves': 15,
'min_data_in_leaf': 1000,
'feature_fraction': 0.3,
'lambda_l2': 0.001
} 
<prepare_x_and_y>
X = X / 255.0
X = X.values.reshape(-1,28,28,1)
 Y = np.array(Y)
 Y = to_categorical(Y, num_classes = 10 )
PARAMS_KFOLD = PARAMS.copy()
PARAMS_KFOLD.update({
'early_stopping_round': 500
})
 def rmse_kfold(parameters, n_fold):
kfold = KFold(n_splits=n_fold)
 rmse_kfold = np.zeros(n_fold)
 for index,(train_index, validation_index)in enumerate(kfold.split(train)) :
X_train, X_validation = train[all_features].iloc[train_index], train[all_features].iloc[validation_index]
y_train, y_validation = train['target'].iloc[train_index], train['target'].iloc[validation_index]
model = LGBMRegressor(**parameters)
 model.fit(X_train, y_train,
eval_set = [(X_validation, y_validation)],
verbose = -1)
 pred_validation = model.predict(X_validation)
 rmse_kfold[index] = mean_squared_error(y_validation, pred_validation, squared=False)
 rmse_average = np.average(rmse_kfold)
 return rmse_average<define_search_space>
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, stratify = Y, random_state = 31, test_size = 0.2 )
num_leaves_range = [2**3-1, 2**4-1, 2**5-1, 2**6-1]
min_data_in_leaf_range = [2000, 1000, 500, 250]
feature_fraction_range = [0.3]
N_FOLD = 10
rmse_grid_search = []
for num_leaves in num_leaves_range:
for min_data_in_leaf in min_data_in_leaf_range:
for feature_fraction in feature_fraction_range:
params = PARAMS_KFOLD.copy()
params.update({
'num_leaves': num_leaves,
'min_data_in_leaf': min_data_in_leaf,
'feature_fraction': feature_fraction
})
 rmse = rmse_kfold(params, N_FOLD)
 rmse_grid_search.append(( rmse, params))
print("parameters " + str(params))
print("rmse: " + str(rmse))<find_best_params>
model = Sequential()
PARAMS_OPTIMIZED = sorted(rmse_grid_search, key=lambda o1: o1[0])[0][1]<train_model>
model.add(Conv2D(32,(5,5),padding = 'Same',activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(64,(3,3), padding = 'same', activation = 'relu'))
model.add(MaxPool2D(pool_size =(2,2)))
 model.add(Dropout(0.2))
model.add(Conv2D(128,(3,3), padding = 'same', activation = 'relu'))
model.add(MaxPool2D(pool_size =(2,2)))
 model.add(Dropout(0.2))
model.add(Conv2D(64,(3,3), padding = 'same', activation = 'relu'))
model.add(MaxPool2D(pool_size =(2,2)))
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(256, activation = 'relu'))
model.add(Dropout(0.2))
model.add(Dense(10, activation = 'softmax'))
PARAMS_PRED = PARAMS_OPTIMIZED.copy()
PARAMS_PRED.pop('early_stopping_round', None)
 print('parameters for final model ' + str(PARAMS_PRED))
model = LGBMRegressor(**PARAMS_PRED)
 model.fit(train[all_features], train['target'] )<save_to_csv>
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=10,
width_shift_range=0.1,
height_shift_range=0.1,
brightness_range=None,
shear_range=0.1,
zoom_range=0.15,
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 )
pred = model.predict(test[all_features])
 submission = pd.DataFrame({'id': test['id'], 'target': pred})
 submission.to_csv('submission.csv', index=False )<load_from_csv>
datagen.fit(X_train )
train_df = pd.read_csv('/kaggle/input/tabular-playground-series-feb-2021/train.csv')
 test_df = pd.read_csv('/kaggle/input/tabular-playground-series-feb-2021/test.csv')
<filter>
model.compile(optimizer = "Nadam", loss = "categorical_crossentropy", metrics = ["accuracy"] )
<concatenate>
max_epochs = 30
batch_size = 256
df = pd.concat([train_df.drop(['target','id'],axis=1),test_df.drop(['id'],axis=1)],axis=0)
 df.head()<count_missing_values>
history = model.fit_generator(datagen.flow(X_train, Y_train, batch_size = batch_size),
epochs = max_epochs, verbose = 1, validation_data =(X_test, Y_test),
steps_per_epoch=X_train.shape[0] // batch_size )
df.isnull().sum()<string_transform>
test_df = test_df / 255.0
test_arr = test_df.values.reshape(-1,28,28,1 )
def divideFeatures(df):
numerical_features = df.select_dtypes(include=[np.number])
 categorical_features = df.select_dtypes(include=[np.object])
 return numerical_features, categorical_features<feature_engineering>
Y_pred_arr = model.predict(test_arr )
cont_features, cat_features =divideFeatures(df )<import_modules>
Y_pred_arr = np.argmax(Y_pred_arr, axis = 1 )
<categorify><EOS> 
results = pd.Series(Y_pred_arr,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )