Kaggle competition: Digit Recognizer
rfPipeline = process_and_fit(randomForest, X_train, y_train)
 rfPred = rfPipeline.predict(X_valid)
 gbPipeline = process_and_fit(gradientBoost, X_train, y_train)
 gbPred = gbPipeline.predict(X_valid)
 cbPipeline = process_and_fit(catBoost, X_train, y_train)
 cbPred = cbPipeline.predict(X_valid)
 xgbPipeline = process_and_fit(xgBoost, X_train, y_train)
 xgbPred = xgbPipeline.predict(X_valid)
 lassoPipeline = process_and_fit(lassoReg, X_train, y_train)
 lassoPred = lassoPipeline.predict(X_valid)
 gbmPipeline = process_and_fit(lightGBM, X_train, y_train)
 gbmPred = gbmPipeline.predict(X_valid )<compute_test_metric>
from tensorflow import keras
models = ['Random Forest', 'Gradient Boosting', 'Cat Boost', 'XGBoost', 'Lasso Regression', 'Light GBM']
model_names = [rfPred, gbPred, cbPred, xgbPred, lassoPred, gbmPred]
avg_score = 0
for m in range(len(models)) :
score = rmse(y_valid, model_names[m])
 avg_score += score
print(models[m], "score:", rmse(y_valid, model_names[m]))
print("Average Score Across Models: ", avg_score/6 )<predict_on_test>
train_data = pd.read_csv('.. /input/train.csv')
 test_data = pd.read_csv('.. /input/test.csv')
preds_test1 = rfPipeline.predict(test)
 preds_test2 = gbPipeline.predict(test)
 preds_test3 = cbPipeline.predict(test)
 preds_test4 = xgbPipeline.predict(test)
 preds_test5 = lassoPipeline.predict(test)
 preds_test6 = gbmPipeline.predict(test)
 preds_test =(preds_test1+preds_test2+ preds_test3 + preds_test4 + preds_test5 + preds_test6)/6<save_to_csv>
x_train =(train_data.iloc[:,1:].values ).astype('float32')
 y_train = train_data.iloc[:,0].values.astype('int32')
 x_test = test_data.values.astype('float32' )
output = pd.DataFrame({'Id': test_all.index,
'SalePrice': preds_test})
 output.to_csv('submission.csv', index=False )<import_modules>
batch_size = 64
num_classes = 10
epochs = 25
input_shape =(28, 28, 1 )
import pandas as pd
from tqdm import tqdm<load_from_csv>
y_train = keras.utils.to_categorical(y_train, num_classes)
 X_train, X_val, Y_train, Y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state=42 )
train = pd.read_csv('.. /input/ames-housing-dataset/AmesHousing.csv')
 train.drop(['PID'], axis=1, inplace=True)
 origin = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 train.columns = origin.columns
test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 submission = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/sample_submission.csv')
 print('Train:{} Test:{}'.format(train.shape,test.shape))<drop_column>
model = Sequential()
model.add(Conv2D(32, kernel_size= 3,activation='relu',kernel_initializer='he_normal',input_shape=input_shape))
model.add(Conv2D(32, kernel_size= 3,activation='relu',kernel_initializer='he_normal'))
model.add(MaxPool2D(( 2, 2)))
 model.add(Dropout(0.20))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size =(3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))
model.add(Conv2D(64, kernel_size =(3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(BatchNormalization())
 model.add(Conv2D(128, kernel_size =(3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))
model.add(Dropout(0.25))
model.add(Flatten())
 model.add(BatchNormalization())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(num_classes, activation='softmax'))
missing = test.isnull().sum()
missing = missing[missing>0]
train.drop(missing.index, axis=1, inplace=True)
 train.drop(['SaleCondition','Electrical'], axis=1, inplace=True)
 test.dropna(axis=1, inplace=True)
 test.drop(['SaleCondition','Electrical'], axis=1, inplace=True )<feature_engineering>
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer= keras.optimizers.RMSprop() ,
metrics=['accuracy'] )
l_test = tqdm(range(0, len(test)) , desc='Matching')
 for i in l_test:
for j in range(0, len(train)) :
for k in range(1, len(test.columns)) :
if test.iloc[i,k] == train.iloc[j,k]:
continue
else:
break
else:
submission.iloc[i, 1] = train.iloc[j, -1]
break
l_test.close()<save_to_csv>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.0001 )
submission.to_csv('result-with-best.csv', index=False )<import_modules>
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 )
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import xgboost as xgb
from hyperopt import STATUS_OK, Trials, fmin, hp, tpe 
<load_from_csv>
datagen.fit(X_train)
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 submission = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/sample_submission.csv' )<prepare_x_and_y>
h = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 1, steps_per_epoch=(X_train.shape[0] // batch_size)
 , callbacks=[learning_rate_reduction], )
Y_train = train.SalePrice
X_train = train.drop(columns= ['SalePrice'] , inplace = False )<drop_column>
final_loss, final_acc = model.evaluate(X_val, Y_val, verbose=0)
 print(final_loss, final_acc )
test.drop(columns = ['Id'] , inplace = True)
 X_train.drop(columns = ['Id'] , inplace = True )<count_missing_values>
predicted_classes = model.predict_classes(X_test )
null_list = X_train.isnull().sum()
h = X_train.shape[0]
columns_name = X_train.columns
for i in range(len(null_list)) :
if null_list[i]>h/3 :
X_train.drop(columns = [columns_name[i]] , inplace = True)
 test.drop(columns = [columns_name[i]] , inplace = True )<data_type_conversions>
submissions=pd.DataFrame({"ImageId": list(range(1,len(predicted_classes)+1)) ,
"Label": predicted_classes})
 submissions.to_csv("final_submission_V5.csv", index=False, header=True )
def fill_nulls(X):
for col in X.columns:
if X[col].isnull().sum() >0:
if X[col].dtype == 'float64' :
X[col].fillna(X[col].mean() ,inplace=True)
 else :
X[col].fillna(X[col].mode() [0],inplace=True )<categorify>
fill_nulls(X_train)
 fill_nulls(test )<categorify>
def category(columns_X , X):
for col in columns_X:
df = pd.get_dummies(X[col] , drop_first = True)
 X.drop(columns =col , inplace = True)
 X = pd.concat([X , df] , axis =1)
 return X<concatenate>
list_categorical = []
for i in X_train.columns :
if X_train[i].dtype == object:
list_categorical +=[i]
X_train_test = pd.concat([X_train , test] , axis = 0)
 X_train_test = category(list_categorical , X_train_test )<count_duplicates>
X_train_test = X_train_test.loc[: , ~X_train_test.columns.duplicated() ]
X_train = X_train_test.iloc[:1460 ,:]
test = X_train_test.iloc[1460: , :]<split>
def objective(trial):
train_x, test_x, train_y, test_y = train_test_split(X_train, Y_train , test_size=0.2,random_state=42)
 param = {
'tree_method':'gpu_hist',
'lambda': trial.suggest_loguniform('lambda', 1e-2, 100.0),
'alpha': trial.suggest_loguniform('alpha', 1e-2, 100.0),
'colsample_bytree': trial.suggest_categorical('colsample_bytree', [0.1,0.2,0.3,0.4,0.5,0.6,0.7, 0.8]),
'subsample': trial.suggest_categorical('subsample', [0.4,0.5,0.6,0.7,0.8,1.0]),
'learning_rate': trial.suggest_float('learning_rate', 0.025,0.1),
'n_estimators': 1500,
'max_depth': trial.suggest_int('max_depth', 2,9),
'random_state': trial.suggest_categorical('random_state', [24, 48,2020]),
'min_child_weight': trial.suggest_int('min_child_weight', 1, 20),
}
model = xgb.XGBRegressor(**param)
 model.fit(train_x,train_y,eval_set=[(test_x,test_y)],early_stopping_rounds=100,verbose=False)
 preds = model.predict(test_x)
 rmse = mean_squared_error(test_y, preds,squared=False)
 return rmse<find_best_params>
best_model = optuna.create_study(direction= 'minimize')
 best_model.optimize(objective, n_trials = 50)
<split>
model = xgb.XGBRegressor(**best_model.best_trial.params)
 x_train , x_val , y_train , y_val = train_test_split(X_train , Y_train , random_state = 0 , test_size = 0.1)
 model.fit(x_train , y_train)
 mean_squared_error(y_val, model.predict(x_val), squared=False )<save_to_csv>