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

5 Clustering Algorithm 17-09-2024

The document outlines a Python script that performs K-Means clustering on the Iris dataset using standardized features and PCA for visualization. It evaluates the clustering model by calculating accuracy and displaying a confusion matrix. The results indicate an accuracy of 24% with a corresponding confusion matrix showing the distribution of predicted versus actual labels.

Uploaded by

rohithvishnuk
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)
13 views2 pages

5 Clustering Algorithm 17-09-2024

The document outlines a Python script that performs K-Means clustering on the Iris dataset using standardized features and PCA for visualization. It evaluates the clustering model by calculating accuracy and displaying a confusion matrix. The results indicate an accuracy of 24% with a corresponding confusion matrix showing the distribution of predicted versus actual labels.

Uploaded by

rohithvishnuk
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

import numpy as np

import pandas as pd
from [Link] import load_iris
from [Link] import KMeans
import [Link] as plt
from [Link] import StandardScaler
from [Link] import PCA
from [Link] import accuracy_score, confusion_matrix

# Load the Iris dataset


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

# Standardize the features


scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Apply PCA for visualization


pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)

# Apply K-Means with 3 clusters


kmeans = KMeans(n_clusters=3, random_state=42)
[Link](X_scaled)

# Predict the cluster for each data point


clusters = [Link](X_scaled)

# Visualize the clusters


[Link](figsize=(8, 6))
[Link](X_pca[:, 0], X_pca[:, 1], c=clusters, cmap='viridis', marker='o')
[Link](kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=300, c='red', marker='x') # Centroids
[Link]('K-Means Clustering of Iris Dataset')
[Link]('PCA Component 1')
[Link]('PCA Component 2')
[Link]()

# Evaluate the model


labels = [Link](kmeans.labels_, [0, 1, 2]).astype(np.int64)
accuracy = accuracy_score(y, labels)
print(f'Accuracy: {accuracy * 100:.2f}%')
conf_matrix = confusion_matrix(y, labels)
print('Confusion Matrix:')
print(conf_matrix)

/usr/local/lib/python3.10/dist-packages/sklearn/cluster/_kmeans.py:1416: FutureWarning:
super()._check_params_vs_input(X, default_n_init=10)
Accuracy: 24.00%
Confusion Matrix:
[[ 0 50 0]
[39 0 11]
[14 0 36]]

You might also like