0% found this document useful (0 votes)
18 views1 page

Naïve Bayes Algorithm in Python Code

The document provides Python code to implement the K-Nearest Neighbors (KNN) algorithm using the Iris dataset. It includes steps for loading the dataset, splitting it into training and testing sets, standardizing the data, training the KNN classifier, making predictions, and evaluating the model's accuracy. The final output displays the accuracy of the KNN model.

Uploaded by

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

Naïve Bayes Algorithm in Python Code

The document provides Python code to implement the K-Nearest Neighbors (KNN) algorithm using the Iris dataset. It includes steps for loading the dataset, splitting it into training and testing sets, standardizing the data, training the KNN classifier, making predictions, and evaluating the model's accuracy. The final output displays the accuracy of the KNN model.

Uploaded by

Gaurang Rane
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Q.

6) Implement Python code for given steps to implement Naïve


Bayes Algorithm

from [Link] import load_iris


from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from [Link] import KNeighborsClassifier
from [Link] import accuracy_score

# Load the Iris dataset


iris = load_iris()
X, y = [Link], [Link]

# Split into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Standardize the dataset (important for KNN)


scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)

# Create and train the KNN classifier


k = 3 # Number of neighbors
knn = KNeighborsClassifier(n_neighbors=k)
[Link](X_train, y_train)

# Make predictions
y_pred = [Link](X_test)

# Evaluate the model


accuracy = accuracy_score(y_test, y_pred)
print(f'KNN Model Accuracy: {accuracy:.2f}')

You might also like