0% found this document useful (0 votes)
20 views5 pages

Experiment 8

The document presents a Python script that implements a binary classification task using the Iris dataset, focusing on two classes and two features. It trains and evaluates K-Nearest Neighbors (KNN) and Decision Tree classifiers, visualizing decision boundaries and plotting accuracy against different K values for KNN. The script includes data preprocessing, model training, and performance evaluation with accuracy metrics.

Uploaded by

Rishab
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)
20 views5 pages

Experiment 8

The document presents a Python script that implements a binary classification task using the Iris dataset, focusing on two classes and two features. It trains and evaluates K-Nearest Neighbors (KNN) and Decision Tree classifiers, visualizing decision boundaries and plotting accuracy against different K values for KNN. The script includes data preprocessing, model training, and performance evaluation with accuracy metrics.

Uploaded by

Rishab
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/ 5

import numpy as np

import matplotlib.pyplot as plt


from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

# Load Iris dataset and simplify to 2 classes and 2 features


iris = load_iris()
X = iris.data[:, :2] # first 2 features for visualization
y = iris.target

# Use only classes 0 and 1 for binary classification


X = X[y != 2]
y = y[y != 2]

# Train/test split
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.3, random_state=42)

# Scale features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
X_all = np.vstack((X_train, X_test))
y_all = np.concatenate((y_train, y_test))

# Function to plot decision boundary


def plot_decision_boundary(model, X, y, title):
h = .02
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.figure(figsize=(6, 4))
plt.contourf(xx, yy, Z, alpha=0.3, cmap=plt.cm.coolwarm)
plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k',
cmap=plt.cm.coolwarm)
plt.title(title)
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.tight_layout()
plt.show()

# Compare KNN for different K values


k_values = range(1, 16)
knn_accuracies = []

for k in k_values:
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)
acc = knn.score(X_test, y_test)
knn_accuracies.append(acc)
if k in [1, 3, 7]: # Only visualize a few
plot_decision_boundary(knn, X_all, y_all, f"KNN (k={k})")

# Decision Tree baseline


tree = DecisionTreeClassifier(random_state=42)
tree.fit(X_train, y_train)
tree_acc = tree.score(X_test, y_test)
plot_decision_boundary(tree, X_all, y_all, f"Decision Tree
(Accuracy={tree_acc:.2f})")

# Plot Accuracy vs K
plt.figure(figsize=(6, 4))
plt.plot(k_values, knn_accuracies, marker='o', label="KNN Accuracy")
plt.axhline(y=tree_acc, color='r', linestyle='--', label=f"Decision
Tree Accuracy = {tree_acc:.2f}")
plt.title("KNN Accuracy vs. K")
plt.xlabel("K (Number of Neighbors)")
plt.ylabel("Accuracy")
plt.legend()
plt.tight_layout()
plt.show()

You might also like