0% found this document useful (0 votes)
7 views2 pages

NB

The document outlines a Python script that implements various machine learning models using the Iris dataset. It includes data preprocessing steps such as scaling and binning, followed by training and evaluating Gaussian Naive Bayes, Bernoulli Naive Bayes, and Multinomial Naive Bayes models. The accuracy of each model on the test set is reported, with Gaussian Naive Bayes achieving the highest accuracy at approximately 96.67%.

Uploaded by

kattaadithya057
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

NB

The document outlines a Python script that implements various machine learning models using the Iris dataset. It includes data preprocessing steps such as scaling and binning, followed by training and evaluating Gaussian Naive Bayes, Bernoulli Naive Bayes, and Multinomial Naive Bayes models. The accuracy of each model on the test set is reported, with Gaussian Naive Bayes achieving the highest accuracy at approximately 96.67%.

Uploaded by

kattaadithya057
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

In [1]: import numpy as np

import pandas as pd
import matplotlib.pyplot as ph
import sklearn.datasets as sk
from sklearn import datasets
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.svm import SVR
from sklearn.metrics import accuracy_score
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import BernoulliNB
from sklearn.naive_bayes import MultinomialNB

In [2]: from sklearn.datasets import load_iris

In [3]: from sklearn.model_selection import train_test_split

In [4]: iris=load_iris()

In [5]: data = pd.DataFrame(iris.data , columns=iris.feature_names)


data['target'] = iris.target

In [6]: X=iris.data
y=data['target']

In [7]: X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=42)

In [8]: print(f"the train data:{X_train}, the train target labels:{y_train}, test data: {X
127 2
26 0
128 2
131 2
145 2
108 2
143 2
45 0
30 0
Name: target, dtype: int64

In [9]: scaler = StandardScaler()


X_train = scaler.fit_transform(X_train)
X_test = scaler.fit_transform(X_test)

In [10]: model_1 = GaussianNB()


model_1.fit(X_train,y_train)
y_pred_1 = model_1.predict(X_test)
print( accuracy_score(y_test, y_pred_1)*100)

96.66666666666667

In [11]: model_2 = BernoulliNB()


model_2.fit(X_train,y_train)
y_pred_2 = model_2.predict(X_test)
print( accuracy_score(y_test, y_pred_2)*100)

86.66666666666667

In [16]: from sklearn.preprocessing import KBinsDiscretizer

binner = KBinsDiscretizer(n_bins=5, encode='ordinal', strategy='uniform')


X_train_binned = binner.fit_transform(X_train)
X_test_binned = binner.transform(X_test)

model_3 = MultinomialNB()
model_3.fit(X_train_binned, y_train)
y_pred_3 = model_3.predict(X_test_binned)

print("Accuracy:", accuracy_score(y_test, y_pred_3) * 100)

Accuracy: 76.66666666666667

In [ ]:

You might also like