Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<prepare_x_and_y>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, Flatten, Conv2D, MaxPooling2D, Dropout
from tensorflow.keras.preprocessing.image import ImageDataGenerator
target= df_train['SalePrice']
df_train = df_train.drop(['SalePrice'],axis=1 )<split>
print(tf.__version__ )
x_train,x_test,y_train,y_test = train_test_split(df_train,target,test_size=0.33,random_state=0 )<choose_model_class>
train_data = pd.read_csv('.. /input/train.csv')
 test_data = pd.read_csv('.. /input/test.csv' )
xgb =XGBRegressor(booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=0.6, gamma=0,
importance_type='gain', learning_rate=0.01, max_delta_step=0,
max_depth=4, min_child_weight=1.5, n_estimators=2400,
n_jobs=1, nthread=None, objective='reg:linear',
reg_alpha=0.6, reg_lambda=0.6, scale_pos_weight=1,
silent=None, subsample=0.8, verbosity=1)
 lgbm = LGBMRegressor(objective='regression',
num_leaves=4,
learning_rate=0.01,
n_estimators=12000,
max_bin=200,
bagging_fraction=0.75,
bagging_freq=5,
bagging_seed=7,
feature_fraction=0.4,
)<train_model>
train_data = train_data.values
test_data = test_data.values
xgb.fit(x_train, y_train)
 lgbm.fit(x_train, y_train,eval_metric='rmse' )<predict_on_test>
np.random.shuffle(train_data )
predict1 = xgb.predict(x_test)
 predict = lgbm.predict(x_test )<compute_test_metric>
train_digits = train_digits / 255.0
val_digits = val_digits / 255.0
test_digits = test_digits / 255.0
print('Root Mean Square Error test = ' + str(math.sqrt(metrics.mean_squared_error(y_test, predict1))))
print('Root Mean Square Error test = ' + str(math.sqrt(metrics.mean_squared_error(y_test, predict))))<train_model>
X0 = Input(shape =(28,28,1))
X = Conv2D(filters = 32, kernel_size = 3, padding = 'Same', activation ='relu' )(X0)
 X = Conv2D(filters = 32, kernel_size = 3, padding = 'Same', activation ='relu' )(X)
 X = MaxPooling2D(pool_size = 2, strides = 2 )(X)
 X = Dropout(0.25 )(X)
 X = Conv2D(filters = 64, kernel_size = 3, padding = 'Same', activation ='relu' )(X)
 X = Conv2D(filters = 64, kernel_size = 3, padding = 'Same', activation ='relu' )(X)
 X = MaxPooling2D(pool_size = 2, strides = 2 )(X)
 X = Dropout(0.25 )(X)
 X = Flatten()(X)
 X = Dense(512, activation = "relu" )(X)
 X = Dropout(0.50 )(X)
 Out = Dense(10, activation = "softmax" )(X )
xgb.fit(df_train, target)
 lgbm.fit(df_train, target,eval_metric='rmse' )<predict_on_test>
model = Model(inputs=X0, outputs=Out)
 model.compile(optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics=['accuracy'])
 initial_model_weights = model.get_weights()
predict4 = lgbm.predict(df_test)
 predict3 = xgb.predict(df_test)
 predict_y =(predict3*0.45 + predict4 * 0.55 )<save_to_csv>
history = model.fit(train_digits, train_labels,
epochs=10,
verbose=2,
validation_data=(val_digits,val_labels))
submission = pd.DataFrame({
"Id": test["Id"],
"SalePrice": predict_y
})
 submission.to_csv('submission.csv', index=False )<import_modules>
model.save("cnn1.h5" )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from lightgbm import LGBMRegressor
from xgboost import XGBRegressor
import sklearn.metrics as metrics
import math<load_from_csv>
predictions = model.predict(test_digits)
 predictions = np.argmax(predictions,axis = 1)
 predictions = pd.Series(predictions,name = 'Label')
 ids = pd.Series(range(1,28001),name = 'ImageId')
 predictions = pd.concat([predictions,ids],axis = 1)
 predictions.to_csv('pred1.csv',index = False)
 del(predictions)
 del(ids )
sample_submission = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/sample_submission.csv")
 test = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv")
 train = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 c_test = test.copy()
c_train = train.copy()<concatenate>
model.set_weights(initial_model_weights )
c_train['train'] = 1
c_test['train'] = 0
df = pd.concat([c_train, c_test], axis=0,sort=False )<create_dataframe>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.1,
zoom_range=0.1,
channel_shift_range=0.,
fill_mode='nearest',
cval=0.,
horizontal_flip=False,
vertical_flip=False,
rescale=None,
preprocessing_function=None,
data_format=None,
validation_split=0.0)
 datagen.fit(train_digits )
NAN = [(c, df[c].isna().mean() *100)for c in df]
NAN = pd.DataFrame(NAN, columns=["column_name", "percentage"] )<sort_values>
batch_size = 64
gen_history = model.fit_generator(datagen.flow(train_digits, train_labels, batch_size=batch_size),
epochs=10,
verbose=2,
steps_per_epoch=train_digits.shape[0]/batch_size,
validation_data=(val_digits,val_labels))
plot_history(gen_history.history )
<drop_column><EOS> 
predictions = model.predict(test_digits)
 predictions = np.argmax(predictions,axis = 1)
 predictions = pd.Series(predictions,name = 'Label')
 ids = pd.Series(range(1,28001),name = 'ImageId')
 predictions = pd.concat([predictions,ids],axis = 1)
 predictions.to_csv('pred2.csv',index = False)
 del(predictions)
 del(ids)
 model.save("cnn2.h5" )