用hexpin对多组数据进行过批处理

2024-10-02 06:26:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在对一个大的、非常密集的数据集进行一些KMeans聚类,我正试图找出可视化集群的最佳方式。在

在2D中,看起来hexbin会做得很好,但是我不能在同一个图形上过多地绘制簇。我想在每个簇上分别使用hexbin,并为每个簇使用不同的颜色映射,但由于某些原因,这似乎不起作用。 图中显示了当我试图绘制第二组和第三组数据时得到的结果。在

有什么建议吗? enter image description here

经过一段时间的摆弄,我可以用Seaborn's kdeplot来制作这个

enter image description here


Tags: 数据图形颜色可视化方式绘制集群原因
1条回答
网友
1楼 · 发布于 2024-10-02 06:26:02

我个人认为你的kdeplot解决方案非常好(尽管我会在集群拦截部分做一些工作)。在任何情况下,作为对您的问题的回答,您可以提供hexbin的最小计数(将所有空单元格保留为透明)。这里有一个小函数,可以为任何想要做一些实验的人生成随机的集群(在评论中,您的问题似乎引起了用户的极大兴趣,可以随意使用它):

import numpy as np
import matplotlib.pyplot as plt

# Building random clusters
def cluster(number):
    def clusterAroundX(a,b,number):
        x = np.random.normal(size=(number,))
        return (x-x.min())*(b-a)/(x.max()-x.min())+a
    def clusterAroundY(x,m,b):
        y = x.copy()
        half   = (x.max()-x.min())/2
        middle = half+x.min()
        for i in range(x.shape[0]):
            std = (x.max()-x.min())/(2+10*(np.abs(middle-x[i])/half))
            y[i] = np.random.normal(x[i]*m+b,std)
        return y + np.abs(y.min())
    m,b = np.random.randint(-700,700)/100,np.random.randint(0,50)
    print(m,b)
    f = np.random.randint(0,30)
    l = f + np.random.randint(10,50)
    x = clusterAroundX(f,l,number)
    y = clusterAroundY(x,m,b)
    return x,y

,使用这段代码,我用散点图绘制了一些聚类图(我通常用这个来做自己的聚类分析,但我想我应该看看seaborn)、hexbin、imshow(更改pcolormesh以获得更多控制)和contourf:

^{pr2}$

,结果如下:

{a1}

hexbin clusters

imshow clusters

countourf clusters

相关问题 更多 >

    热门问题