Kaggle competition: ML Hackathon 2019 Q1
<SOS> metric: categorizationaccuracy Kaggle data source: ml-hackathon-2019-q1<load_from_csv>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers.core import Dense,Activation,Dropout
from keras.layers import LSTM
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import seaborn as sns
from sklearn.ensemble import RandomForestClassifier
from sklearn import svm
from xgboost import XGBClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
from sklearn.tree import DecisionTreeClassifier
from keras.callbacks import EarlyStopping
import math
train_df = pd.read_csv(base_dir + 'train.csv')
 y_train = train_df['target']
class_weights = class_weight.compute_class_weight('balanced',
classes=np.unique(y_train),
y=y_train)
 class_weights = {0: class_weights[0],1: class_weights[1]}
if not BALANCE_DATA:
class_weights = {0: 1,1: 2}
print(class_weights )<choose_model_class>
df=pd.read_csv('.. /input/Gastric_Ulcer_Train.csv',delimiter=',')
 df.head(3)
learning_rate_reduction =ReduceLROnPlateau(
 monitor='loss',
factor=0.25,
patience=2,
verbose=1,
mode="auto",
min_delta=0.0001,
cooldown=0,
min_lr=0.00001 
)
 es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=3, restore_best_weights=True )<init_hyperparams>
df=pd.read_csv('.. /input/Gastric_Ulcer_Train.csv',delimiter=',')
 df.head(3)
 del df['ID']
del df['Age']
df=pd.concat([df,pd.get_dummies(df['Personality Type'], drop_first=True, prefix=1)], axis=1,sort=False)
 df=pd.concat([df,pd.get_dummies(df['Gender'], drop_first=True, prefix=2)], axis=1,sort=False)
 df=pd.concat([df,pd.get_dummies(df['Fixed Mealtimes'], drop_first=True, prefix=3)], axis=1,sort=False)
 df=pd.concat([df,pd.get_dummies(df['Alcohol Consumption'], drop_first=True, prefix=4)], axis=1,sort=False)
 df=pd.concat([df,pd.get_dummies(df['Vegetarian'], drop_first=True, prefix=5)], axis=1,sort=False)
 df=pd.concat([df,pd.get_dummies(df['Smoker'], drop_first=True, prefix=6)], axis=1,sort=False)
 df=pd.concat([df,pd.get_dummies(df['CLO Test'], drop_first=True, prefix=7)], axis=1,sort=False)
 df=pd.concat([df,pd.get_dummies(df['Coronary Disease'], drop_first=True, prefix=8)], axis=1,sort=False)
 df=df.drop(['Personality Type','Gender','Fixed Mealtimes','Alcohol Consumption','Vegetarian','Smoker','CLO Test','Coronary Disease'],axis=1)
 df.head()
df1=df
target=df['Gastric Ulcer']
sns.countplot(target)
 del df['Gastric Ulcer']
sc=StandardScaler()
sc.fit_transform(df)
 X_train,X_test,Y_train,Y_test=train_test_split(df,target,test_size=0.3)
def get_lr_callback() :
lr_start = 0.000003
lr_max = 0.000020 * REPLICAS
lr_min = 0.000001
lr_ramp_ep = 5
lr_sus_ep = 0
lr_decay = 0.8
def lrfn(epoch):
if epoch < lr_ramp_ep:
lr =(lr_max - lr_start)/ lr_ramp_ep * epoch + lr_start
elif epoch < lr_ramp_ep + lr_sus_ep:
lr = lr_max
else:
lr =(lr_max - lr_min)* lr_decay**(epoch - lr_ramp_ep - lr_sus_ep)+ lr_min
return lr
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=False)
 return lr_callback<choose_model_class>
model = XGBClassifier(objective="binary:logistic",n_estimators=500,max_depth=8,learning_rate=0.07)
 model.fit(X_train, Y_train)
 predict3=model.predict(X_test)
 c=0
for i in range(len(predict3)) :
if(predict3[i]==Y_test.iloc[i]):
c+=1
c3=(c/len(predict3)) *100
print('XGBoost Accuracy Score is',c3)
def get_model() :
model_input = tf.keras.Input(shape=input_shape, name='input_layer')
 dummy = tf.keras.layers.Lambda(lambda x:x )(model_input)
 outputs = []
x = DenseNet201(include_top=False, weights='imagenet',
input_shape=input_shape,
pooling='avg' )(dummy)
 x = tf.keras.layers.Dense(1, activation='sigmoid' )(x)
 outputs.append(x)
 model = tf.keras.Model(model_input, outputs, name='transfNetwork')
 model.summary()
return model<train_model>
model= XGBClassifier(objective="binary:logistic",n_estimators=500,max_depth=8,learning_rate=0.07)
 model.fit(df,target )
model = get_model()
model.compile(optimizer='adam', loss=BinaryCrossentropy(label_smoothing=0.05),
metrics=['AUC'])
 hist_train = model.fit(train_dataset,
validation_data=valid_dataset,
steps_per_epoch=STEPS_PER_EPOCH,
validation_steps=VALID_STEPS,
epochs=epochs,callbacks=[get_lr_callback() ], class_weight=class_weights )<create_dataframe>
df=pd.read_csv('.. /input/Gastric_Ulcer_Test.csv',delimiter=',')
 del df['ID']
del df['Age']
df=pd.concat([df,pd.get_dummies(df['Personality Type'], drop_first=True, prefix=1)], axis=1,sort=False)
 df=pd.concat([df,pd.get_dummies(df['Gender'], drop_first=True, prefix=2)], axis=1,sort=False)
 df=pd.concat([df,pd.get_dummies(df['Fixed Mealtimes'], drop_first=True, prefix=3)], axis=1,sort=False)
 df=pd.concat([df,pd.get_dummies(df['Alcohol Consumption'], drop_first=True, prefix=4)], axis=1,sort=False)
 df=pd.concat([df,pd.get_dummies(df['Vegetarian'], drop_first=True, prefix=5)], axis=1,sort=False)
 df=pd.concat([df,pd.get_dummies(df['Smoker'], drop_first=True, prefix=6)], axis=1,sort=False)
 df=pd.concat([df,pd.get_dummies(df['CLO Test'], drop_first=True, prefix=7)], axis=1,sort=False)
 df=pd.concat([df,pd.get_dummies(df['Coronary Disease'], drop_first=True, prefix=8)], axis=1,sort=False)
 df=df.drop(['Personality Type','Gender','Fixed Mealtimes','Alcohol Consumption','Vegetarian','Smoker','CLO Test','Coronary Disease'],axis=1)
 df.head()
sc=StandardScaler()
sc.fit_transform(df)
test_ds = get_test_dataset(ordered=True)
 test_images_ds = test_ds.map(lambda image, idnum: image )<predict_on_test>
predict1=model.predict(df)
 iid=np.arange(0,200,1)+801
ind=np.arange(1,201,1)
 g=pd.DataFrame({'Id':iid,'Gastric Ulcer':predict1},index=ind)
 g.head()
g.to_csv("output.csv",index=False)