用 sklearn GMM 计算概率

b10*_*ard 5 python statistics gaussian scikit-learn

我想确定一个数据点属于一组数据的概率。我读到 sklearn GMM 可以做到这一点。我尝试了以下....

import numpy as np
from sklearn.mixture import GMM

training_data = np.hstack((
    np.random.normal(500, 100, 2000).reshape(-1, 1),
    np.random.normal(500, 100, 2000).reshape(-1, 1),
))

# train the classifier and get max score
g = GMM(n_components=1)
g.fit(training_data)
scores = g.score(training_data)
max_score = np.amax(scores)

# create a candidate data point and calculate the probability
# it belongs to the training population
candidate_data = np.array([[490, 450]])
candidate_score = g.score(candidate_data)
Run Code Online (Sandbox Code Playgroud)

从这一点开始,我不知道该怎么做。我读到我必须对对数概率进行归一化,以获得属于一个群体的候选数据点的概率。会不会是这样的……

candidate_probability = (np.exp(candidate_score)/np.exp(max_score)) * 100

print candidate_probability
>>> [ 87.81751913]
Run Code Online (Sandbox Code Playgroud)

这个数字似乎并不合理,但我真的超出了我的舒适区,所以我想我会问。谢谢!

小智 1

您使用的候选概率在统计上不正确。我认为您需要做的是计算样本点仅属于单个高斯函数之一的概率(根据权重和多元累积分布函数(CDF))并对这些概率求和。最大的问题是我找不到一个好的 python 包来计算多元 CDF。除非您能找到一篇论文,否则本文将是一个很好的起点https://upload.wikimedia.org/wikipedia/commons/a/a2/Cumulative_function_n_Dimensional_Gaussians_12.2013.pdf