python - 具有离散数据的高斯混合模型
问题描述
我有 136 个数字,它们具有 8 个高斯分布的重叠分布。我想找到它的平均值,以及每个高斯分布的方差!你能发现我的代码有什么错误吗?
file = open("1.txt",'r') #data is in 1.txt like 0,0,0,0,0,0,1,0,0,1,4,4,6,14,25,43,71,93,123,194...
y=[int (i) for i in list((file.read()).split(','))] # I want to make list which element is above data
x=list(range(1,len(y)+1)) # it is x values
z=list(zip(x,y)) # z elements consist as (1, 0), (2, 0), ...
因此,通过上述过程,对于以第一个给定数据为y值的xy平面上的136个点(x,y),得到了以此为元素的列表z。现在我想获得每个高斯分布的均值,方差。此时,基本假设是给定数据由重叠的 8 个高斯分布组成。
import numpy as np
from sklearn.mixture import GaussianMixture
data = np.array(z).reshape(-1,1)
model = GaussianMixture(n_components=8).fit(data)
print(model.means_)
file.close()
实际上,我不知道如何制作代码来打印 8 种均值和方差...任何人都可以帮助我吗?
解决方案
您可以使用它,我为您的可视化制作了示例代码 -
import numpy as np
from sklearn.mixture import GaussianMixture
import scipy
import matplotlib.pyplot as plt
%matplotlib inline
#Sample data
x = [0,0,0,0,0,0,1,0,0,1,4,4,6,14,25,43,71,93,123,194]
num_components = 2
#Fit a model onto the data
data = np.array(x).reshape(-1,1)
model = GaussianMixture(n_components=num_components).fit(data)
#Get list of means and variances
mu = np.abs(model.means_.flatten())
sd = np.sqrt(np.abs(model.covariances_.flatten()))
#Plotting
extend_window = 50 #this is for zooming into or out of the graph, higher it is , more zoom out
x_values = np.arange(data.min()-extend_window, data.max()+extend_window, 0.1) #For plotting smooth graphs
plt.plot(data, np.zeros(data.shape), linestyle='None', markersize = 10.0, marker='o') #plot the data on x axis
#plot the different distributions (in this case 2 of them)
for i in range(num_components):
y_values = scipy.stats.norm(mu[i], sd[i])
plt.plot(x_values, y_values.pdf(x_values))
推荐阅读
- java - 是否有任何功能可以将 CIDR 块拆分为 IP 开始和 IP 结束而不在雪花中使用 Parse_IP
- wordpress - 为什么添加新菜单项时 wordpress 导航菜单层次结构会中断?
- c# - WPF TabControl 标头问题
- c# - ReactiveUI 是否提供类似 PRISM ViewModelLocator 的服务?
- excel - VBA Excel中的编译错误“变量未定义”
- blazor - Blazor 自定义身份验证状态提供程序
- android - 'npx react-native bundle --platform android --dev false...' 为什么我每次都需要运行这个来更新我的发布 APK?
- react-native - 来自函数的 React Native useState 更改不会重新渲染组件
- toggle - 手风琴关闭问题
- reactjs - 如何隐藏 Ant Design 4 日期选择器年份?