Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 聚类1D数据并在matplotlib直方图上表示聚类_Python 3.x_Matplotlib_Scikit Learn_Histogram_Cluster Analysis - Fatal编程技术网

Python 3.x 聚类1D数据并在matplotlib直方图上表示聚类

Python 3.x 聚类1D数据并在matplotlib直方图上表示聚类,python-3.x,matplotlib,scikit-learn,histogram,cluster-analysis,Python 3.x,Matplotlib,Scikit Learn,Histogram,Cluster Analysis,我有以下格式的1D数据: areas = ... plt.figure(figsize=(10, 10)) plt.hist(areas, bins=80) plt.show() 这幅图的情节大致如下: 现在我希望能够对这些数据进行集群。我知道我有两种选择,一种是K-Means,另一种是K-Means。但是一旦我有了这些值,我如何在直方图上表示这些簇呢?你只需要计算出你的簇分配,然后分别绘制数据的每个子集,同时注意每次的容器都是相同的 您只需计算出集群分配,然后分别绘制数据的每个子集,同时

我有以下格式的1D数据:

areas = ...
plt.figure(figsize=(10, 10))
plt.hist(areas, bins=80)
plt.show()
这幅图的情节大致如下:


现在我希望能够对这些数据进行集群。我知道我有两种选择,一种是K-Means,另一种是K-Means。但是一旦我有了这些值,我如何在直方图上表示这些簇呢?

你只需要计算出你的簇分配,然后分别绘制数据的每个子集,同时注意每次的容器都是相同的


您只需计算出集群分配,然后分别绘制数据的每个子集,同时注意每次存储箱都是相同的


您想在
直方图上绘制
KDE
,对吗?@JayPatel我想要如上所示的直方图,但颜色表示这些数据点来自的集群。显示每种颜色的群集中心的图例也很好。是否要在
直方图上绘制
KDE
,对吗?@JayPatel我想要如上所示的直方图,但这些数据点所来自的群集的颜色表示。为每种颜色显示集群中心的图例也很不错。
import numpy as np
import matplotlib.pyplot as plt

from sklearn.cluster import KMeans

import matplotlib as mpl
mpl.rcParams['axes.spines.top'] = False
mpl.rcParams['axes.spines.right'] = False

# simulate some fake data
n = 10000
mu1, sigma1 = 0, 1
mu2, sigma2 = 6, 2
a = mu1 + sigma1 * np.random.randn(n)
b = mu2 + sigma2 * np.random.randn(n)
data = np.concatenate([a, b])

# determine which K-Means cluster each point belongs to
cluster_id = KMeans(2).fit_predict(data.reshape(-1, 1))

# determine densities by cluster assignment and plot
fig, ax = plt.subplots()
bins = np.linspace(data.min(), data.max(), 40)
for ii in np.unique(cluster_id):
    subset = data[cluster_id==ii]
    ax.hist(subset, bins=bins, alpha=0.5, label=f"Cluster {ii}")
ax.legend()
plt.show()