<jupyter_start><jupyter_text>Table of content
 1) Data Loading:
 a)Install Libraries
b) Load data
 c) Concat train and test
d) Statsitical Summary<jupyter_code># Install Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
#load file from local system
from google.colab import files
uploaded = files.upload()
import io
train = pd.read_csv(io.BytesIO(uploaded['train.csv']),index_col=0)
from google.colab import files
uploaded = files.upload()

test = pd.read_csv(io.BytesIO(uploaded['test.csv']),index_col=0)
print("Train dataset shape -->",train.shape)
print("Test dataset shape --->",test.shape)<jupyter_output>Train dataset shape --> (891, 11)
Test dataset shape ---> (418, 10)<jupyter_text>concadinate train and test data so that it is easy to make transforations on data<jupyter_code>#concadinate train and test data
data = pd.concat([train,test],axis = 0).reset_index(drop = True)
print("Combined dataset shape-->",data.shape)
# Statistical Summary
data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 1309 entries, 0 to 1308
Data columns (total 11 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   Survived  891 non-null    float64
 1   Pclass    1309 non-null   int64  
 2   Name      1309 non-null   object 
 3   Sex       1309 non-null   object 
 4   Age       1046 non-null   float64
 5   SibSp     1309 non-null   int64  
 6   Parch     1309 non-null   int64  
 7   Ticket    1309 non-null   object 
 8   Fare      1308 non-null   float64
 9   Cabin     295 non-null    object 
 10  Embarked  1307 non-null   object 
dtypes: float64(3), int64(3), object(5)
memory usage: 112.6+ KB<jupyter_text>There are 5 Object type features are present so they need to be handled by feature engineering.<jupyter_code>data.describe(include='all')
data.columns<jupyter_output><empty_output><jupyter_text>2) Explotary Data Analysis (EDA)
a)EDA b)correlation with target value First let's see the distribution of survived in data by Pclass.
Here you can clearly see that the Pclass 3 has the higest number of passengers 491 which is 55.11% of total but the survival rate is only 24.24%. In Pclass 1 Total number of passengers are 216 bu the survival rate is 62.96% that shows lot of people traveled in the Pclass 1 is survived.<jupyter_code>fig = plt.figure(figsize = (10, 7))
ax = fig.add_subplot()

ax.set_ylim(0,1)

pclass_gp = train.groupby('Pclass')['Survived'].agg([np.mean , len]).reset_index()

Pclass_x = pclass_gp['Pclass'].values
height_y = pclass_gp['mean'].values
no_persons = pclass_gp['len'].values
total_percent = no_persons/sum(pclass_gp['len'])
colors = ["#6EC3EE", "#F79646", "#F08691"]

#plot the bar chart
ax.bar(x=Pclass_x , height  = height_y , color = colors)

#set the font style
fontdict = {'horizontalalignment': 'center', "fontsize":"15"}

for x , y , person , total_person in zip(Pclass_x,height_y,no_persons,total_percent):
    text = f"Total persons: {person}\n ({round(total_person*100, 2)}% of total)"
    ax.text(x, y + .05, s = text, fontdict = fontdict)
    
    text = f"Survival rate\n {round(y*100,2)}%"
    ax.text(x,y/2,s=text , fontdict = fontdict)
#set the ticks 
ax.set_xticks([1, 2, 3])
ax.set_yticks([])

#set the labels
ax.set_xlabel("Pclass type", size = 20)
ax.set_ylabel("Survival rate", size = 20)

# remove the spines
ax.spines["top"].set_color("None")
ax.spines["left"].set_color("None")
ax.spines["right"].set_color("None")

ax.set_title("Survive rate by each class", size = 24)
fig.tight_layout()
# distribution of the sex
sns.factorplot(x="Sex", y ="Survived", data=data, kind="bar", size=3)
plt.show()
data.groupby('Sex')['Survived'].sum()<jupyter_output><empty_output><jupyter_text>The C Embarked has the high survival rate  it has totally 270 passengers from those people 93 people are survived so it has he high 55.35% survival rate. 'S' Embarked has the low 33.69% survival rate it has the large passenger list of 914 and from that only 217 where survived.<jupyter_code># distribution of the Embarked
sns.factorplot(x="Embarked", y ="Survived", data=data, kind="bar", size=3)
plt.show()
data.groupby('Embarked')['Survived'].agg([np.mean,len,sum])
# distribution of the SibSp
sns.factorplot(x="SibSp", y ="Survived", data=data, kind="bar", size=3)
plt.show()
data.groupby('SibSp')['Survived'].agg([np.mean,len,sum])
# distribution of the Parch 
sns.factorplot(x="Parch", y ="Survived", data=data, kind="bar", size=3)
plt.show()
data.groupby('Parch')['Survived'].agg([np.mean,len,sum])
sex_Pclass = data.groupby(['Pclass','Sex'])['Survived'].mean()
sex_Pclass.unstack().plot(kind = 'bar', xlabel='Pclass',ylabel = 'Survival rate' ,rot=10)
sex_Pclass.unstack()
sex_Pclass = data.groupby(['Pclass','Sex'])['Survived'].count()
sex_Pclass.unstack().plot(kind = 'bar', xlabel='Pclass',ylabel = 'Survival rate' ,rot=10)
sex_Pclass.unstack()
fig = plt.figure(figsize = (11,11))
ax = fig.add_subplot()

ax.set_ylim(0,1)

sex_Pclass = data.groupby(['Pclass','Sex'])['Survived'].agg([np.mean,len]).reset_index()
sex_Pclass['PclassSex'] = sex_Pclass.apply(lambda row : str.title(row["Sex"]) + " " + str(row["Pclass"]), axis = 1)

Pclass_x = sex_Pclass['PclassSex'].values
height_y = sex_Pclass['mean'].values
no_persons = sex_Pclass['len'].values
total_percent = no_persons/sum(sex_Pclass['len'])
colors = ["#6EC3EE", "#F79646", "#F08691"]

#plot the bar chart
ax.bar(x=Pclass_x , height  = height_y , color = colors)

#set the font style
fontdict = {'horizontalalignment': 'center', "fontsize":"15"}

for x , y , person , total_person in zip(Pclass_x,height_y,no_persons,total_percent):
    text = f"Total persons: {person}\n ({round(total_person*100, 2)}% of total)"
    ax.text(x, y + .05, s = text, fontdict = fontdict)
    
    text = f"Survival rate\n {round(y*100,2)}%"
    ax.text(x,y/2,s=text , fontdict = fontdict)
#set the ticks 

ax.set_yticks([])

#set the labels
ax.set_xlabel("Pclass type", size = 20)
ax.set_ylabel("Survival rate", size = 20)

# remove the spines
ax.spines["top"].set_color("None")
ax.spines["left"].set_color("None")
ax.spines["right"].set_color("None")

ax.set_title("Survive rate by each class", size = 24)
fig.tight_layout()
data['Agelimit']=pd.cut(data["Age"].T,bins=8,labels=['<10','<20','<30','<40','<50','<60','<70','<80'])
age_sur = data.groupby(['Sex','Agelimit'])['Survived'].mean().reset_index()
sns.barplot(x='Agelimit',y='Survived',hue='Sex',data=age_sur)
#CorrelTION BETWEEN THE FEATURES
data['Sex'].replace(['male','female'],[1,0],inplace=True)
data['Embarked'].replace(['S','C','Q'],[0,1,2],inplace=True)
data.corr()['Survived']<jupyter_output><empty_output><jupyter_text>Here you can see the Fare highly positive corrilation with 'Survived'  and the Sex has high Negative correlation with 'Survived'.<jupyter_code>sns.heatmap(data[["Age","Sex","SibSp","Parch","Pclass","Fare","Embarked", "Survived"]].corr(), annot = True)<jupyter_output><empty_output><jupyter_text>Missing Value Imputation
a) Find the missing value b) fill the missing value The dataset contain missing values in Survived,Age,Cabin,Fare,Embaked but the survived column having the missing values in the test dataset so no need to fill that value. Fare and Embarked having so small amoun of missing values and Age and Cabin need to fill with oppropriate  values.<jupyter_code>data.isnull().sum()
sns.heatmap(data.isnull())
# Fill the missing values
data.loc[data['Fare'].isnull(),:]
data['Fare']=data['Fare'].fillna(np.mean(data[data['Pclass']==3]['Fare']))
data[data['Fare'].isnull()]
#handke the Embarked column
data.loc[data['Embarked'].isnull(),:]

data['Embarked']=data['Embarked'].fillna(np.mean(data[((data['Pclass']==1) & (data['Sex'] == 0)) ]['Embarked']))
data[data['Embarked'].isnull()]
#handle the Age column
#there are lots of values missing in age so gonna fill that value with the average value of Pclass
sns_box=sns.boxplot(x='Pclass',y='Age',data = data)

median = data.groupby('Pclass')['Age'].median()
median<jupyter_output><empty_output><jupyter_text>The median age for each Pclass is 39,29,24 . So change the Age of the missing values to the median value of the Pclass.<jupyter_code>age_null_index = data[data['Age'].isnull()].index

age_null_index = np.array(age_null_index)

for index in age_null_index:
  data.loc[index,'Age'] = median[data.loc[index,'Pclass']]


data[data['Age'].isnull()]<jupyter_output><empty_output><jupyter_text>Now all the null values are cleared<jupyter_code>sns.heatmap(data.isnull())<jupyter_output><empty_output><jupyter_text>Feature Engineering
a)New values b) Drop values c) Dealing with catogarical values d)correlation<jupyter_code># New value Family Size
data['FamilySize'] = data['SibSp'] + data['Parch'] + 1
data.head() 
# New Value -- CabbinCode
data['CabinCode'] = data.Cabin.str[0]
data['CabinCode'].unique()
CabinCode_null_index = data[data['CabinCode'].isnull()].index
CabinCode_null_index =np.array(CabinCode_null_index)
data.loc[CabinCode_null_index,'CabinCode']='O'
data['CabinCode'].unique()

cabin_sur = data.groupby('CabinCode')['Survived'].mean()
cabin_sur.plot(kind='bar')
data
#drop the features
data = data.drop(['Name','Ticket','Cabin','Fare','Age','SibSp','Parch'],axis=1)

data.head(5)
# Handle with Catogarical Data
data = pd.get_dummies(data,columns=["Pclass"])
data = pd.get_dummies(data,columns=["Embarked"])
data = pd.get_dummies(data,columns=["FamilySize"])
data = pd.get_dummies(data,columns=["Agelimit"])
data = pd.get_dummies(data,columns=["CabinCode"])

data.head()<jupyter_output><empty_output><jupyter_text>Modeling
a)prepare data for modeling b)classification models c)ensemble learning<jupyter_code># split it to train and test
train_data = data.iloc[0:len(train) , :]
predict_data = data.iloc[len(train):,:]
predict_data = predict_data.drop('Survived',axis = 1)
print("shape of the train dataset-->",train_data.shape)
print("Shape of the test dataset--->" , predict_data.shape)
predict_data.head()
y = train_data['Survived'].copy()
X = train_data.drop('Survived',axis= 1)

print("shape of the train dataset-->",X.shape)
print("Shape of the train target dataset--->" , y.shape)

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.25,random_state = 42)
print("shape of the train dataset-->",X_train.shape)
print("Shape of the train target dataset--->" , y_train.shape)
print("shape of the test dataset-->",X_test.shape)
print("Shape of the test target dataset--->" , y_test.shape)<jupyter_output>shape of the train dataset--> (668, 34)
Shape of the train target dataset---> (668,)
shape of the test dataset--> (223, 34)
Shape of the test target dataset---> (223,)<jupyter_text>Classification Models
1) Linear Classification 
2) Logistric Regression 
3) KNN 
4) Decision Tree Classifier 
5) Random Forest Classification 
6) Support Vector Machine (SVM)<jupyter_code>#Install libraries
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC

# helper function
def train_predict(algoritm,X_train,y_train,X_test,y_test,cv):
  print("Training Started!!!")
  algoritm.fit(X_train,y_train)
  print("Predicting........................")
  y_pred = algoritm.predict(X_test)

  accu_score = accuracy_score(y_test , y_pred)
  cross_score =cross_val_score(algoritm ,X_train,y_train , cv=cv )

  print("Train and Prediction completed!!!")
  print("accuracy Score -->",accu_score)
  print("Cross Validation Score -->",cross_score)<jupyter_output><empty_output><jupyter_text>Logistrc Regression<jupyter_code>line = LogisticRegression()
train_predict(line ,X_train,y_train,X_test,y_test,5 )<jupyter_output>Training Started!!!
Predicting........................
Train and Prediction completed!!!
accuracy Score --> 0.7937219730941704
Cross Validation Score --> [0.80597015 0.84328358 0.76865672 0.76691729 0.84962406]<jupyter_text>KNN<jupyter_code>knn = KNeighborsClassifier()
train_predict(knn ,X_train,y_train,X_test,y_test,5 )<jupyter_output>Training Started!!!
Predicting........................
Train and Prediction completed!!!
accuracy Score --> 0.7802690582959642
Cross Validation Score --> [0.75373134 0.81343284 0.70895522 0.72932331 0.82706767]<jupyter_text>DecisionTreeClassifier<jupyter_code>tree = DecisionTreeClassifier()
train_predict(tree ,X_train,y_train,X_test,y_test,5 )<jupyter_output>Training Started!!!
Predicting........................
Train and Prediction completed!!!
accuracy Score --> 0.7937219730941704
Cross Validation Score --> [0.79104478 0.82089552 0.76865672 0.78195489 0.86466165]<jupyter_text>Random Forest Classifier<jupyter_code>rfc = RandomForestClassifier()
train_predict(rfc ,X_train,y_train,X_test,y_test,5 )<jupyter_output>Training Started!!!
Predicting........................
Train and Prediction completed!!!
accuracy Score --> 0.7937219730941704
Cross Validation Score --> [0.76119403 0.84328358 0.76865672 0.79699248 0.84962406]<jupyter_text>Support Vecrot Machine<jupyter_code>svc = SVC()
train_predict(svc ,X_train,y_train,X_test,y_test,5 )<jupyter_output>Training Started!!!
Predicting........................
Train and Prediction completed!!!
accuracy Score --> 0.8116591928251121
Cross Validation Score --> [0.82089552 0.85820896 0.78358209 0.78195489 0.84962406]<jupyter_text>Ensemple Modeling
 Voting Classification<jupyter_code>from sklearn.ensemble import VotingClassifier
estimators = [('li',line) , ('knn_cls', knn) , ('dec_tree' , tree) , ('ran_forest' , rfc) , ('SVC', svc)]

model = VotingClassifier(estimators = estimators , voting = 'hard' , n_jobs= -1)
#fit the model
model.fit(X_train , y_train)
# finding the prediction score
accu_score_ens = model.score(X_test , y_test)

print("Accuracy Score for the Voting Ensemple Learning--->",accu_score_ens)<jupyter_output>Accuracy Score for the Voting Ensemple Learning---> 0.8116591928251121<jupyter_text>Result 

we are taking the Voting Classification model.<jupyter_code>test_result = pd.Series(model.predict(predict_data), name = "Survived").astype(int)
index = np.array([x for x in range(892,1310)])
index = pd.Series(index,name='PassengerId').astype(int)
results = pd.concat([index, test_result],axis = 1)
results.to_csv("titanic_submission.csv", index = False)
results.head(4)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1542278412180533, 'fraction_numerical': 0.0432300163132137, 'mean_word_length': 5.317303563761271, 'pattern_counts': {'":': 2, '<': 77, '<?xml version=': 0, '>': 100, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '542425', 'license': 'MIT', 'n_tokens_mistral': 5739, 'n_tokens_neox': 5246, 'n_words': 1200, 'path': 'Titanic_Kaggle/Titanic_Kaggle.ipynb', 'repo_name': 'pavi-ninjaac/Kaggle', 'size': '14712'}