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

K-Means Clustering on Iris Dataset

The document outlines a Python program that performs k-means clustering on the iris dataset, which is an unsupervised dataset. It includes steps for standardizing features, reducing dimensions using PCA for visualization, and applying the k-means algorithm to group the data into clusters. Finally, it visualizes the clusters using a scatter plot of the principal components.

Uploaded by

lavanyagongati12
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)
3 views2 pages

K-Means Clustering on Iris Dataset

The document outlines a Python program that performs k-means clustering on the iris dataset, which is an unsupervised dataset. It includes steps for standardizing features, reducing dimensions using PCA for visualization, and applying the k-means algorithm to group the data into clusters. Finally, it visualizes the clusters using a scatter plot of the principal components.

Uploaded by

lavanyagongati12
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

Program 4

Read an unsupervised dataset and group the dataset based on similarity based on
k-means clustering

import numpy as np

import pandas as pd

import [Link] as plt

from [Link] import KMeans

from sklearn import datasets

from [Link] import StandardScaler

from [Link] import PCA

# Load iris dataset

iris = datasets.load_iris()

X = [Link] # We ignore the target (unsupervised)

# Standardize features

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)

# Optional: Reduce dimensions for visualization

pca = PCA(n_components=2)

X_pca = pca.fit_transform(X_scaled)

# Apply KMeans

kmeans = KMeans(n_clusters=3, random_state=42)

[Link](X_scaled)

labels = kmeans.labels_

# Add labels to DataFrame

df = [Link](X_pca, columns=['PC1', 'PC2'])

df['Cluster'] = labels

# Visualize Clusters
[Link](df['PC1'], df['PC2'], c=df['Cluster'], cmap='viridis', s=50)

[Link]("K-Means Clustering (Iris Dataset - PCA Reduced)")

[Link]("Principal Component 1")

[Link]("Principal Component 2")

[Link](True)

[Link]()

Output

You might also like