Kaggle competition: Digit Recognizer
cv_output = xgb.cv(xgb_params,
dmatrix_train,
num_boost_round=1000,
early_stopping_rounds=20,
verbose_eval=50,
show_stdv=False)
 num_boost_rounds = len(cv_output )<train_model>
import matplotlib.pyplot as plt
model = xgb.train(dict(xgb_params, verbose=1), dmatrix_train, num_boost_round=num_boost_rounds )<predict_on_test>
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv" )
predict = np.exp(model.predict(dmatrix_test))
submission = pd.DataFrame({'id': id_test, 'price_doc': predict})
 submission.head()<save_to_csv>
sample_submission = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv' )
submission.to_csv('XGB_CV.csv', index=False )<split>
X_train = train.drop(['label'], axis = 1)
 y_train = train['label']
X_test = test
X_train = X_train / 255.0
X_test = X_test / 255.0
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=2022)
 X_test = df_test_encoded<categorify>
X_train=X_train.values
X_test=X_test.values
test=test.values
X_train=X_train.reshape(X_train.shape[0], 28, 28, 1)
 X_test=X_test.reshape(X_test.shape[0], 28, 28, 1)
 test=test.reshape(test.shape[0] , 28 , 28 , 1 )
pca = PCA(n_components=20 ).fit(X_train)
 X_train_pca=pca.transform(X_train)
 X_val_pca=pca.transform(X_val )<prepare_x_and_y>
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPool2D, Dropout
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import EarlyStopping
dmatrix_train = xgb.DMatrix(X_train_pca, y_train)
 dmatrix_val = xgb.DMatrix(X_val_pca, y_val)
 dmatrix_test = xgb.DMatrix(X_test )<train_model>
input_shape = X_train[0].shape
xgb_params = {
'eta': 0.05,
'max_depth': 5,
'subsample': 1.0,
'colsample_bytree': 0.7,
'objective': 'reg:squarederror',
'eval_metric': 'rmse',
'verbosity': 0
}
partial_model = xgb.train(xgb_params,
dmatrix_train,
num_boost_round=1000,
evals=[(dmatrix_val, 'val')],
early_stopping_rounds=20,
verbose_eval=20)
 num_boost_round = partial_model.best_iteration<train_model>
early_stop = EarlyStopping(monitor = 'val_loss', mode = 'min', verbose = 2, patience = 0 )
model = xgb.train(dict(xgb_params, verbose=1), dmatrix_train, num_boost_round=num_boost_round )<compute_test_metric>
model1 = Sequential()
model1.add(Conv2D(filters = 32, kernel_size =(3,3), padding = 'valid', activation = 'relu', input_shape = input_shape))
model1.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'valid', activation = 'relu'))
model1.add(MaxPool2D(pool_size=(2,2)))
 model1.add(Dropout(0.25))
model1.add(Conv2D(filters = 96, kernel_size =(3,3), padding = 'valid', activation = 'relu'))
model1.add(Conv2D(filters = 128, kernel_size =(5,5), padding = 'valid', activation = 'relu'))
model1.add(MaxPool2D(pool_size=(3,3)))
 model1.add(Dropout(0.33))
model1.add(Flatten())
 model1.add(Dense(128, activation='relu'))
model1.add(Dropout(0.50))
model1.add(Dense(10, activation='softmax'))
predict = np.exp(model.predict(dmatrix_val))
rmsle = np.sqrt(mean_squared_log_error(np.exp(y_val), predict))
print('RMSLE: {:.3f}'.format(rmsle))<split>
model1.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'] )
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=2022)
 X_test = df_test_encoded<concatenate>
model1.fit(X_train, y_train, epochs = 100, batch_size = 64, verbose = 1, callbacks = [early_stop], shuffle = True )
train_dataset = cb.Pool(X_train, y_train)
 test_dataset = cb.Pool(X_val, y_val )<choose_model_class>
predictions1 = model1.predict(X_test )
model = cb.CatBoostRegressor(loss_function='RMSE' )<compute_test_metric>
model2 = Sequential()
model2.add(Conv2D(filters = 32, kernel_size =(3,3), padding = 'valid', activation = 'relu', input_shape = input_shape))
model2.add(MaxPool2D(pool_size=(2,2)))
 model2.add(Dropout(0.25))
model2.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'valid', activation = 'relu'))
model2.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'valid', activation = 'relu'))
model2.add(MaxPool2D(pool_size=(2,2)))
 model2.add(Dropout(0.33))
model2.add(Conv2D(filters = 128, kernel_size =(3,3), padding = 'valid', activation = 'relu'))
model2.add(MaxPool2D(pool_size=(2,2)))
 model2.add(Dropout(0.4))
model2.add(Flatten())
 model2.add(Dense(1024, activation='relu'))
model2.add(Dropout(0.50))
model2.add(Dense(10, activation='softmax'))
pred = np.exp(model.predict(X_val))
rmsle = np.sqrt(mean_squared_log_error(np.exp(y_val), pred))
print('RMSLE: {:.3f}'.format(rmsle))<predict_on_test>
model2.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'] )
predict = np.exp(model.predict(X_test))
submission = pd.DataFrame({'id': id_test, 'price_doc': predict})
 submission.head()<save_to_csv>
model2.fit(X_train, y_train, epochs = 100, batch_size = 128, verbose = 1, callbacks = [early_stop], shuffle = True )
submission.to_csv('Catboost.csv', index=False )<import_modules>
predictions2 = model1.predict(X_test )
import zipfile<load_pretrained>
model3 = Sequential()
model3.add(Conv2D(filters = 32, kernel_size =(3,3), padding = 'valid', activation = 'relu', input_shape = input_shape))
model3.add(Conv2D(filters = 32, kernel_size =(3,3), padding = 'valid', activation = 'relu'))
model3.add(Dropout(0.25))
model3.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'valid', activation = 'relu'))
model3.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'valid', activation = 'relu'))
model3.add(MaxPool2D(pool_size=(2,2)))
 model3.add(Dropout(0.33))
model3.add(Conv2D(filters = 96, kernel_size =(3,3), padding = 'valid', activation = 'relu'))
model3.add(Conv2D(filters = 96, kernel_size =(3,3), padding = 'valid', activation = 'relu'))
model3.add(MaxPool2D(pool_size=(2,2)))
 model3.add(Dropout(0.4))
model3.add(Flatten())
 model3.add(Dense(512, activation='relu'))
model3.add(Dropout(0.50))
model3.add(Dense(1024, activation='relu'))
model3.add(Dropout(0.50))
model3.add(Dense(10, activation='softmax'))
train = zipfile.ZipFile('.. /input/sberbank-russian-housing-market/train.csv.zip', 'r')
 test = zipfile.ZipFile('.. /input/sberbank-russian-housing-market/test.csv.zip', 'r')
 macro = zipfile.ZipFile('.. /input/sberbank-russian-housing-market/macro.csv.zip', 'r' )<load_from_csv>
model3.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'] )
train.extract('train.csv')
 test.extract('test.csv')
 macro.extract('macro.csv' )<load_from_csv>
model3.fit(X_train, y_train, epochs = 100, batch_size = 128, verbose = 1, callbacks = [early_stop], shuffle = True )
train = pd.read_csv('./train.csv')
 test = pd.read_csv('./test.csv')
 macro = pd.read_csv('./macro.csv' )<data_type_conversions>
predictions3 = model1.predict(X_test )
train_mean_life_sq = train['life_sq'].mean()
train = train.fillna(value={'life_sq':train_mean_life_sq})
 train = train.fillna(value={'floor':3})
 train_mean_max_floor = train['max_floor'].mean()
train = train.fillna(value={'max_floor':train_mean_max_floor})
 train = train.fillna(value={'material':1})
 del train['timestamp'], train['id'], train['sub_area']<prepare_x_and_y>
predictions =(predictions1 + predictions2 + predictions3)/3
Predictions = []
for i in range(0, len(predictions)) :
Predictions.append(predictions[i].argmax())
 id = []
for i in range(1, len(Predictions)+1):
id.append(i )
x_train = train.iloc[:, 0:5].copy()
y_train = train['price_doc'].copy()<create_dataframe>
output = pd.DataFrame({'ImageId': id, 'Label': Predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!")