Practical 6
Practical 6
Ans 1: INPUT:
# Import required libraries
import numpy as np
import pandas as pd
iris = load_iris()
y = dataset.iloc[:, 4].values
# Splitting the dataset into the Training set and Test set
# Feature Scaling
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)
classifier = GaussianNB()
classifier.fit(x_train, y_train)
y_pred = classifier.predict(x_test)
# Accuracy
y_pred_train = classifier.predict(x_train)
# Confusion matrix
cm = confusion_matrix(y_test, y_pred)
# Classification report
print(classification_report(y_test, y_pred))
# Plotting
# Test set
ax[0].set_xlim(X1.min(), X1.max())
ax[0].set_ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
ax[0].legend()
# Training set
ax[1].set_xlim(X1.min(), X1.max())
ax[1].set_ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
ax[1].legend()
plt.show()
OUTPUT:
Gaussian Naive Bayes model accuracy(in %): 90.0
Confusion matrix
[[65 3]
[ 7 25]]
True Positives(TP) = 65
True Negatives(TN) = 25
False Positives(FP) = 3
False Negatives(FN) = 7
d:\sem 7\fai\practical\pf2\untitled0.py:60: UserWarning: *c* argument looks like a single numeric RGB or
RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches
with *x* & *y*. Please use the *color* keyword-argument or provide a 2D array with a single row if you
intend to specify the same RGB or RGBA value for all points.
d:\sem 7\fai\practical\pf2\untitled0.py:76: UserWarning: *c* argument looks like a single numeric RGB or
RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches
with *x* & *y*. Please use the *color* keyword-argument or provide a 2D array with a single row if you
intend to specify the same RGB or RGBA value for all points.
Practical : 7
ANS 1: INPUT
# Import required libraries
import numpy as np
import pandas as pd
iris = load_iris()
y = dataset.iloc[:, 4].values
# Splitting the dataset into the Training set and Test set
# Feature Scaling
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)
classifier = GaussianNB()
classifier.fit(x_train, y_train)
# Accuracy
y_pred_train = classifier.predict(x_train)
# Confusion matrix
cm = confusion_matrix(y_test, y_pred)
# Classification report
print(classification_report(y_test, y_pred))
# Plotting
# Test set
ax[0].set_xlim(X1.min(), X1.max())
ax[0].set_ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
ax[0].legend()
# Training set
ax[1].set_xlim(X1.min(), X1.max())
ax[1].set_ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
ax[1].legend()
plt.show()
OUTPUT:
Gaussian Naive Bayes model accuracy(in %): 76.31578947368422
Confusion matrix
[[13 0 0]
[ 0 12 4]
[ 0 5 4]]
True Positives(TP) = 13
True Negatives(TN) = 12
False Positives(FP) = 0
False Negatives(FN) = 0
accuracy 0.76 38