4

如何修改Infer.NET教程 4:贝叶斯点机器以包含更多结果?

例如,如何添加 willRent 并获得 willBuy 和 willRent 的单独概率?

        double[] incomes = { 63, 16, 28, 55, 22, 20 };
        double[] ages = { 38, 23, 40, 27, 18, 40 };
        bool[] willBuy = { true, false, true, true, false, false };
        bool[] willRent = { false, false, true, false, true, false };

编辑 - 这是复制/粘贴格式的示例:

static void Main()
{
    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 x vector, augmented by 1
    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);

    // Create target y
    VariableArray<bool> y = Variable.Observed(willBuy, x.Range);

    Variable<Vector> w = Variable.Random(new VectorGaussian(Vector.Zero(3), PositiveDefiniteMatrix.Identity(3)));
    Range j = y.Range;
    double noise = 0.1;
    y[j] = Variable.GaussianFromMeanAndVariance(Variable.InnerProduct(w, x[j]), noise) > 0;

    InferenceEngine engine = new InferenceEngine(new ExpectationPropagation());
    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));
    BayesPointMachine(incomesTest, agesTest, Variable.Random(wPosterior), ytest);
    Console.WriteLine("output=\n" + engine.Infer(ytest));

    Console.ReadKey();
}

static void BayesPointMachine(double[] incomes,double[] ages,Variable<Vector> w,VariableArray<bool> y)
{
    // Create x vector, augmented by 1
    Range j = y.Range;
    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);

    // Bayes Point Machine
    double noise = 0.1;
    y[j] = Variable.GaussianFromMeanAndVariance(Variable.InnerProduct(w, x[j]), noise) > 0;
}
4

1 回答 1

1

这里有 4 个变量。

                   Will Buy?
                  ^        ^
                 /          \
                Age        Income
                 \          /
                  v        v
                   Will Rent?

给定(Age,Income)和是自变量Will BuyWill Rent条件独立

这样您就可以构建两个单独的贝叶斯点机器:

  • 一个为Age和。IncomeWill Rent
  • 一个为Age和。IncomeWill Buy
于 2012-11-20T19:03:21.927 回答