# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
df_train = pd.read_csv("../input/titanic/train.csv")
df_test = pd.read_csv("../input/titanic/test.csv")
df_train.head()
df_train.shape

df_test.shape
df_test.head()
df_train.info()
train_df = df_train[["PassengerId","Survived","Pclass","Sex","Age","SibSp","Parch","Ticket","Fare","Cabin","Embarked"]]
test_df = df_test[["PassengerId","Pclass","Sex","Age","SibSp","Parch","Ticket","Fare","Cabin","Embarked"]]
train_df.isnull().sum()
train_df.drop("Cabin", inplace = True, axis = 1)
test_df.drop("Cabin", inplace = True, axis = 1)
train_df
train_df.drop("Ticket", inplace = True, axis = 1)
test_df.drop("Ticket", inplace = True, axis = 1)
train_df
train_df.groupby("Embarked").size()
def function(x):
    if (x["Embarked"] == "C"):
        return int(1)
    elif(x["Embarked"] == "Q"):
        return int(2)
    elif(x["Embarked"] == "S"):
        return int(3)
train_df1 = train_df.apply(function, axis = 1)
test_df1 = test_df.apply(function,axis = 1)
train_df["Embarked"] = train_df1
test_df["Embarked"] = test_df1
train_df.head()
train_df["Age"] = train_df["Age"].fillna(train_df["Age"].mean())
test_df["Age"] = test_df["Age"].fillna(test_df["Age"].mean())
train_df["Age"] = train_df["Age"].astype(int)
train_df.shape
test_df.head()
test_df.shape
test_df.isnull().sum()
train_df["Embarked"] = train_df["Embarked"].fillna(train_df["Embarked"].mean())
test_df["Fare"] = test_df["Fare"].fillna(test_df["Fare"].mean())
train_df.info()
test_df.info()
train_df.describe()
def function(x):
    if (x["Sex"] == "male"):
        return int(0)
    elif(x["Sex"] == "female"):
        return int(1)
   
train_df2 = train_df.apply(function, axis = 1)
test_df2 = test_df.apply(function,axis = 1)
train_df["Sex"] = train_df2
test_df["Sex"] = test_df2
train_df.head()
test_df.head()
x = train_df[["PassengerId","Pclass","Sex","Age","SibSp","Parch","Fare","Embarked"]]
y = train_df["Survived"]
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import r2_score
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.20, random_state = 0)
x_train.info()
def get_mae(max_leaf_nodes, x_train, x_test, y_train, y_test):
    model = DecisionTreeRegressor(max_leaf_nodes= max_leaf_nodes , random_state=0)
    model.fit(x_train, y_train,)
    preds_val = model.predict(x_test)
    mae = mean_absolute_error(y_test, preds_val)
    r_square = (round(r2_score(y_test,preds_val)*100, 2))
    return mae, r_square

for max_leaf_nodes in [5, 50, 500, 5000]:
    my_mae, r_square = get_mae(max_leaf_nodes, x_train, x_test, y_train, y_test)
    print("Max leaf nodes: %d  \t\t Mean Absolute Error:  %d \t\t R-squared:  %d" %(max_leaf_nodes, my_mae, r_square))
model = DecisionTreeRegressor(max_leaf_nodes= 5 , random_state=0)
model.fit(x_train, y_train,)
preds_val = model.predict(x_test)
mae = mean_absolute_error(y_test, preds_val)
print("mae = ",mae, "accuracy score = " , model.score(x_test,y_test)*100)
linear = LinearRegression()
linear.fit(x_train,y_train)
predict = linear.predict(x_test)
mae = mean_absolute_error(y_test, predict)
r_squared = (round(r2_score(y_test, predict)*100, 2))
print("mean squared error: ", mae, "r_square: ", r_squared )
print("accuracy score = ", linear.score(x_test, y_test)*100)
logistic = LogisticRegression(max_iter = 10000)
logistic.fit(x_train,y_train)
predicted = logistic.predict(x_test)
mae = mean_absolute_error(y_test, predicted)
r_squared = (round(r2_score(y_test, predicted)*100, 2))
print("mean squared error: ", mae, "r_square: ", r_squared )
print("accuracy score = ", logistic.score(x_test, y_test)*100)
forest_model = RandomForestRegressor(random_state=16)
forest_model.fit(x_train, y_train)
preds = forest_model.predict(x_test)
mae = mean_absolute_error(y_test, preds)
r_squared = (round(r2_score(y_test, preds)*100, 2))
print("mean squared error: ", mae, "r_square: ", r_squared )
print("accuracy score = ", forest_model.score(x_test, y_test)*100)

model2= GaussianNB(var_smoothing=1e-08)
model2.fit(x_train, y_train)
Y_pred= model2.predict(x_test)
model2.score(x_test, y_test)
"""
Training the Model (Since logistic regression has the highest accuracy. thus we are going to fit logistic regression model)
"""
logistic_model = LogisticRegression(max_iter = 10000)
logistic_model.fit(x,y)
predicted_values = logistic_model.predict(test_df)
final_data = {'PassengerId': test_df.PassengerId, 'Survived': predicted_values}
final_submission = pd.DataFrame(data=final_data)
final_submission.to_csv('submission_file.csv',index =False)
