|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +# In[16]: |
| 5 | + |
| 6 | + |
| 7 | +#importing required packages |
| 8 | +import pandas as pd |
| 9 | +from sklearn import preprocessing |
| 10 | +from sklearn.model_selection import train_test_split |
| 11 | +from sklearn.naive_bayes import GaussianNB |
| 12 | +from sklearn.metrics import accuracy_score |
| 13 | +from sklearn.metrics import confusion_matrix |
| 14 | +from sklearn.naive_bayes import BernoulliNB |
| 15 | +clf=BernoulliNB() |
| 16 | + |
| 17 | +#loading the data |
| 18 | +dataset=pd.read_csv("C:/Users/ASUS/Downloads/train.csv") |
| 19 | +dataset.head() |
| 20 | + |
| 21 | +#getting the description of the dataset |
| 22 | +dataset.describe() |
| 23 | +dataset.describe().sum() |
| 24 | + |
| 25 | +#get some info about the data |
| 26 | +dataset.info() |
| 27 | + |
| 28 | +#getting the amount of null values in each column |
| 29 | +dataset.isnull().sum() |
| 30 | + |
| 31 | +#dropping the unimportant columns |
| 32 | + |
| 33 | +dataset=dataset.drop('PassengerId', axis=1) |
| 34 | + |
| 35 | +dataset=dataset.drop('Name', axis=1) |
| 36 | + |
| 37 | +dataset=dataset.drop('Ticket', axis=1) |
| 38 | + |
| 39 | +dataset=dataset.drop('Cabin', axis=1) |
| 40 | + |
| 41 | +dataset.head() |
| 42 | + |
| 43 | +#label encoding the categorical values which are of object type |
| 44 | +le=preprocessing.LabelEncoder() |
| 45 | +dataset['Sex']=le.fit_transform(dataset['Sex']) |
| 46 | +dataset['Embarked']=le.fit_transform(dataset['Embarked']) |
| 47 | +dataset.head() |
| 48 | + |
| 49 | +"""this functions takes the independent variable column and trains the model after dividing the dataset into x and y |
| 50 | +and also spliting the dataset into training and testing data.This also prints the accuracy score and the confusion matrix""" |
| 51 | + |
| 52 | +def navbaiyes(value): |
| 53 | + x=dataset.drop([value], axis=1) |
| 54 | + y=dataset[value] |
| 55 | + x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0) |
| 56 | + |
| 57 | + |
| 58 | + y_pred= clf.fit(x_train,y_train).predict(x_test) |
| 59 | + print("The accuracy score is:") |
| 60 | + print(accuracy_score(y_test, y_pred)*100) |
| 61 | + print('--------------------------------------------') |
| 62 | + print("The confusion matrix is:") |
| 63 | + print(confusion_matrix(y_test,y_pred)) |
| 64 | + |
| 65 | +#Calling the function |
| 66 | +navbaiyes('Survived') |
| 67 | + |
| 68 | + |
| 69 | +# In[ ]: |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
0 commit comments