<jupyter_start><jupyter_text>Titanic - Machine Learning from Disaster  Kaggle competition<jupyter_code>import pandas as pd
import numpy as np
url= "train.csv"
df=pd.read_csv(url)
df.head()
df.shape
url1="test.csv"
data=pd.read_csv(url1)
data.head()
data.shape
df.info()
df.describe()
df.isnull().sum()
df = df.replace(np.nan,0)
df.isnull().sum()
df.head()
# We can drop Name and Ticket as it is not showing any insight
df.drop(['Name','Ticket'],axis=1,inplace=True)
df.head()
#Drop "Cabin Number" and "Embarked"

df.drop(['Cabin','Embarked'],axis=1,inplace=True)
df.head()
df['Survived'].value_counts()
df['Pclass'].value_counts()<jupyter_output><empty_output><jupyter_text>pclass: A proxy for socio-economic status (SES)1st = Upper2nd = Middle3rd = Lower<jupyter_code>df['Sex'].value_counts()
df['SibSp'].value_counts()<jupyter_output><empty_output><jupyter_text>sibsp: The dataset defines family relations in this way...    Sibling = brother, sister, stepbrother, stepsisterSpouse = husband, wife (mistresses and fiancés were ignored)<jupyter_code>df['Parch'].value_counts()<jupyter_output><empty_output><jupyter_text>parch: The dataset defines family relations in this way...Parent = mother, fatherChild = daughter, son, stepdaughter, stepsonSome children travelled only with a nanny, therefore parch=0 for them.<jupyter_code>df['Age'].value_counts()
df['Age'] = df['Age'].astype('int64')
df.head()
# Data Binning on Age:
bins = [0,20,50,100]
category = ['low','medium','high']
df['Age'] = pd.cut(df['Age'],bins,labels= category)
df['Age'].value_counts()
df.Age
df.Age.isnull().sum()
df['Age'] = df['Age'].replace(np.nan,0)
df['Age'].replace({"low":"0","medium":"1","high":"2"},inplace=True)
df.Age.isnull().sum()
df['Age'] = df['Age'].replace(np.nan,0)
df['Age'] = df['Age'].replace(np.nan,0)
df['Age'] = df['Age'].astype('int64')
df.head()
df.Age.value_counts()
import seaborn as sns
sns.barplot(x=df.Age,y=df.Survived)
# Middle Age had survived more.
df['Sex'] = df['Sex'].replace({"male": 0, "female": 1})
df.head()
df['Fare'] = df['Fare'].astype('int64')
df.head()
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 8 columns):
 #   Column       Non-Null Count  Dtype
---  ------       --------------  -----
 0   PassengerId  891 non-null    int64
 1   Survived     891 non-null    int64
 2   Pclass       891 non-null    int64
 3   Sex          891 non-null    int64
 4   Age          891 non-null    int64
 5   SibSp        891 non-null    int64
 6   Parch        891 non-null    int64
 7   Fare         891 non-null    int64
dtypes: int64(8)
memory usage: 55.8 KB<jupyter_text>Plotting Histograms<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
df.hist(bins=50,figsize=(20,15))<jupyter_output><empty_output><jupyter_text>Check Correlations with 'Survived'<jupyter_code>corr_matrix = df.corr()
corr_matrix['Survived'].sort_values(ascending=False)<jupyter_output><empty_output><jupyter_text>Survival has high correlation with Sex<jupyter_code># Create a Barplot between SEX and SURVIVED
import seaborn as sns
sns.barplot(y=df.Survived,x=df.Sex)
# Mostly Women survived<jupyter_output><empty_output><jupyter_text>Train-Test Splitting<jupyter_code>#pip install --upgrade pandas
from sklearn.model_selection import train_test_split
train_set,test_set = train_test_split(df, test_size=0.3, random_state=42)
print(f"Rows in train set:{len(train_set)}\n Rows in test set:{len(test_set)}\n")
#equal distribution of class(0,1)-----------using Stratified Sampling
from sklearn.model_selection import StratifiedShuffleSplit
split= StratifiedShuffleSplit(n_splits=1,test_size=0.3,random_state=42)
for train_index,test_index in split.split(df,df['Survived']):
    strat_train_set= df.loc[train_index]
    strat_test_set= df.loc[test_index]
for train_index,test_index in split.split(df,df['Sex']):
    strat_train_set= df.loc[train_index]
    strat_test_set= df.loc[test_index]
strat_test_set
strat_train_set
strat_test_set['Survived'].value_counts()
strat_train_set['Survived'].value_counts()
strat_test_set['Sex'].value_counts()
strat_train_set['Sex'].value_counts()
df = strat_train_set.copy()<jupyter_output><empty_output><jupyter_text>Splitting Features and Labels<jupyter_code>df= strat_train_set.drop("Survived",axis=1)
df_labels = strat_train_set["Survived"].copy()
df.head()
df_labels.head()<jupyter_output><empty_output><jupyter_text>Selecting a desired Model for Project<jupyter_code>from sklearn.linear_model import LogisticRegression
model = LogisticRegression(tol = 0.1, solver = "lbfgs")<jupyter_output><empty_output><jupyter_text>Training the Logistic Regression Model with Training Data<jupyter_code>model.fit(df,df_labels)<jupyter_output>C:\Users\VishAish\anaconda3\lib\site-packages\sklearn\linear_model\_logistic.py:763: ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.

Increase the number of iterations (max_iter) or scale the data as shown in:
    https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
  n_iter_i = _check_optimize_result(<jupyter_text>Model Evaluation  Accuracy Scores<jupyter_code>from sklearn.metrics import accuracy_score
df_prediction = model.predict(df)
training_data_accuracy = accuracy_score(df_prediction,df_labels)
print('Accuracy on training data:',training_data_accuracy)
from sklearn.model_selection import cross_val_predict
df_labels_prediction = cross_val_predict(model,df,df_labels,cv=3)
df_labels_prediction<jupyter_output><empty_output><jupyter_text>Calculating confusion matrix<jupyter_code>from sklearn.metrics import confusion_matrix
confusion_matrix(df_labels,df_labels_prediction) #here true negative value=227339(not fraud) and true positive=265(fraud)
#also it wrongly detect that 129(fraud) and 112 is not fraud
#confusion matrix---tells us difference between actual and predicted values
#confusion_matrix(df_labels,df_labels)----------this is the C.M. for perfect predictions
# Precision and Recall
from sklearn.metrics import precision_score , recall_score
#This is my precision score
precision_score(df_labels , df_labels_prediction)
#This is my recall score
recall_score(df_labels , df_labels_prediction)
#F1 Score--------it is harmonic mean of Precision and Recall
from sklearn.metrics import f1_score
f1_score(df_labels , df_labels_prediction)
# check Classification Report
from sklearn.metrics import classification_report
print(classification_report(df_labels,df_prediction))<jupyter_output>precision    recall  f1-score   support

           0       0.82      0.89      0.85       394
           1       0.78      0.66      0.71       229

    accuracy                           0.81       623
   macro avg       0.80      0.77      0.78       623
weighted avg       0.80      0.81      0.80       623<jupyter_text>Testing the Model on Test dataset<jupyter_code>X_test = strat_test_set.drop("Survived",axis=1)
Y_test = strat_test_set["Survived"].copy()
X_test.head()
Y_test.head()<jupyter_output><empty_output><jupyter_text>Testing the Logistic Regression Model with Testing Data<jupyter_code>from sklearn.linear_model import LogisticRegression
model1 = LogisticRegression(tol = 0.1, solver = 'lbfgs')
model1.fit(X_test,Y_test)
from sklearn.metrics import accuracy_score
X_test_prediction = model1.predict(X_test)
testing_data_accuracy = accuracy_score(X_test_prediction,Y_test)
print('Accuracy on training data:',testing_data_accuracy)
from sklearn.model_selection import cross_val_predict
Y_test_prediction = cross_val_predict(model,X_test,Y_test,cv=3)
Y_test_prediction
# confusion matrix
from sklearn.metrics import confusion_matrix
confusion_matrix(Y_test,Y_test_prediction)
from sklearn.metrics import precision_score , recall_score
precision_score(Y_test,Y_test_prediction)
recall_score(Y_test,Y_test_prediction)
from sklearn.metrics import f1_score
f1_score(Y_test,Y_test_prediction)
#check classification report
from sklearn.metrics import classification_report
print(classification_report(df_labels,df_prediction))
X_test.head()
Y_test.head(10)
print(X_test.iloc[4])
b= model.predict(([[440,2,0,1,0,0,10]]))
if b==0:
    print("survived")
else:
    print("not-survived")<jupyter_output>survived<jupyter_text>Predicting Actual Test  Values<jupyter_code>data.head()
data = data.replace(np.nan,0)
data.isnull().sum()
# Drop unnecessary Columns
data.drop(['Name','Ticket'],axis=1,inplace=True)
data.drop(['Cabin','Embarked'],axis=1,inplace=True)
data['Age'] = data['Age'].astype('int64')
bins = [0,20,50,100]
category = ['low','medium','high']
data['Age'] = pd.cut(data['Age'],bins,labels= category)
data.Age
data['Age'] = data['Age'].replace(np.nan,0)
data['Age'].replace({"low":"0","medium":"1","high":"2"},inplace=True)
data.Age.isnull().sum()
data.replace(np.nan,0)
data['Age'] = data['Age'].replace(np.nan,0)
data['Age'].replace({"low":"0","medium":"1","high":"2"},inplace=True)
data.Age.isnull().sum()
data['Age'] = data['Age'].replace(np.nan,0)
data['Age'] = data['Age'].astype('int64')
data.Age.value_counts()
data['Fare'] = data['Fare'].astype('int64')
data['Sex'] = data['Sex'].replace({"male": 0, "female": 1})
data.info()
data.head()
model_test = LogisticRegression(tol = 0.1, solver = 'lbfgs')
model_test.fit(df, df_labels)
predictions = model.predict(data)
output = pd.DataFrame({'PassengerId': data.PassengerId, 'Survived': predictions})
output.to_csv('titanic_survival.csv', index=False)
print("Your submission was successfully saved!")
output<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13853799958583557, 'fraction_numerical': 0.025678194243114516, 'mean_word_length': 6.256949661908339, 'pattern_counts': {'":': 13, '<': 62, '<?xml version=': 0, '>': 62, 'https://': 2, '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': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '419845', 'license': 'Apache-2.0', 'n_tokens_mistral': 3621, 'n_tokens_neox': 3399, 'n_words': 658, 'path': 'Titanic - Machine Learning from Disaster.ipynb', 'repo_name': 'aishwaryaa-01/Titanic_Survival', 'size': '9658'}