Kmeans clustering

 from sklearn.cluster import KMeans

import matplotlib.pyplot as plt

import pandas as pd


data = pd.read_csv("data.csv") # Load dataset (ensure data.csv is available)

X = data.iloc[:, :].values # Use all columns as features

kmeans = KMeans(n_clusters=3, random_state=42).fit(X) # Apply K-Means with 3 clusters

labels = kmeans.labels_ # Cluster labels for each data point

plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis') # Visualize clusters

plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], color='red', marker='x') # Centers

plt.title("K-Means Clustering")

plt.show()

Comments

Popular posts from this blog

Lab 1 ai

Web