Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Unsupervised Learning

This repository contains implementations of unsupervised learning algorithms, including KMeans Clustering and other common unsupervised learning techniques. These algorithms are widely used in machine learning for tasks such as clustering and dimensionality reduction.

Contents

Introduction

Unsupervised Learning

Unsupervised learning is a type of machine learning where the model is trained on unlabeled data. The goal is to infer the natural structure present within a set of data points. Common unsupervised learning algorithms include:

  • KMeans Clustering
  • Hierarchical Clustering
  • Principal Component Analysis (PCA)
  • t-Distributed Stochastic Neighbor Embedding (t-SNE)
  • DBSCAN

KMeans Clustering

KMeans is an unsupervised learning algorithm used to partition a dataset into K clusters. It assigns data points to clusters in such a way that the sum of the squared distances between the data points and the cluster centroids is minimized.

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/unsupervised-learning.git
    cd unsupervised-learning
  2. Install the required Python packages:

    pip install -r requirements.txt

Usage

Example: KMeans Clustering

Below is an example of how to use the KMeans Clustering algorithm to solve a clustering problem.

  1. Load your dataset.
  2. Preprocess the data.
  3. Determine the optimal number of clusters using the elbow method.
  4. Train the KMeans model.
  5. Visualize the clusters.
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from kneed import KneeLocator
import matplotlib.pyplot as plt

# Generate sample data
X, _ = make_blobs(n_samples=300, centers=4, cluster_std=0.60, random_state=0)

# Elbow method to find optimal number of clusters
wcss = []
for i in range(1, 11):
    kmeans = KMeans(n_clusters=i, init='k-means++', random_state=42)
    kmeans.fit(X)
    wcss.append(kmeans.inertia_)

kneedle = KneeLocator(range(1, 11), wcss, curve='convex', direction='decreasing')

# Plot the elbow graph
plt.plot(range(1, 11), wcss, marker='o')
plt.vlines(kneedle.elbow, plt.ylim()[0], plt.ylim()[1], linestyles='dashed', colors='red')
plt.title('The Elbow Method')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.show()

print(f'The optimal number of clusters is: {kneedle.elbow}')

# Train the model with optimal number of clusters
optimal_clusters = kneedle.elbow
kmeans = KMeans(n_clusters=optimal_clusters, init='k-means++', random_state=42)
y_kmeans = kmeans.fit_predict(X)

# Visualize the clusters
plt.scatter(X[:, 0], X[:, 1], c=y_kmeans, s=50, cmap='viridis')
centers = kmeans.cluster_centers_
plt.scatter(centers[:, 0], centers[:, 1], c='red', s=200, alpha=0.75, marker='X')
plt.title(f'KMeans Clustering with {optimal_clusters} Clusters')
plt.show()

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages