Kaggle competition: Tabular Playground Series - Jan 2021
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(64,(7,7), input_shape=(28, 28, 1), padding='SAME'))
model.add(tf.keras.layers.BatchNormalization())
 model.add(tf.keras.layers.Activation('relu'))
model.add(tf.keras.layers.MaxPooling2D(2, 2))
prev_filters = 64
for filters in [64]*2 + [128]*2 + [256]*2:
print(filters)
 strides = 1 if filters == prev_filters else 2
model.add(ResidualUnit(filters, strides=strides))
prev_filters = filters
model.add(tf.keras.layers.GlobalAvgPool2D())
 model.add(tf.keras.layers.Flatten())
 model.add(tf.keras.layers.Dropout(0.5*0.5))
model.add(tf.keras.layers.Dense(int(filters/2), activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))<choose_model_class>
import plotly.express as px
import plotly.graph_objects as go
import plotly.figure_factory as ff
from plotly.subplots import make_subplots
import matplotlib.pyplot as plt
from pandas_profiling import ProfileReport
import seaborn as sns
from sklearn import metrics
from scipy import stats
from copy import deepcopy
from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
import optuna
from optuna import Trial, visualization
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score,confusion_matrix
from sklearn.metrics import accuracy_score, mean_squared_error
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['acc'])
 checkpoint = ModelCheckpoint(
 filepath=f'resnet-{int(time.time())}.dhf5',
monitor='loss',
save_best_only=True 
)
 annealer = LearningRateScheduler(lambda x: 1e-3 * 0.8**x)
 callbacks = [checkpoint, annealer]<train_model>
train_df = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/train.csv')
 test_df = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/test.csv')
 sub_df = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/sample_submission.csv')
 train_df.head()
batch_size = 64
history = model.fit(datagen.flow(X_train, y_label, batch_size=batch_size),
epochs = 30,
verbose = 1, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=callbacks, )<compute_test_metric>
feature_cols = train_df.drop(['id', 'target'], axis=1 ).columns
x = train_df[feature_cols]
y = train_df['target']
print(x.shape, y.shape )
model.evaluate(X_train, y_label )<predict_on_test>
train_indexs = train_df.index
test_indexs = test_df.index
df = pd.concat(objs=[train_df, test_df], axis=0 ).reset_index(drop=True)
 df = df.drop('id', axis=1)
 len(train_indexs), len(test_indexs )
def predict_proba(X, model, num_samples):
preds = [model(X, training=True)for _ in range(num_samples)]
return np.stack(preds ).mean(axis=0)
 def predict_class(X, model, num_samples):
proba_preds = predict_proba(X, model, num_samples)
 return np.argmax(proba_preds, axis=1 )<predict_on_test>
def fix_skew(features):

 numerical_columns = features.select_dtypes(include=['int64','float64'] ).columns
skewed_features = features[numerical_columns].apply(lambda x: stats.skew(x)).sort_values(ascending=False)
 high_skew = skewed_features[abs(skewed_features)> 0.5]
skewed_features = high_skew.index
for column in skewed_features:
features[column] = boxcox1p(features[column], boxcox_normmax(features[column] + 1))
return features
y_pred = predict_class(X_test, model, 10 )<save_to_csv>
param_grid = {
'n_estimators': [5, 10, 15, 20],
'max_depth': [2, 5, 7, 9]
}
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)
 clf = XGBRegressor(random_state = 42)
 clf.fit(x_train, y_train )
res = pd.DataFrame(y_pred, columns=['Label'])
 res.index = res.index + 1
res.index.rename('ImageId', inplace=True)
 res.to_csv('res.csv' )<choose_model_class>
predictions = clf.predict(x_test)
 errors = abs(predictions - y_test)
 print('Mean Absolute Error:', round(np.mean(errors), 2), 'degrees.')
successive_outputs = [layer.output for layer in model.layers[0:]]
visualization_model = tf.keras.models.Model(inputs = model.input, outputs = successive_outputs)
 img = random.choice(X_train)
 plt.imshow(img, cmap=plt.cm.binary)
 plt.show()<predict_on_test>
def objective(trial,data=x,target=y):
train_x, test_x, train_y, test_y = train_test_split(data, target, test_size=0.15,random_state=42)
 param = {
'tree_method':'gpu_hist',
'lambda': trial.suggest_loguniform(
 'lambda', 1e-3, 10.0 
),
'alpha': trial.suggest_loguniform(
 'alpha', 1e-3, 10.0 
),
'colsample_bytree': trial.suggest_categorical(
 'colsample_bytree', [0.5,0.6,0.7,0.8,0.9,1.0] 
),
'subsample': trial.suggest_categorical(
 'subsample', [0.6,0.7,0.8,1.0] 
),
'learning_rate': trial.suggest_categorical(
 'learning_rate', [0.008,0.009,0.01,0.012,0.014,0.016,0.018, 0.02] 
),
'n_estimators': trial.suggest_categorical(
 "n_estimators", [150, 200, 300, 3000] 
),
'max_depth': trial.suggest_categorical(
 'max_depth', [4,5,7,9,11,13,15,17] 
),
'random_state': 42,
'min_child_weight': trial.suggest_int(
 'min_child_weight', 1, 300 
),
}
model = 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
successive_feature_maps = visualization_model.predict(img)
 layer_names = [layer.name for layer in model.layers]
for layer_name, feature_map in zip(layer_names, successive_feature_maps):
if len(feature_map.shape)== 4:
n_features = feature_map.shape[-1]
size = feature_map.shape[1]
pic_num_per_row = n_features // 8 + 1
display_grid_arr = []
display_grid = np.zeros(( size, size * pic_num_per_row))
for i in range(n_features):
if i>0 and(i % pic_num_per_row == 0 or i == n_features):
display_grid_arr.append(display_grid)
 display_grid = np.zeros(( size, size * pic_num_per_row))
index = i % pic_num_per_row
x = feature_map[0, :, :, i]
x -= x.mean()
x /= x.std()
x *= 64
x += 128
x = np.clip(x, 0, 255 ).astype('uint8')
 display_grid[:, index * size :(index + 1)* size] = x
row_num = len(display_grid_arr)
 scale = 200./ n_features
fig, axes = plt.subplots(row_num, 1, figsize=(scale * pic_num_per_row, scale * row_num))
fig.suptitle(layer_name, fontsize=40)
 for i in range(row_num):
axes[i].imshow(display_grid_arr[i], aspect='auto', cmap='viridis' )<predict_on_test>
study = optuna.create_study(direction='minimize')
 study.optimize(objective, n_trials=25)
 print('Number of finished trials:', len(study.trials))
print('Best trial:', study.best_trial.params )
y_pred = model.predict_classes(X_train )<import_modules>
study.best_params
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score<compute_test_metric>
best_params = study.best_params
best_params['tree_method'] = 'gpu_hist'
best_params['random_state'] = 42
clf = XGBRegressor(**(best_params))
clf.fit(x, y )
conf_max = confusion_matrix(y_label, y_pred)
 conf_max<define_variables>
preds = pd.Series(clf.predict(test_df.drop('id', axis=1)) , name='target')
 preds = pd.concat([test_df['id'], preds], axis=1 )
<prepare_x_and_y><EOS> 
preds.to_csv("submission.csv", index=False )