private void OnHostPick() { if (state != 1) { return; } // Host cannot pick the user pick, or the car position int hc = rnd.Next(3); while (hc == p.ObservedValue || hc == actualCarPosition) { hc = rnd.Next(3); } doAnimation("opendoor" + (hc + 1)); doAnimation("showgoatg" + (hc + 1)); h.ObservedValue = hc; hostIsObserved.ObservedValue = true; cProbs = getProbs(c); string fmtStr = "{0:0.0000}"; Lbl1.Content = String.Format(fmtStr, cProbs[0]); Lbl2.Content = String.Format(fmtStr, cProbs[1]); Lbl3.Content = String.Format(fmtStr, cProbs[2]); UpdateDoors(cProbs); label1.Content = "P ( Car Inside )"; button2.Visibility = Visibility.Hidden; state = 2; }
private void UpdateDoors(Microsoft.ML.Probabilistic.Math.Vector cProbs) { for (int a = 0; a < 3; a++) { if (cProbs[a] == 1) { doAnimation("showcarg" + (a + 1)); doAnimation("opendoor" + (a + 1)); } } }
private void BeforeHostPick() { h.ClearObservedValue(); hostIsObserved.ObservedValue = false; Microsoft.ML.Probabilistic.Math.Vector cProbs = getProbs(c); Microsoft.ML.Probabilistic.Math.Vector hProbs = getProbs(h); string fmtStr = "{0:0.0000}\n{1:0.0000}"; Lbl1.Content = String.Format(fmtStr, cProbs[0], hProbs[0]); Lbl2.Content = String.Format(fmtStr, cProbs[1], hProbs[1]); Lbl3.Content = String.Format(fmtStr, cProbs[2], hProbs[2]); UpdateDoors(cProbs); state = 1; }
/// <summary> /// 根据均值和协方差数据生成高斯对象 /// </summary> public void InitGaussian() { if (gaussian != null) { return; } Microsoft.ML.Probabilistic.Math.Vector mean = null; Microsoft.ML.Probabilistic.Math.PositiveDefiniteMatrix covar = null; try { mean = this.means.toMathVector(); covar = new Microsoft.ML.Probabilistic.Math.PositiveDefiniteMatrix(covariance); gaussian = VectorGaussian.FromMeanAndVariance(mean, covar); checkGaussian(); } catch (Exception e) { //这里异常的主要原因是得到的协方差矩阵不是正定矩阵。 //This happens if the diagonal values of the covariance matrix are (very close to) zero. //A simple fix is add a very small constant number to c. //logger.Error(e.Message); for (int i = 0; i < covariance.GetLength(0); i++) { for (int j = 0; j < covariance.GetLength(1); j++) { if (i == j) { covariance[i, j] += 0.001; } //if (i == j && covariance[i, j] < 0) covariance[i, j] += 0.01; //if (i != j) covariance[i,j] = 0; } } covar = new Microsoft.ML.Probabilistic.Math.PositiveDefiniteMatrix(covariance); gaussian = VectorGaussian.FromMeanAndVariance(mean, covar); checkGaussian(); } }
/// <summary> /// 高斯分布概率值 /// </summary> /// <param name="values"></param> /// <returns></returns> public double Prob(List <Vector> values) { this.InitGaussian(); Microsoft.ML.Probabilistic.Math.Vector v = values.toMathVector(); return(Math.Exp(gaussian.GetLogProb(v))); }
internal bool Reclassify() { if (classifying) { return(false); } classifying = true; List <Item> positive = new List <Item>(); List <Item> negative = new List <Item>(); foreach (Item item in Items) { if (item.State == 1) { positive.Add(item); } if (item.State == -1) { negative.Add(item); } } int count = positive.Count + negative.Count; if (count == 0) { Reset(); classifying = false; return(false); } Vector[] trainData = new Vector[count]; bool[] trainLabels = new bool[count]; Vector[] testData = new Vector[Items.Count - count]; int trainCount = 0, testCount = 0; foreach (Item item in Items) { if (item.State != 0) { trainData[trainCount] = item.Data; trainLabels[trainCount] = item.State == 1; trainCount++; } else { testData[testCount++] = item.Data; } } form1.bpm.Train(trainData, trainLabels); double[] probs = form1.bpm.Test(testData); testCount = 0; foreach (Item item in Items) { if (item.State == 0) { item.probTrue = probs[testCount++]; } } classifying = false; return(true); }
public Item(string filename, Vector data) { this.Filename = filename; this.Data = data; }