Professional Documents
Culture Documents
In [1]:
# Naive Bayes Classification
In [2]:
# Importing the dataset
dataset = pd.read_csv('iris.csv')
In [3]:
#looking at the first 5 values of the dataset
dataset.head()
In [4]:
%matplotlib inline
img=mpimg.imread('iris_types.jpg')
plt.figure(figsize=(20,40))
plt.axis('off')
plt.imshow(img)
In [5]:
#Spliting the dataset in independent and dependent variables
X = dataset.iloc[:,:4].values
y = dataset['species'].values
1 of 3 18-04-2022, 12:08 pm
Naiye Bayes classification application http://localhost:8888/nbconvert/html/Assignment%206/Naiye%20Baye...
In [6]:
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20, random_state
In [7]:
# Feature Scaling to bring the variable in a single scale
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
In [8]:
# Fitting Naive Bayes Classification to the Training set with linear kernel
from sklearn.naive_bayes import GaussianNB
nvclassifier = GaussianNB()
nvclassifier.fit(X_train, y_train)
Out[8]: GaussianNB()
In [9]:
# Predicting the Test set results
y_pred = nvclassifier.predict(X_test)
print(y_pred)
In [13]:
#lets see the actual and predicted value side by side
y_compare = np.vstack((y_test,y_pred)).T
#actual value on the left side and predicted value on the right hand side
#printing the top 5 values
y_compare
2 of 3 18-04-2022, 12:08 pm
Naiye Bayes classification application http://localhost:8888/nbconvert/html/Assignment%206/Naiye%20Baye...
['setosa', 'setosa'],
['virginica', 'virginica'],
['setosa', 'setosa'],
['virginica', 'virginica'],
['virginica', 'virginica'],
['versicolor', 'versicolor'],
['virginica', 'virginica'],
['setosa', 'setosa'],
['virginica', 'virginica'],
['versicolor', 'versicolor']], dtype=object)
In [11]:
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)
[[11 0 0]
[ 0 8 1]
[ 0 1 9]]
In [12]:
#finding accuracy from the confusion matrix.
a = cm.shape
corrPred = 0
falsePred = 0
Correct predictions: 28
False predictions 2
3 of 3 18-04-2022, 12:08 pm