EDA365电子论坛网
标题:
高斯混合模型里面的参数可以用到混核主元分析里面吗?
[打印本页]
作者:
小小鲁班
时间:
2020-12-24 11:27
标题:
高斯混合模型里面的参数可以用到混核主元分析里面吗?
高斯混合模型里面的参数可以用到混核主元分析里面吗?有混合概率主元分析的EM算法的程序吗
! ]' P8 e' H8 [. M" R j
function ppca_mixture
( o+ ` ]+ B0 z+ |- F) Q* b- n6 S
filename = 'virus3.dat';
8 ~* h t$ V9 g5 ^
T = importdata(filename);
" d) N$ V* z7 d5 b
[N, d] = size(T);
+ Z: @2 s2 e: U0 p
M = 3; % number of ppca analysers considered 考虑的ppca分析仪数量
" P8 I) |; n- \8 i+ @- ?* o6 g
q = 2; % dimension of ppcas ppca的维数
% p& O" Z9 H: X, m8 Y5 H
% init
- H- s" }2 Y, [& {6 l. T
% initializing the posteriors (p(i|t_n), indexed by (n, i). R in text)
. j9 v T# G6 m" S: p8 A6 S0 J
%初始化后验(p(i|t_n),在文本中由(n, i). R索引
" J" P4 I3 h7 q2 n b( u1 I. _# N
classes = [3, 3, 3, 3, 2, 2, 3, 1, 3, 3, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2];
0 t9 d7 w/ p# r9 F; l
for n=1:N
7 p/ B' p! p. x: v
for i=1:M
. u- G6 E3 Q$ T# D
if(i==classes(n))
; G) A }% {( a7 C- M3 [4 `
posteriors(n, i) = 1;
8 U8 ~- J3 L$ [
else
2 R2 A0 r. p0 ~
posteriors(n, i) = 0;
# }3 K. @5 G, w, [4 E
end
9 ]' E+ Q7 R: n4 }% Q e
end
0 v$ c& G& _* h' K, h
end
. \. r Y' d% a; H- \# c( p# p
% precision for convergence checking 收敛检验精度
% N: s6 G, r- G( E
epsilon = 0.01;
. ?1 W3 M" u% ^( e8 @
entered = false;
0 k& T! T0 G2 T U: y
$ \$ o! C- W t2 S% E) f1 Q6 j1 a
% loop
! R, x7 c* w# r' }4 S! e
while(true)
' V6 m Z# q9 B. r
% updating priors (p(i), pi in text) and mean vectors (mu in text)
4 ?. \, x. s( Q/ N9 W
% 更新先验(p(i),文本中的pi)和平均向量(文本中的mu)
' W+ S E5 z) X
new_priors = 1/N * sum(posteriors);
9 v) i9 ?% F! F
new_mus = zeros(M, d);
! z8 F- B$ l( z: I6 h
for i=1:M
* X# P' s ]# y1 G4 Y# q
for n=1:N
4 j/ y& r( M' g9 ~) u
new_mus(i,
= new_mus(i,
+ posteriors(n, i) * T(n,
;
& |# O* ]$ B9 F3 @" L, e& F$ |
end
8 E/ s7 c- p3 ?5 {
new_mus(i, :) = new_mus(i, :) / sum(posteriors(:, i));
6 ?0 F: L( E+ I
end
$ x7 S# s W( L' w; i
3 S7 Q! v/ W3 e# f% A. p; K7 S1 W# l
% computing covariance matrices (S in text)计算协方差矩阵(文本中的S)
( S9 `. m3 x+ E# g, Q
covariances = cell(M);
9 E3 z; z7 ~! V" q( {) E i0 M. \
for i=1:M
3 B/ P" m( u$ ] E% @
covariances{i} = zeros(d, d);
8 H" `/ [, J( Y9 U$ d9 d
for n=1:N
9 {! t3 ]* Z; L+ C4 G9 `! N7 J7 j
covariances{i} = covariances{i} + posteriors(n, i)*(T(n, :)' - new_mus(i, :)')*(T(n, :)' - new_mus(i, :)')';
Y; X- x4 |8 e( s7 Y
end
1 d l7 F% z4 \- x
covariances{i} = covariances{i} / (new_priors(i) * N);
2 {) N3 g$ ^0 E5 H) q- O
end
: m1 k& S7 t6 X+ w3 |
4 X$ E; i l7 v8 r0 V
5 \8 {) ?- D+ O: \: `* D
% applying ppca using covariance matrices 使用协方差矩阵应用ppca
# f- j3 ]: M; ^1 ~4 {% m
% (Ws are weight matrices; sigmas are variances) (Ws为权重矩阵;(差异)
8 r+ a7 b! @$ N$ U
new_Ws = cell(M);
" s. C# H M4 i0 l, `
for i=1:M
$ ~9 q# f) J8 \2 h. T# I
[new_Ws{i}, new_sigmas{i}] = ppca_from_covariance(covariances{i}, q);
$ z2 o4 Z* U( o! {0 k6 q
end
8 m) [, e7 K& |+ m( t
. G* g: \* E6 j4 U2 ^ F
% convergence check 收敛性检查
$ c: {' w' o5 g2 l" ~' L
if(entered && max(abs(new_priors - priors)) < epsilon && max(max(abs(new_mus - mus))) < epsilon && max(max(max(abs(cell2mat(new_Ws) - cell2mat(Ws))))) < epsilon && max(abs(new_sigmas - sigmas)) < epsilon)
! x7 x( S. W: W+ `5 o
break;
9 A8 j% N* p" T& j0 B& s$ e. s
end
; Z& W( q1 X* D3 l5 b, {
: X# f5 O$ G% x# z' O; \% [
% replacing old parameter values 替换旧的参数值
3 o+ s B% G. K }% b8 [
priors = new_priors;
) [! c/ q% `! F2 d, x: l, k$ S
mus = new_mus;
0 U* M& c$ g3 b- B' V
Ws = new_Ws;
3 D) R; D4 v1 t( Z: `% G
sigmas = new_sigmas;
/ h) R5 A3 p1 B. M; a4 c* N
1 R+ M4 j Q) X, U& x
% computing the likelihoods (p(t_n|i)) 计算概率(p(t_n|i))
! q8 n5 N' Y2 q) Y" q
for i=1:M
( k" [6 z3 z! ^. E
C = sigmas{i}^2 * eye(d) + Ws{i}*Ws{i}';
7 u, ]# J' R! ~
detC = det(C);
' W; j9 X x' V) _) v5 H5 a6 R
invC = inv(C);
# @: h$ m6 \$ b+ y6 F
for n=1:N
5 H4 ~ x+ H3 j
likelihoods(n, i) = (2*pi)^(-d/2)*detC^(-1/2)*exp(-1/2*(T(n, :)' - mus(i, :)')'*invC*(T(n, :)' - mus(i, :)'));
4 m$ o }) d# K/ v# N
end
0 r& x9 M, ]" w: q7 C* m) p
end
! N0 i+ @ B7 E }# C- w" x
% computing the joint probabilities (p(t_n, i)) 计算联合概率(p(t_n, i))
/ C1 t( A* _8 t$ d
for i=1:M
# M" b. h# L2 L6 l0 i! |; ~ c: E# i
joint(:, i) = likelihoods(:, i) * priors(i);
S7 i5 `) d; V! P( Z9 r1 ?
end
# T i3 F+ x' j, o+ c: ]
% computing the data priors (p(t_n)) 计算数据先验(p(t_n))
6 _& W9 J1 ?" D% ~2 B0 m! Y6 y
data_priors = sum(joint');
" x+ @5 D6 p' Y$ [- J
% computing and displaying the likelihood of the data set 计算和显示数据集的可能性
4 }2 I7 u1 \8 ]& B1 p2 ~' b
disp(sum(log(data_priors)));
9 U9 ]/ u7 B! _/ D7 Z( k0 Y
% computing the posteriors (p(i|t_n), indexed by (n, i). R in text)
0 A2 x# E; P) k( g
% 计算后验(p(i|t_n),用(n, i)索引。R在文本中)
2 e5 n8 Z& G$ W1 @: r
for n=1:N
S1 `) c' u, Y2 Q$ ]! D9 K! Z
posteriors(n, :) = joint(n, :) / data_priors(n);
$ C* B* _6 F) Q' O' t/ w* u
end
6 h( J4 i, b& i: t
' r, x0 O1 g. T. s9 I( \. y P
% we went through the loop at least once 我们至少做了一次循环
$ g6 r3 `8 D0 u5 u1 V
entered = true;
) |2 j2 o5 y5 Y6 c/ v# H
end
I) K% r$ f0 s
3 G! T4 u. T9 ~# o) d
# f- \2 Z1 Y, N/ f( k% W
% computing the latent variables 计算潜变量
8 u! g2 _2 \) x" |
latent = cell(M);
7 W( l7 y" w; S9 {0 A0 k
for i=1:M
% G) ^% r" p/ h) z$ Y: g
latent{i} = ppca_latent(T, Ws{i}, sigmas(i));
- y( x6 P3 o. T6 \8 C
end
0 H' m& ` b; u2 H6 h
% selecting likely points for each ppca analyser 为每个ppca分析器选择可能的点
8 M+ _' W) M1 |
for n=1:N
3 R8 E+ ~* o& Z( J6 ]: [3 p
[~, i] = sort(posteriors(n, :), 'descend');
$ p& u1 l& r& k
points_to_classes(n) = i(1);
$ D2 d0 j& L- A6 c- |
end
_) U! V/ U9 X
classes_to_points = cell(M);
3 E0 l ?( i; B4 ~5 F0 d
classes_to_point_numbers = cell(M);
4 U2 j0 U9 k L! s. P; s9 b
for n=1:N
' x' k) e% u# i2 U+ D v7 ?
classes_to_points{points_to_classes(n)}(:, end+1) = latent{points_to_classes(n)}(:, n);
' ]! N- w7 A, c! s" m# p. g) D
classes_to_point_numbers{points_to_classes(n)}(end+1) = n;
. F. T- l. j0 Y8 U
end
3 F3 h! [5 G/ a" m2 w
. X( q7 t0 l( ]- W1 A
% display the results of the automatic classification 显示自动分类结果
* ^8 | ~6 J J
for i=1:M
2 E: q. ^* q( y9 Q( L8 ?
disp(classes_to_point_numbers{i});
1 R/ a' L; \: h" M0 [* D- G' j
end
# ^7 G; O& i {) a" d
& r' a; \; p, P A* M
ppca_plot2d(classes_to_points{1});
8 K/ d5 Q- M. q- q/ X
end
( U+ Z, S, U# J, O7 v" k* Q
作者:
zzz.dan
时间:
2020-12-24 13:13
帮你顶一下
作者:
kekek
时间:
2020-12-24 13:28
作者:
zaiyiaaaa
时间:
2020-12-24 13:35
欢迎光临 EDA365电子论坛网 (https://bbs.eda365.com/)
Powered by Discuz! X3.2