You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DBSCAN参数迭代子图绘制及多参数循环实现技术问询

DBSCAN Parameter Tuning & Visualization Guide

Got it, let's tackle your DBSCAN visualization and parameter iteration questions step by step!

Part 1: Side-by-Side Subplots for Different eps Values

To visualize all your eps values (0.1, 0.2, 0.3, 0.4) in a single figure with side-by-side subplots, you'll use Matplotlib's subplots() to create a grid of axes, then loop through each eps value to fit DBSCAN and plot results. Here's a complete example (assuming you have your dataset stored in X):

import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN
# If you don't have a dataset, use make_blobs to generate sample data:
# from sklearn.datasets import make_blobs
# X, _ = make_blobs(n_samples=500, centers=4, random_state=42)

# Define your eps values
eps_values = [0.1, 0.2, 0.3, 0.4]
fixed_min_samples = 10  # Use your base min_samples value here

# Create a 1x4 grid of subplots
fig, axes = plt.subplots(nrows=1, ncols=len(eps_values), figsize=(16, 4))

# Iterate over each eps value and plot results
for idx, eps in enumerate(eps_values):
    # Initialize and fit DBSCAN
    dbscan = DBSCAN(eps=eps, min_samples=fixed_min_samples)
    cluster_labels = dbscan.fit_predict(X)
    
    # Plot on the corresponding axis
    axes[idx].scatter(X[:, 0], X[:, 1], c=cluster_labels, cmap='viridis', alpha=0.6)
    axes[idx].set_title(f'DBSCAN (eps={eps})')
    axes[idx].set_xlabel('Feature 1')
    axes[idx].set_ylabel('Feature 2')

# Adjust layout to prevent overlapping labels
plt.tight_layout()
plt.show()

This code creates a single row of 4 subplots, each showing the clustering result for one eps value. The viridis colormap helps distinguish clusters clearly, and alpha=0.6 makes overlapping points easier to see.

Part 2: Iterating Over min_samples with an Outer Loop

Absolutely, you can use an outer loop to iterate over your min_samples values (10, 12, 15)! This will create a grid of subplots where each row corresponds to a min_samples value, and each column corresponds to an eps value. Here's how to implement it:

# Define both parameter lists
min_samples_values = [10, 12, 15]
eps_values = [0.1, 0.2, 0.3, 0.4]

# Create a 3x4 grid of subplots (one row per min_samples)
fig, axes = plt.subplots(nrows=len(min_samples_values), ncols=len(eps_values), figsize=(16, 12))

# Nested loops to iterate over both parameters
for row_idx, min_sample in enumerate(min_samples_values):
    for col_idx, eps in enumerate(eps_values):
        # Fit DBSCAN with current parameters
        dbscan = DBSCAN(eps=eps, min_samples=min_sample)
        cluster_labels = dbscan.fit_predict(X)
        
        # Calculate number of clusters (excluding noise points labeled -1)
        num_clusters = len(set(cluster_labels)) - (1 if -1 in cluster_labels else 0)
        
        # Plot results
        axes[row_idx, col_idx].scatter(X[:, 0], X[:, 1], c=cluster_labels, cmap='viridis', alpha=0.6)
        axes[row_idx, col_idx].set_title(f'min_samples={min_sample}\neps={eps}\nClusters: {num_clusters}')
        axes[row_idx, col_idx].set_xlabel('Feature 1')
        axes[row_idx, col_idx].set_ylabel('Feature 2')

plt.tight_layout()
plt.show()

Key Notes for This Approach:

  • The nested loops let you test every combination of eps and min_samples efficiently.
  • Adding the number of clusters to the title makes it easier to compare how each parameter affects clustering outcomes.
  • If your dataset has more than 2 features, you'll want to apply dimensionality reduction (like PCA) first to visualize results in 2D.

内容的提问来源于stack exchange,提问作者Mike_H

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:52:44