Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<categorify>
%matplotlib inline
for column in features.columns :
if features[column].dtype == object :
df_dummies = pd.get_dummies(features[column],drop_first=True,prefix=column)
 features.drop([column],axis=1,inplace=True)
 features = pd.concat([features,df_dummies],axis=1)
 features.head()<split>
training_data = pd.read_csv('.. /input/train.csv')
 testing_data = pd.read_csv('.. /input/test.csv' )
x_train = features.iloc[:len(y_train), :]
x_test = features.iloc[len(x_train):, :]<normalization>
label = training_data.label.value_counts()
label
def bi_directional_elimination(x, y):
Sig_Level = 0.05;
features = x.columns.tolist()
selected_features = []
while(len(features)> 0):
remain_features = list(set(features)- set(selected_features))
pval = pd.Series(index=remain_features, dtype='float64')
 for column in remain_features :
model = sm.OLS(y, sm.add_constant(x[selected_features + [column]])).fit()
pval[column] = model.pvalues[column]
min_pval = pval.min()
if(min_pval < Sig_Level):
selected_features.append(pval.idxmin())
 while(len(selected_features)> 0):
selected_features_with_constant = sm.add_constant(x[selected_features])
 p_values = sm.OLS(y, selected_features_with_constant ).fit().pvalues[1:]
max_pval = p_values.max()
if(max_pval >= Sig_Level):
removed_feature = p_values.idxmax()
selected_features.remove(removed_feature)
 else :
break
else :
break
return selected_features<compute_test_metric>
train_data = training_data.drop(['label'], axis=1 ).values.astype('float32')
 target = training_data['label'].values.astype('int32')
 test_data = testing_data.values.astype('float32')
 train_data = train_data.reshape(train_data.shape[0], 28, 28)/ 255.0
test_data = test_data.reshape(test_data.shape[0], 28, 28)/ 255.0
selected_features = bi_directional_elimination(x_train, y_train )<create_dataframe>
num = 10
target = keras.utils.to_categorical(target,num )
x_train = x_train[selected_features].copy()
x_test = x_test[selected_features].copy()<init_hyperparams>
num_classes = 10
input_shape =(28, 28, 1 )
regressor = xgboost.XGBRegressor()
booster=['gbtree','gblinear']
base_score=[0.25,0.5,0.75,1]
n_estimators = [100, 500, 900, 1100, 1500]
max_depth = [2, 3, 5, 10, 15]
learning_rate=[0.05,0.1,0.15,0.20]
min_child_weight=[1,2,3,4]
hyperparameter_grid = {
'n_estimators': n_estimators,
'max_depth':max_depth,
'learning_rate':learning_rate,
'min_child_weight':min_child_weight,
'booster':booster,
'base_score':base_score
}
random_cv = RandomizedSearchCV(estimator=regressor,
param_distributions=hyperparameter_grid,
cv=5, n_iter=50,
scoring = 'neg_mean_absolute_error',n_jobs = 4,
verbose = 5,
return_train_score = True,
random_state=42)
 random_cv.fit(x_train,y_train )<find_best_params>
X_train, X_val, Y_train, Y_val = train_test_split(train_data, target , test_size = 0.1, random_state=42 )
random_cv.best_estimator_<choose_model_class>
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.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )
regressor = xgboost.XGBRegressor(base_score=0.25, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,
importance_type='gain', interaction_constraints='',
learning_rate=0.1, max_delta_step=0, max_depth=2,
min_child_weight=1, missing=nan, monotone_constraints='() ',
n_estimators=900, n_jobs=8, num_parallel_tree=1, random_state=0,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, subsample=1,
tree_method='exact', validate_parameters=1, verbosity=None )<train_model>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.0001 )
regressor.fit(x_train,y_train )<predict_on_test>
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',kernel_initializer='he_normal',input_shape=input_shape))
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',kernel_initializer='he_normal'))
model.add(MaxPool2D(( 2, 2)))
 model.add(BatchNormalization())
 model.add(Dropout(0.20))
model.add(Conv2D(64,(3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))
model.add(Conv2D(64,(3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Conv2D(128,(3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))
model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(num_classes, activation='softmax'))
y_pred = regressor.predict(x_test )<save_to_csv>
model.compile(loss='categorical_crossentropy',
optimizer='Adam',
metrics=['accuracy'] )
df_pred = pd.DataFrame(y_pred)
 df_sub = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/sample_submission.csv')
 df_sub = pd.concat([df_sub['Id'],df_pred],axis=1)
 df_sub.columns = ['Id','SalePrice']
df_sub.to_csv('sample_submission.csv',index=False )<import_modules>
fitting = model.fit_generator(datagen.flow(X_train, Y_train, batch_size = 90),
epochs = 50, validation_data =(X_val, Y_val),
verbose = 1, callbacks = [learning_rate_reduction] )
import numpy as np
import pandas as pd
import seaborn as sns
import sklearn
from sklearn import metrics
import tensorflow as tf
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split, StratifiedKFold
import numpy as np<load_from_csv>
predicted_classes = model.predict_classes(test_data )
<compute_train_metric><EOS> 
submissions=pd.DataFrame({"ImageId": list(range(1,len(predicted_classes)+1)) ,
"Label": predicted_classes})
 submissions.to_csv("submission.csv", index=False, header=True )