Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<load_from_csv>
sns.set(style="darkgrid")
 warnings.filterwarnings('ignore')
 SEED = 42
for dirname, _, filenames in os.walk('/kaggle/input/'):
for filename in filenames:
print(os.path.join(dirname, filename))
Trainset = pd.read_csv("/kaggle/input/hackstat2k19/Trainset.csv", header = 0)
 Trainset = Trainset.dropna()
sampleset = pd.read_csv("/kaggle/input/hackstat2k19/sample_submisison.csv", header = 0)
 sampleset = sampleset.dropna()
Trainset = Trainset.replace(to_replace=['Jan', 'Feb','Mar','Apr','May','June','Jul','Aug','Sep','Oct','Nov','Dec'], value=[0,2,166,0,307,21,54,69,72,97,641,183])
 Trainset = Trainset.replace(to_replace=['Returning_Visitor', 'New_Visitor' , 'Other'], value=[1242, 366, 11])
 xset = pd.read_csv("/kaggle/input/hackstat2k19/xtest.csv")
 xset = xset.dropna()
xset = xset.replace(to_replace=['Jan', 'Feb','Mar','Apr','May','June','Jul','Aug','Sep','Oct','Nov','Dec'], value=[0,2,166,0,307,21,54,69,72,97,641,183])
 xset = xset.replace(to_replace=['Returning_Visitor', 'New_Visitor' , 'Other'], value=[1242, 366, 11])
 print(Trainset)
 Train, Test = Trainset,xset
print('Number of Training examples', len(Train))
print('Number of Testing examples', len(Test))
features = Trainset.columns[:16]
print(features)
 y = Train['Revenue']
y
id_ = sampleset['ID']
clf = RandomForestClassifier(n_jobs = 2, random_state = 0)
 clf.fit(Train[features],y)
 clf.predict(Test[features])
 clf.predict_proba(Test[features])
 preds = clf.predict(Test[features])
 YArray= id_.as_matrix(columns=None)
 print(YArray)
 df = pd.DataFrame({"ID" : YArray, "Revenue" : preds})
 df.to_csv("submission.csv", index=False)
<set_options>
train=pd.read_csv("/kaggle/input/titanic/train.csv")
 test=pd.read_csv("/kaggle/input/titanic/test.csv")
 titanic = train.append(test, ignore_index=True)
 train_idx = len(train)
 test_idx = len(titanic)- len(test)
 passengerId = test.PassengerId
%matplotlib inline<load_from_csv>
titanic.isnull().sum() [titanic.isnull().sum() >0]
dftrain = pd.read_csv('.. /input/train.csv',index_col='Id')
 xtest = pd.read_csv('.. /input/Xtest.csv',index_col='Id')
 dftrain.head()<prepare_x_and_y>
titanic['Title'] = titanic.Name.apply(lambda name: name.split(',')[1].split('.')[0].strip())
 print(titanic.Title.value_counts() )
xtrain = dftrain.x.values.reshape(-1,1)
 ytrain = dftrain.y.values.reshape(-1,1 )<choose_model_class>
standardized_titles = {
"Capt": "Officer",
"Col": "Officer",
"Major": "Officer",
"Jonkheer": "Aristorcat",
"Don": "Aristorcat",
"Sir" : "Aristorcat",
"Dr": "Officer",
"Rev": "Officer",
"the Countess":"Aristorcat",
"Dona": "Aristorcat",
"Mme": "Mrs",
"Mlle": "Miss",
"Ms": "Mrs",
"Mr" : "Mr",
"Mrs" : "Mrs",
"Miss" : "Miss",
"Master" : "Master",
"Lady" : "Aristorcat"
}
titanic.Title = titanic.Title.map(standardized_titles)
 print(titanic.Title.value_counts() )
reg = LinearRegression()<train_model>
titanic.Age = grouped.Age.apply(lambda x: x.fillna(x.median()))
reg.fit(xtrain,ytrain )<predict_on_test>
titanic.Embarked = titanic.Embarked.fillna(titanic.Embarked.mode() [0] )
yhat_train = reg.predict(xtrain ).ravel()
yhat_test = reg.predict(xtest ).ravel()<save_to_csv>
med_fare = titanic.groupby(['Pclass', 'Parch', 'SibSp'] ).Fare.median() [3][0][0]
titanic['Fare'] = titanic['Fare'].fillna(med_fare )
submission.to_csv('testsubmission.csv',header=True )<import_modules>
titanic['Cabin'] = titanic['Cabin'].fillna('U')
 titanic["Cabin"] = pd.Series([i[0] if not pd.isnull(i)else 'X' for i in titanic['Cabin'] ] )
from fastai.vision import *
from fastai.metrics import error_rate<define_variables>
titanic.isnull().sum() [titanic.isnull().sum() >0]
path=pathlib.Path(os.path.normpath('.. /input/car_data/car_data'))<define_variables>
grouped = titanic.groupby(['Sex'])
 grouped.Survived.mean()
train=path/'train'
test=path/'test'<feature_engineering>
grouped = titanic.groupby(['Pclass'])
 grouped.Survived.mean()
tfms=get_transforms()<load_pretrained>
grouped = titanic.groupby(['SibSp'])
 grouped.Survived.mean()
data=ImageDataBunch.from_folder(path=path,valid='test',bs=32,ds_tfms=tfms,size=(320,320)).normalize(imagenet_stats )<define_variables>
grouped = titanic.groupby(['Parch'])
 grouped.Survived.mean()
data.show_batch(rows=3,figsize=(12,12))<choose_model_class>
grouped = titanic.groupby(['Cabin'])
 grouped.Survived.mean()
learn=cnn_learner(data,models.resnet50,pretrained=True,metrics=error_rate,model_dir='/tmp/model/' )<train_model>
grouped = train.groupby(['Embarked'])
 grouped.Survived.mean()
learn.fit_one_cycle(10 )<save_model>
titanic['Ticket_Frequency'] = titanic.groupby('Ticket')['Ticket'].transform('count')
 grouped = titanic.groupby(['Ticket_Frequency'])
 grouped.Survived.mean()
learn.save('stage-1')
<find_best_params>
titanic['Fare'] =(titanic['Fare'] / titanic['Ticket_Frequency'] )
learn.lr_find()<train_model>
def extract_surname(data):
families = []
for i in range(len(data)) :
name = data.iloc[i]
if '(' in name:
name_no_bracket = name.split('(')[0]
else:
name_no_bracket = name
family = name_no_bracket.split(',')[0]
title = name_no_bracket.split(',')[1].strip().split(' ')[0]
for c in string.punctuation:
family = family.replace(c, '' ).strip()
families.append(family)
 return families
titanic['Family'] = extract_surname(titanic['Name'])
 df_train = titanic.loc[:890]
df_test = titanic.loc[891:]
dfs = [df_train, df_test]
learn.fit_one_cycle(10,max_lr=slice(3e-06,4e-06))<save_model>
mean_survival_rate = np.mean(df_train['Survived'])
 train_family_survival_rate = []
train_family_survival_rate_NA = []
test_family_survival_rate = []
test_family_survival_rate_NA = []
for i in range(len(df_train)) :
if df_train['Family'][i] in family_rates:
train_family_survival_rate.append(family_rates[df_train['Family'][i]])
 train_family_survival_rate_NA.append(1)
 else:
train_family_survival_rate.append(mean_survival_rate)
 train_family_survival_rate_NA.append(0)
 for i in range(len(df_test)) :
if df_test['Family'].iloc[i] in family_rates:
test_family_survival_rate.append(family_rates[df_test['Family'].iloc[i]])
 test_family_survival_rate_NA.append(1)
 else:
test_family_survival_rate.append(mean_survival_rate)
 test_family_survival_rate_NA.append(0)
 df_train['Family_Survival_Rate'] = train_family_survival_rate
df_train['Family_Survival_Rate_NA'] = train_family_survival_rate_NA
df_test['Family_Survival_Rate'] = test_family_survival_rate
df_test['Family_Survival_Rate_NA'] = test_family_survival_rate_NA
train_ticket_survival_rate = []
train_ticket_survival_rate_NA = []
test_ticket_survival_rate = []
test_ticket_survival_rate_NA = []
for i in range(len(df_train)) :
if df_train['Ticket'][i] in ticket_rates:
train_ticket_survival_rate.append(ticket_rates[df_train['Ticket'][i]])
 train_ticket_survival_rate_NA.append(1)
 else:
train_ticket_survival_rate.append(mean_survival_rate)
 train_ticket_survival_rate_NA.append(0)
 for i in range(len(df_test)) :
if df_test['Ticket'].iloc[i] in ticket_rates:
test_ticket_survival_rate.append(ticket_rates[df_test['Ticket'].iloc[i]])
 test_ticket_survival_rate_NA.append(1)
 else:
test_ticket_survival_rate.append(mean_survival_rate)
 test_ticket_survival_rate_NA.append(0)
 df_train['Ticket_Survival_Rate'] = train_ticket_survival_rate
df_train['Ticket_Survival_Rate_NA'] = train_ticket_survival_rate_NA
df_test['Ticket_Survival_Rate'] = test_ticket_survival_rate
df_test['Ticket_Survival_Rate_NA'] = test_ticket_survival_rate_NA
learn.save('stage-2' )<define_variables>
for df in [df_train, df_test]:
df['Survival_Rate'] =(df['Ticket_Survival_Rate'] + df['Family_Survival_Rate'])/ 2
df['Survival_Rate_NA'] =(df['Ticket_Survival_Rate_NA'] + df['Family_Survival_Rate_NA'])/ 2
root='.. /input/car_data/car_data/test/'
images=list()
for i in listofSubdirectory:
images.append(os.listdir(root+i))
images<load_pretrained>
non_numeric_features = ['Embarked', 'Sex', 'Deck', 'Title', 'Family_Size_Grouped', 'Age', 'Fare', 'NameLength']
for df in dfs:
for feature in non_numeric_features:
df[feature] = LabelEncoder().fit_transform(df[feature] )
learn.load('stage-2' )<import_modules>
cat_features = ['Pclass', 'Sex', 'Deck', 'Embarked', 'Title', 'Family_Size_Grouped']
encoded_features = []
for df in dfs:
for feature in cat_features:
encoded_feat = OneHotEncoder().fit_transform(df[feature].values.reshape(-1, 1)).toarray()
n = df[feature].nunique()
cols = ['{}_{}'.format(feature, n)for n in range(1, n + 1)]
encoded_df = pd.DataFrame(encoded_feat, columns=cols)
 encoded_df.index = df.index
encoded_features.append(encoded_df)
 df_train = pd.concat([df_train, *encoded_features[:6]], axis=1)
 df_test = pd.concat([df_test, *encoded_features[6:]], axis=1 )
learn.export('/tmp/model/export.pkl' )<load_pretrained>
titanic = pd.concat([df_train, df_test], sort=True ).reset_index(drop=True)
 drop_cols = ['Deck', 'Cabin', 'Embarked', 'Family', 'Family_Size', 'Family_Size_Grouped', 'Survived',
'Name', 'Parch', 'PassengerId', 'Pclass', 'Sex', 'SibSp', 'Ticket', 'Title',
'Ticket_Survival_Rate', 'Family_Survival_Rate', 'Ticket_Survival_Rate_NA', 'Family_Survival_Rate_NA']
titanic.drop(columns=drop_cols, inplace=True)
 titanic.head()
learn=load_learner('/tmp/model/' )<predict_on_test>
X_train = StandardScaler().fit_transform(df_train.drop(columns=drop_cols))
y_train = df_train['Survived'].values
X_test = StandardScaler().fit_transform(df_test.drop(columns=drop_cols))
print('X_train shape: {}'.format(X_train.shape))
print('y_train shape: {}'.format(y_train.shape))
print('X_test shape: {}'.format(X_test.shape))
columns = ['Id','Predicted']
listofprediction=list()
for i in range(len(listofSubdirectory)) :
for j in images[i]:
pt=root+listofSubdirectory[i]+'/'+j
img=open_image(pt)
 name=j.replace('.jpg','')
 _,pred_idx,output=learn.predict(img)
 listofprediction.append([name,pred_idx.item() ])
<define_variables>
max_evals = 2
space={
'n_estimators': hp.uniform("n_estimators", 500, 2000),
'max_features': hp.choice("max_features", ["auto", "sqrt"]),
'max_depth': hp.uniform("max_depth", 2, 12),
'min_samples_split': hp.uniform("min_samples_split", 2, 11),
'min_samples_leaf': hp.uniform("min_samples_leaf", 2, 11),
}
def hyperparameter_tuning(space):
model=RandomForestClassifier(
 n_estimators =int(space['n_estimators']),
max_depth =int(space['max_depth']),
max_features = space['max_features'],
min_samples_split =int(space['min_samples_split']),
min_samples_leaf =int(space['min_samples_leaf']),
random_state = SEED 
)
 scores = cross_val_score(model, X_train, y_train, scoring='accuracy', cv=StratifiedKFold(n_splits=5,shuffle=True))
print("SCORE: %0.2f" %(-scores.mean()))
return {'loss':-scores.mean() , 'status': STATUS_OK }
trials = Trials()
best = fmin(fn=hyperparameter_tuning,
space=space,
algo=tpe.suggest,
max_evals=max_evals,
trials=trials)
 print(best )
array=np.array(listofprediction )<create_dataframe>
model = RandomForestClassifier(criterion='gini',
n_estimators=1750,
max_depth=7,
min_samples_split=6,
min_samples_leaf=6,
max_features='auto',
oob_score=True,
random_state=SEED,
n_jobs=-1,
verbose=1 )
submission=pd.DataFrame(array,columns=columns)
 submission<save_to_csv>
N = 5
oob = 0
probs = pd.DataFrame(np.zeros(( len(X_test), N * 2)) , columns=['Fold_{}_Prob_{}'.format(i, j)for i in range(1, N + 1)for j in range(2)])
 importances = pd.DataFrame(np.zeros(( X_train.shape[1], N)) , columns=['Fold_{}'.format(i)for i in range(1, N + 1)], index=titanic.columns)
 skf = StratifiedKFold(n_splits=N, random_state=N, shuffle=True)
 for fold,(trn_idx, val_idx)in enumerate(skf.split(X_train, y_train), 1):
print('Fold {}
'.format(fold))
model.fit(X_train[trn_idx], y_train[trn_idx])
 probs.loc[:, 'Fold_{}_Prob_0'.format(fold)] = model.predict_proba(X_test)[:, 0]
probs.loc[:, 'Fold_{}_Prob_1'.format(fold)] = model.predict_proba(X_test)[:, 1]
importances.iloc[:, fold - 1] = model.feature_importances_
oob += model.oob_score_ / N
print('Fold {} OOB Score: {}
'.format(fold, model.oob_score_))
print('Average OOB Score: {}'.format(oob))
submission.to_csv('submission.csv',index=False )<load_from_csv>
class_survived = [col for col in probs.columns if col.endswith('Prob_1')]
probs['1'] = probs[class_survived].sum(axis=1)/ N
probs['0'] = probs.drop(columns=class_survived ).sum(axis=1)/ N
probs['pred'] = 0
pos = probs[probs['1'] >= 0.50].index
probs.loc[pos, 'pred'] = 1
y_pred = probs['pred'].astype(int)
 submission_df = pd.DataFrame(columns=['PassengerId', 'Survived'])
 submission_df['PassengerId'] = df_test['PassengerId']
submission_df['Survived'] = y_pred.values
class CarDataset(datasets.ImageFolder):

 def __init__(self,root,csv_file,transform=None):
super(CarDataset,self ).__init__(root,transform)
 self.df = pd.read_csv(csv_file,header=None)
 def __getitem__(self, index):
original_tuple = super(CarDataset, self ).__getitem__(index)
 path = self.imgs[index][0]
label = path.split('/')[5]
if label=='Ram C-V Cargo Van Minivan 2012':
label='Ram C/V Cargo Van Minivan 2012'
name_index = self.df.index[self.df[0]==label][0]
target = torch.zeros([1,196])
 target[0][name_index] = 1
path = path.split('/')[6][0:-4]
tuple_with_path =(original_tuple +(path,)+(name_index,))
return tuple_with_path<train_model>
os.chdir('/kaggle/working')
 submission_df.to_csv('titanic_submissions.csv', header=True, index=False )