Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<predict_on_test>
warnings.filterwarnings('ignore' )
test_pred = np.zeros(( 9944, 5), dtype=np.int32)
 for i, model in enumerate(models):
y_pred = model.predict(test_feature_matrix)
 test_pred[:, i] = y_pred.argmax(axis=1)
 def voting(arr):
return np.bincount(arr ).argmax()
predictions = np.apply_along_axis(voting, 1, test_pred )<save_to_csv>
titanic = pd.read_csv('.. /input/titanic/train.csv')
 titanic.head(5 )
result = pd.Series(pd.Categorical.from_codes(predictions, cuisines), test_df.id, name="cuisine")
 result.to_csv("submission.csv", header=True)
 result.value_counts()<import_modules>
titanic['Sex_label'] = titanic['Sex'].astype('category' ).cat.codes
titanic[['Sex','Sex_label']].sample(3 )
import json
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from collections import Counter
from sklearn.preprocessing import LabelEncoder
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import Perceptron
from sklearn.multiclass import OneVsRestClassifier<load_from_disk>
onehots = pd.get_dummies(titanic['Embarked'], prefix='Embarked')
 titanic = titanic.join(onehots )
data = pd.read_json('.. /input/train.json')
 data.head()<create_dataframe>
print('Missing value =','
',titanic.isna().sum())
 print('-'*50)
 print('Duplicated data =',titanic.duplicated().sum() )
n = 6714
frame= pd.DataFrame(Counter([i for sublist in data.ingredients for i in sublist] ).most_common(n))
frame = frame.head(10)
 frame<categorify>
X = titanic[['Pclass',
'Sex_label',
'Age',
'SibSp',
'Parch',
'Fare',
'Embarked_C',
'Embarked_Q',
'Embarked_S']]
y = titanic['Survived']
X_train, X_test,y_train,y_test = train_test_split(X,
y,
test_size = 0.3,
random_state = 789 )
train = json.load(open('.. /input/train.json'))
test = json.load(open('.. /input/test.json'))
train_doc = [" ".join(doc['ingredients'] ).lower() for doc in train]
test_doc = [" ".join(doc['ingredients'] ).lower() for doc in test]
y_train = [doc['cuisine'] for doc in train]
lb = LabelEncoder()
y_train = lb.fit_transform(y_train)
 y_train<feature_engineering>
logreg = LogisticRegression(random_state=789)
 logreg.fit(X_train, y_train )
tfidf = TfidfVectorizer(binary=True)
 X_train = tfidf.fit_transform(train_doc)
 X_test = tfidf.transform(test_doc)
 X_train<train_model>
y_predicted = logreg.predict(X_test)
 y_predicted
svc = SVC(C=100, gamma=0.9, coef0=1, tol=0.001, decision_function_shape=None)
 model_svc = OneVsRestClassifier(svc, n_jobs=1)
 model_svc.fit(X_train, y_train )<choose_model_class>
y_predicted_proba = logreg.predict_proba(X_test)
 y_predicted_proba[:10]
<save_to_csv>
print('
confustion matrix')
 print(confusion_matrix(y_test, y_predicted))
print('
accuracy')
 print(accuracy_score(y_test, y_predicted))
print('
classification report')
 print(classification_report(y_test, y_predicted))
y_test = model_svc.predict(X_test)
 print(y_test)
 y_pred = lb.inverse_transform(y_test)
 print(y_pred)
 test_id = [doc['id'] for doc in test]
sub = pd.DataFrame({'id': test_id, 'cuisine': y_pred}, columns=['id', 'cuisine'])
 sub.to_csv('svm_output.csv', index=False )<save_to_csv>
THRESHOLD = 0.58
predsfinal = np.where(logreg.predict_proba(X_test)[:,1] > THRESHOLD, 1, 0)
 print(classification_report(y_test, predsfinal))
test_id = [doc['id'] for doc in test]
sub = pd.DataFrame({'id': test_id, 'cuisine': y_pred}, columns=['id', 'cuisine'])
 sub.to_csv('svm_output.csv', index=False )<import_modules>
fpr, tpr, thresholds = roc_curve(y_test, predsfinal, pos_label=1)
 print('Nilai Area Under ROC Curve:', auc(fpr, tpr))
print("train Accuracy : ",logreg.score(X_train,y_train))
print("test Accuracy : ",logreg.score(X_test,y_test))
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from nltk import word_tokenize, download
from sklearn.feature_extraction.text import TfidfVectorizer
from nltk.stem import WordNetLemmatizer
from sklearn.multiclass import OneVsRestClassifier
from sklearn.ensemble import ExtraTreesClassifier<prepare_x_and_y>
neigh = KNeighborsClassifier(n_neighbors = 30)
 neigh.fit(X_train,y_train)
 y_predicted = neigh.predict(X_test)
 print('
confustion matrix')
 print(confusion_matrix(y_test, y_predicted))
print('
accuracy')
 print(accuracy_score(y_test, y_predicted))
print('
classification report')
 print(classification_report(y_test, y_predicted))
train = pd.read_json(".. /input/train.json")
 test = pd.read_json(".. /input/test.json")
 y_tr = train.cuisine<remove_duplicates>
fpr, tpr, thresholds = roc_curve(y_test, y_predicted, pos_label=1)
 print('Nilai Area Under ROC Curve:', auc(fpr, tpr))
print("train Accuracy : ",neigh.score(X_train,y_train))
print("test Accuracy : ",neigh.score(X_test,y_test))
lemmatizer = WordNetLemmatizer()
def lemmatize_ingredients(df):
all_ingredients = set()
ingredients_list = []
for i in range(len(df.ingredients)) :
lemmatized_list = []
for ingr in df.ingredients[i]:
split_ingr = ingr.split(" ")
 lemmatized = []
for word in split_ingr:
lemmatized.append(lemmatizer.lemmatize(word.lower()))
all_ingredients.add(" ".join(lemmatized))
lemmatized_list.append(" ".join(lemmatized))
ingredients_list.append(lemmatized_list)
 df['ingredients_lem'] = ingredients_list
return df, all_ingredients<normalization>
tree = DecisionTreeClassifier()
tree.fit(X_train,y_train)
 y_predicted = tree.predict(X_test)
 print('
confustion matrix')
 print(confusion_matrix(y_test, y_predicted))
print('-'*111)
 print('
accuracy')
 print(accuracy_score(y_test, y_predicted))
print('-'*111)
 print('
classification report')
 print(classification_report(y_test, y_predicted))
print('-'*111 )
train, all_ingredients = lemmatize_ingredients(train)
 test, _ = lemmatize_ingredients(test )<categorify>
fpr, tpr, thresholds = roc_curve(y_test, y_predicted, pos_label=1)
 print('Nilai Area Under ROC Curve:', auc(fpr, tpr))
print("train Accuracy : ",tree.score(X_train,y_train))
print("test Accuracy : ",tree.score(X_test,y_test))
train_tfidf_features = tfidf.fit_transform(arraytotext(train.ingredients_lem))
test_tfidf_features= tfidf.transform(arraytotext(test.ingredients_lem))<choose_model_class>
rf = RandomForestClassifier(n_estimators= 100, max_depth=10,
max_features = 'sqrt',
min_samples_split = 8,
min_samples_leaf = 7,
bootstrap = True,
n_jobs = -1,
random_state = 42)
 rf.fit(X_train, y_train)
 y_predicted = rf.predict(X_test)
 print('
confustion matrix')
 print(confusion_matrix(y_test, y_predicted))
print('
accuracy')
 print(accuracy_score(y_test, y_predicted))
print('
precision')
 print(precision_score(y_test, y_predicted))
print('
classification report')
 print(classification_report(y_test, y_predicted))
et = ExtraTreesClassifier(n_estimators=25, max_depth=300, min_samples_split=5, min_samples_leaf=1, random_state=None, min_impurity_decrease=1e-7 )<train_model>
fpr, tpr, thresholds = roc_curve(y_test, y_predicted, pos_label=1)
 print('Nilai Area Under ROC Curve:', auc(fpr, tpr))
print("train Accuracy : ",rf.score(X_train,y_train))
print("test Accuracy : ",rf.score(X_test,y_test))
model = OneVsRestClassifier(et)
 model.fit(train_tfidf_features, y_tr )<save_to_csv>
X = titanic[['Pclass',
'Sex_label',
'Age',
'SibSp',
'Parch',
'Fare',
'Embarked_C',
'Embarked_Q',
'Embarked_S']]
y = titanic['Survived']
X_train, X_test,y_train,y_test = train_test_split(X,
y,
test_size = 0.3,
random_state = 789 )
predictions = model.predict(test_tfidf_features)
 submission = pd.DataFrame()
submission['id'] = test.id
submission['cuisine'] = predictions
submission.to_csv('20180718_ova_etc_4.csv', index=False )<set_options>
clf = AdaBoostClassifier()
clf.fit(X, y)
 y_predicted = clf.predict(X_test)
 print('
confustion matrix')
 print(confusion_matrix(y_test, y_predicted))
print('
accuracy')
 print(accuracy_score(y_test, y_predicted))
print('
precision')
 print(precision_score(y_test, y_predicted))
print('
classification report')
 print(classification_report(y_test, y_predicted))
%matplotlib inline
warnings.filterwarnings("ignore", category=FutureWarning )<load_from_disk>
fpr, tpr, thresholds = roc_curve(y_test, y_predicted, pos_label=1)
 print('Nilai Area Under ROC Curve:', auc(fpr, tpr))
print("train Accuracy : ",clf.score(X_train,y_train))
print("test Accuracy : ",clf.score(X_test,y_test))
def open_json_file(file):

 json_opened = json.load(open(file))
return json_opened<load_from_disk>
titanictest = pd.read_csv('.. /input/titanic/test.csv')
 titanictest.info()
train = open_json_file('.. /input/train.json')
 test = open_json_file('.. /input/test.json' )<define_variables>
titanictest['Sex_label'] = titanictest['Sex'].astype('category' ).cat.codes
onehots = pd.get_dummies(titanictest['Embarked'], prefix='Embarked')
 titanictest = titanictest.join(onehots)
 titanictest = titanictest[['Pclass',
'Sex_label',
'Age',
'SibSp',
'Parch',
'Fare',
'Embarked_C',
'Embarked_Q',
'Embarked_S']]
titanictest.info()
print("--- sample of training data set ---")
 train[0:1]<define_variables>
titanictest.isna().sum()
print("--- sample of testing data set ---")
 test[0:1]<train_model>
titanictest['Age'] = titanictest['Age'].fillna(30)
 titanictest.isna().sum()
print("Training data set size:",len(train))
print("Testing data set size:",len(test))<string_transform>
titanictest['Fare'] = titanictest['Fare'].fillna(35)
 titanictest.isna().sum()
def pre_process_data(data):

 new_data = []
for recipe in data:
new_recipe = []
for ingredient in recipe:
new_ingredient_list = []
for word in ingredient.split() :
word = re.sub('[^a-zA-Z -]+', '', word)
 new_ingredient_list.append(wn.morphy(word.lower().strip(",.!:?;' ")) or word.strip(",.!:?;' "))
new_recipe.append(' '.join(new_ingredient_list))
new_data.append(new_recipe)
 return new_data<prepare_x_and_y>
X = titanic[['Pclass',
'Sex_label',
'Age',
'SibSp',
'Parch',
'Fare',
'Embarked_C',
'Embarked_Q',
'Embarked_S']]
y = titanic['Survived']
X_train, X_test,y_train,y_test = train_test_split(X,
y,
test_size = 0.3,
random_state = 789 )
x_train = list([train[i]['ingredients'] for i in range(len(train)) ])
 y_train_labels = [train[i]['cuisine'] for i in range(len(train)) ]<prepare_x_and_y>
logreg = LogisticRegression(random_state=789)
 logreg.fit(X_train, y_train)
 y_predicted = logreg.predict(titanictest)
 THRESHOLD = 0.58
y_predicted = np.where(logreg.predict_proba(titanictest)[:,1] > THRESHOLD, 1, 0 )
x_test = list([test[i]['ingredients'] for i in range(len(test)) ] )<prepare_x_and_y>
pd.DataFrame(y_predicted ).rename(columns = {0 : 'Survived'} )
new_x_train = pre_process_data(x_train)
 new_x_test = pre_process_data(x_test )<categorify>
dfkagglesubmission = pd.read_csv('.. /input/titanic/test.csv')
 dfid = dfkagglesubmission[['PassengerId']]
dfkagglesubmission = pd.concat([dfid, pd.DataFrame(y_predicted ).rename(columns = {0 : 'Survived'})], axis=1)
 dfkagglesubmission
print("--- Label Encode the Target Variable ---")
 lb_enc = LabelEncoder()
y_enc_labels = lb_enc.fit_transform(y_train_labels)
 print(y_enc_labels )<string_transform>
dfkagglesubmission['Survived'].value_counts()
def bag_of_words(data):
	text_data = [' '.join(recipe ).lower() for recipe in data]
	return text_data
def concatenated_words(data):
	text_data = [' '.join(word.replace(" ","_" ).lower() for word in recipe)for recipe in data]
	return text_data<string_transform>
dfkagglesubmission.to_csv('Titanic_submission.csv',index=False )