Kaggle competition: Pneumonia Diagnosis
param_meta1={'cv': 10,
'estimators': [['Logis',
LogisticRegression(C=0.4, class_weight=10, l1_ratio=0.5, max_iter=10000,
penalty='l1', random_state=81, solver='saga')],
['Rando',
RandomForestClassifier(max_depth=3, min_samples_leaf=5, min_samples_split=5,
n_estimators=25, random_state=81)],
['LGBMC',
LGBMClassifier(colsample_bytree=1, max_depth=3, metric='None',
min_child_samples =10, min_child_weight=10.0, n_estimators=500,
n_jobs=4, num_leaves=6, random_state=81, reg_alpha=2,
reg_lambda=50, subsample=0.6)]],
'final_estimator': LogisticRegression(C=3.3334, l1_ratio=0.5, max_iter=10000, multi_class='ovr',
random_state=81, solver='saga'),
'n_jobs': None,
'passthrough': False,
'stack_method': 'auto',
'verbose': 0}
param_meta3={'cv': 10,
'estimators': [['Logis',
LogisticRegression(C=30, class_weight=1, l1_ratio=0.5, max_iter=10000,
penalty='none', random_state=81, solver='saga')],
['Rando',
RandomForestClassifier(max_depth=10, max_features='sqrt', min_samples_leaf=2,
min_samples_split=15, n_estimators=1000,
random_state=81)],
['LGBMC',
LGBMClassifier(colsample_bytree=0.5, max_depth=1, metric='None',
min_child_samples =15, min_child_weight=0.01, n_estimators=1000,
n_jobs=4, num_leaves=6, random_state=81, reg_alpha=0,
reg_lambda=50, subsample=0.6)]],
'final_estimator': LogisticRegression(C=3.3334, l1_ratio=0.5, max_iter=10000, multi_class='ovr',
random_state=81, solver='saga'),
'n_jobs': None,
'passthrough': False,
'stack_method': 'auto',
'verbose': 0}<choose_model_class>
img_size =(224, 224)
 img_array_list = []
cls_list = []
dir = '.. /input/train/PNEUMONIA'
img_list = glob.glob(dir + '/*.jpeg')
 for i in img_list:
img = load_img(i, color_mode='grayscale', target_size=(img_size))
img_array = img_to_array(img)/ 255
img_array_list.append(img_array)
 cls_list.append(1)
 dir = '.. /input/train/NORMAL'
img_list = glob.glob(dir + '/*.jpeg')
 for i in img_list:
img = load_img(i, color_mode='grayscale', target_size=(img_size))
img_array = img_to_array(img)/ 255
img_array_list.append(img_array)
 cls_list.append(0)
 X_train = np.array(img_array_list)
 y_train = np.array(cls_list)
 print(X_train.shape, y_train.shape )
stack_clf1=StackingClassifier(**param_meta1)
 stack_clf3=StackingClassifier(**param_meta3 )<train_model>
X_learn, X_valid, y_learn, y_valid = train_test_split(X_train, y_train, test_size=0.3, random_state=0)
 shape = X_learn.shape
X_learn = np.reshape(X_learn,(shape[0], shape[1] * shape[2] * shape[3]))
ros = RandomOverSampler(random_state=0)
 X_res, y_res = ros.fit_resample(X_learn, y_learn)
 X_res = np.reshape(X_res,(X_res.shape[0], shape[1], shape[2], shape[3]))
Y_res = to_categorical(y_res)
 Y_valid = to_categorical(y_valid)
 print('Train:', X_res.shape, Y_res.shape)
 print('Test:', X_valid.shape, Y_valid.shape )
stack_clf1.fit(train_1[col_final],train_1['Survived'])
 stack_clf3.fit(train_3[col_final],train_3['Survived'] )<predict_on_test>
def auc(y_true, y_pred):
auc = tf.py_func(roc_auc_score,(y_true, y_pred), tf.double)
 return auc
model = Sequential()
model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu', input_shape=(img_size[0], img_size[1], 1)))
 model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(3, 3)))
 model.add(Dropout(rate=0.25))
model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))
model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(3, 3)))
 model.add(Dropout(rate=0.25))
model.add(Flatten())
 model.add(Dense(units=128, activation='relu'))
model.add(Dropout(rate=0.25))
model.add(Dense(units=2, activation='softmax'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy', auc])
 model.summary()
y_pred_1=stack_clf1.predict(test_1[col_final])
 y_pred_3=stack_clf3.predict(test_3[col_final] )<create_dataframe>
model.fit(X_res, Y_res, batch_size=32, epochs=10)
 model.evaluate(X_valid, Y_valid, batch_size=32 )
y_pred_1=pd.DataFrame({'PassengerId':test_1['PassengerId'],'Survived':y_pred_1 }, dtype=int)
 y_pred_2=pd.DataFrame({'PassengerId':test_2['PassengerId'],'Survived':1 }, dtype=int)
 y_pred_3=pd.DataFrame({'PassengerId':test_3['PassengerId'],'Survived':y_pred_3 }, dtype=int )<save_to_csv>
shape = X_train.shape
X_train = np.reshape(X_train,(shape[0], shape[1] * shape[2] * shape[3]))
ros = RandomOverSampler(random_state=0)
 X_res, y_res = ros.fit_resample(X_train, y_train)
 X_train = np.reshape(X_train,(shape[0], shape[1], shape[2], shape[3]))
X_res = np.reshape(X_res,(X_res.shape[0], shape[1], shape[2], shape[3]))
Y_res = to_categorical(y_res)
 print('Train:', X_res.shape, Y_res.shape )
y_sub=pd.concat([y_pred_1,y_pred_2,y_pred_3], axis=0 ).sort_values('PassengerId')
 y_sub.to_csv("sub.csv",index=False )<import_modules>
model.fit(X_res, Y_res, batch_size=32, epochs=10 )
import pandas as pd
import numpy as np
import seaborn as sns
from sklearn.preprocessing import OneHotEncoder, LabelEncoder, StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.feature_selection import RFECV, RFE
from sklearn.model_selection import ParameterGrid, GridSearchCV, KFold, cross_val_score
from sklearn.metrics import classification_report
from sklearn.pipeline import Pipeline
import matplotlib.pyplot as plt
import catboost 
<define_variables>
img_array_list = []
dir = '.. /input/test'
img_list = glob.glob(dir + '/*.jpeg')
 img_list.sort()
for i in img_list:
img = load_img(i, color_mode='grayscale', target_size=(img_size))
img_array = img_to_array(img)/ 255
img_array_list.append(img_array)
 X_test = np.array(img_array_list)
 print(X_test.shape )
random_state = 101<load_from_csv>
predict = model.predict(X_test)[:, 1]
<categorify><EOS> 
submit = pd.read_csv('.. /input/sampleSubmission.csv')
 submit['pneumonia'] = predict
submit.to_csv('submission.csv', index=False )