<jupyter_start><jupyter_text>* TITANIC DATASET ON KAGGLE<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
train = pd.read_csv('titanic_train.csv')
train.head()
#To find out which rows have missing data(NaN)
train.isnull()
sns.heatmap(train.isnull(),yticklabels = False,cbar = False,cmap = 'viridis')
#Nearly 20% data is missing for age,so analysis can be done to fill it up. But most of cabin is not filled,so we may
#have to drop the entire column
sns.countplot(x = 'Survived',hue = 'Sex',data = train)
sns.countplot(x = 'Survived',hue = 'Pclass',data = train)
sns.distplot(train['Age'].dropna(),kde = False,bins = 30) #dropna removes problems caused by nullvalues
#Another method
train['Age'].plot.hist(bins = 30)
sns.countplot(x = 'SibSp',data = train)
train['Fare'].plot.hist(bins = 30,figsize = (10,4))
#To use iplot
import cufflinks as cf
cf.go_offline()
train['Fare'].iplot(kind = 'hist',bins = 30)
#CLEANING THE DATA - Filling up the age column(imputation)
plt.figure(figsize = (10,7))
sns.boxplot(x = 'Pclass',y = 'Age', data = train)
def impute_age(cols):
    Age = cols[0]
    Pclass = cols[1]
    
    if pd.isnull(Age):
        if Pclass == 1:
            return 37
        elif Pclass == 2:
            return 29
        else:
            return 24
    
    else:
        return Age        
train['Age'] = train[['Age','Pclass']].apply(impute_age,axis = 1)
sns.heatmap(train.isnull(),yticklabels=False,cbar = False,cmap = 'viridis') #Age has been filled
train.drop('Cabin',axis = 1,inplace = True) #Delete cabin column
sns.heatmap(train.isnull(),yticklabels=False,cbar = False,cmap = 'viridis') #Age has been filled
train.head()
#To remove ALL missing values
train.dropna(inplace = True)
sns.heatmap(train.isnull(),yticklabels=False,cbar = False,cmap = 'viridis') #No missing values
train.head()
#We now create a dummy variable column for both sex and embarked strings, and convert them into 0 and 1 so that the
#ML algo can understand them(it doesn't recognise 'male' and 'female')
#Embarked has C,Q,S as values
pd.get_dummies(train['Sex']) #Convert categorical variable into dummy/indicator variables.
#We only need 1 of these columns.If female = 1, male = 0 and vice versa.This double column problem is called
#MULTI-COLLINEARITY
sex = pd.get_dummies(train['Sex'],drop_first=True)
sex.head()
embark = pd.get_dummies(train['Embarked'],drop_first=True)
embark.head()
train = pd.concat([train,sex,embark],axis=1)
train.head()
#We don't need Sex,Embarked(redundant);Name,Ticket(Can't use it for anything)
train.drop(['Sex','Embarked','Name','Ticket'],axis=1,inplace=True)
train.head() #All data is numerical, and hence perfect for a Machine Learning algorithm
train.drop('PassengerId',axis = 1,inplace=True) #PassengerId is of no use to algo
train.head()
#We will try Pclass with 1,2,3 values, and then try dummy implementation on it later
from sklearn.model_selection import train_test_split
X = train.drop('Survived',axis = 1)
y = train['Survived']
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)
from sklearn.linear_model import LogisticRegression
logmodel = LogisticRegression()
logmodel.fit(X_train,y_train)
predictions = logmodel.predict(X_test)
from sklearn.metrics import classification_report
print(classification_report(y_test,predictions))
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test,predictions)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12446351931330472, 'fraction_numerical': 0.012875536480686695, 'mean_word_length': 5.333333333333333, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '85600', 'license': 'MIT', 'n_tokens_mistral': 1261, 'n_tokens_neox': 1185, 'n_words': 340, 'path': 'Machine Learning Projects/CNN Cats and Titanic Dataset/Titanic Notebook.ipynb', 'repo_name': 'samlaubscher/HacktoberFest2020-Contributions', 'size': '3495'}