To Construct a Bayes Classifier to classiy iris dataset using Python.
Input:
- X: the training data, where each row represents a sample and each column represents a feature.
- y: the target labels for the training data.
- X_test: the testing data, where each row represents a sample and each column represents a feature.
Output:
- y_pred: the predicted labels for the testing data.
a. init method to initialize the Gaussian Naive Bayes classifier from scikit-learn. b. fit method to fit the classifier to the training data using the Gaussian Naive Bayes algorithm from scikit-learn. c. predict method to make predictions on the testing data using the fitted classifier from scikit-learn.
3. Split the data into training and testing sets using the train_test_split function from scikit-learn.
Developed by: Jeeva Abishake A
Register Number: 212221240018
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score
class BayesClassifier:
def __init__(self):
self.clf = GaussianNB()
def fit(self, X, y):
self.clf.fit(X, y)
def predict(self, X):
return self.clf.predict(X)
ir = load_iris()
X_train, X_test, y_train, y_test = train_test_split(ir.data, ir.target,test_size=0.3, random_state = 42)
clf = BayesClassifier()
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
accu = accuracy_score(y_test, y_pred)
print("Accuracy:",accu*100)
Hence, Bayes classifier for iris dataset is implemented successfully