# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3.8.5 64-bit
#     name: python_defaultSpec_1598403118222
# ---

# + tags=[]
import pandas as pd
import numpy as np
import os
import sweetviz as sv
working_dir = os.getcwd()
file_dir = os.path.join(working_dir , "Data")
train_file = os.path.join(file_dir , "train.csv")
test_file = os.path.join(file_dir , "test.csv")




# + tags=[]
train = pd.read_csv(train_file)
train.head()
result = sv.analyze([train, "Train"],target_feat='Survived')
result.show_html('Report.html')
# -

test = pd.read_csv(test_file)
test.head()

# + tags=[]
print(train.isnull().sum())
print('percentage of missing values')
print(train.isnull().sum() / len(train) * 100)
# -

col = ['Cabin']
train = train.drop(col , axis = 1)
train.head()

#removing Ticket cause not related
col = ["Ticket"]
train = train.drop(col , axis= 1)
train.head()


# +
# converting categorical values to numeric ones

dummies = []

cols = ["Sex", "Embarked","Pclass"]
for col in cols:
    dummies.append(pd.get_dummies(train[col]))
dummy_df = pd.concat(dummies , axis= 1)
dummy_df.head()
# -

train = pd.concat((train , dummy_df) , axis=1)
train = train.drop(cols , axis=1)
train.head()

train.isna().sum()

train["Age"] = train["Age"].interpolate()


# creating family size
train['Family_size'] = train['SibSp'] + train["Parch"] + 1


# + tags=[]

# train['Title'] = train['Name'].str.split(", " , expand = True)[1].str.split(".",expand = True)[0]
train['FarePerPerson'] = train["Fare"] / train["Family_size"]
train.drop(["Name"] , axis = 1 , inplace = True)
train.head()

    

# +
# train["Title"].value_counts()
# from sklearn.preprocessing import LabelEncoder
# label = LabelEncoder()

# train['Title'] = label.fit_transform(list(train['Title'].values))
train.head()
# -

train.describe()

y = train['Survived']
X = train.drop(['Survived' , "PassengerId" , "Fare"] , axis=1)
X.head()

# +
from sklearn.model_selection import train_test_split

X_train , X_test , y_train , y_test = train_test_split(X,y,test_size = 0.3,random_state = 0)

# + tags=[]
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score

model = DecisionTreeClassifier(max_depth = 6)
model.fit(X_train , y_train)
cv = cross_val_score(model , X_train , y_train , cv = 7)
print(cv)
y_pred = model.predict(X_test)
accuracy_score(y_test,y_pred)
# -

from sklearn import ensemble
classifier = ensemble.RandomForestClassifier(n_estimators=130)
classifier.fit(X_train , y_train)
classifier.score(X_test , y_test)


classifier = ensemble.GradientBoostingClassifier(n_estimators=50)
classifier.fit(X_train , y_train)
classifier.score(X_test, y_test)

# pre_processing test data
cols = ["Ticket" , "Cabin"]
test = test.drop(cols , axis=1)
dummies = []
cols = ["Sex" , "Embarked" , "Pclass"]
for column in cols:
    dummies.append(pd.get_dummies(test[column]))
dummy_df = pd.concat(dummies,axis=1)
test = pd.concat((test , dummy_df) , axis=1)
test = test.drop(cols , axis = 1)
test.head()

test['Age'] = test['Age'].interpolate()
test['Fare'] = test['Fare'].fillna(test['Fare'].mean())
test.head()

test["Family_size"] = test["SibSp"] + test["Parch"] + 1
# test["Title"] = test["Name"].str.split(", ",expand =  True)[1].str.split(".",expand = True)[0]
test["FarePerPerson"] = test["Fare"] / test["Family_size"]
test.head()

# +
cols = ["Name" , "Fare"]
test.drop(cols , axis=1 ,inplace=True)
# test['Title'] = label.fit_transform(list(test['Title'].values))
test.head()


# -

X_res = test.drop(["PassengerId"] , axis = 1)
X_res.head()


# + tags=[]
y_pred = classifier.predict(X_res)
# -

output_df = pd.DataFrame(test["PassengerId"])
output_df["Survived"] = y_pred
output_df.head()

# + tags=[]
# output_df.to_csv('results.csv' , index = False)
parameter_list = [15.0, 2, 2, 1, 0, 1, 0, 0, 1, 0, 0, 5, 2.0]

array = np.array([parameter_list])
prediction = classifier.predict(array)
print(prediction)
# -

from joblib import dump , load
dump(classifier  , "titanic.joblib")



StarcoderdataJupyterScriptsDedupFiltered