top of page
Interesting Recent Posts :
Writer's pictureRohit chopra

Unsupervised Learning Example :Artificial Intelligence





KMeans is a popular unsupervised machine learning algorithm for clustering. The main idea behind KMeans is to partition a set of data points into K clusters, where K is a user-defined number, in such a way that the sum of squared distances between each data point and the center of its assigned cluster (also called centroid) is minimized. The algorithm iteratively reassigns data points to different clusters and updates the centroids until convergence, meaning that the assignment of data points to clusters no longer changes.

KMeans is widely used in various fields, such as computer vision, natural language processing, and market segmentation. By grouping similar data points into clusters, KMeans can help discover hidden patterns and structure in the data, which can then be used for various purposes such as dimensionality reduction, data compression, and anomaly detection.

In this code example, we use KMeans from the sklearn.cluster library to perform clustering on a set of 2-dimensional data points. The results of the clustering are visualized using matplotlib, a popular data visualization library in Python.



Here is an example of unsupervised learning using KMeans clustering algorithm in Python with the use of matplotlib to represent the results:


import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

# Generate sample data
np.random.seed(0)
X = np.random.randn(100, 2)

# Fit the KMeans model
kmeans = KMeans(n_clusters=3)
kmeans.fit(X)

# Get the cluster assignments for each data point
labels = kmeans.labels_

# Plot the data points with different colors for each cluster
plt.scatter(X[:,0], X[:,1], c=labels)

# Add a title and axis labels
plt.title("KMeans Clustering")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")

# Show the plot
plt.show()

In this example, 100 random data points are generated using numpy's randn function. These data points are then fit to a KMeans model with 3 clusters. The cluster assignments for each data point are obtained using the labels_ attribute of the fitted model. Finally, the data points are plotted using matplotlib's scatter function, where each data point is colored based on its cluster assignment.





Matplot Lib Chart :






1 view

Recent Posts

See All

Machine Learning : KNN model

Introduction to K-Nearest Neighbors (KNN) Algorithm in Machine Learning: K-Nearest Neighbors (KNN) is one of the simplest and most...

Comentarios


bottom of page