Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<drop_column>
import pandas as pd
import numpy as np
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import LabelBinarizer
from sklearn.preprocessing import normalize
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers.core import Dense
import keras
from keras.optimizers import *
from keras.initializers import *
from sklearn.metrics import classification_report
import matplotlib.pyplot as plt
from itertools import chain
test_data=test_data[train_data.drop('cuisine',axis=1 ).columns]<prepare_x_and_y>
df = pd.read_csv(".. /input/train.csv")
 df.head(10 )
X=train_data.drop(['id','ingredients','cuisine'],axis=1)
 y=train_data['cuisine']<split>
mean_fare = np.mean(df.Fare)
 df.loc[df.Fare == 0, "Fare"] = mean_fare
Xtrain,Xval,ytrain,yval=train_test_split(X,y,random_state=42 )<import_modules>
from sklearn.linear_model import LogisticRegression<train_model>
lr=LogisticRegression(solver='liblinear')
 lr.fit(Xtrain,ytrain )<compute_test_metric>
binar = LabelBinarizer().fit(df.loc[:, "Sex"])
 df["Sex"] = binar.transform(df["Sex"])
 df.head(10 )
lr.score(Xval,yval )<predict_on_test>
test_data['cuisine']=lr.predict(test_data.drop(['id','ingredients'],axis=1))<save_to_csv>
df["A_Class"] = 0
df["B_Class"] = 0
df["C_Class"] = 0
df.loc[df.Pclass == 1, "A_Class"] = 1
df.loc[df.Pclass == 2, "B_Class"] = 1
df.loc[df.Pclass == 3, "C_Class"] = 1
df.head()
sub=test_data[['id','cuisine']]
sub.set_index('id',inplace=True)
 sub.to_csv('subWCng.csv' )<import_modules>
df_Embarked = pd.get_dummies(df.Embarked)
 df_Embarked.head()
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import sklearn
import os
import json
import re
import nltk
import zipfile
from datetime import datetime
from sklearn.preprocessing import LabelEncoder
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn.feature_extraction.text import CountVectorizer<load_pretrained>
df.Age.isna().sum()
for t in ['train','test']:
with zipfile.ZipFile(".. /input/whats-cooking/{}.json.zip".format(t),"r")as z:
z.extractall(".")
 with open('./train.json')as data_file:
data = json.load(data_file)
 with open('./test.json')as test_file:
test = json.load(test_file )<prepare_output>
df.Age[df.Age.notna() ][df.Age[df.Age.notna() ] % 1 != 0].head()
df = pd.DataFrame(data)
 test_df = pd.DataFrame(test)
 test_ids = test_df['id']
df.head()<count_missing_values>
( df.isnull().sum() / len(df)) *100<count_missing_values>
df.groupby(["Sex", "Pclass"] ).Age.mean()
( test_df.isnull().sum() / len(test_df)) *100<categorify>
df.loc[(df.Sex == 0)&(df.Age.isna())&(df.Pclass == 1), "Age"] = 34.6
df.loc[(df.Sex == 0)&(df.Age.isna())&(df.Pclass == 2), "Age"] = 28.7
df.loc[(df.Sex == 0)&(df.Age.isna())&(df.Pclass == 3), "Age"] = 21.7
df.loc[(df.Sex == 1)&(df.Age.isna())&(df.Pclass == 1), "Age"] = 41.2
df.loc[(df.Sex == 1)&(df.Age.isna())&(df.Pclass == 2), "Age"] = 30.7
df.loc[(df.Sex == 1)&(df.Age.isna())&(df.Pclass == 3), "Age"] = 26.5
def preprocess_df(df):
def process_string(x):
x = [" ".join([WordNetLemmatizer().lemmatize(q)for q in p.split() ])for p in x]
x = list(map(lambda x: re.sub(r'\ (.*oz.\)|crushed|crumbles|ground|minced|powder|chopped|sliced','', x), x))
x = list(map(lambda x: re.sub("[^a-zA-Z]", " ", x), x))
x = " ".join(x)
 x = x.lower()
return x
df = df.drop('id',axis=1)
 df['ingredients'] = df['ingredients'].apply(process_string)
 return df<prepare_output>
def get_cuisine_cumulated_ingredients(df):
cuisine_df = pd.DataFrame(columns=['ingredients'])
 for cus in cuisine:
st = ""
for x in df[df.cuisine == cus]['ingredients']:
st += x
st += " "
cuisine_df.loc[cus,'ingredients'] = st
cuisine_df = cuisine_df.reset_index()
cuisine_df = cuisine_df.rename(columns ={'index':'cuisine'})
 return cuisine_df<categorify>
for i in range(len(df)) :
if df.loc[i, "SibSp"] + df.loc[i, "Parch"] == 0:
df.loc[i, "Alone"] = 1
else:
df.loc[i, "Alone"] = 0
df.Alone = df.Alone.astype(int)
 df.head()
df = preprocess_df(df)
 test_df = preprocess_df(test_df)
 cuisine_df = get_cuisine_cumulated_ingredients(df )<prepare_x_and_y>
df_new = pd.concat([df, df_Embarked], axis=1)
 df_new.head()
train = df['ingredients']
target = df['cuisine']
test = test_df['ingredients']<feature_engineering>
feature_name = ["Sex", "Age", "Fare", "Alone", "A_Class", "B_Class", "C_Class", "C", "Q", "S", "SibSp", "Parch"]
dfX = df_new[feature_name]
dfY = df_new["Survived"]
def count_vectorizer(train, test=None):
cv = CountVectorizer()
train = cv.fit_transform(train)
 if test is not None:
test = cv.transform(test)
 return train, test, cv
else:
return train, cv<feature_engineering>
scaler = MinMaxScaler()
def tfidf_vectorizer(train, test=None):
tfidf = TfidfVectorizer(stop_words='english',
ngram_range =(1 , 1),analyzer="word",
max_df =.57 , binary=False , token_pattern=r'\w+' , sublinear_tf=False)
 train = tfidf.fit_transform(train)
 if test is not None:
test = tfidf.transform(test)
 return train, test, tfidf
else:
return train, tfidf<feature_engineering>
scaler.fit(dfX )
train_tfidf, test_tfidf, tfidf = tfidf_vectorizer(train,test)
 cuisine_data_tfidf, cuisine_tfidf = tfidf_vectorizer(cuisine_df['ingredients'] )<statistical_test>
dfX = scaler.transform(dfX )
red_cuisine_pca, cus_pca, var_cus_pca = get_PCA(( cuisine_data_tfidf ).toarray() ,2 )<set_options>
dfX = pd.DataFrame(dfX, columns=feature_name )
%%time
wcss_pca = get_kmeans_wcss(red_cuisine_pca,20 )<compute_train_metric>
cluster_cus_pca, km_cus_pca = kmeans(red_cuisine_pca,3)
 cluster_cus_pca<set_options>
model = Sequential()
model.add(Dense(32, input_dim=12, activation="elu", kernel_initializer="he_normal"))
model.add(Dense(64, activation="elu", kernel_initializer="he_normal"))
model.add(Dense(128, activation="elu", kernel_initializer="he_normal"))
model.add(keras.layers.Dropout(0.3))
model.add(Dense(512, activation="elu", kernel_initializer="he_normal"))
model.add(Dense(1024, activation="elu", kernel_initializer="he_normal"))
model.add(Dense(512, activation="elu", kernel_initializer="he_normal"))
model.add(keras.layers.Dropout(0.3))
model.add(Dense(512, activation="elu", kernel_initializer="he_normal"))
model.add(Dense(1024, activation="elu", kernel_initializer="he_normal"))
model.add(Dense(512, activation="elu", kernel_initializer="he_normal"))
model.add(keras.layers.Dropout(0.3))
model.add(Dense(128, activation="elu", kernel_initializer="he_normal"))
model.add(Dense(64, activation="elu", kernel_initializer="he_normal"))
model.add(Dense(32, activation="elu", kernel_initializer="he_normal"))
model.add(keras.layers.Dropout(0.3))
model.add(Dense(1, activation="sigmoid"))
model.compile(optimizer="SGD", loss='binary_crossentropy', metrics=["binary_accuracy"] )
%%time
wcss = get_kmeans_wcss(train_tfidf,30 )<predict_on_test>
model_result = model.fit(dfX, dfY, batch_size=100, epochs=200, validation_split=0.2, shuffle=True, verbose=2 )
cluster, km = kmeans(train_tfidf,19)
 cluster_test = km.predict(test_tfidf)
 cluster<categorify>
test = pd.read_csv(".. /input/test.csv")
 test.head()
enc = OneHotEncoder(handle_unknown='ignore')
 enc.fit(cluster.reshape(-1, 1))
cluster_encoded = enc.transform(cluster.reshape(-1, 1)).toarray()<categorify>
cluster_test_encoded = enc.transform(cluster_test.reshape(-1, 1)).toarray()<concatenate>
np.mean(df.Fare), mean_fare
train_tfidf_nonsparse = np.append(( train_tfidf ).toarray() , cluster_encoded, axis=1 )<concatenate>
test.loc[test.Fare == 0, "Fare"] = mean_fare
test_tfidf_nonsparse = np.append(( test_tfidf ).toarray() , cluster_test_encoded, axis=1 )<train_model>
test["Sex"] = binar.transform(test["Sex"] )
train = train_tfidf
test = test_tfidf<choose_model_class>
param_grid = {'C': [0.001, 0.1, 1, 10, 50, 100, 500, 1000, 5000],
'penalty': ['l1','l2'],
'loss': ['hinge','squared hinge']}
grid = GridSearchCV(LinearSVC() , param_grid, refit = True, verbose = 3, n_jobs=-1, scoring='f1_micro' )<train_model>
test["A_Class"] = 0
test["B_Class"] = 0
test["C_Class"] = 0
test.loc[test.Pclass == 1, "A_Class"] = 1
test.loc[test.Pclass == 2, "B_Class"] = 1
test.loc[test.Pclass == 3, "C_Class"] = 1
%%time
grid.fit(train, target )<find_best_params>
test_Embarked = pd.get_dummies(test.Embarked )
grid.best_params_<find_best_score>
grid.best_score_<compute_train_metric>
test.groupby(["Sex", "Pclass"] ).Age.mean()
def evalfn(C, gamma):
s = SVC(C=float(C), gamma=float(gamma), kernel='rbf', class_weight='balanced')
 f = cross_val_score(s, train, target, cv=5, scoring='f1_micro')
 return f.max()<choose_model_class>
new_opt = BayesianOptimization(evalfn, {'C':(0.1, 1000),
'gamma':(0.0001, 1)} )<init_hyperparams>
C = 604.5300203551828
gamma = 0.9656489284085462
clf = SVC(C=float(C), gamma=float(gamma), kernel='rbf' )<train_model>
test.loc[(test.Sex == 0)&(test.Age.isna())&(test.Pclass == 1), "Age"] = 34.6
test.loc[(test.Sex == 0)&(test.Age.isna())&(test.Pclass == 2), "Age"] = 28.7
test.loc[(test.Sex == 0)&(test.Age.isna())&(test.Pclass == 3), "Age"] = 21.7
test.loc[(test.Sex == 1)&(test.Age.isna())&(test.Pclass == 1), "Age"] = 41.2
test.loc[(test.Sex == 1)&(test.Age.isna())&(test.Pclass == 2), "Age"] = 30.7
test.loc[(test.Sex == 1)&(test.Age.isna())&(test.Pclass == 3), "Age"] = 26.5
%%time
clf.fit(train, target )<load_pretrained>
now = datetime.now()
print("MODEL SAVED AT {}".format(now))
model_name = "SVC-whats-cooking-trial-final2-{}.pickle.dat".format(now)
 pickle.dump(clf, open(model_name, "wb"))<predict_on_test>
for i in range(len(test)) :
if test.loc[i, "SibSp"] + test.loc[i, "Parch"] == 0:
test.loc[i, "Alone"] = 1
else:
test.loc[i, "Alone"] = 0
test.Alone = test.Alone.astype(int )
y_pred = clf.predict(test )<save_to_csv>
test_new = pd.concat([test, test_Embarked], axis=1)
 test_new.head()
my_submission = pd.DataFrame({'id':test_ids})
 my_submission['cuisine'] = y_pred
now = datetime.now()
my_submission.to_csv('submission_{}.csv'.format(now), index=False)
 print('Saved file to disk as submission_{}.csv.'.format(now))<load_pretrained>
testX = test_new[feature_name]
archive_train = zipfile.ZipFile("/kaggle/input/whats-cooking/train.json.zip",'r')
 archive_train<load_from_disk>
testX = scaler.transform(testX )
train_data = pd.read_json(archive_train.read('train.json'))
train_data<load_pretrained>
testX = pd.DataFrame(testX, columns=feature_name)
 testX.head()
archive_test = zipfile.ZipFile("/kaggle/input/whats-cooking/test.json.zip",'r')
 test_data = pd.read_json(archive_test.read("test.json"))
test_data<count_values>
predict = model.predict_classes(testX )
<feature_engineering><EOS> 
my_submission = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': predict})
 my_submission.to_csv('submission.csv', index=False )