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

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import random
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import StandardScaler

df = pd.read_csv("train.csv",encoding="utf-8")

df.shape

df.head()

df.describe()

df.info()

df.isnull().sum()

# # Imputing Missing Values

# ## HomePlanet 

df.HomePlanet.unique()

sns.countplot(x ='HomePlanet', data = df)
plt.show()

df.HomePlanet.ffill(axis=0,inplace=True)

# ## Destination

df.Destination.unique()

sns.countplot(x ='Destination', data = df)
plt.show()

df.Destination.ffill(axis=0,inplace=True)

# ## CryoSleep

sns.countplot(x ='CryoSleep', data = df)
plt.show()

## Replacing Null values with random value (True or False)
for i,n in enumerate(df.CryoSleep):
    if(n!=True and n!=False):
        val = random.choice([True,False])
        df.loc[i,"CryoSleep"] = val

df.head()

# ## We can find if the people belong to same family by grouping them by their last name. But here we are dropping the name column

df.drop('Name', axis=1, inplace=True)

# ## Cabin

Deck = [cab.split("/")[0] if type(cab)!= type(2.33) else np.nan for cab in df.Cabin]
Num = [cab.split("/")[1] if type(cab)!= type(2.33) else np.nan for cab in df.Cabin]
Side = [cab.split("/")[2] if type(cab)!= type(2.33) else np.nan for cab in df.Cabin]

df["Deck"] = Deck
df["Num"] = Num
df["Side"] = Side

df.drop('Cabin', axis=1, inplace=True)

df.Deck.unique()

sns.countplot(x ='Deck', data = df)
plt.show()

df.Deck.ffill(axis=0,inplace=True)

# ## From the analysis we found that the range of seating number is from 0 to 1894. So we are checking and creating a list of numbers that are not assigned to anyone.
#
# ## We can say, these are the missing values(nan) so we can randomly assign values from the list

num_ls = list(range(0,1895))
for i in df.Num.unique():
    
    if(type(i) == type(2.23)):
        continue
        
    if int(i) in num_ls:
        num_ls.remove(int(i))

## Replacing Null values with random value (from num_ls)
for i,n in enumerate(df.Num):
    if(type(n) == type(2.23)):
        val = random.choice(num_ls)
        df.loc[i,"Num"] = val

df.Side.unique()

sns.countplot(x ='Side', data = df)
plt.show()

## Replacing Null values with random value (True or False)
for i,n in enumerate(df.Side):
    if(n!=True and n!=False):
        val = random.choice(['P','S'])
        df.loc[i,"Side"] = val

# ## Analysing RoomService, FoodCourt, ShoppingMall, Spa, VRDeck
#
# ## We are imputing all the data with median value for the above columns as all the histogram looks similar. We are taking the median value"

sns.histplot(data = df,x = 'RoomService',bins=7)
plt.show()

df['RoomService'] = df['RoomService'].fillna(df['RoomService'].median())
df['FoodCourt'] = df['FoodCourt'].fillna(df['FoodCourt'].median())
df['ShoppingMall'] = df['ShoppingMall'].fillna(df['ShoppingMall'].median())
df['Spa'] = df['Spa'].fillna(df['Spa'].median())
df['VRDeck'] = df['VRDeck'].fillna(df['VRDeck'].median())



sns.countplot(x ='VIP', data = df)
plt.show()

df.VIP.value_counts()

df["Extra"] = df.RoomService + df.FoodCourt + df.ShoppingMall + df.Spa + df.VRDeck

df.head()

df.describe()

sns.histplot(data = df,x = 'Extra',bins=25)
plt.show()

df_vip = df[df.VIP==True]
df_vip.describe()

sns.histplot(data = df_vip,x = 'Extra')
plt.show()

df_general = df[df.VIP==False]
df_general.describe()

sns.histplot(data = df_general,x = 'Extra',bins=25)
plt.show()

## Replacing Null values
for i,n in enumerate(df.VIP):
    if(type(n) == type(2.23)):
        if(n >= 5000):
            df.loc[i,"VIP"] = True
        else:
            df.loc[i,"VIP"] = False

Group_no = [int(idd.split("_")[1]) for idd in df.PassengerId]

df["Group_no"] = Group_no

# ## Imputing the age column with most occuring value

df['Age'] = df['Age'].groupby(df['Group_no']).apply(lambda x: x.fillna(x.mean()))

df.drop('PassengerId', axis=1, inplace=True)
df.drop('Extra', axis=1, inplace=True)

df.isnull().sum()

# ## Converting categorical variables to numeric

df.CryoSleep = df.CryoSleep.map({True: 1, False: 0})
df.VIP = df.VIP.map({True: 1, False: 0})
df.Transported = df.Transported.map({True: 1, False: 0})

df.head()

df.HomePlanet.unique()

enc = OneHotEncoder(drop='first')
arrival = enc.fit_transform(df.HomePlanet.values.reshape(-1, 1)).toarray()
arrival = pd.DataFrame(arrival,columns=['Earth','Mars'])

df = pd.concat([df,arrival],axis=1)

df.drop('HomePlanet', axis=1, inplace=True)

df.Destination.unique()

# +
enc2 = OneHotEncoder(drop='first')
destination = enc2.fit_transform(df.Destination.values.reshape(-1, 1)).toarray()
destination = pd.DataFrame(destination,columns=['D2','D3'])

df = pd.concat([df,destination],axis=1)

df.drop('Destination', axis=1, inplace=True)
# -

df.Side.unique()

# +
enc3 = OneHotEncoder(drop='first')
side = enc3.fit_transform(df.Side.values.reshape(-1, 1)).toarray()
side = pd.DataFrame(side,columns=['S'])

df = pd.concat([df,side],axis=1)

df.drop('Side', axis=1, inplace=True)
# -

df.head()

df.Deck.unique()

sns.countplot(x ='Deck', data = df)
plt.show()

## Applying Mean encoding
mean_encoded = df.groupby("Deck")["Transported"].mean()
df["Deck"] = df["Deck"].map(dict(mean_encoded))

df.head()

df.Group_no.unique()

sns.countplot(x ='Group_no', data = df)
plt.show()

## Applying Mean encoding
mean_encoded = df.groupby("Group_no")["Transported"].mean()
df["Group_no"] = df["Group_no"].map(dict(mean_encoded))

df.head()

# ## Scaling the data - StandardScalar

scaler = StandardScaler()
df[['Age','RoomService','FoodCourt','ShoppingMall','Spa','VRDeck']] = scaler.fit_transform(df[['Age','RoomService','FoodCourt','ShoppingMall','Spa','VRDeck']])

df.head()

# ## Modelling Classification Model

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score

X = df.loc[:, df.columns != 'Transported']
y = df.Transported

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25,random_state=223, stratify=y)

sns.countplot(x ='Transported', data = df)
plt.show()

model = LogisticRegression()
model.fit(X_train, y_train)

model.score(X_train, y_train)

model.score(X_test, y_test)

## Predicting
predict = model.predict(X_test)
accuracy_score(y_test, predict)

confusion_matrix(y_test, predict)/len(y_test)



StarcoderdataJupyterScriptsDedupFiltered