Kaggle competition: Digit Recognizer
train_X = train_user_df.drop(['id','country_destination'], axis='columns' )<find_best_model_class>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
def cross_validation_with_ndcg(pipe, X, y, scorer, cv=5):
skf = StratifiedKFold(n_splits=cv, shuffle=True, random_state=100)
 scores = []
for train_index, holdout_index in skf.split(X, y):
X_train, X_test = X.iloc[train_index], X.iloc[holdout_index]
y_train, y_test = y[train_index], y[holdout_index]
pipe.fit(X_train, y_train)
 predict = pipe.predict_proba(X_test)
 y_test = pd.get_dummies(y_test ).to_numpy()
score = scorer(y_test, predict)
 scores.append(round(score, 6))
print(f'{len(scores)} / {cv} DONE!', end='\r')
 return scores<choose_model_class>
label = np.array(train.iloc[:,0],np.str)
 data = np.array(train.iloc[:,1:],np.float32)
 label_test = np.array([])
 data_test = np.array(test.iloc[:,:],np.float32)
n_estimaters_param=[50, 100, 200]
max_depth_param=[3,4,5]
learning_rate_param=[0.1,0.2]
params = [(x, y, z)for x in learning_rate_param for y in n_estimaters_param for z in max_depth_param]
result_list=[]
for learning_rates,n_estimaters, max_depth in params:
xg_model_ = XGBClassifier(max_depth=max_depth,learning_rate=learning_rates, n_estimators=n_estimaters,verbosity=0,
objective='multi:softprob',n_jobs=-1)
 search_pipe = Pipeline([ 
('customproccess',Custom_Proccess()), 
('preprocessor', preprocessor), 
("model", xg_model_)
 ])
 print(f'learning_rate: {learning_rates}, n_estimaters: {n_estimaters}, max_depth: {max_depth}')
 scores = cross_validation_with_ndcg(search_pipe, train_X, train_y, ndcg_score)
 result_list.append([learning_rates,n_estimaters,max_depth,np.mean(scores)] )<prepare_output>
data = data.reshape(data.shape[0],28,28,1)
 data_test = data_test.reshape(data_test.shape[0],28,28,1)
 data = data/255
data_test = data_test/255
result_df=pd.DataFrame(result_list,columns=['learning_rate','n_estimator','max_depth','mean_score'])
 result_df.sort_values(by='mean_score',ascending=False ).head(5 )<train_model>
print("Before conding")
 print(label[:10])
 labels = np_utils.to_categorical(label,10)
 print("Encoded Data")
 print(labels[:10] )
xg_model = XGBClassifier(max_depth=5,learning_rate=0.1, n_estimators=200,verbosity=0,objective='multi:softprob',n_jobs=-1)
 pipe = Pipeline([ 
('customproccess',Custom_Proccess()), 
('preprocessor', preprocessor), 
("model", xg_model)
 ])
 pipe.fit(train_X, train_y)
 predict = pipe.predict_proba(test_X )<feature_engineering>
def model_generator(dropout=[0.25],denses=[512,10],activation="relu"):
model = Sequential()
model.add(Conv2D(filters=32,kernel_size=3,padding='same', activation='relu', input_shape=(28, 28,1)))
 model.add(Conv2D(filters=32, kernel_size=3, border_mode='same', activation='relu'))
model.add(MaxPool2D(pool_size=3))
model.add(Dropout(0.20))
model.add(Conv2D(filters=64,kernel_size=3,padding='same', activation='relu'))
model.add(Conv2D(filters=64, kernel_size=3, border_mode='same', activation='relu'))
model.add(MaxPool2D(pool_size=2))
model.add(Dropout(0.25))
model.add(Conv2D(filters=128, kernel_size=3, border_mode='same', activation='relu'))
model.add(MaxPool2D(pool_size=2))
model.add(Dropout(0.20))
model.add(Flatten())
 model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(10))
model.add(Activation('softmax'))
model.summary()
return model
ids = []
cts = []
for i in range(len(test_id)) :
idx = test_id[i]
ids += [idx] * 5
cts += le.inverse_transform(np.argsort(predict[i])[::-1])[:5].tolist()<save_to_csv>
def model_generator2(dropout=[0.25],denses=[512,10],activation="relu"):
model = Sequential()
model.add(Conv2D(filters=16,kernel_size=2,padding='same', activation='relu', input_shape=(28, 28,1)))
 model.add(MaxPool2D(pool_size=2))
model.add(Dropout(0.20))
model.add(Conv2D(filters=32, kernel_size=2, border_mode='same', activation='relu'))
model.add(MaxPool2D(pool_size=2))
model.add(Dropout(0.20))
model.add(Conv2D(filters=64,kernel_size=2,padding='same', activation='relu'))
model.add(MaxPool2D(pool_size=2))
model.add(Dropout(0.15))
model.add(Flatten())
 model.add(Dense(512, name='aux_output'))
model.add(Activation('relu'))
model.add(Dropout(0.4))
model.add(Dense(10, name='aux_output2'))
model.add(Activation('softmax'))
model.summary()
return model
sub_df = pd.DataFrame(np.column_stack(( ids, cts)) , columns=['id', 'country'])
 sub_df.to_csv('sub-03.csv',index=False )<load_from_csv>
def model_fit(model,batch_size=64,epochs=10):
optimizer = Adam(lr=0.0001)
 model.compile(loss="categorical_crossentropy",optimizer=optimizer,metrics=['accuracy'])
 checkpointer = ModelCheckpoint(filepath='mnist.model.best', verbose=1, monitor='val_loss', save_best_only=True)
 training = model.fit(data, labels,batch_size=batch_size, epochs=epochs,validation_split=0.25, callbacks=[checkpointer],verbose=1, shuffle=True)
 return training
train = pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/train_users_2.csv.zip')
 age_gender = pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/age_gender_bkts.csv.zip')
 countries_df = pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/countries.csv.zip')
 session_df = pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/sessions.csv.zip')
 test = pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/test_users.csv.zip')
 print('train shape: ', train.shape)
 print('test shape: ', test.shape)
 print('age_gender_bkts: ', age_gender.shape)
 print('countries: ', countries_df.shape)
 print('session: ', session_df.shape )<categorify>
model1 = model_generator(dropout=[0.25],denses=[128,10],activation="relu")
 training = model_fit(model1,batch_size=128,epochs=100 )
def glimpse(df, maxvals=10, maxlen=110):
print('Shape: ', df.shape)
 def pad(y):
max_len = max([len(x)for x in y])
 return [x.ljust(max_len)for x in y]
toprnt = pad(df.columns.tolist())
 toprnt = pad([toprnt[i] + ' ' + str(df.iloc[:,i].dtype)for i in range(df.shape[1])])
 num_nas = [df.iloc[:,i].isnull().sum() for i in range(df.shape[1])]
num_nas_ratio = [int(round(x*100/df.shape[0])) for x in num_nas]
num_nas_str = [str(x)+ '(' + str(y)+ '%)' for x,y in zip(num_nas, num_nas_ratio)]
max_len = max([len(x)for x in num_nas_str])
 num_nas_str = [x.rjust(max_len)for x in num_nas_str]
toprnt = [x + ' ' + y + ' NAs' for x,y in zip(toprnt, num_nas_str)]
toprnt = [x + ' : ' for x in toprnt]
toprnt = [toprnt[i] + ', '.join([str(y)for y in df.iloc[:min([maxvals,df.shape[0]]), i]])for i in range(df.shape[1])]
toprnt = [x[:min(maxlen, len(x)) ] for x in toprnt]
for x in toprnt:
print(x )<count_values>
def scoring(model):
model.load_weights('mnist.model.best')
 score = model.evaluate(data[:2000], labels[:2000], verbose=0)
 accuracy = 100*score[1]
print('Test accuracy: %.4f%%' % accuracy)
 label_test = model.predict_classes(data_test)
 print("Sample of the prdiction",label_test[:10])
 return label_test
train.first_affiliate_tracked.value_counts()<count_values>
label_test = scoring(model1 )
<count_duplicates><EOS> 
np.savetxt("submission.csv", np.dstack(( np.arange(1, label_test.size+1),label_test)) [0],"%d,%d",header="ImageId,Label",comments="" )