提供的模型不是YellowBrick中的聚类估计器

Jam*_* L. 0 machine-learning scikit-learn expectation-maximization yellowbrick

我正在尝试使用YellowBrick的KElbowVisualizer和SKLearn的Expectation Maximization算法类:GaussianMixture可视化我的数据的弯头图。

运行此命令时,标题出现错误。(我也尝试过ClassificationReport,但这也失败了)

model = GaussianMixture()

data = get_data(data_name, preprocessor_name, train_split=0.75)
X, y, x_test, y_test = data

visualizer = KElbowVisualizer(model, k=(4,12))
visualizer.fit(X)        # Fit the data to the visualizer
visualizer.show()        # Finalize and render the figure
Run Code Online (Sandbox Code Playgroud)

我在YellowBrick中找不到任何可以帮助我估计期望最大化的组件数量的东西。

bbe*_*ort 8

Yellowbrick 使用sklearn估计器类型检查,以确定是否一个模型非常适合的可视化。您可以使用force_model参数绕过类型检查(尽管似乎KElbow需要对此进行更新)。

但是,即使force_model=True您通过了YellowbrickTypeError它,也并不意味着GaussianMixture可以使用KElbow。这是因为肘部可视化器已设置为可与形心群集API一起使用,并且需要n_clusters超参数和labels_学习的参数。期望最大化模型不支持此API。

但是,可以围绕高斯混合模型创建一个包装器,使其可以与肘部可视化工具一起使用(并且分类报告也可以使用类似的方法)。

from sklearn.base import ClusterMixin
from sklearn.mixture import GaussianMixture
from yellowbrick.cluster import KElbow
from yellowbrick.datasets import load_nfl

class GMClusters(GaussianMixture, ClusterMixin):

    def __init__(self, n_clusters=1, **kwargs):
        kwargs["n_components"] = n_clusters
        super(GMClusters, self).__init__(**kwargs)

    def fit(self, X):
        super(GMClusters, self).fit(X)
        self.labels_ = self.predict(X)
        return self 


X, _ = load_nfl()
oz = KElbow(GMClusters(), k=(4,12), force_model=True)
oz.fit(X)
oz.show()
Run Code Online (Sandbox Code Playgroud)

确实会生成一个KElbow图(尽管对于该特定数据集而言不是一个很好的图):

失真的KElbow

另一个答案提到了Calinksi Harabasz分数,您可以在KElbow可视化工具中使用它,如下所示:

oz = KElbow(GMClusters(), k=(4,12), metric='calinski_harabasz', force_model=True)
oz.fit(X)
oz.show()
Run Code Online (Sandbox Code Playgroud)

创建包装器并不是理想的选择,但是对于不适合标准分类器或群集器sklearn API的模型类型,它们通常是必需的,并且这是个可以执行许多ML任务的好策略。