# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="hTcFv398beRg"
# #STEP 1 and 2: Setup & Get the Data#

# + id="eMVo6BkK-Z3y"
import sys
import sklearn
import numpy as np
import os
# %matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt

# + colab={"base_uri": "https://localhost:8080/"} id="QgzMyp_6Tyi3" outputId="1bcf197b-6243-4fdf-ae5e-5b767ca820c0"
#importing the necessary modules
import pandas as pd
from zipfile import ZipFile
from urllib.request import urlopen
import io

#loading the dataset
file_path = 'https://raw.githubusercontent.com/aiforsec/RIT-DSCI-633-FDS/main/Assignments/titanic.zip'
f = urlopen(file_path).read()
zip_file = ZipFile(io.BytesIO(f))

#Reading the test and train dataframes
test_df = pd.read_csv(zip_file.open('test.csv'))
train_df = pd.read_csv(zip_file.open('train.csv'))

#Print the first 5 rows of the dataframe
print(train_df.head())
print()
print(test_df.head())
print()

#Print the number of rows and columns of dataframe
print("Shape of train dataframe: " , train_df.shape)
print()
print("Shape of test dataframe: " , test_df.shape)

# + [markdown] id="TEQKMhdycEGK"
# #STEP 3: Data Manipulation and Analysis using Pandas framework#

# + colab={"base_uri": "https://localhost:8080/"} id="y-vvjTODUpn-" outputId="c7c0e7d6-b4de-4bcd-ee05-6f2cbc674e82"
#Print the summary of the DataFrame
print(train_df.info())
print()
print(test_df.info())

# + colab={"base_uri": "https://localhost:8080/"} id="tWd1KUZtYd9J" outputId="be381e06-4bc6-4306-d977-df6c56fd064d"
#Print the individual description for both the columns of the dataframe
print(train_df.isnull().sum())
print()
print(test_df.isnull().sum())

# + [markdown] id="W3aevlCcmnDt"
# ##As you can see above, there are 177 NULL values in the Age column, 687 in the Cabin colum and 2 values in the Embarked. 

# + colab={"base_uri": "https://localhost:8080/"} id="o8fVIY3Yl7Tt" outputId="33457447-a6dc-469d-b129-cec751c8d3d8"
print(train_df.columns.values, test_df.columns.values)

# + colab={"base_uri": "https://localhost:8080/"} id="bKk5rTqomPh0" outputId="f183833c-79c7-4e0f-9e1b-701da6bf4803"
print(train_df.describe())
print(test_df.describe())

# + colab={"base_uri": "https://localhost:8080/"} id="8SV1OQZum8CV" outputId="44288930-3a65-4468-a5b9-a43b76b72a5d"
#total = train_df.shape[0] + test_df.shape[0]
#print(train_df.shape[0]/ total)
#print(test_df.shape[0]/ total)

print(train_df.Pclass.value_counts())

print()

print(train_df.groupby("Pclass").Survived.value_counts())

# + [markdown] id="bOWXr_FopM1K"
# #STEP 4: DATA VISUALISATION

# + colab={"base_uri": "https://localhost:8080/", "height": 276} id="CLw4-QjRnfEM" outputId="1e13e017-9ca9-4cab-b038-b1052e82fb15"
train_df.groupby('Pclass').Survived.mean().plot(kind='bar')
plt.ylabel('Survived')
plt.xlabel('Pclass')
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 265} id="npBLmqbZnyYA" outputId="3f66e8e4-ea1c-46fe-b789-48f4af2aa0ca"
train_df_3rd_class = train_df[train_df['Pclass'] == 3]
plt.hist(train_df_3rd_class['Age'])
plt.show()

# + id="ECIrvIWAn70W"
train_test_df = [train_df, test_df]

# + [markdown] id="meqdSbfuokof"
# # STEP 5: Prepare the data for Machine Learning Model

# + id="lbl-xBTHoUNz"
for dataset in train_test_df:
    dataset['Sex'] = dataset['Sex'].map( {'female': 1, 'male': 0} ).astype(int)

# + id="JzaGqycVot-Z"
for tr in train_test_df:
  tr['Embarked'] = tr['Embarked'].fillna('S') #filling missing embarked value with 'S'
  tr['Embarked'] = tr['Embarked'].map( {'S': 0, 'C': 1, 'Q': 2} ).astype(int)

# + colab={"base_uri": "https://localhost:8080/"} id="7A2_4qahoxhT" outputId="306456cd-1c49-4460-f9f3-69ccae2c2841"
for tr in train_test_df:
  age_avg = tr['Age'].mean()
  age_std = tr['Age'].std()
  age_null = tr['Age'].isnull().sum()
  age_null_random_list = np.random.randint(age_avg - age_std, age_avg + age_std, size=age_null)
  tr['Age'][np.isnan(tr['Age'])] = age_null_random_list
  tr['Age'] = tr['Age'].astype(int)
train_df['AgeBand'] = pd.cut(train_df['Age'], 5)
for dataset in train_test_df:
    dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
    dataset.loc[(dataset['Age'] > 16) & (dataset['Age'] <= 32), 'Age'] = 1
    dataset.loc[(dataset['Age'] > 32) & (dataset['Age'] <= 48), 'Age'] = 2
    dataset.loc[(dataset['Age'] > 48) & (dataset['Age'] <= 64), 'Age'] = 3
    dataset.loc[ dataset['Age'] > 64, 'Age'] = 4

# + id="Aii6Z6lDo1gZ"
for dataset in train_test_df:
    dataset['Fare'] = dataset['Fare'].fillna(train_df['Fare'].median())
train_df['FareBand'] = pd.qcut(train_df['Fare'], 4) 
for dataset in train_test_df:
    dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
    dataset.loc[(dataset['Fare'] > 7.91) & (dataset['Fare'] <= 14.454), 'Fare'] = 1
    dataset.loc[(dataset['Fare'] > 14.454) & (dataset['Fare'] <= 31), 'Fare']   = 2
    dataset.loc[ dataset['Fare'] > 31, 'Fare'] = 3
    dataset['Fare'] = dataset['Fare'].astype(int)

# + colab={"base_uri": "https://localhost:8080/"} id="MuMNM9wlphzV" outputId="ccffd2ae-fbb4-47be-efca-35803edb9a50"
features_drop = ['Name', 'SibSp', 'Parch', 'Ticket', 'Cabin']
train_df = train_df.drop(features_drop, axis=1)
print(train_df.head())
test_df = test_df.drop(features_drop, axis=1)
train_df = train_df.drop(['PassengerId', 'AgeBand', 'FareBand'], axis=1)

# + [markdown] id="k7ayEaDZpqtc"
# #STEP 6 & 7: Select and Train a Model and Evaluating Accuracy
#

# + colab={"base_uri": "https://localhost:8080/"} id="FmoZtKuRpwoB" outputId="a0c83200-c775-4109-8f89-dd288ed8c102"
X_train = train_df.drop('Survived', axis=1)
y_train = train_df['Survived']
X_test = test_df.drop("PassengerId", axis=1).copy()

X_train.shape, y_train.shape, X_test.shape

# + colab={"base_uri": "https://localhost:8080/"} id="KbtIh5MFp4Sm" outputId="95b5329d-c78f-4fa7-8102-22f00e047d4b"
from sklearn.linear_model import LogisticRegression

clf = LogisticRegression()
clf.fit(X_train, y_train)
y_pred_log_reg = clf.predict(X_test)
acc_log_reg = round( clf.score(X_train, y_train) * 100, 2)
print (str(acc_log_reg) + '%')

StarcoderdataJupyterScriptsDedupFiltered