算法艺术创作
algorithmic-art
通过代码和可调参数创作生成艺术与交互式视觉效果。
cluster-model-selection【设计与创意】Use Yellowbrick clustering diagnostics and model-selection visualizers for k selection, validation curves, cross-validation scores, RFECV, feature importances, and dropping curves.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Use this sub-skill when the user needs Yellowbrick visual diagnostics for unsupervised clustering or for scikit-learn model-selection workflows that train many estimator clones. It covers:
KElbowVisualizer, SilhouetteVisualizer, InterclusterDistance, and kelbow_visualizer/silhouette_visualizer/intercluster_distance quick methods;ValidationCurve, LearningCurve, and CVScores;RFECV, FeatureImportances, and DroppingCurve.../text-and-datasets/SKILL.md when that sub-skill is available.show(outpath=...), axes reuse, Yellowbrick style, or shared lifecycle mechanics: read root visualizer patterns first, then return here for cluster/model-selection choices.| User goal | Use | Key requirements |
|---|---|---|
Pick a candidate number of clusters k | KElbowVisualizer | Centroid-style clusterer with n_clusters, fit, and labels_; tune k, metric, timings, and locate_elbow. |
Compare cluster density and imbalance for one k | SilhouetteVisualizer | Clusterer with fit_predict or fit+predict; at least two non-empty clusters. |
| Show relative cluster-center distances | InterclusterDistance | Clusterer with cluster_centers_ and labels_; embedding is "mds" or "tsne"; scoring is currently "membership". |
| Tune one estimator hyperparameter | ValidationCurve | Exact param_name, 1-D param_range, explicit cv, valid scoring, bounded n_jobs. |
| Ask whether more data helps | LearningCurve | Bounded train_sizes, explicit cv, valid scoring; works with classifiers, regressors, and some clusterers. |
| Show fold-to-fold score spread | CVScores | Estimator plus cv strategy and scoring; displays per-fold bars and mean line. |
| Choose number of features with recursive elimination | RFECV | Estimator must expose coef_ or feature_importances_ after fit; step must be positive. |
| Rank features from a fitted model | FeatureImportances | Estimator must expose feature_importances_ or coef_; pass labels, relative, absolute, stack, and topn deliberately. |
| Estimate how many random features are enough | DroppingCurve | Bounded feature_sizes, explicit cv, scoring, n_jobs, and random_state; trains many clones. |
Agg, performs no network access, and writes PNGs plus manifest.json.import matplotlib
matplotlib.use("Agg")
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
from yellowbrick.cluster import KElbowVisualizer, SilhouetteVisualizer
X, _ = make_blobs(n_samples=240, n_features=6, centers=4, random_state=42)
elbow = KElbowVisualizer(
KMeans(random_state=42, n_init=10),
k=(2, 8),
metric="distortion",
timings=False,
)
elbow.fit(X)
elbow.show(outpath="k_elbow.png", clear_figure=True, bbox_inches="tight")
chosen_k = elbow.elbow_value_ or 4
sil = SilhouetteVisualizer(KMeans(n_clusters=chosen_k, random_state=42, n_init=10))
sil.fit(X)
sil.show(outpath="silhouette.png", clear_figure=True, bbox_inches="tight")
Do not treat elbow_value_ is None as a hard failure. A smooth elbow curve often means the data are not strongly clustered or the candidate range/metric is not informative; compare metric="silhouette", metric="calinski_harabasz", and explicit silhouette plots before recommending a final k.
import numpy as np
import matplotlib
matplotlib.use("Agg")
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold
from yellowbrick.model_selection import ValidationCurve, CVScores
X, y = make_classification(
n_samples=180, n_features=10, n_informative=5, random_state=42
)
cv = StratifiedKFold(n_splits=3, shuffle=True, random_state=42)
validation = ValidationCurve(
LogisticRegression(max_iter=1000, solver="liblinear"),
param_name="C",
param_range=np.logspace(-2, 1, 4),
logx=True,
cv=cv,
scoring="f1_weighted",
n_jobs=1,
)
validation.fit(X, y)
validation.show(outpath="validation_curve.png", clear_figure=True, bbox_inches="tight")
scores = CVScores(
LogisticRegression(max_iter=1000, solver="liblinear"),
cv=cv,
scoring="f1_weighted",
)
scores.fit(X, y)
scores.show(outpath="cv_scores.png", clear_figure=True, bbox_inches="tight")
Model-selection visualizers clone and fit estimators many times. Before running on user data, bound the work:
cv with a small fold count during exploration; increase folds only after the visualizer works.param_range, train_sizes, and feature_sizes short; expand ranges in a second pass.n_jobs=1 for debuggability and memory-constrained agents. Raise n_jobs only when the estimator is thread/process safe and the host has capacity.pre_dispatch="2*n_jobs" or another bounded value when parallel jobs allocate large arrays.random_state on estimators, CV splitters, and DroppingCurve for reproducible review artifacts.show(outpath=..., clear_figure=True, bbox_inches="tight") over interactive display in scripts and CI.Before handing off a cluster/model-selection result:
param_name, param_range, train_sizes, feature_sizes, cv, scoring, n_jobs, and pre_dispatch are explicit and bounded.k_values_, k_scores_, silhouette_score_, train_scores_mean_, test_scores_mean_, cv_scores_, features_, feature_importances_, support_, ranking_, and valid_scores_mean_ before summarizing conclusions.k.