【发布时间】:2021-06-23 00:11:42
【问题描述】:
我想使用高斯混合模型来找到看起来像这样的多峰分布的中心:
为此我想使用sklearn.mixture.GaussianMixture。此代码将混合高斯分布回归到数据。通常这样做的方式类似于this:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
from sklearn import mixture
n_samples = 300
# generate random sample, two components
np.random.seed(0)
# generate spherical data centered on (20, 20)
shifted_gaussian = np.random.randn(n_samples, 2) + np.array([20, 20])
# generate zero centered stretched Gaussian data
C = np.array([[0., -0.7], [3.5, .7]])
stretched_gaussian = np.dot(np.random.randn(n_samples, 2), C)
# concatenate the two datasets into the final training set
X_train = np.vstack([shifted_gaussian, stretched_gaussian])
# fit a Gaussian Mixture Model with two components
clf = mixture.GaussianMixture(n_components=2, covariance_type='full')
clf.fit(X_train)
关键是,数据以形成高斯云的二维点列表的形式给出。我的数据有点不同——更像是加权 x,y 点。鉴于我的形象,我可以这样做:
import numpy, cv2
image = cv2.imread("double_blob.jpg")
xs, ys = np.meshgrid(list(range(image.shape[0])), list(range(image.shape[1])))
xs, ys = xs.flatten(), ys.flatten()
weights = image[xs, ys].flatten()
获取 x,y 图像坐标和权重的列表。但我不知道如何将其提供给GaussianMixture 函数。有什么想法吗?
【问题讨论】:
标签: scikit-learn