Unsupervised ML Introduction

Unsupervised machine learning is a type of machine learning where we don't provide the model with labeled data or explicit instructions on how to solve a problem. Instead, the model is given a set of data and it tries to find patterns, similarities, or structures within that data on its own.

Think of it like giving a computer a pile of unsorted objects and asking it to organize them. The computer doesn't know what these objects are or how they should be grouped, but it starts looking for similarities or common features among them. Based on those similarities, it creates clusters or groups of similar objects.

Similarly, in unsupervised machine learning, the model analyzes the data without any prior knowledge about the desired outcome. It looks for hidden patterns, relationships, or structures in the data by grouping similar data points together. This process is called clustering.

Unsupervised learning can also involve dimensionality reduction, which means reducing the number of variables or features in the data while preserving important information. It helps in visualizing and understanding complex datasets.

So, in simple terms, unsupervised machine learning is like letting the computer explore and make sense of the data by itself, finding patterns or organizing it in a way that makes sense without any specific instructions or guidance from us.

Python Example

import numpy as np
from sklearn.cluster import KMeans

# Create some example data points
data = np.array([[1, 2], [1.5, 1.8], [5, 8], [8, 8], [1, 0.6], [9, 11]])

# Create a K-means clustering model with 2 clusters
kmeans = KMeans(n_clusters=2)

# Fit the model to the data
kmeans.fit(data)

# Get the cluster labels assigned to each data point
labels = kmeans.labels_

# Get the centroids of the clusters
centroids = kmeans.cluster_centers_

# Print the cluster labels and centroids
print("Cluster Labels:", labels)
print("Cluster Centroids:", centroids)

Results:

Cluster Labels: [1 1 0 0 1 0]
Cluster Centroids: [[7.33333333 9.        ]
                   [1.16666667 1.46666667]]

In this example, we have a set of 2-dimensional data points stored in the data array. We create a KMeans object with n_clusters=2, indicating that we want to identify two clusters. Then, we fit the model to the data using kmeans.fit(data). After fitting, we can access the cluster labels assigned to each data point using kmeans.labels_, and the centroids of the clusters using kmeans.cluster_centers_.

When you run this code, you'll see the cluster labels assigned to each data point and the coordinates of the cluster centroids printed to the console.

Note that this is a simple example, and unsupervised learning can be used for more complex tasks such as anomaly detection, feature extraction, or recommendation systems, among others.