<jupyter_start><jupyter_text>TITANIC   Importing the library<jupyter_code>import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Collecting the data of Titanic<jupyter_code>data=pd.read_csv('C:/Users/sachi/Downloads/titanic_train.csv')
data.shape
data.head()<jupyter_output><empty_output><jupyter_text>Analyzing the data<jupyter_code>print(data.groupby('Survived').size())
sns.countplot(x='Survived',data=data)
sns.countplot(x='Survived',hue='Sex',data=data)
sns.countplot(x='Survived',hue='Pclass',data=data)
sns.countplot(x='Survived',hue='Embarked',data=data)
sns.boxplot(x='Pclass',y='Age', data=data)
plt.rcParams['figure.figsize']=(16,12)
data.hist();<jupyter_output><empty_output><jupyter_text>Data Wrangling<jupyter_code>data.isnull().sum()
data.drop('Cabin',axis=1,inplace=True)
data.head()
not_zeros=['Age']
for column in not_zeros:
    data[column]=data[column].replace(0,np.NAN)
    mean=int(data[column].mean(skipna=True))
    data[column]=data[column].replace(np.NAN,mean)
data.isnull().sum()
data.fillna('S',inplace=True)
data.isnull().sum()
sex=pd.get_dummies(data['Sex'],drop_first=True)
pclass=pd.get_dummies(data['Pclass'],drop_first=True)
embarked=pd.get_dummies(data['Embarked'],drop_first=True)
data=pd.concat([data,sex,pclass,embarked],axis=1)
data.head()
data.drop(['PassengerId','Pclass','Name','Sex','Ticket','Embarked'],axis=1,inplace=True)
data.head()<jupyter_output><empty_output><jupyter_text>Train and Test<jupyter_code>X=data.drop(['Survived'],axis=1)
y=data['Survived']
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(X,y,test_size=0.30)<jupyter_output><empty_output><jupyter_text>1)logistic Regression<jupyter_code>from sklearn.linear_model import LogisticRegression

model=LogisticRegression()
model.fit(x_train,y_train)
y_pred=model.predict(x_test)
model.score(x_test,y_test)
from sklearn.metrics import accuracy_score,classification_report,confusion_matrix
print('accuracy_score:',accuracy_score(y_pred,y_test))
from sklearn.metrics import classification_report,accuracy_score,confusion_matrix
print('classification_report:',classification_report(y_pred,y_test))
print('confusion_matrix:',confusion_matrix(y_pred,y_test))
print('accuracy_score:',accuracy_score(y_pred,y_test)*100)<jupyter_output>classification_report:              precision    recall  f1-score   support

          0       0.88      0.83      0.85       181
          1       0.68      0.77      0.72        87

avg / total       0.82      0.81      0.81       268

confusion_matrix: [[150  31]
 [ 20  67]]
accuracy_score: 80.97014925373134<jupyter_text>2) KNN<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
training_accuracy=[]
testing_accuracy=[]
neighbors_setting=range(1,11)
for n_neighbors in neighbors_setting:
    knn=KNeighborsClassifier(n_neighbors=n_neighbors)
    knn.fit(x_train,y_train)
    training_accuracy.append(knn.score(x_train,y_train))
    testing_accuracy.append(knn.score(x_test,y_test))
    
plt.plot(neighbors_setting,training_accuracy,label='training')
plt.plot(neighbors_setting,testing_accuracy,label='testing')
plt.xlabel('neighbors_setting')
plt.ylabel('accuarcy')
plt.legend()
plt.show()

knn=KNeighborsClassifier(n_neighbors=11)
knn.fit(x_train,y_train)
print('training:{:}'.format(knn.score(x_train,y_train)))
print("testing:{:}".format(knn.score(x_test,y_test)))<jupyter_output>training:0.7303370786516854
testing:0.7014925373134329<jupyter_text>3) SVM<jupyter_code>from sklearn.svm import SVC
model=SVC(kernel='linear')
model.fit(x_train,y_train)
y_pred=model.predict(x_test)
model.score(x_test,y_test)
from sklearn.metrics import classification_report,accuracy_score,confusion_matrix
print('classification_report:',classification_report(y_pred,y_test))
print('confusion_matrix:',confusion_matrix(y_pred,y_test))
print('accuracy_score:',accuracy_score(y_pred,y_test))<jupyter_output>classification_report:              precision    recall  f1-score   support

          0       0.84      0.82      0.83       173
          1       0.68      0.71      0.69        95

avg / total       0.78      0.78      0.78       268

confusion_matrix: [[142  31]
 [ 28  67]]
accuracy_score: 0.7798507462686567<jupyter_text>4) Naive Baye<jupyter_code>from sklearn.naive_bayes import GaussianNB
model=GaussianNB()
model.fit(x_train,y_train)
y_pred=model.predict(x_test)
model.score(x_test,y_test)
from sklearn.metrics import classification_report,accuracy_score,confusion_matrix
print('classification_report:',classification_report(y_pred,y_test))
print('confusion_matrix:',confusion_matrix(y_pred,y_test))
print('accuracy_score:',accuracy_score(y_pred,y_test))<jupyter_output>classification_report:              precision    recall  f1-score   support

          0       0.85      0.84      0.84       172
          1       0.71      0.73      0.72        96

avg / total       0.80      0.80      0.80       268

confusion_matrix: [[144  28]
 [ 26  70]]
accuracy_score: 0.7985074626865671<jupyter_text>5)RandomForest<jupyter_code>from sklearn.ensemble import RandomForestClassifier
model=RandomForestClassifier(n_estimators=100)
model.fit(x_train,y_train)
y_pred=model.predict(x_test)
model.score(x_test,y_test)
from sklearn.metrics import classification_report,accuracy_score,confusion_matrix
print('classification_report:',classification_report(y_pred,y_test))
print('confusion_matrix:',confusion_matrix(y_pred,y_test))
print('accuracy_score:',accuracy_score(y_pred,y_test)*100)<jupyter_output>classification_report:              precision    recall  f1-score   support

          0       0.91      0.87      0.89       179
          1       0.76      0.83      0.79        89

avg / total       0.86      0.85      0.86       268

confusion_matrix: [[155  24]
 [ 15  74]]
accuracy_score: 85.44776119402985
{'fraction_non_alphanumeric': 0.12533875338753386, 'fraction_numerical': 0.05403116531165312, 'mean_word_length': 5.6422947131608545, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '436002', 'license': 'MIT', 'n_tokens_mistral': 2397, 'n_tokens_neox': 2197, 'n_words': 265, 'path': 'titanic  for all classification 2.ipynb', 'repo_name': 'ManasaCM/Titanic-problem', 'size': '5904'}