Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<train_model>
%matplotlib inline
warnings.filterwarnings('ignore' )
reslearner.unfreeze()
reslearner.fit_one_cycle(2,slice(1e-6))<predict_on_test>
print(os.listdir(".. /input"))
train= pd.read_csv('.. /input/train.csv')
 train_init= pd.read_csv('.. /input/train.csv')
 test= pd.read_csv('.. /input/test.csv')
 test_init= pd.read_csv('.. /input/test.csv')
 train.head()
preds,_ = reslearner.get_preds(ds_type=DatasetType.Test )<filter>
train.set_index('PassengerId', inplace=True)
 test.set_index('PassengerId', inplace=True)
 train.head()
test_df.has_cactus = preds.numpy() [:, 0]<save_to_csv>
print("Missing Values: ",train.Name.isna().sum())
 train.Name.head(5 )
test_df.to_csv('submission.csv', index=False )<set_options>
train['title'] = train.Name.map(lambda x: x.split(',')[1].split('.')[0].strip())
 train['title'] = train['title'].replace('Mlle', 'Miss')
 train['title'] = train['title'].replace(['Mme','Lady','Ms','the Countess'], 'Mrs')
 train.title.loc[(train.title != 'Master')&(train.title != 'Mr')&(train.title != 'Miss')&(train.title != 'Mrs')]
train.title.loc[(train.title != 'Master')&(train.title != 'Mr')&(train.title != 'Miss')&(train.title != 'Mrs')] = 'Others'
print("For train data, title count is:
",train['title'].value_counts())
 test['title'] = test.Name.map(lambda x: x.split(',')[1].split('.')[0].strip())
 test['title'] = test['title'].replace('Mlle', 'Miss')
 test['title'] = test['title'].replace(['Mme','Lady','Ms'], 'Mrs')
 test.title.loc[(test.title != 'Master')&(test.title != 'Mr')&(test.title != 'Miss')&(test.title != 'Mrs')] = 'Others'
test=test.drop(['Name'], axis=1)
 train=train.drop(['Name'], axis=1)
warnings.filterwarnings("ignore" )<load_from_csv>
train['family'] = train.SibSp + train.Parch
test['family'] = test.SibSp + test.Parch
test=test.drop(['SibSp','Parch'], axis=1)
 train=train.drop(['SibSp','Parch'], axis=1 )
train = pd.read_csv('.. //input//train.csv')
 print(train.shape)
 print(train.head(10))<count_values>
print("Fare Values(first 20)
 ",train.Fare.unique() [:20])
 train['fare_value']=round(train.Fare/10)*10
test['fare_value']=round(test.Fare/10)*10
col=train['fare_value']
print("
Fare values after rounding off for Simplification
",col.unique())
train.has_cactus.value_counts()<data_type_conversions>
test[test.Fare.isna() ]
def create_training_model_data(dataframe, batch_size=32, mode='categorical'):
dataframe['has_cactus'] = dataframe['has_cactus'].astype(str)
 IDG = ImageDataGenerator(rescale=1./255.,
horizontal_flip=True,
vertical_flip=True)
 train_data = IDG.flow_from_dataframe(dataframe=dataframe[:15925],
directory='.. //input//train//train',
x_col='id',
y_col='has_cactus',
class_mode='binary',
batch_size=batch_size,
color_mode='grayscale',
target_size=(32,32))
validation_data = IDG.flow_from_dataframe(dataframe=dataframe[15925:],
directory='.. //input//train//train//',
x_col='id',
y_col='has_cactus',
class_mode='binary',
batch_size=batch_size,
color_mode='grayscale',
target_size=(32,32))
return train_data, validation_data<train_model>
test[test.Ticket=="3"][test.title=="Mr"][test.Embarked=="S"][test.Pclass==3]
train_data, validation_data = create_training_model_data(train )<choose_model_class>
test['fare_value']=test['fare_value'].replace(np.nan,10)
 test.fare_value.isna().sum()
model_alexnet = Sequential()
model_alexnet.add(Conv2D(32,(3,3),
input_shape=(32, 32, 1),
padding='same',
activation='relu'))
model_alexnet.add(MaxPooling2D(pool_size=(2,2), padding='same'))
model_alexnet.add(BatchNormalization())
 model_alexnet.add(Conv2D(64,(3,3),
padding='same',
activation='relu'))
model_alexnet.add(MaxPooling2D(pool_size=(2,2), padding='same'))
model_alexnet.add(BatchNormalization())
 model_alexnet.add(Conv2D(64,(3,3),
padding='same',
activation='relu'))
model_alexnet.add(BatchNormalization())
 model_alexnet.add(Conv2D(128,(3,3),
padding='same',
activation='relu'))
model_alexnet.add(BatchNormalization())
 model_alexnet.add(Conv2D(128,(3,3),
padding='same',
activation='relu'))
model_alexnet.add(MaxPooling2D(pool_size=(3,3), padding='same'))
model_alexnet.add(BatchNormalization())
 model_alexnet.add(Flatten())
 model_alexnet.add(Dense(128,
activation='relu', kernel_initializer='glorot_uniform'))
model_alexnet.add(Dropout(0.10))
model_alexnet.add(BatchNormalization())
 model_alexnet.add(Dense(256,
activation='relu', kernel_initializer='glorot_uniform'))
model_alexnet.add(Dropout(0.20))
model_alexnet.add(BatchNormalization())
 model_alexnet.add(Dense(512,
activation='relu', kernel_initializer='glorot_uniform'))
model_alexnet.add(Dropout(0.2))
model_alexnet.add(BatchNormalization())
 model_alexnet.add(Dense(1, activation='sigmoid'))
model_alexnet.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])
 callbacks = [EarlyStopping(monitor='val_loss', patience=20),
ModelCheckpoint(filepath='best_model_alexnet.h5', monitor='val_loss', save_best_only=True)]
model_alexnet.summary()<train_model>
print("Percentage of missing values: ",train.Cabin.isna().sum() /len(train.Cabin)*100 )
history = model_alexnet.fit_generator(train_data,
epochs=100,
steps_per_epoch=(15925/32),
callbacks=callbacks,
validation_data = validation_data,
validation_steps=(1575/32),
class_weight=class_weights,
verbose=2 )<define_variables>
test=test.drop(['Cabin'], axis=1)
 train=train.drop(['Cabin'], axis=1 )
test_IDG = ImageDataGenerator(rescale=1./255.)
test_data = test_IDG.flow_from_directory(
 directory='.. //input//test//',
target_size=(32,32),
color_mode='grayscale',
class_mode='binary',
batch_size=1,
shuffle=False )<predict_on_test>
print("Percentage of missing values: ",train.Embarked.isna().sum() /len(train.Embarked)*100 )
y_pred = model_alexnet.predict_generator(test_data,steps=4000)
 y_pred = np.hstack(y_pred ).tolist()<count_values>
print("Unique Values:
", train.Embarked.value_counts() )
has_cactus = [0 if proba<0.50 else 1 for proba in y_pred]
print(Counter(has_cactus ).keys())
 print(Counter(has_cactus ).values() )<define_variables>
train[train.Embarked.isna() ]
files=[]
files = [f for f in sorted(os.listdir('.. //input//test//test')) ]<create_dataframe>
train[(train.fare_value==80)&(train.Ticket=="1")]
submission = pd.DataFrame({'id':files,
'has_cactus':y_pred} )<save_to_csv>
train.Embarked=train.Embarked.replace(np.nan,"S" )
submission.to_csv('submission.csv', index=False )<save_to_csv>
print("Percentage of missing values: ",train.Age.isna().sum() /len(train.Age)*100 )
submission.to_csv('submission.csv', index=False )<install_modules>
train['age_round']=round(train.Age/10)*10
train.age_round=train.age_round.replace(0,10)
 train.age_round.unique()
!pip install efficientnet_pytorch<define_variables>
train.age_round[train['age_round'] == 10]="child"
train['age_round'] = train['age_round'].replace([0,10], 'child')
 train['age_round'] = train['age_round'].replace([20,30], 'young')
 train['age_round'] = train['age_round'].replace([40,50], 'adult')
 train['age_round'] = train['age_round'].replace([60,70,80], 'old' )
path = Path('.. /input/' )<set_options>
train=train.drop('Age',axis=1)
 train=train.drop('age_round',axis=1)
 test=test.drop('Age',axis=1)
 train=train.drop('Fare',axis=1)
 test=test.drop('Fare',axis=1 )
path.ls()<load_from_csv>
def scale(data):
print("
Scaling Data:
")
 min_max_scaler = preprocessing.MinMaxScaler()
data_scale = min_max_scaler.fit_transform(data)
 data_scale=pd.DataFrame(data_scale, columns=data.columns.values, index=data.index.values)
 print(data_scale.head())
 return(data_scale)
 def cat_to_num(data):
print("
Converting Categorical Data To Numerical:
")
 obj_columns=[]
nonobj_columns=[]
for col in data.columns.values:
if data[col].dtype=='object':
obj_columns.append(col)
 else:
nonobj_columns.append(col)
 print(len(obj_columns)," Object Columns are
",obj_columns,'
')
 print(len(nonobj_columns),"Non-object columns are
",nonobj_columns)
 data_obj=data[obj_columns]
data_nonobj=data[nonobj_columns]
for col in data_obj.columns.values:
data_obj[col]=data_obj[col].astype('category' ).cat.codes
data_merge=pd.concat([data_nonobj,data_obj],axis=1)
 print("
Data after conversion:
",data_merge.head())
 return data_merge
def data_preprocess(data):
data=cat_to_num(data)
 data=scale(data)
 return(data )
train = pd.read_csv(path/'train.csv')
 test = pd.read_csv(path/'sample_submission.csv' )<categorify>
X=data_preprocess(train)
 test_data=data_preprocess(test)
 y=label
test_img = ImageList.from_df(test, path=path/'test', folder='test')
 tfms = get_transforms()
data =(ImageList.from_df(train, path=path/'train', folder='train')
.split_by_rand_pct(0.01)
.label_from_df() 
.add_test(test_img)
.transform(tfms, size=224, resize_method=ResizeMethod.SQUISH)
.databunch(path='.', bs=64, device= torch.device('cuda:0')) 
.normalize(imagenet_stats)
 )<import_modules>
temp=pd.concat([X,label],axis=1 )
from efficientnet_pytorch import EfficientNet<choose_model_class>
html=
display(HTML(html))
model_name = 'efficientnet-b3'
def getModel(pret):
model = EfficientNet.from_pretrained(model_name)
 model._fc = nn.Linear(model._fc.in_features,data.c)
 return model<choose_model_class>
train, test,train_labels, test_labels = train_test_split(X, y, train_size=0.8, random_state=42 )
learn = Learner(data,getModel(True),metrics=[accuracy] )<define_search_space>
clf_dt = DecisionTreeClassifier(random_state=42)
 clf_dt = clf_dt.fit(train, train_labels)
 y_pred=clf_dt.predict(test)
 accuracy_score(test_labels, y_pred )
lr=5e-3<train_model>
clf_rf = RandomForestClassifier(
 random_state=42, 
)
 clf_rf = clf_rf.fit(train, train_labels)
 y_pred=clf_rf.predict(test)
 accuracy_score(test_labels, y_pred )
learn.fit_one_cycle(3,lr )<predict_on_test>
cv_value=5
scores=cross_val_score(clf_dt, X, y, cv=cv_value)
 print("Decision Trees Accuracy: %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores=cross_val_score(clf_rf, X, y, cv=cv_value)
 print("Random Forest Accuracy: %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
preds,_ = learn.get_preds(ds_type=DatasetType.Test)
 idx = preds.numpy() [:,0]<save_to_csv>
clf=clf_rf
print("Default Parameters of estimator are:
",clf.get_params )
test.has_cactus = idx
test.to_csv('submission.csv', index=False )<import_modules>
parameters = {'n_estimators': [10, 100 , 200 , 400, 500],
'max_depth': [10, 20, 40, None],
'warm_start':[True,False],
'min_samples_leaf': [1, 2, 4],
}
p = GridSearchCV(clf , param_grid=parameters, cv=3 )
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from skimage.io import imread
from keras.applications import resnet50
from keras.optimizers import Adam
from keras.layers import GlobalAveragePooling2D, Dense
from keras.models import Model
from keras.callbacks import ModelCheckpoint<load_from_csv>
start_time = time.time()
p.fit(X,y);
elapsed_time = time.time() - start_time
print("Time consumed to fit model: ",time.strftime("%H:%M:%S", time.gmtime(elapsed_time)) )
train = pd.read_csv('.. /input/aerial-cactus-identification/train.csv')
 test = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv')
 y_train = np.array(train.has_cactus )<prepare_x_and_y>
print("Scores for all Parameter Combination:
",p.cv_results_['mean_test_score'])
 print("
Optimal C and Gamma Combination: ",p.best_params_)
 print("
Maximum Accuracy acheieved on LeftOut Data: ",p.best_score_ )
X_train = []
for name in train.id:
X_train.append(imread('.. /input/aerial-cactus-identification/train/train/'+name))<prepare_x_and_y>
clf_rf = RandomForestClassifier(
 random_state=42,
n_estimators=p.best_params_['n_estimators'],
warm_start=p.best_params_['warm_start'],
max_depth=p.best_params_['max_depth'],
min_samples_leaf=p.best_params_['min_samples_leaf'], 
)
X_test = []
for name in test.id:
X_test.append(imread('.. /input/aerial-cactus-identification/test/test/'+name))<prepare_x_and_y>
clf_rf = clf_rf.fit(train, train_labels)
 y_pred=clf_rf.predict(test)
 accuracy_score(test_labels, y_pred )
X_train = np.array(X_train)
 X_test = np.array(X_test )<normalization>
cv_value=5
scores=cross_val_score(clf_dt, X, y, cv=cv_value)
 print("Decision Trees Accuracy: %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores=cross_val_score(clf_rf, X, y, cv=cv_value)
 print("Random Forest Accuracy: %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
X_train = resnet50.preprocess_input(X_train)
 X_test = resnet50.preprocess_input(X_test )<choose_model_class>
clf_rf = clf_rf.fit(X, y )
base_model = resnet50.ResNet50(include_top=False, weights='imagenet' )<choose_model_class>
y_pred=clf_rf.predict(test_data )
<choose_model_class><EOS> 
my_submission = pd.DataFrame({'PassengerId': test_data.index.values, 'Survived': y_pred})
 my_submission.to_csv('submission.csv', index=False)
 my_submission