You are on page 1of 2

import Numpy,Pandas ,Matplotlib and sklearn Library

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import load_iris

load the IRIS dataset


# Load the iris dataset
iris = load_iris()
X = iris.data

Perform k means clustering


# Perform k-means clustering
kmeans = KMeans(n_clusters=3, random_state=42)
kmeans.fit(X)

KMeans(n_clusters=3, random_state=42)

# Get cluster labels


labels = kmeans.labels_
labels

array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1,
1, 1, 1, 1, 1, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 2, 2, 2, 0, 2, 2,
2,
2, 2, 2, 0, 0, 2, 2, 2, 2, 0, 2, 0, 2, 0, 2, 2, 0, 0, 2, 2, 2,
2,
2, 0, 2, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 0])

cluster_names = {0: "Setosa", 1: "Versicolor", 2: "Virginica"}


cluster_labels = [cluster_names[label] for label in labels]

Plot the graph to see cluster


plt.scatter(X[:, 0], X[:, 1], c=labels)
plt.xlabel("Sepal Length")
plt.ylabel("Sepal Width")
plt.title("K-means Clustering - Python")
plt.show()
Count the eash cluster instances
cluster_counts = np.bincount(kmeans.labels_)
for i, count in enumerate(cluster_counts):
print(f"Cluster {i+1}: {count} instances")

Cluster 1: 62 instances
Cluster 2: 50 instances
Cluster 3: 38 instances

You might also like