EM and GMM(Code)
In EM and GMM(Theory), I have introduced the theory of em algorithm for gmm. Now lets practice it in matlab!
1. Generate 1000 pieces of random 2-dimention data which obey 5 gaussian distribution.
function X = GenerateData
Sigma = [1, 0; 0, 1];
mu1 = [1, -1];
x1 = mvnrnd(mu1, Sigma, 200);
mu2 = [5.5, -4.5];
x2 = mvnrnd(mu2, Sigma, 200);
mu3 = [1, 4];
x3 = mvnrnd(mu3, Sigma, 200);
mu4 = [6, 4.5];
x4 = mvnrnd(mu4, Sigma, 200);
mu5 = [9, 0.0];
x5 = mvnrnd(mu5, Sigma, 200);
% obtain the 1000 data points to be clustered
X = [x1; x2; x3; x4; x5];
end
2. Complete em algorithm.
function [Mu, Sigma, Pi, r_nk] = EmForGmm(Data, classNum, initVal)
% Data : Matrix(n * d), n is the quantity of the data and d is the data
% dimention
% classNum : Scale
% initVal : Cell(3 * 1), initial value for Mu, Sigma and Pi
% cell 1: Mu
% cell 2: Sigma
% cell 3: Pi
[sampleNum, sampleDim] = size(Data);
indexPoint = zeros(sampleNum, 1);
while(1)
for n = 1 : sampleNum
x = Data(n, :);
px_nk_sumk = 0;
for k = 1 : classNum
Sigma_k = initVal{2}(:,:,k);
Mu_k = initVal{1}(k,:);
Pi_k = initVal{3}(k);
px(n,k) = (1/(2*pi^(sampleDim/2)*det(Sigma_k)^(0.5))) ...
* exp(-0.5 * (x - Mu_k)*inv(Sigma_k)*(x - Mu_k)');
px_nk_sumk = px_nk_sumk + Pi_k * px(n, k);
end
for k = 1 : classNum
Sigma_k = initVal{2}(:,:,k);
Mu_k = initVal{1}(k,:);
Pi_k = initVal{3}(k);
r(n, k) = Pi_k * px(n, k) / px_nk_sumk;
end
end
Nk = sum(r)';
newMuK = r' * Data;
Nkk = repmat(Nk,1,2);
newMuK = newMuK ./ Nkk;
for i = 1 : classNum
nk = Nk(i);
MuT = repmat(newMuK(i,:),sampleNum,1);
xT = Data - MuT;
rT = r(:,i);
rT = repmat(rT,1,2);
newSigma(:,:,i) = xT' * (xT .* rT) / nk;
end
newPiK = Nk / sampleNum;
indexPointT = indexPoint;
[aa,indexPoint] = max(r,[],2);
j1 = sum(sum(abs(newMuK - initVal{1}))) < 1e-6;
j2 = sum(sum(sum(abs(newSigma - initVal{2})))) < 1e-6;
j3 = sum(abs(newPiK - initVal{3})) < 1e-6;
clf;
if (j1 && j2 && j3)
for i = 1:sampleNum
if (indexPoint(i)==1)
plot(Data(i,1), Data(i,2), 'r.')
end
if (indexPoint(i)==2)
plot(Data(i,1), Data(i,2), 'b.')
end
if (indexPoint(i)==3)
plot(Data(i,1), Data(i,2), 'k.')
end
if (indexPoint(i)==4)
plot(Data(i,1), Data(i,2), 'g.')
end
if (indexPoint(i)==5)
plot(Data(i,1), Data(i,2), 'm.')
end
hold on;
end
break;
else
initVal{1} = newMuK;
initVal{2} = newSigma;
initVal{3} = newPiK;
end
end
Mu = newMuK;
Sigma = newSigma;
Pi = newPiK;
r_nk = r;
end
3. Complete main function.
clear,clc,clf
Data = GenerateData;
classNum = 5;
[sampleNum, sampleDia] = size(Data); %% Initial value
% indexNum = floor(1 + (sampleNum - 1) * rand(1,classNum));
indexNum = [50,300,500,700,900];
initMu = Data(indexNum,:); initSigmaT = [1 0.2;0.2 1];
initSigma = zeros(2,2,classNum);
for i = 1 : classNum
initSigma(:,:,i) = initSigmaT;
initPi(i,1) = 1 / classNum;
end
initVal = cell(3,1);
initVal{1} = initMu;
initVal{2} = initSigma;
initVal{3} = initPi; %% EM algorithm
[Mu, Sigma, Pi, r_nk] = EmForGmm(Data, classNum, initVal);
4. Result.
The cluster result can be show as figure 3.
Figure 3
The probality distribution function can be writen as:
\[ p(\mathbf{x}) = \sum_{k=1}^{K}\pi_kp(\mathbf{x}|\mu_k\Sigma_k) \]
where,
$\mu_1 = (1.028, -1.158) $, $\mu_2 = (5.423, -4.538) $, $\mu_3 = (1.036, 3.975) $, $\mu_4 = (5.835, 4.474) $, $\mu_5 = (9.074, -0.063) $
Notice that, when generate the data:
$\mu_1 = (1, -1) $, $\mu_2 = (5.5, -4.5) $, $\mu_3 = (1, 4) $, $\mu_4 = (6, 4.5) $, $\mu_5 = (9, 0) $)
\[
\Sigma_1 = \left(
\begin{array}{cc}
1.0873& 0.0376\\
0.0376& 0.8850
\end{array}
\right),
\Sigma_2 = \left(
\begin{array}{cc}
1.1426& 0.0509\\
0.0509& 0.9192
\end{array}
\right),
\Sigma_3 = \left(
\begin{array}{cc}
0.9752& -0.0712\\
-0.0712& 0.9871
\end{array}
\right),
\Sigma_4 = \left(
\begin{array}{cc}
1.0111& -0.0782\\
-0.0782& 1.2034
\end{array}
\right),
\Sigma_5 = \left(
\begin{array}{cc}
0.8665& -0.1527\\
-0.1527& 0.9352
\end{array}
\right)
\]
Notice that, when generate the data:
\[\Sigma = \left(
\begin{array}{cc}
1& 0\\
0& 1
\end{array}
\right)
\]
$\pi_1 = 0.1986$, $\pi_2 = 0.2004 $, $\pi_3 = 0.1992$, $\pi_4 = 0.2015 $, $\pi_5 = 0.2002$
Notice that, when generate the data: each guassian components occupy 20% of all data. (1000 data point, 200 for each guassian components)
EM and GMM(Code)的更多相关文章
- EM and GMM(Theory)
Part 1: Theory 目录: What's GMM? How to solve GMM? What's EM? Explanation of the result What's GMM? GM ...
- 微信小程序API登录凭证(code),获得的用户登录态拥有一定的时效性
调用接口获取登录凭证(code)进而换取用户登录态信息,包括用户的唯一标识(openid) 及本次登录的 会话密钥(session_key).用户数据的加解密通讯需要依赖会话密钥完成. OBJECT参 ...
- 三维卷积:全景图像Spherical CNNs(Code)
卷积神经网络(CNN)可以很好的处理二维平面图像的问题.然而,对球面图像进行处理需求日益增加.例如,对无人机.机器人.自动驾驶汽车.分子回归问题.全球天气和气候模型的全方位视觉处理问题. 将球形信号的 ...
- css里px em rem特点(转)
1.px特点: 1.IE无法调整px作为单位的字体大小: 2.Firefox能够调整px.em和rem. px是像素,是相对长度单位,是相对于显示器屏幕分辨率而言的. 2.em特点: 1.em的值并不 ...
- PCL:描述三维离散点的ROPS特征(Code)
前言: 三维点云为三维欧式空间点的集合.对点云的形状描述若使用局部特征,则可分为两种:固定世界坐标系的局部描述和寻找局部主方向的局部描述,ROPS特征为寻找局部主方向的特征描述. 1.寻找主方向(对X ...
- Python:Matplotlib 画曲线和柱状图(Code)
原文链接:http://blog.csdn.net/ikerpeng/article/details/20523679 参考资料:http://matplotlib.org/gallery.html ...
- Qt:&OpenCV—Q图像处理基本操作(Code)
原文链接:http://www.cnblogs.com/emouse/archive/2013/03/31/2991333.html 作者写作一系列:http://www.cnblogs.com/em ...
- 编码(Code)
很遗憾,直接搜索Code或者编码是很难得找到这本书的,我也是无意中才接触到本书. 第一次读本书,对各方面的知识都不算很懂,觉得很多地方都写的太多浅显,好像本该就是这样子,一个编码系统说的那么麻烦干嘛, ...
- px和em的区别(转)
在国内网站中,包括三大门户,以及“引领”中国网站设计潮流的蓝色理想,ChinaUI等都是使用了px作为字体单位.只有百度好歹做了个可调的表率.而 在大洋彼岸,几乎所有的主流站点都使用em作为字体单位, ...
随机推荐
- STM32驱动TEA5767收音机模块
Tea5767是飞利浦公司出的一款集成化的收音机芯片,大四的时候机缘巧合遇到了这个芯片,用了一下,写点资料 主要特性 TEA5767HN是一款低功耗立体声收音IC,广泛应用于手机MP3 .MP 4 播 ...
- tp框架
<?php namespace Admin\Controller; use Think\Controller; class DengluController extends Controller ...
- IOS开发中长按的手势事件编程
长按手势事件: 长按按钮1S后改变按钮颜色: // 长按事件 #import "ViewController.h" @interface ViewController (){ UI ...
- 升级PHP版本
2014-07-08 今天升级了PHP版本,出现一些错误,解决了. 主要是phpadmin的错误. 升级的目的是为了学习thinkphp.它要求最低5.3版本
- OD提示 "为了执行系统不支持的动作, OllyICE 在这个被调试的程序中注入了一点代码, 但是经过5秒仍未收到响应..." 解决办法
别的OD就可以,我自己整合过的一个很顺手的OD就是不行,最后找到了解决办法: 转自:http://bbs.pediy.com/showthread.PHP?t=97629 -------------- ...
- PHP根据设备类型自动跳转相应网址页面,这个现在实用
现在移动设备上网也很方便,比如Android智能手机,iPhone/iPad等,很多网站都相继推出了针对电脑和这些手机等移动设备访问的网页,如果你的系统是用PHP写的,那面本代码对你会很实用,可根据这 ...
- 差分硬盘的merge(合并差异)实验分析
实验: 1:在c:中新建father.vhd,及其差分硬盘son.vhd,command: diskpart create vdisk file=c:\father.vhd #建立父硬盘 ...
- JDBC oracle 错误总结
ORA-28040: No matching authentication protocol jdk:1.8 oracle:12c 使用ojdbc14.jar 报错:ORA-28040: No mat ...
- Swift之UITabBarController 导航控制器颜色的改变
废话不多 直接上代码 self.window = UIWindow(frame: UIScreen.mainScreen().bounds) self.window!.backgroundColor ...
- Xamarin.Android 入门之:Xamarin+vs2015 环境搭建
一.前言 此篇博客主要写了如何使用搭建xamarin开发的环境,防止我自己万一哪天电脑重装系统了,可以直接看这篇博客. 二.准备工作 在安装之前需要下载好xamarin所需要的软件并一个个安装他们: ...