SKLearner Home | About | Contact | Examples

Scikit-Learn chi2_kernel() Metric

The chi2_kernel() function in scikit-learn calculates the chi-squared kernel between two arrays. This kernel can be used to compute pairwise distances between data points, which is particularly useful for comparing histograms or distributions.

Under the hood, chi2_kernel() applies the chi-squared kernel function to each pair of rows in the input arrays. The chi-squared kernel is a measure of similarity between two probability distributions, making it well-suited for comparing histograms or other non-negative features.

The chi-squared kernel is commonly used in problems where the data is represented as histograms or frequency counts, such as text classification or image comparison. It is particularly effective when the data is sparse and high-dimensional.

However, the chi-squared kernel assumes that the input features are non-negative and can be interpreted as frequencies or counts. It may not be appropriate for data with negative values or features that do not represent distributions.

from sklearn.metrics.pairwise import chi2_kernel
import numpy as np

# Generate synthetic data representing histograms
X = np.random.randint(0, 10, (100, 50))
Y = np.random.randint(0, 10, (100, 50))

# Calculate pairwise distances using chi2_kernel()
distances = chi2_kernel(X, Y)

# Demonstrate using the distances for clustering
from sklearn.cluster import AgglomerativeClustering

clusterer = AgglomerativeClustering(n_clusters=5, metric='precomputed', linkage='average')
clusterer.fit(distances)

print(f"Cluster labels: {clusterer.labels_}")

Running the example gives an output like:

Cluster labels: [4 0 0 0 1 1 1 0 0 0 4 2 2 2 2 2 1 0 0 1 3 0 1 0 1 1 0 1 3 1 2 1 4 4 1 1 0
 0 4 1 0 1 1 3 0 1 4 1 1 2 0 2 1 1 4 3 1 2 4 2 3 2 3 0 2 2 1 2 0 1 1 3 0 0
 2 0 3 2 1 3 0 4 3 1 0 2 4 2 4 2 2 2 3 3 0 1 4 3 0 0]

The steps in this example are:

  1. Generate synthetic data (X and Y) representing histograms or frequency counts.
  2. Calculate pairwise distances between X and Y using chi2_kernel().
  3. Demonstrate using the calculated distances for clustering with AgglomerativeClustering.
  4. Print the resulting cluster labels.

We start by generating synthetic data to simulate histograms or frequency counts. The X array represents a dataset of 100 samples, each with 50 features, while Y represents a second dataset of 100 samples, also with 50 features.

Next, we use chi2_kernel() to calculate the pairwise distances between the samples in X and Y. The resulting distances array contains the chi-squared kernel values for each pair of samples.

To demonstrate how these distances can be used in practice, we perform hierarchical clustering using AgglomerativeClustering from scikit-learn. By setting affinity='precomputed', we indicate that we are providing a precomputed distance matrix (in this case, the distances array). We specify linkage='average' to use the average linkage criterion for merging clusters.

Finally, we print the cluster labels assigned to each sample in X. These labels indicate which cluster each sample belongs to based on the chi-squared kernel distances.

This example showcases how to use chi2_kernel() to calculate pairwise distances between data points and how those distances can be utilized in a practical application like clustering.



See Also