Kaggle competition: Titanic - Machine Learning from Disaster
learn= cnn_learner(data, base_arch=models.vgg19_bn, metrics = [accuracy,quadratic_kappa] )<train_model>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
learn.fit_one_cycle(7)
 learn.lr_find()
learn.recorder.plot()<train_model>
test = pd.read_csv("/kaggle/input/titanic/test.csv")
 test.head()
learn.data = data =(
 src.transform(get_transforms(tfms),size=255)
.databunch(bs=bs,num_workers=4)
.normalize() 
)
 learn.freeze()<train_model>
train_data = train_data.drop(columns = ['Cabin','Name','Ticket','PassengerId'] )
learn.unfreeze()
learn.fit_one_cycle(9,slice(1e-4,1e-3))<load_from_csv>
train_data['Age'].fillna(( train_data['Age'].mean()), inplace=True )
sample_df = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 learn.data.add_test(ImageList.from_df(sample_df,'.. /input/aptos2019-blindness-detection/',folder='test_images',suffix='.png'))
preds,y = learn.get_preds(DatasetType.Test )<save_to_csv>
train_data[["Parch", "Survived"]].groupby(['Parch'], as_index=False ).mean().sort_values(by='Survived', ascending=False)
 train_data[["SibSp", "Survived"]].groupby(['SibSp'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
sample_df.diagnosis = preds.argmax(1)
 sample_df.head()
sample_df.to_csv('submission.csv',index=False )<set_options>
yf = train_data.Survived
base_features = ['Parch','SibSp','Age', 'Fare','Pclass']
Xf = train_data[base_features]
train_X, val_X, train_y, val_y = train_test_split(Xf, yf, random_state=1)
 first_model = RandomForestRegressor(n_estimators=21, random_state=1 ).fit(train_X, train_y )
%reload_ext autoreload
%autoreload 2
%matplotlib inline
warnings.filterwarnings("ignore")
 %matplotlib inline 
<load_pretrained>
train_data['FamilySize'] = train_data['SibSp'] + train_data['Parch']
train_data[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=False ).agg('mean' )
md_ef = EfficientNet.from_pretrained('efficientnet-b5', num_classes=1)
 !mkdir models
!cp '.. /input/kaggle-public/abcdef.pth' 'models'<feature_engineering>
train_data['IsAlone'] = 0
train_data.loc[train_data['FamilySize'] == 0, 'IsAlone'] = 1
train_data[['IsAlone', 'Survived']].groupby(['IsAlone'], as_index=False ).mean()
bs = 64
tfms = get_transforms(do_flip=True,flip_vert=True )<choose_model_class>
train_data[["Fare", "Survived"]].groupby(['Survived'], as_index=False ).mean().sort_values(by='Survived', ascending=False)
 train_data.groupby(['Sex','Survived'])[['Fare']].agg(['min','mean','max'] )
learn.load('abcdef');
opt = OptimizedRounder()<predict_on_test>
y2 = train_data.Survived
base_features2 = ['Parch','SibSp','Age', 'Fare','Pclass','Age*Class','FamilySize','IsAlone']
X2 = train_data[base_features2]
train_X2, val_X2, train_y2, val_y2 = train_test_split(X2, y2, random_state=1)
 second_model = RandomForestRegressor(n_estimators=21, random_state=1 ).fit(train_X2, train_y2)
 inter2 = pdp.pdp_interact(model=second_model, dataset=val_X2, model_features=base_features2, features=['Age', 'Pclass'])
 pdp.pdp_interact_plot(pdp_interact_out=inter2, feature_names=['Age', 'Pclass'], plot_type='contour')
 plt.show()
preds0,y = learn.get_preds(DatasetType.Test )<compute_test_metric>
dummies_Sex = pd.get_dummies(train_data.Sex)
 dummies_Embarked = pd.get_dummies(train_data.Embarked)
 train_ready = pd.concat([train_data, dummies_Sex,dummies_Embarked], axis=1)
 train_ready.head()
train_ready = train_ready.drop(columns = ['Sex','Embarked'])
 train_ready.info()
preds =(preds0 + preds1 + preds2 + preds3 + preds4)/5<save_to_csv>
train_ready = train_ready.drop(columns = ['Age*Class'])
 train_ready = train_ready.drop(columns = ['FamilySize'] )
tst_pred = opt.predict(preds, coef=[0.5, 1.5, 2.5, 3.5])
 test_df.diagnosis = tst_pred.astype(int)
 test_df.to_csv('submission.csv',index=False)
 print('done' )<import_modules>
for name in train_ready:
print(name, "column entropy :", round(stats.entropy(train_ready[name].value_counts(normalize=True), base=2),2))
import tensorflow
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import cv2
import os<load_from_csv>
test = test.drop(columns = ['Cabin','Name','Ticket','PassengerId'])
 test.head()
train_df = pd.read_csv('.. /input/aptos2019-blindness-detection/train.csv')
 train_df['id_code'] = train_df['id_code'].apply(lambda x:x+'.png')
 train_df['diagnosis'] = train_df['diagnosis'].astype(str)
 test_df = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 test_df['id_code'] = test_df['id_code'].apply(lambda x:x+'.png' )<define_variables>
test['Age'].fillna(( test['Age'].mean()), inplace=True)
 test['Fare'].fillna(( test['Fare'].mean()), inplace=True)
 test.loc[ test['Fare'] <= 7.22, 'Fare'] = 0
test.loc[(test['Fare'] > 7.22)&(test['Fare'] <= 21.96), 'Fare'] = 1
test.loc[(test['Fare'] > 21.96)&(test['Fare'] <= 40.82), 'Fare'] = 2
test.loc[ test['Fare'] > 40.82, 'Fare'] = 3
test['FamilySize'] = test['SibSp'] + test['Parch'] + 1
test['IsAlone'] = 0
test.loc[test['FamilySize'] == 1, 'IsAlone'] = 1
test.loc[ test['Age'] <= 16, 'Age'] = 1
test.loc[(test['Age'] > 16)&(test['Age'] <= 32), 'Age'] = 2
test.loc[(test['Age'] > 32)&(test['Age'] <= 64), 'Age'] = 3
test.loc[ test['Age'] > 64, 'Age'] = 4
test['Age*Class'] = test.Age * test.Pclass
test.info()
num_classes = train_df['diagnosis'].nunique()
TRAIN_DATA_ROOT = './train_images_preprocessed/'
TEST_DATA_ROOT = './test_images_preprocessed/'
BATCH_SIZE = 16
train_datagen = ImageDataGenerator(
 rescale = 1/255,
rotation_range = 360,
horizontal_flip = True,
vertical_flip = True,
zoom_range = [0.98, 1.02],
width_shift_range = 0.01,
height_shift_range = 0.01,
validation_split = 0.20)
 validation_datagen = ImageDataGenerator(
 rescale = 1/255,
validation_split = 0.20)
 train_generator = train_datagen.flow_from_dataframe(
 dataframe = train_df,
directory = TRAIN_DATA_ROOT,
x_col = 'id_code',
y_col = 'diagnosis',
batch_size = BATCH_SIZE,
shuffle = True,
class_mode = 'categorical',
target_size =(IMAGE_HEIGHT, IMAGE_WIDTH),
subset = 'training')
 validation_generator = validation_datagen.flow_from_dataframe(
 dataframe = train_df,
directory = TRAIN_DATA_ROOT,
x_col = 'id_code',
y_col = 'diagnosis',
batch_size = BATCH_SIZE,
shuffle = True,
class_mode = 'categorical',
target_size =(IMAGE_HEIGHT, IMAGE_WIDTH),
subset = 'validation' )<normalization>
test_dummies_Sex = pd.get_dummies(test.Sex)
 test_dummies_Embarked = pd.get_dummies(test.Embarked)
 test_ready = pd.concat([test, test_dummies_Sex,test_dummies_Embarked], axis=1)
 test_ready.head()
test_ready = test_ready.drop(columns = ['Sex','Embarked'])
 test_ready = test_ready.drop(columns = ['Age*Class'])
 test_ready = test_ready.drop(columns = ['FamilySize'] )
sklearn_class_weights = class_weight.compute_class_weight(
 'balanced',
np.unique(train_generator.classes),
train_generator.classes)
 print(sklearn_class_weights )<choose_model_class>
y = train_ready['Survived'].values
X = train_ready.drop('Survived',axis=1 ).values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state=21, stratify=y)
 warnings.filterwarnings("ignore")
def create_resnet50_model(input_shape, n_out):
base_model = ResNet50(weights = None,
include_top = False,
input_shape = input_shape)
 base_model.load_weights('.. /input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')
 model = Sequential()
model.add(base_model)
 model.add(GlobalAveragePooling2D())
 model.add(Dropout(0.5))
model.add(Dense(n_out, activation = 'sigmoid'))
return model
def create_densenet121_model(input_shape, n_out):
base_model = DenseNet121(weights = None,
include_top = False,
input_shape = input_shape)
 base_model.load_weights('.. /input/densenet-keras/DenseNet-BC-121-32-no-top.h5')
 model = Sequential()
model.add(base_model)
 model.add(GlobalAveragePooling2D())
 model.add(Dropout(0.5))
model.add(Dense(n_out, activation = 'sigmoid'))
return model<load_pretrained>
clfs = []
seed = 3
clfs.append(( "LogReg", Pipeline([("Scaler", StandardScaler()),("LogReg", LogisticRegression())])))
 clfs.append(( "XGBClassifier",Pipeline([("Scaler", StandardScaler()),("XGB", XGBClassifier())])))
 clfs.append(( "KNN",Pipeline([("Scaler", StandardScaler()),("KNN", KNeighborsClassifier(n_neighbors=8)) ])))
 clfs.append(( "DecisionTreeClassifier",Pipeline([("Scaler", StandardScaler()),("DecisionTrees", DecisionTreeClassifier())])))
 clfs.append(( "RandomForestClassifier",Pipeline([("Scaler", StandardScaler()),("RandomForest", RandomForestClassifier())])))
 clfs.append(( "GradientBoostingClassifier",Pipeline([("Scaler", StandardScaler()),("GradientBoosting", GradientBoostingClassifier(n_estimators=100)) ])))
 clfs.append(( "RidgeClassifier",Pipeline([("Scaler", StandardScaler()),("RidgeClassifier", RidgeClassifier())])))
 clfs.append(( "BaggingRidgeClassifier", Pipeline([("Scaler", StandardScaler()),("BaggingClassifier", BaggingClassifier())])))
 clfs.append(( "ExtraTreesClassifier",Pipeline([("Scaler", StandardScaler()),("ExtraTrees", ExtraTreesClassifier())])))
 scoring = 'accuracy'
n_folds = 7
results, names = [], []
for name, model in clfs:
kfold = KFold(n_splits=n_folds, random_state=seed)
 cv_results = cross_val_score(model, X_train, y_train,cv= 5, scoring=scoring, n_jobs=-1)
 names.append(name)
 results.append(cv_results)
 msg = "%s: %f(+/- %f)" %(name, cv_results.mean() , cv_results.std())
 print(msg)
 fig = plt.figure(figsize=(15,6))
fig.suptitle('Classifier Algorithm Comparison', fontsize=22)
 ax = fig.add_subplot(111)
 sns.boxplot(x=names, y=results)
 ax.set_xticklabels(names)
 ax.set_xlabel("Algorithmn", fontsize=20)
 ax.set_ylabel("Accuracy of Models", fontsize=18)
 ax.set_xticklabels(ax.get_xticklabels() ,rotation=45)
 plt.show()
PRETRAINED_MODEL = '.. /input/pretrained_blindness_detector/blindness_detector.h5'
if(os.path.exists(PRETRAINED_MODEL)) :
print('Restoring model from ' + PRETRAINED_MODEL)
 model.load_weights(PRETRAINED_MODEL)
 else:
print('No pretrained model found.Using fresh model.')
 current_epoch = 0<train_model>
scaler = StandardScaler()
scaler.fit(X)
 scaled_features = scaler.transform(X)
 train_sc = pd.DataFrame(scaled_features)
 X_csv_test = test_ready.values
scaler.fit(X_csv_test)
 scaled_features_test = scaler.transform(X_csv_test)
 test_sc = pd.DataFrame(scaled_features_test)
 scaled_features_test.shape
<choose_model_class><EOS> 
clf = xgb.XGBClassifier(n_estimators=250, random_state=4,bagging_fraction= 0.791787170136272, colsample_bytree= 0.7150126733821065,feature_fraction= 0.6929758008695552,gamma= 0.6716290491053838,learning_rate= 0.030240003246947006,max_depth= 2,min_child_samples= 5,num_leaves= 15,reg_alpha= 0.05822089056228967,reg_lambda= 0.14016232510869098,subsample= 0.9)
 clf.fit(scaled_features, y)
 y_pred_xgb= clf.predict(scaled_features_test)
 print(y_pred_xgb)
 result_xgb = pd.read_csv("/kaggle/input/titanic/gender_submission.csv")
 result_xgb['Survived'] = y_pred_xgb
result_xgb.to_csv('Titanic_xgb.csv', index=False)
 result_xgb.head()