<jupyter_start><jupyter_text>Project - Titanic Dataset<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
titanic_data = pd.read_csv('train.csv')
len(titanic_data)
# Taking a glimpse of the Data

titanic_data.head()
titanic_data.index
titanic_data.columns
titanic_data.info()
titanic_data.dtypes
titanic_data.describe()<jupyter_output><empty_output><jupyter_text>Data Analysis  importing seaborn for visualizing the data  how many survived vs died using seaborn<jupyter_code># count plot of survived vs not survived

sns.countplot(x='Survived',data = titanic_data,hue = 'Sex')
titanic_data.isna().sum()
sns.heatmap(titanic_data.isna())
# % of null values in Age

titanic_data['Age'].isna().sum()/len(titanic_data['Age'])*100
# % of null values in Cabin

titanic_data['Cabin'].isna().sum()/len(titanic_data['Cabin'])*100
sns.displot(x='Age',data=titanic_data)<jupyter_output><empty_output><jupyter_text>Data Cleaning<jupyter_code># filling missing values of Age
age_mean = titanic_data['Age'].mean()

titanic_data['Age'].fillna(age_mean,inplace = True)
# Verifying

titanic_data['Age'].isna().sum()
# visualizing the null values now

sns.heatmap(titanic_data.isna())<jupyter_output><empty_output><jupyter_text>As Cabin is of no use in our prediction we'll drop it<jupyter_code>titanic_data.drop('Cabin',axis = 1, inplace = True)
titanic_data.head()<jupyter_output><empty_output><jupyter_text>Now we will drop all the non numerical values and then see how our prediction is performing<jupyter_code># checking all non numerical values

titanic_data.info()
titanic_data.dtypes
## Female = 0 
## Male = 1
def gender(string):
    if string == 'female':
        return 0
    else:
        return 1
titanic_data['Gender'] = titanic_data.Sex.apply(gender)
titanic_data.head()<jupyter_output><empty_output><jupyter_text>Dropping Name, Sex, Ticket, Embarked as they'll not be helping us in predicting the survival<jupyter_code>titanic_data.drop(['Sex','Name','Embarked','Ticket'], axis =1, inplace = True)
titanic_data.head()
# Seperating dependent and independent variables

x = titanic_data[['Pclass','Age','SibSp','Parch','Fare','Gender']]
y = titanic_data['Survived']<jupyter_output><empty_output><jupyter_text>Data Modelling Building Model Using Logistic Regression<jupyter_code>from sklearn import model_selection
x_train, x_test, y_train, y_test = model_selection.train_test_split(x,y, test_size = 0.33, random_state = 42)
# import logistic regression
from sklearn.linear_model import LogisticRegression
# fit logistic regression
alg = LogisticRegression()
alg.fit(x_train,y_train)
# Predicting 
predict = alg.predict(x_test)<jupyter_output><empty_output><jupyter_text>Testingmodel performance<jupyter_code>from sklearn.metrics import confusion_matrix
pd.DataFrame(confusion_matrix(y_test,predict), columns = ['Predicted No', 'Predicted Yes'], index = ['True No','True Yes'])
# finding the accuracy by importing classification report
from sklearn.metrics import classification_report
print(classification_report(y_test,predict))<jupyter_output>precision    recall  f1-score   support

           0       0.79      0.83      0.81       124
           1       0.77      0.71      0.74        97

    accuracy                           0.78       221
   macro avg       0.78      0.77      0.77       221
weighted avg       0.78      0.78      0.78       221<jupyter_text>Now importing the test file to do predict the survival of passengers<jupyter_code>titanic_data2 = pd.read_csv('test.csv')
test_data = titanic_data2.copy()
titanic_data2.head()<jupyter_output><empty_output><jupyter_text>using the analysis that we already did on the training file and then applying that on the testing file<jupyter_code>titanic_data2.isna().sum()
sns.heatmap(titanic_data2.isna())
## Data cleaning
age_mean = titanic_data2['Age'].mean()

titanic_data2['Age'].fillna(age_mean,inplace = True)
titanic_data2.drop('Cabin',axis = 1, inplace = True)
titanic_data2.head()
def gender(string):
    if string == 'female':
        return 0
    else:
        return 1
titanic_data2['Gender'] = titanic_data2.Sex.apply(gender)
titanic_data2.head()
titanic_data2.drop(['Sex','Name','Embarked','Ticket'], axis =1, inplace = True)
titanic_data2.head()
X = titanic_data2[['Pclass','Age','SibSp','Parch','Fare','Gender']]<jupyter_output><empty_output><jupyter_text>Data Modelling on testing data<jupyter_code>from sklearn.linear_model import LogisticRegression
predict_for_test = alg.predict(X)
pd.DataFrame(predict_for_test, columns = ['Survived'])
def integervals(zeroone):
    if zeroone == 0:
        return 'No'
    else:
        return 'Yes'
titanic_data2['Survived'] = predict_for_test
titanic_data2['Survived(yes or no)'] = titanic_data2.Survived.apply(integervals)
titanic_data2.head()
test_data['Survived'] = titanic_data2['Survived(yes or no)']
test_data.head()<jupyter_output><empty_output><jupyter_text>Bar Graph for Predicted Survival w.r.t gender<jupyter_code>sns.countplot(x='Survived',data = test_data ,hue = 'Sex')<jupyter_output><empty_output><jupyter_text>Saving DataFrame to_csv file format<jupyter_code>test_data.to_csv('Test_Prediction.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11753445635528331, 'fraction_numerical': 0.01875957120980092, 'mean_word_length': 5.8569553805774275, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '81140', 'license': 'MIT', 'n_tokens_mistral': 1946, 'n_tokens_neox': 1845, 'n_words': 375, 'path': 'Predicting_Survival/Predicting_Survival_Of_Titanic_Passengers.ipynb', 'repo_name': 'amanchamola/ML-Titanic-dataset', 'size': '5224'}