Asked by:
Using BPM Sparse

Question
-
I was wondering if anyone could provide an example of using the BPMSparse method with the "WillBuy" example. I'm using the "willbuy" example because my model is similar to it, however, the main difference is that I'm using a few additional string identifiers (1 of N coded). Because of these string identifiers I've gone down the path of using BPMSparse as my classifier. However, I'm confused on how to incorporate BPMSparse into the "willbuy" example.
Friday, March 9, 2012 3:12 PM
All replies
-
The following should give you some idea. Create a new console project, reference Infer.NET and the BayesPointMachine dll from building the BPM project. I have just shown code for the trainer. Do a similar thing for the test data except test data is not indexed by class.
John
using System; using System.Collections.Generic; using System.Linq; using System.Text; using MicrosoftResearch.Infer; using MicrosoftResearch.Infer.Distributions; using MicrosoftResearch.Infer.Factors; using MicrosoftResearch.Infer.Models; using BayesPointMachine; namespace willbuy { class Program { static void Main(string[] args) { double[] incomes = { 63, 16, 28, 55, 22, 20 }; double[] ages = { 38, 23, 40, 27, 18, 40 }; bool[] willBuy = { true, false, true, true, false, false }; // Class 0 will be true, Class 1 will be false int numData = incomes.Length; int nClass = 2; var indexList = new List<List<int>>[nClass]; var valueList = new List<List<double>>[nClass]; for (int classX = 0; classX < nClass; classX++) { indexList[classX] = new List<List<int>>(); valueList[classX] = new List<List<double>>(); } // Build the data in form of indices and values for (int dataX = 0; dataX < numData; dataX++) { List<int> indx = new List<int>(); List<double> vals = new List<double>(); // income index and value indx.Add(0); vals.Add(incomes[dataX]); // age index and value indx.Add(1); vals.Add(ages[dataX]); // bias input and value indx.Add(2); vals.Add(1.0); // The label int classX = willBuy[dataX] ? 0 : 1; indexList[classX].Add(indx); valueList[classX].Add(vals); } // Get data into array form int[][][] indices = indexList.Select(c => c.Select(d => d.Select(i => i).ToArray()).ToArray()).ToArray(); double[][][] values = valueList.Select(c => c.Select(d => d.Select(v => v).ToArray()).ToArray()).ToArray(); BPMSparse bpmSparse = new BPMSparse(nClass, 3, 0.1); var wInferred = bpmSparse.Train(indices, values); } } }
Friday, March 9, 2012 9:15 PMOwner -
Hi John,
Thanks for the quick reply. I started to look at your trainer above and the test version from the TestBPM project. And added the below test set from the above code:
double[] incomes = { 63, 16, 28, 55, 22, 20 }; double[] ages = { 38, 23, 40, 27, 18, 40 }; bool[] willBuy = { true, false, true, true, false, false }; // Class 0 will be true, Class 1 will be false int numData = incomes.Length; int nClass = 2; var indexList = new List<List<int>>[nClass]; var valueList = new List<List<double>>[nClass]; for (int classX = 0; classX < nClass; classX++) { indexList[classX] = new List<List<int>>(); valueList[classX] = new List<List<double>>(); } // Build the data in form of indices and values for (int dataX = 0; dataX < numData; dataX++) { List<int> indx = new List<int>(); List<double> vals = new List<double>(); // income index and value indx.Add(0); vals.Add(incomes[dataX]); // age index and value indx.Add(1); vals.Add(ages[dataX]); // bias input and value indx.Add(2); vals.Add(1.0); // The label int classX = willBuy[dataX] ? 0 : 1; indexList[classX].Add(indx); valueList[classX].Add(vals); } // Get data into array form int[][][] indices = indexList.Select(c => c.Select(d => d.Select(i => i).ToArray()).ToArray()).ToArray(); double[][][] values = valueList.Select(c => c.Select(d => d.Select(v => v).ToArray()).ToArray()).ToArray(); BPMSparse bpmSparse = new BPMSparse(nClass, 3, 0.1); var wInferred = bpmSparse.Train(indices, values); //test............................................................. int[][] indicesTestData = new int[2][]; double[][] valuesTestData = new double[2][]; indicesTestData[0] = new int[] { 0 }; indicesTestData[1] = new int[] { 1 }; valuesTestData[0] = new double[] { 58, 18, 22 }; valuesTestData[1] = new double[] { 36, 24, 37 }; Console.WriteLine("\nPredictions:"); Discrete[] predictions = bpmSparse.Test(indicesTestData, valuesTestData); foreach (Discrete pred in predictions) Console.WriteLine(pred); Console.ReadLine();
The output is:
Discrete(0.9943 0.00566)
Discrete(0.04148 0.9585)How do the above probabilities correspond to person 1, person 2, and person 3 from my test set?
Monday, March 12, 2012 7:44 PM