Yellowbrick 是一套名为「Visualizers」的视觉诊断工具,它扩展了 Scikit-Learn API 以允许我们监督模型的选择过程。简而言之,Yellowbrick 将 Scikit-Learn 与 Matplotlib 结合在一起,并以传统 Scikit-Learn 的方式对模型进行可视化。
可视化器
可视化器(Visualizers)是一种从数据中学习的估计器,其主要目标是创建可理解模型选择过程的可视化。在 Scikit-Learn 的术语中,它们类似于转换器(transformer),其在可视化数据空间或包装模型估计器上类似「ModelCV」(例如 RidgeCV 和 LassoCV)方法的过程。Yellowbrick 的主要目标是创建一个类似于 Scikit-Learn 的 API,其中一些流行的可视化器包括:
#特征之间协方差可视化
from yellowbrick.features import Rank2D
from sklearn.datasets import load_iris
data=load_iris()
visualizer = Rank2D(features=data['feature_names'], algorithm='covariance')
visualizer.fit(data['data'], data['target']) # Fit the data to the visualizer
visualizer.transform(data['data']) # Transform the data
visualizer.poof() # Draw/show/poof the data
#梯度提升树中特征重要性可视化
import matplotlib.pyplot as plt
from sklearn.ensemble import GradientBoostingClassifier
from yellowbrick.features import FeatureImportances
from sklearn.datasets import load_iris
data=load_iris()
fig = plt.figure()
ax = fig.add_subplot()
viz = FeatureImportances(GradientBoostingClassifier(), relative=False)
viz.fit(data['data'],data['target']) # Fit the data to the visualizer
viz.poof() # Draw/show/poof the data
#线性支持向量机ROC曲线可视化
from sklearn.svm import LinearSVC
from yellowbrick.classifier import ROCAUC
model = LinearSVC()
model.fit(data['data'],data['target'])
visualizer = ROCAUC(model)
visualizer.score(data['data'],data['target'])
visualizer.poof()
#主成分分析二维降维可视化
from yellowbrick.features.pca import PCADecomposition
visualizer = PCADecomposition(scale=True, center=False, color="g", proj_dim=2)
visualizer.fit_transform(data['data'],data['target'])
visualizer.poof()
#线性支持向量机准确率、召回率、f1-score可视化
from sklearn.svm import LinearSVC
from yellowbrick.classifier import ClassificationReport
from sklearn.model_selection import train_test_split
model = LinearSVC()
X_train, X_test, y_train, y_test = train_test_split(data['data'],data['target'], test_size=0.2)
visualizer = ClassificationReport(model, classes=data['target_names'])
visualizer.fit(X_train, y_train) # Fit the visualizer and the model
visualizer.score(X_test, y_test) # Evaluate the model on the test data
g = visualizer.poof() # Draw/show/poof the data
#alpha 的选择如何影响正则化可视化
import numpy as np
from sklearn.linear_model import LassoCV
from yellowbrick.regressor import AlphaSelection
# Create a list of alphas to cross-validate against
alphas = np.logspace(-10, 1, 400)#以10为底对数,-10到1分成400份
# Instantiate the linear model and visualizer
model = LassoCV(alphas=alphas)
visualizer = AlphaSelection(model)
visualizer.fit(data['data'],data['target'])
g = visualizer.poof()
#肘部法则选择最佳聚类的k
from sklearn.cluster import MiniBatchKMeans
from yellowbrick.cluster import KElbowVisualizer
# Instantiate the clustering model and visualizer
visualizer = KElbowVisualizer(MiniBatchKMeans(), k=(4,12))
visualizer.fit(data["data"]) # Fit the training data to the visualizer
visualizer.poof() # Draw/show/poof the data
#训练集数量对模型表现可视化
import numpy as np
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import StratifiedKFold
from yellowbrick.model_selection import LearningCurve
# Create the learning curve visualizer
cv = StratifiedKFold(12)#k折交叉切分
sizes = np.linspace(0.3, 1.0, 10)
viz = LearningCurve(
MultinomialNB(), cv=cv, train_sizes=sizes,
scoring='f1_weighted', n_jobs=4
)
# Fit and poof the visualizer
viz.fit(data['data'],data['target'])
viz.poof()