-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBayesPointMachineExample.cs
More file actions
executable file
·62 lines (54 loc) · 2.6 KB
/
BayesPointMachineExample.cs
File metadata and controls
executable file
·62 lines (54 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Microsoft.ML.Probabilistic.Models;
using Microsoft.ML.Probabilistic.Distributions;
using Microsoft.ML.Probabilistic.Math;
namespace Microsoft.ML.Probabilistic.Tutorials
{
[Example("Tutorials", "Demonstrates how to train and apply a Bayes Point Machine classifier.", Prefix = "4.")]
public class BayesPointMachineExample
{
public void Run()
{
// data
double[] incomes = { 63, 16, 28, 55, 22, 20 };
double[] ages = { 38, 23, 40, 27, 18, 40 };
bool[] willBuy = { true, false, true, true, false, false };
// Create target y
VariableArray<bool> y = Variable.Observed(willBuy).Named("y");
Variable<Vector> w = Variable.Random(new VectorGaussian(Vector.Zero(3), PositiveDefiniteMatrix.Identity(3))).Named("w");
BayesPointMachine(incomes, ages, w, y);
InferenceEngine engine = new InferenceEngine();
if (!(engine.Algorithm is Algorithms.GibbsSampling))
{
VectorGaussian wPosterior = engine.Infer<VectorGaussian>(w);
Console.WriteLine("Dist over w=\n" + wPosterior);
double[] incomesTest = { 58, 18, 22 };
double[] agesTest = { 36, 24, 37 };
VariableArray<bool> ytest = Variable.Array<bool>(new Range(agesTest.Length)).Named("ytest");
BayesPointMachine(incomesTest, agesTest, Variable.Random(wPosterior).Named("w"), ytest);
Console.WriteLine("output=\n" + engine.Infer(ytest));
}
else
{
Console.WriteLine("This model has a non-conjugate factor, and therefore cannot use Gibbs sampling");
}
}
public void BayesPointMachine(double[] incomes, double[] ages, Variable<Vector> w, VariableArray<bool> y)
{
// Create x vector, augmented by 1
Range j = y.Range.Named("person");
Vector[] xdata = new Vector[incomes.Length];
for (int i = 0; i < xdata.Length; i++)
{
xdata[i] = Vector.FromArray(incomes[i], ages[i], 1);
}
VariableArray<Vector> x = Variable.Observed(xdata, j).Named("x");
// Bayes Point Machine
double noise = 0.1;
y[j] = Variable.GaussianFromMeanAndVariance(Variable.InnerProduct(w, x[j]).Named("innerProduct"), noise) > 0;
}
}
}