HMM的Viterbi算法C#实现
发布时间:2020-12-15 17:56:11 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Viterbi { class Program { const int m = 2; /
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Viterbi { class Program { const int m = 2; //隐含状态数 const int n = 3; //观察状态数 static int[] sArray = new int[n]; //隐含状态序列 static int[] oArray = { 0,1,2 }; //可观察状态序列 static double[] iArray = { 0.6,0.4 };//初始概率矩阵 static double[,] aArray = { { 0.7,0.3 },{ 0.4,0.6 } };//转移概率矩阵 static double[,] bArray = { { 0.5,0.4,0.1 },{ 0.1,0.3,0.6 } };//发射概率矩阵 static double[,] resultArray = new double[n,m];//保存结果 static void Main(string[] args) { Viterbi(sArray,oArray,iArray,aArray,bArray); foreach (int i in sArray) { Console.Write(i); } Console.ReadKey(); } static void Viterbi(int[] s,int[] o,double[] i,double[,] a,] b) { for (int j = 0; j < m; j++) { resultArray[0,j] = iArray[j] * bArray[j,oArray[0]]; } for (int j = 1; j < n; j++) { for (int p = 0; p < m; p++) { for (int k = 0; k < m; k++) { double tmp = resultArray[j - 1,p] * aArray[p,k] * bArray[k,oArray[j]]; if (tmp > resultArray[j,k]) resultArray[j,k] = tmp; } } } for (int j = 0; j < n; j++) { double tmp = resultArray[j,0]; sArray[j] = 0; for (int p = 0; p < m; p++) { if (resultArray[j,p] > tmp) sArray[j] = p; } } } } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |