[파이썬] scipy k-means 클러스터링 (scipy.cluster)

Clustering is a popular technique used in machine learning and data analysis to group similar data points together. One commonly used clustering algorithm is k-means, which partitions the data into k clusters such that the sum of the squared distances between data points and their nearest cluster centroid is minimized.

In Python, the scipy.cluster module provides an implementation of the k-means algorithm through the scipy.cluster.vq submodule. This allows us to perform k-means clustering on a dataset easily.

Installing the Required Packages

Before we get started, make sure you have the scipy package installed. You can install it using pip:

pip install scipy

Example Usage

Let’s assume that we have a dataset containing information about customers, such as their age and annual income. We want to cluster these customers into three groups based on their similarities in age and income.

First, let’s import the necessary modules and generate some random data points:

import numpy as np
from scipy.cluster.vq import kmeans, vq

# Generate random data
np.random.seed(0)
data = np.random.rand(100, 2)

Next, we can apply k-means clustering to the data using the kmeans function:

# Perform k-means clustering
centroids, labels = kmeans(data, 3)

The kmeans function takes two arguments: the data to be clustered and the desired number of clusters (k). It returns two outputs: the centroids of the clusters (represented as ndarray) and the labels assigning each data point to one of the clusters.

We can use the vq function to assign each data point to its nearest centroid:

# Assign data points to clusters
assignments, _ = vq(data, centroids)

The vq function takes two arguments: the data and the centroids. It returns two outputs: the assignments (labels) of the data points and the distortion (a measure of how well the data points fit the centroids).

Finally, we can visualize the results by plotting the data points with different colors representing different clusters:

import matplotlib.pyplot as plt

# Plot the data points and clusters
plt.scatter(data[:, 0], data[:, 1], c=assignments)
plt.scatter(centroids[:, 0], centroids[:, 1], c='r', marker='x', label='Centroids')
plt.xlabel('Age')
plt.ylabel('Annual Income')
plt.legend()
plt.show()

This will generate a scatter plot with each data point colored according to its cluster assignment, and the centroids marked with red crosses.

Conclusion

The scipy.cluster module in Python provides a powerful implementation of the k-means clustering algorithm through the scipy.cluster.vq submodule. By using this module, we can easily perform k-means clustering on our dataset and analyze the results.

K-means clustering is just one of many clustering algorithms available in Python, but it is a good starting point for exploring the basics of clustering.