python - 使用 sklearn GMM 计算概率

标签 python statistics scikit-learn gaussian

我想确定一个数据点属于一组数据的概率。我读到 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)

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

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

print candidate_probability
>>> [ 87.81751913]

这个数字似乎并不合理,但我真的不在我的舒适区,所以我想问一下。谢谢!

最佳答案

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

关于python - 使用 sklearn GMM 计算概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27476980/

相关文章:

java - Java的量化金融/数学库

已作为非阻塞打开的管道上的 Python readline

mysql - ORDER BY RAND() 似乎不那么随机

r - 如何在R中反转反双曲正弦变换?

python - 从 TFIDFVectorizer/CountVectorizer 减少词向量的维度

python - 如何将 sklearn "LinearRegression"与列表一起使用?

python - pandas 将函数列表应用于数据框

python - 迭代 .json 文件并更改属性值

Python datetime 使用 timedelta 减去 1 天

python - 发生异常:AttributeError模块'calendar'没有属性'FIRDAY'