Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<init_hyperparams>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from keras.models import Sequential
from keras.layers import Conv2D, Lambda, MaxPooling2D
from keras.layers import Dense, Dropout, Flatten
from keras.layers.normalization import BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.utils.np_utils import to_categorical
def objective(trial):
xgb_params = dict(
 max_depth=trial.suggest_int("max_depth", 2, 10),
learning_rate=trial.suggest_float("learning_rate", 1e-4, 1e-1, log=True),
n_estimators=trial.suggest_int("n_estimators", 1000, 8000),
min_child_weight=trial.suggest_int("min_child_weight", 1, 10),
colsample_bytree=trial.suggest_float("colsample_bytree", 0.2, 1.0),
subsample=trial.suggest_float("subsample", 0.2, 1.0),
reg_alpha=trial.suggest_float("reg_alpha", 1e-4, 1e2, log=True),
reg_lambda=trial.suggest_float("reg_lambda", 1e-4, 1e2, log=True), 
)
 xgb = XGBRegressor(tree_method="gpu_hist", gpu_id=0, **xgb_params)
 return score_dataset(X_train, y_train, xgb)
 study = optuna.create_study(direction="minimize")
 study.optimize(objective, n_trials=20)
 xgb_params = study.best_params<save_to_csv>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 sub = pd.read_csv('.. /input/sample_submission.csv')
X_train, X_test = create_features(df_train, df_test)
 y_train = df_train.loc[:, "SalePrice"]
xgb = XGBRegressor(tree_method="gpu_hist", gpu_id=0, **xgb_params)
 xgb.fit(X_train, np.log(y))
predictions = np.exp(xgb.predict(X_test))
output = pd.DataFrame({'Id': X_test.index, 'SalePrice': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )<import_modules>
print(f"Training data size is {train.shape}
Testing data size is {test.shape}" )
warnings.filterwarnings('ignore' )<compute_test_metric>
X = train.drop(['label'], 1 ).values
y = train['label'].values
test_x = test.values
def evaluation_metric(predicted_value, actual_value)-> float:

 rmse = mean_squared_error(actual_value,predicted_value, squared=False)
 return rmse
def verify_remodelling(remodelled_year:int, built_year:int):

 if remodelled_year > built_year:
return True
elif remodelled_year == built_year:
return False
else:
return -1
def selling_period(month_sold: int)-> str:

 if month_sold >= 3 and month_sold <= 5:
return 'spring'
elif month_sold >= 6 and month_sold <= 8:
return 'summer'
elif month_sold >= 9 and month_sold <= 11:
return 'autumn'
else:
return 'winter'
RANDOM_STATE = 0<load_from_csv>
X = X / 255.0
test_x = test_x / 255.0
train_set = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 test_set = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv' )<feature_engineering>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0 )
train_set['total_square_footage'] = train_set['TotalBsmtSF'] + train_set['GrLivArea']
test_set['total_square_footage'] = test_set['TotalBsmtSF'] + test_set['GrLivArea']
train_set['remodelling_done'] = train_set.apply(lambda x: verify_remodelling(x['YearRemodAdd'], x['YearBuilt']), axis=1)
 test_set['remodelling_done'] = test_set.apply(lambda x: verify_remodelling(x['YearRemodAdd'], x['YearBuilt']), axis=1)
 train_set['selling_season'] = train_set.apply(lambda x: selling_period(x['MoSold']), axis=1)
 test_set['selling_season'] = test_set.apply(lambda x: selling_period(x['MoSold']), axis=1)
 train_set['total_area_of_house'] = train_set['TotalBsmtSF'] + train_set['1stFlrSF'] + train_set['2ndFlrSF']
test_set['total_area_of_house'] = test_set['TotalBsmtSF'] + test_set['1stFlrSF'] + test_set['2ndFlrSF']
train_set['age_of_building'] = train_set['YearBuilt'].apply(lambda x: pd.datetime.now().year - x)
 test_set['age_of_building'] = test_set['YearBuilt'].apply(lambda x: pd.datetime.now().year - x)
<count_missing_values>
mean = np.mean(X_train)
 std = np.std(X_train)
 def standardize(x):
return(x-mean)/std
train_set[ordinal_cat_features].isnull().sum()<categorify>
epochs = 50
batch_size = 64
for col in ordinal_cat_features:
train_set[col] = train_set[col].map(ordinal_ranking)
 test_set[col] = test_set[col].map(ordinal_ranking)
 train_set[col] = train_set[col].fillna(0)
 test_set[col] = test_set[col].fillna(0 )<count_missing_values>
model=Sequential()
model.add(Conv2D(filters=64, kernel_size =(3,3), activation="relu", input_shape=(28,28,1)))
 model.add(Conv2D(filters=64, kernel_size =(3,3), activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters=128, kernel_size =(3,3), activation="relu"))
model.add(Conv2D(filters=128, kernel_size =(3,3), activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters=256, kernel_size =(3,3), activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Flatten())
 model.add(BatchNormalization())
 model.add(Dense(512,activation="relu"))
model.add(Dense(10,activation="softmax"))
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"] )
train_set[continious_features].isnull().sum()<count_missing_values>
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)
 train_gen = datagen.flow(X_train, y_train, batch_size=batch_size)
 test_gen = datagen.flow(X_test, y_test, batch_size=batch_size )
test_set[continious_features].isnull().sum()<feature_engineering>
history = model.fit_generator(train_gen,
epochs = epochs,
steps_per_epoch = X_train.shape[0] // batch_size,
validation_data = test_gen,
validation_steps = X_test.shape[0] // batch_size )
imp_mean = SimpleImputer(missing_values=np.nan, strategy='mean')
 for col in continious_features:
imp_mean.fit(train_set[col].values.reshape(-1,1))
train_set[col] = imp_mean.transform(train_set[col].values.reshape(-1,1))
test_set[col] = imp_mean.transform(test_set[col].values.reshape(-1,1))
train_set[col] = np.log(train_set[col] + 1)
 test_set[col] = np.log(test_set[col] + 1 )<count_missing_values>
pred = model.predict_classes(test_x, verbose=1 )
train_set[norminal_features].isnull().sum()<count_missing_values>
sub['Label'] = pred
sub.to_csv("sub.csv", index=False)
 sub.head()