Kaggle competition: Kannada MNIST
df_train = df_train[df_train['ind_nuevo'] == 0]
df_train = df_train[df_train['antiguedad'] != -999999]
df_train = df_train[df_train['indrel'] == 1]
df_train = df_train[df_train['indresi'] == 'S']
df_train = df_train[df_train['indfall'] == 'N']
df_train = df_train[df_train['tipodom'] == 1]
df_train = df_train[df_train['ind_empleado'] == 'N']
df_train = df_train[df_train['pais_residencia'] == 'ES']
df_train = df_train[df_train['indrel_1mes'] == 1]
df_train = df_train[df_train['tiprel_1mes'] ==('A' or 'I')]
df_train = df_train[df_train['indext'] == 'N']<count_missing_values>
%%time
train_data = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test_data = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv' )
df_train.isnull().sum()<count_values>
train_data.groupby(by='label' ).size()
df_train.tiprel_1mes.value_counts()<drop_column>
IMG_SIZE = 28
drop_column = ['ind_nuevo','indrel','indresi','indfall','tipodom','ind_empleado','pais_residencia','indrel_1mes','indext','conyuemp','fecha_alta','tiprel_1mes']
df_train.drop(drop_column, axis=1, inplace = True)
 df_test.drop(drop_column, axis=1, inplace = True )<data_type_conversions>
img_train = train_data.drop(["label"], axis=1 ).values.reshape(-1, IMG_SIZE, IMG_SIZE, 1 ).astype('float32')
 img_label = to_categorical(train_data["label"])
 img_test = test_data.drop(["id"], axis=1 ).values.reshape(-1, IMG_SIZE, IMG_SIZE, 1 ).astype('float32')
 print("img_train.shape = ", img_train.shape)
 print("img_label.shape = ", img_label.shape)
 print("img_test.shape = ", img_test.shape )
df_train["renta"] = pd.to_numeric(df_train["renta"], errors="coerce")
 df_test["renta"] = pd.to_numeric(df_test["renta"], errors="coerce" )<feature_engineering>
x_train, x_test, y_train, y_test = train_test_split(img_train, img_label, test_size=0.15)
 print("x_train.shape = ", x_train.shape)
 print("y_train.shape = ", y_train.shape)
 print("x_test.shape = ", x_test.shape)
 print("y_test.shape = ", y_test.shape )
for cod in unique_prov:
df_test.loc[df_test['cod_prov']==cod,['renta']] = df_test.loc[df_test['cod_prov']==cod,['renta']].fillna({'renta':grouped[cod]} ).values
df_train.loc[df_train['cod_prov']==cod,['renta']] = df_train.loc[df_train['cod_prov']==cod,['renta']].fillna({'renta':grouped[cod]} ).values<correct_missing_values>
import keras
from keras.datasets import mnist
from keras.layers import Input, Dense, Dropout, Flatten, add
from keras.layers import Conv2D, Activation, MaxPooling2D, AveragePooling2D, BatchNormalization
from keras import backend as K
from keras.callbacks import ModelCheckpoint
import tensorflow as tf
from keras.models import Model
from keras.utils import plot_model
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Input, Dense, PReLU, Dropout
from keras.models import Model
from keras.callbacks import LearningRateScheduler, ModelCheckpoint, TensorBoard, EarlyStopping, ReduceLROnPlateau
from keras.optimizers import SGD, Adam
df_test.renta.fillna(df_test["renta"].median() , inplace=True )<count_missing_values>
def build_model(input_shape=(28, 28, 1), classes = 10):
input_layer = Input(shape=input_shape)
 x = Conv2D(16,(3,3), strides=1, padding="same", name="conv1" )(input_layer)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform", name="batch1" )(x)
 x = Activation('relu',name='relu1' )(x)
 x = Dropout(0.1 )(x)
 x = Conv2D(32,(3,3), strides=1, padding="same", name="conv2" )(x)
 x = BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch2" )(x)
 x = Activation('relu',name='relu2' )(x)
 x = Dropout(0.15 )(x)
 x = MaxPooling2D(pool_size=2, strides=2, padding="same", name="max2" )(x)
 x = Conv2D(64,(5,5), strides=1, padding ="same", name="conv3" )(x)
 x = BatchNormalization(momentum=0.17, epsilon=1e-5, gamma_initializer="uniform", name="batch3" )(x)
 x = Activation('relu', name="relu3" )(x)
 x = MaxPooling2D(pool_size=2, strides=2, padding="same", name="max3" )(x)
 x = Conv2D(128,(5,5), strides=1, padding="same", name="conv4" )(x)
 x = BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch4" )(x)
 x = Activation('relu', name="relu4" )(x)
 x = Dropout(0.17 )(x)
 x = Conv2D(64,(3,3), strides=1, padding="same", name="conv5" )(x)
 x = BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch5" )(x)
 x = Activation('relu', name='relu5' )(x)
 x = Dropout(0.2 )(x)
 x = Conv2D(32,(3,3), strides=1, padding="same", name="conv6" )(x)
 x = BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch6" )(x)
 x = Activation('relu', name="relu6" )(x)
 x = Dropout(0.05 )(x)
 x = Flatten()(x)
 x = Dense(50, name="Dense1" )(x)
 x = Activation('relu', name='relu7' )(x)
 x = Dropout(0.05 )(x)
 x = Dense(25, name="Dense2" )(x)
 x = Activation('relu', name='relu8' )(x)
 x = Dropout(0.03 )(x)
 x = Dense(classes, name="Dense3" )(x)
 x = Activation('softmax' )(x)
 model = Model(inputs=input_layer, outputs=x)
 return model
df_train.isnull().sum()<count_missing_values>
model = build_model(input_shape=(28, 28, 1), classes = 10 )
df_test.isnull().sum()<categorify>
train_datagen = ImageDataGenerator(
 rotation_range=9,
zoom_range=0.25,
width_shift_range=0.25,
height_shift_range=0.25,
rescale=1./255 
)
 train_datagen.fit(x_train)
 test_datagen = ImageDataGenerator(rescale=1./255)
 adam = Adam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
 sgd = SGD(lr=0.01, momentum=0.0, decay=0.0, nesterov=False)
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss', patience=5, verbose=1, factor=0.5, min_lr=0.00001)
 checkpoint = ModelCheckpoint("bestmodel.model", monitor='val_acc', verbose=1, save_best_only=True)
 earlyStopping = EarlyStopping(monitor='val_acc', patience=15, verbose=1, mode='min' )
replace_mapping = {
"sexo": {"V":0,'H':1},
}
df_train.replace(replace_mapping, inplace= True )<define_variables>
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['acc'] )
dummy_col = ['sexo','canal_entrada','cod_prov','segmento']<count_values>
epochs = 80
batch_size = 128
limit = int(0.05 * len(df_train.index))
for col in dummy_col:
if len(df_train[col].unique())> 6:
trainlist = df_train[col].value_counts()
print(trainlist)
 use_col = []
for i,item in enumerate(trainlist):
if item > limit:
use_col.append(df_train[col].value_counts().index[i])
 print(use_col)
 for item in df_train[col].unique() :
if item not in use_col:
row_index = df_train[col] == item
print(row_index)
 df_train.loc[row_index,col] = np.nan
print(df_train.loc[row_index,col])
 for item in df_test[col].unique() :
if item not in use_col:
row_index = df_test[col] == item
df_test.loc[row_index,col] = np.nan<categorify>
history = model.fit_generator(
 train_datagen.flow(x_train, y_train, batch_size=batch_size),
steps_per_epoch=x_train.shape[0] // batch_size,
epochs=epochs,
validation_data=test_datagen.flow(x_test, y_test),
validation_steps=x_test.shape[0] // batch_size,
callbacks=[checkpoint, learning_rate_reduction] )
df_train = pd.get_dummies(df_train, prefix=dummy_col, columns = dummy_col)
 df_test = pd.get_dummies(df_test, prefix=dummy_col, columns = dummy_col )<categorify>
model.load_weights('bestmodel.model' )
df_train["age"] = pd.to_numeric(df_train["age"], errors="coerce")
 df_test["age"] = pd.to_numeric(df_test["age"], errors="coerce")
 age_group = [[0,19],[19,26],[26,36],[36,41],[41,47],[47,55],[55,60],[60,70],[70,80],[80,170]]
def create_age_group(df):
df['age_group'] = np.nan
for i,age in enumerate(age_group):
row_index =(df['age'] >= age[0])&(df['age'] < age[1])
 df.loc[row_index,'age_group'] = i
create_age_group(df_train)
 create_age_group(df_test)
 df_train.drop('age', axis=1, inplace = True)
 df_test.drop('age', axis=1, inplace = True )<prepare_x_and_y>
results=model.predict(img_test/255.0 )
models = {}
model_preds = {}
id_preds1 = defaultdict(list)
 ids = df_test['ncodpers'].values
for c in product_col:
if c != 'ncodpers':
print(c)
 y_train = df_train[c]
x_train = df_train.drop(product_col + ['ncodpers',"fecha_dato"], 1)
 clf = LogisticRegression()
clf.fit(x_train, y_train)
 x_train2 = df_test.drop(['ncodpers',"fecha_dato"], 1)
 p_train = clf.predict_proba(x_train2)[:,1]
p_train2 = clf.predict_proba(x_train)[:,1]
models[c] = clf
model_preds[c] = p_train
for id, p in zip(ids, p_train):
id_preds1[id].append(p)
<train_model>
x2_train, x2_test, y2_train, y2_test = train_test_split(img_test, results, test_size=0.15 )
models = {}
model_preds = {}
id_preds3 = defaultdict(list)
 ids = df_test['ncodpers'].values
for c in product_col:
if c != 'ncodpers':
print(c)
 y_train = df_train[c]
x_train = df_train.drop(product_col + ['ncodpers',"fecha_dato"], 1)
 clf = ensemble.RandomForestClassifier(n_estimators=60, n_jobs=-1,max_depth=10, min_samples_split=10, verbose=1)
 clf.fit(x_train, y_train)
 x_train2 = df_test.drop(['ncodpers',"fecha_dato"], 1)
 p_train = clf.predict_proba(x_train2)[:,1]
p_train2 = clf.predict_proba(x_train)[:,1]
models[c] = clf
model_preds[c] = p_train
for id, p in zip(ids, p_train):
id_preds3[id].append(p)
<prepare_output>
x_train_final = np.concatenate(( img_train,x2_train), axis=0)
 y_train_final = np.concatenate(( img_label,y2_train), axis=0 )
fraction1 = 0.4
fraction3 = 0.6
id_preds_combined = {}
for uid, p in id_preds1.items() :
id_preds_combined[uid] = fraction1*np.asarray(id_preds1[uid])+ fraction3*np.asarray(id_preds3[uid])
 id_preds = id_preds_combined<load_from_csv>
x_train, x_test, y_train, y_test = train_test_split(x_train_final, y_train_final, test_size=0.15)
 print("x_train.shape = ", x_train.shape)
 print("y_train.shape = ", y_train.shape)
 print("x_test.shape = ", x_test.shape)
 print("y_test.shape = ", y_test.shape )
df_recent = pd.read_csv('.. /input/santander-product-recommendation/train_ver2.csv',usecols=['ncodpers'] + product_col)
<load_from_csv>
keras.backend.clear_session()
sample = pd.read_csv('.. /input/santander-product-recommendation/sample_submission.csv' )<define_variables>
model2 = build_model(input_shape=(28, 28, 1), classes = 10 )
already_active = {}
for row in df_recent.values:
row = list(row)
 id = row.pop(0)
 active = [c[0] for c in zip(tuple(product_col), row)if c[1] > 0]
already_active[id] = active
train_preds = {}
for id, p in id_preds.items() :
preds = [i[0] for i in sorted([i for i in zip(tuple(product_col), p)if i[0] not in already_active[id]],
key=lambda i:i [1],
reverse=True)[:7]]
train_preds[id] = preds
test_preds = []
for row in sample.values:
id = row[0]
p = train_preds[id]
test_preds.append(' '.join(p)) 
<prepare_output>
sgd2 = SGD(lr=0.1, momentum=0.0, decay=0.0, nesterov=False)
 learning_rate_reduction2 = ReduceLROnPlateau(monitor='val_loss', patience=5, verbose=1, factor=0.5, min_lr=0.00001)
 checkpoint2 = ModelCheckpoint("bestmodel2.model", monitor='val_acc', verbose=1, save_best_only=True )
sample['added_products'] = test_preds 
<save_to_csv>
model2.compile(loss='categorical_crossentropy', optimizer=sgd2, metrics=['acc'] )
sample.to_csv('LR-RF_sub.csv', index=False)
 print(datetime.datetime.now() -start_time)
<define_variables>
history2 = model2.fit_generator(
 train_datagen.flow(x_train, y_train, batch_size=batch_size),
steps_per_epoch=x_train.shape[0] // batch_size,
epochs=epochs,
validation_data=test_datagen.flow(x_test, y_test),
validation_steps=x_test.shape[0] // batch_size,
callbacks=[checkpoint2, learning_rate_reduction2] )
start_time = datetime.datetime.now()
demographic_cols = ['fecha_dato',
'ncodpers','ind_empleado','pais_residencia','sexo','age','fecha_alta','ind_nuevo','antiguedad','indrel',
'indrel_1mes','tiprel_1mes','indresi','indext','conyuemp','canal_entrada','indfall',
'tipodom','cod_prov','ind_actividad_cliente','renta','segmento']
notuse = ["ult_fec_cli_1t","nomprov"]
product_col = [
'ind_ahor_fin_ult1','ind_aval_fin_ult1','ind_cco_fin_ult1','ind_cder_fin_ult1','ind_cno_fin_ult1','ind_ctju_fin_ult1',
'ind_ctma_fin_ult1','ind_ctop_fin_ult1','ind_ctpp_fin_ult1','ind_deco_fin_ult1','ind_deme_fin_ult1',
'ind_dela_fin_ult1','ind_ecue_fin_ult1','ind_fond_fin_ult1','ind_hip_fin_ult1','ind_plan_fin_ult1',
'ind_pres_fin_ult1','ind_reca_fin_ult1','ind_tjcr_fin_ult1','ind_valo_fin_ult1','ind_viv_fin_ult1','ind_nomina_ult1',
'ind_nom_pens_ult1','ind_recibo_ult1']
train_cols = demographic_cols + product_col<load_from_csv>
results=model2.predict(img_test/255.0)
 results=np.argmax(results, axis=1)
 sub=pd.DataFrame()
sub['id']=list(test_data.values[0:,0])
 sub['label']=results
df_train = pd.read_csv('.. /input/juneextra/juneExtra.csv', usecols=train_cols )<load_from_csv>
sub.to_csv("submission.csv", index=False )