You are on page 1of 2

In

[1]:

import warnings
warnings.filterwarnings("ignore")

In [2]:

#Import Libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

Importing the Data

In [3]:

df = pd.read_csv("default_2k.csv")

Check Head

In [4]:

df.head()

Out[4]:

default student balance income

0 No No 412.071615 48347.296982

1 No Yes 579.721305 18555.752586

2 No No 694.398583 36570.425441

3 No No 0.000000 41933.095770

4 No Yes 407.340440 25376.728632

Shape

In [5]:

df.shape

Out[5]:

(1994, 4)

Splitting and Encoding the training data

In [6]:
x= pd.get_dummies(df.drop("default",axis=1),drop_first=True)
y = df['default']

In [7]:

# Split X and y into training and test set in 70:30 ratio


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, random_state=1)

Question-8 What is the accuracy/Model Score of the model(Naive Bayes) on the Train set?

In [8]:

from sklearn.naive_bayes import GaussianNB # using Gaussian algorithm from Naive Bayes

# creatw the model


model = GaussianNB()

model.fit(x_train, y_train)

Out[8]:

GaussianNB()
This study source was downloaded by 100000834959320 from CourseHero.com on 02-27-2022 01:37:52 GMT -06:00

https://www.coursehero.com/file/105014895/Week-1-Graded-Quiz-on-Solutionpdf/
In [9]:

train_predict = model.predict(x_train)

from sklearn import metrics

print("Model Accuracy: {}".format(metrics.accuracy_score(y_train, train_predict)))

Model Accuracy: 0.9698924731182795

Or

In [10]:
model.score(x_train, y_train)

Out[10]:

0.9698924731182795

Question-9 What is the accuracy/Model Score of the model(Naive Bayes) on the Test set?

In [11]:

test_predict = model.predict(x_test)

from sklearn import metrics

print("Model Accuracy: {}".format(metrics.accuracy_score(y_test, test_predict)))

Model Accuracy: 0.9749582637729549

Or

In [12]:

model.score(x_test, y_test)

Out[12]:

0.9749582637729549

Question-10 What is the recall for target classes-No and Yes for the test data?

In [13]:

print("Classification Report")
print(metrics.classification_report(y_test, test_predict))

Classification Report
precision recall f1-score support

No 0.97 1.00 0.99 582


Yes 1.00 0.12 0.21 17

accuracy 0.97 599


macro avg 0.99 0.56 0.60 599
weighted avg 0.98 0.97 0.97 599

This study source was downloaded by 100000834959320 from CourseHero.com on 02-27-2022 01:37:52 GMT -06:00

https://www.coursehero.com/file/105014895/Week-1-Graded-Quiz-on-Solutionpdf/
Powered by TCPDF (www.tcpdf.org)

You might also like