Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<create_dataframe>
import os
import warnings
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from sklearn import metrics, preprocessing
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
subm = pd.DataFrame({'id':df_test['id'], 'cuisine':Y_test} )<save_to_csv>
warnings.filterwarnings("ignore")
 df_train_original = pd.read_csv(".. /input/train.csv")
 df_test_original = pd.read_csv(".. /input/test.csv")
subm.to_csv('submission.csv', index=False )<set_options>
def trataDados(train, test, drop_list, target):
dados_origin = pd.concat(( train, test))
dados_origin = dados_origin.drop(drop_list, axis=1)
 dados_origin[dados_origin.isnull().any(axis=1)]
if dados_origin.isnull().values.any() :
dados_origin = dados_origin.fillna(dados_origin.mean())
 dados_origin = pd.get_dummies(dados_origin)
 target_rounded = [round(label, ndigits=None)for label in dados_origin[target]]
target_rounded = [int(survived)for survived in target_rounded]
dados_origin[target] = target_rounded
return_train = dados_origin[: train.shape[0]]
train1, test1 = train_test_split(
 return_train, test_size=0.20, random_state=600, stratify=train[target] 
)
 yo_train = train1[target]
yo_test = test1[target]
Xo_train = train1.drop(target, axis=1)
 Xo_test = test1.drop(target, axis=1)
 return_test = dados_origin[train.shape[0] :]
return_test = return_test.drop(target, axis=1)
 return Xo_train, Xo_test, yo_train, yo_test, return_test
def export(name_csv, predict, base_test):
d_test = base_test[["PassengerId"]]
d_test["Survived"] = predict
d_test.to_csv(name_csv + "_titanic.csv", index=False )
%matplotlib inline<string_transform>
drop_list = ["Name", "Fare", "Cabin", "Ticket"]
X_train, X_test, y_train, y_test, df_validate = trataDados(
 df_train_original, df_test_original, drop_list, "Survived"
)
tokenize = TweetTokenizer().tokenize<load_from_disk>
temp = []
classifier = [
"Decision Tree",
"Random Forest",
"KNN",
"Extra Trees",
"Ada Boost",
"Gradient Boosting",
]
models = [
DecisionTreeClassifier(
 criterion="gini",
max_depth=2,
max_features=None,
min_samples_leaf=1,
min_samples_split=2,
random_state=8,
splitter="random", 
),
RandomForestClassifier(
 min_samples_split=5,
min_samples_leaf=5,
criterion="entropy",
max_depth=1,
max_features="auto",
n_estimators=10,
random_state=10,
verbose=3, 
),
KNeighborsClassifier(
 algorithm="auto", leaf_size=30, n_neighbors=4, p=2, weights="uniform" 
),
ExtraTreesClassifier(
 criterion="gini",
max_depth=5,
max_features=None,
min_samples_leaf=1,
min_samples_split=2,
n_estimators=10,
random_state=5,
verbose=5, 
),
AdaBoostClassifier(algorithm="SAMME", n_estimators=10, random_state=1),
GradientBoostingClassifier(
 min_samples_split=5,
min_samples_leaf=5,
learning_rate=0.1,
loss="exponential",
max_depth=2,
max_features="sqrt",
n_estimators=20,
random_state=2,
verbose=1, 
),
]
print(os.listdir())
 train = pd.read_json('/kaggle/input/whats-cooking-kernels-only/train.json')
 test = pd.read_json('/kaggle/input/whats-cooking-kernels-only/test.json')
 print(train.head())
 ytrain = train['cuisine']
print(ytrain.head(5))
Id = test['id']
print(Id.head(5))<feature_engineering>
for index, model in enumerate(models):
model.fit(X_train, y_train)
 predict = model.predict(X_test)
 temp.append(metrics.accuracy_score(predict, y_test))
predict = model.predict(df_validate)
 export(classifier[index], predict, df_validate )
tfidf = TfidfVectorizer(binary=True )<data_type_conversions>
models_dataframe = pd.DataFrame(temp, index=classifier)
 models_dataframe.columns = ["Accuracy"]
print(models_dataframe)