<jupyter_start><jupyter_text>Survival Prediction  Importing all Library's<jupyter_code>import pandas as pd
import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier
data=pd.read_csv('titanic_data.csv')
data.head()<jupyter_output><empty_output><jupyter_text>Viewing the Data<jupyter_code>testing_data=pd.read_csv('test.csv')
testing_data.head()
dff=data.copy()
data.head(2)<jupyter_output><empty_output><jupyter_text>Dropping Non required columns<jupyter_code>data.drop(['Ticket','Cabin','Name'],axis=1,inplace=True)
testing_data.drop(['Ticket','Cabin','Name'],axis=1,inplace=True)
testing_data.head()<jupyter_output><empty_output><jupyter_text>Plotting countplot<jupyter_code>font = {
    'weight' : 'bold',
    'size'   : 22
}
plt.figure(figsize=(12,9))
plt.title("Survival On Basis of Number of sibling", fontdict=font)
plt.legend(loc='best')
sns.countplot(x='SibSp',hue='Survived',data=data)
plt.figure(figsize=(12,9))
plt.title("Survival On Basis of Number of parent", fontdict=font)
plt.legend(loc='best')
sns.countplot(x='Parch',hue='Survived',data=data)
data.head(2)
testing_data.head(2)<jupyter_output><empty_output><jupyter_text>data Wrangling<jupyter_code>data.Age=data.Age.fillna(data.Age.mean())
data.isnull().any()
testing_data.Age=testing_data.Age.fillna(data.Age.mean())
testing_data.Fare=testing_data.Fare.fillna(data.Fare.mean())

testing_data.isnull().any()
data.head(2)
testing_data.head(2)<jupyter_output><empty_output><jupyter_text>Creating dummies( Done to convert categorical to continous )<jupyter_code>embark_d=pd.get_dummies(data.Embarked)
embark_t=pd.get_dummies(testing_data.Embarked)
sex_d=pd.get_dummies(data.Sex)
sex_t=pd.get_dummies(testing_data.Sex)
embark_t.head()

data=pd.concat([data,embark_d,sex_d],axis=1)
data.head()
testing_data=pd.concat([testing_data,embark_t,sex_t],axis=1)
testing_data.head()
data.drop(['Embarked','Sex'],axis=1,inplace=True)
testing_data.drop(['Embarked','Sex'],axis=1,inplace=True)

data.head(2)
testing_data.head(2)
data.shape[0]
X,Y=data.drop(['Survived'],axis=1),data['Survived']<jupyter_output><empty_output><jupyter_text>Creating Testing And Training Data set<jupyter_code>x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.20, random_state=42)<jupyter_output><empty_output><jupyter_text>Creating 2 Models one Of KNN and Second Of Decision Tree<jupyter_code>model=KNeighborsClassifier(n_neighbors=6)
model2=DecisionTreeClassifier()
model.fit(x_train,y_train)
model2.fit(x_train,y_train)
prediction=model.predict(x_test)
prediction2=model2.predict(x_test)<jupyter_output><empty_output><jupyter_text>Creating Confusion matrix<jupyter_code>from sklearn.metrics import confusion_matrix
kconfusion=confusion_matrix(prediction,y_test)
dconfusion=confusion_matrix(prediction2,y_test)
kconfusion
dconfusion<jupyter_output><empty_output><jupyter_text>Checking the accuracy score<jupyter_code>from sklearn.metrics import accuracy_score
score=round(accuracy_score(prediction,y_test)*100,2)
score
score1=round(accuracy_score(prediction2,y_test)*100,2)
score1
sub1=model2.predict(testing_data)
submission_file1 = pd.DataFrame({'PassengerId': testing_data['PassengerId'], 'Survived': sub1})
submission_file1.head()
submission_file1.to_csv('DecisionSubmission.csv')<jupyter_output><empty_output><jupyter_text>Another Approach<jupyter_code>df=pd.read_csv('titanic_data.csv')
df.head()
df['Family']=df['SibSp']+df['Parch']
df.drop(['SibSp','Parch','Cabin'],axis=1,inplace=True)
df.head()
df=pd.concat([df,embark_d,sex_d],axis=1)
df.head()
df.drop(['Embarked','Sex'],axis=1,inplace=True)
df.head(2)
df.Age=df.Age.fillna(df.Age.mean())
testing_data.Fare=testing_data.Fare.fillna(data.Fare.mean())

df.head(2)<jupyter_output><empty_output><jupyter_text>Not Considering Name And Ticket<jupyter_code>df.drop(['Name','Ticket'],axis=1,inplace=True)
df.head(2)
testing_data.head(2)
testing_data['Family']=testing_data['SibSp']+testing_data['Parch']
testing_data.head(2)
df.head()
X,Y=df.drop(['Survived'],axis=1),df['Survived']

x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.20, random_state=42)


model=KNeighborsClassifier(n_neighbors=6)

model.fit(x_train,y_train)

testing_data.drop(['SibSp','Parch'],axis=1,inplace=True)
new_prediction=model.predict(testing_data)

submission_file1 = pd.DataFrame({'PassengerId': testing_data['PassengerId'], 'Survived': new_prediction})
submission_file1.head()

submission_file1.to_csv('KnnSubmission.csv')
score=round(accuracy_score(prediction,y_test)*100,2)
score
accuracy=data[(data.female==1)]
accuracy.shape[0]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1536189069423929, 'fraction_numerical': 0.01645916860097067, 'mean_word_length': 12.73913043478261, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '477158', 'license': 'MIT', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1871, 'n_words': 201, 'path': '24. MachineLearning_Projects/3. Titanic-Survival-Prediction/Titanic-Survival-Prediction-using-KNN and Decision Tree.ipynb', 'repo_name': 'sgeek28/Data-Science', 'size': '4739'}