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

Kmeans

The document presents a Python script that performs K-means clustering on the Iris dataset using the scikit-learn library. It scales the data, fits a K-means model with three clusters, and visualizes the results with scatter plots showing the clusters and their centroids. The plots illustrate the relationship between sepal length and width for the different iris species.

Uploaded by

sugikrish
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)
5 views2 pages

Kmeans

The document presents a Python script that performs K-means clustering on the Iris dataset using the scikit-learn library. It scales the data, fits a K-means model with three clusters, and visualizes the results with scatter plots showing the clusters and their centroids. The plots illustrate the relationship between sepal length and width for the different iris species.

Uploaded by

sugikrish
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
You are on page 1/ 2

import numpy as np

import matplotlib.pyplot as plt

from sklearn.cluster import KMeans

from sklearn.datasets import load_iris

from sklearn.preprocessing import scale

iris = load_iris()

X = iris.data

X = scale(X)

y = iris.target

k=3

model = KMeans(n_clusters=k, random_state=0)

model.fit(X)

colors = np.array(['darkorange', 'navy', 'forestgreen'])

plt.scatter(X[:, 0], X[:, 1], c=colors[y])

plt.scatter(model.cluster_centers_[:, 0], model.cluster_centers_[:, 1], marker='x', s=200,


linewidths=3,

color='black', zorder=10)

plt.title('K-means clustering on the Iris dataset')

plt.xlabel('Sepal length')

plt.ylabel('Sepal width')

plt.show()

colors = np.array(['darkorange', 'navy', 'forestgreen'])

plt.scatter(X[:, 0], X[:, 1], c=colors[y])

plt.scatter(model.cluster_centers_[:, 0], model.cluster_centers_[:, 1], marker='x', s=200,


linewidths=3,
color='black', zorder=10)

plt.title('K-means clustering on the Iris dataset')

plt.xlabel('Sepal length')

plt.ylabel('Sepal width')

plt.show()

You might also like