public void PredictWinner_PositiveWins() { // Given var bags = new[] { new BagTestData(1, 2, new[] { new[] {26.4386, 25.5379} }), new BagTestData(1, 3, new[] { new[] {44.9671, 32.1093} }), new BagTestData(-1, 4, new[] { new[] {20.7886, 17.2379} }) }; var bagToTest = new SparseBag(1, "1", new[] { new Dictionary<int, float> {{1, 22.8529f}, {2, 22.6664f}}, new Dictionary<int, float> {{1, 59.1986f}, {2, 34.5393f}} }); int predictedLabelForOne = PredictBagLabel(bags, bagToTest, 2); // Then Assert.That(predictedLabelForOne, Is.EqualTo(+1)); }
public void PredictWinner_SimpleRuleSet() { // Given var bagsToTrain = new SparseBag[0]; var bagsToTest = new[] { new SparseBag(-1, "1", new[] { new Dictionary<int, float> {{1, 1.0f}, {2, 1.9f}} }), new SparseBag(+1, "2", new[] { new Dictionary<int, float> {{1, 1.0f}, {2, 3.0f}} }) }; var classifier = ClassifierFactory.GetMultiInstanceClassifier("rules:class+1=1>0.9^2>2.0"); // When classifier.Train(bagsToTrain); foreach (var bag in bagsToTest) { var predictedClass = classifier.PredictWinner(bag); // Then Assert.That(predictedClass, Is.EqualTo(bag.Label)); } }
public void PredictWinner_SparseBagsAreSingletons_NegativeWins() { // Given var bags = new[] { new BagTestData(-1, 2, new[] { new[] {26.4386, 25.5379} }), new BagTestData(-1, 3, new[] { new[] {44.9671, 32.1093} }), new BagTestData(+1, 4, new[] { new[] {20.7886, 17.2379} }) }; var bagToTest = new SparseBag(1, "1", new[] { new Dictionary<int, float> {{1, 22.8529f}, {2, 22.6664f}}, new Dictionary<int, float> {{1, 59.1986f}, {2, 34.5393f}} }); var miClassifier = ClassifierFactory.GetMultiInstanceClassifier("mi:model=svm,gamma=123"); // When var predictedLabel = PredictBagLabel(miClassifier, bags, bagToTest); // Then Assert.That(predictedLabel, Is.EqualTo(-1)); }
public void Train_Test_NegativeWins() { // Given var bags = new[] { new BagTestData(+1, 1, new[] { new[] {22.8529, 22.6664}, new[]{59.1986, 34.5393} }), new BagTestData(-1, 2, new[] { new[] {26.4386, 25.5379} }), new BagTestData(-1, 3, new[] { new[] {44.9671, 32.1093} }), new BagTestData(1, 4, new[] { new[] {20.7886, 17.2379} }) }; var bagToTest = new SparseBag(+1, "1", new[] { new Dictionary<int, float> {{1, 44.9671f}, {2, 32.1093f}} }); var predictedLabelForOne = PredictBagLabel(bags, bagToTest, 1); // Then Assert.That(predictedLabelForOne, Is.EqualTo(-1)); }
public void Write_Verify() { var tmpFileName = Path.GetTempFileName(); try { // Given var bag1 = new SparseBag(1, "bag1", new[] {new Dictionary<int, float> {{1, 0.5f}}}); var bag2 = new SparseBag(1, "bag2", new[] { new Dictionary<int, float> { { 1, 0.5f } }, new Dictionary<int, float> { { 2, 0.6f }, { 3, 1.6f } } }); var sparseBagWriter = DatasetWriterFactory.GetDatasetWriter<SparseBag>(tmpFileName); // When sparseBagWriter.Write(bag1); sparseBagWriter.Write(bag2); sparseBagWriter.Dispose(); var sparseBagReader = DatasetReaderFactory.GetDatasetReader<SparseBag>(tmpFileName); var readBag1 = sparseBagReader.Next(); var readBag2 = sparseBagReader.Next(); var readBag3 = sparseBagReader.Next(); // Then Assert.That(readBag1, Is.EqualTo(bag1)); Assert.That(readBag2, Is.EqualTo(bag2)); Assert.That(readBag3, Is.Null); } finally { if(File.Exists(tmpFileName)) { File.Delete(tmpFileName); } } }
private static int PredictBagLabel(IClassifier<SparseBag> classifier, BagTestData[] bags, SparseBag bagToTest) { var sparseBags = BagTestData.ToSparseBags(bags); classifier.Train(sparseBags); return classifier.PredictWinner(bagToTest); }
public void HasValidIstances_True_OnlyNotNullFeatures() { // Given var bag = new SparseBag(1, "1", new[] { new Dictionary<int, float> {{1, 22.8529f}, {2, 22.6664f}} }); // When/Then Assert.That(bag.HasValidInstances, Is.True); }
public void HasValidIstances_False() { // Given var bag = new SparseBag(1, "1", new[] { new Dictionary<int, float> {{1, 0.0f}, {2, 0.0f}} }); // When/Then Assert.That(bag.HasValidInstances, Is.False); }
public void Distance_Reflexive() { // Given var bag1 = new SparseBag(1, "1", new[] { new Dictionary<int, float> {{1, 22.8529f}, {2, 22.6664f}}, new Dictionary<int, float> {{1, 59.1986f}, {2, 34.5393f}} }); // When var distance = m_hausdorffDistance.Distance(bag1, bag1); // Then Assert.That(distance, Is.EqualTo(0.0f)); }
static internal IEnumerable<SparseBag> ToSparseBags(BagTestData[] bags) { var sparseBags = new SparseBag[bags.Length]; for (var bagId = 0; bagId < sparseBags.Length; bagId++) { var istances = new IDictionary<int, float>[bags[bagId].Points.Length]; for (var pointId = 0; pointId < bags[bagId].Points.Length; pointId++) { var j = 1; var features = (from f in bags[bagId].Points[pointId] select f).ToDictionary(i => (j++), i => (float)i); istances[pointId] = features; } sparseBags[bagId] = new SparseBag(bags[bagId].Label, bags[bagId].Name.ToString(), istances); } return sparseBags; }
public void RemoveEmptyIstances_WithNothingToRemove() { // Given var bag = new SparseBag(1, "1", new[] { new Dictionary<int, float> {{1, 22.8529f}, {2, 22.6664f}}, }); var toObtain = new SparseBag(1, "1", new[] { new Dictionary<int, float> {{1, 22.8529f}, {2, 22.6664f}} }); // When var simplifiedIstance = bag.RemoveEmptyIstances(); // Then Assert.That(simplifiedIstance, Is.EqualTo(toObtain)); }
public void PredictWinner_EmptyRuleSet_PositiveClassWins() { // Given var bagsToTrain = new SparseBag[0]; var bagsToTest = new SparseBag(+1, "2", new[] { new Dictionary<int, float> {{1, 1.0f}, {2, 3.0f}} }); var classifier = ClassifierFactory.GetMultiInstanceClassifier("rules:class+1="); // When classifier.Train(bagsToTrain); var predictedClass = classifier.PredictWinner(bagsToTest); // Then Assert.That(predictedClass, Is.EqualTo(bagsToTest.Label)); }
public void Distance_RealCase_1() { var istances1 = new[] { new Dictionary<int, float> { {1, 1} }, new Dictionary<int, float> { {1, 2} }, new Dictionary<int, float> { {1, 3} } }; var bag1 = new SparseBag(0, "a", istances1); var istances2 = new[] { new Dictionary<int, float> { {1, 4} }, new Dictionary<int, float> { {1, 5} }, new Dictionary<int, float> { {1, 6} } }; var bag2 = new SparseBag(0, "a", istances2); // When var distance = m_minHausdorffDistance.Distance(bag1, bag2); // Then Assert.That(distance, Is.EqualTo(1)); }
public void Distance_Zero() { // Given var bag1 = new SparseBag(1, "1", new[] { new Dictionary<int, float> {{1, 1.0f}, {2, 2.0f}}, new Dictionary<int, float> {{1, 100.0f}, {2, 200.0f}} }); var bag2 = new SparseBag(1, "1", new[] { new Dictionary<int, float> {{1, 200.0f}, {2, 100.0f}}, new Dictionary<int, float> {{1, 1.0f}, {2, 2.0f}}, }); // When var distanceA = m_hausdorffDistance.Distance(bag1, bag2); var distanceB = m_hausdorffDistance.Distance(bag2, bag1); // Then Assert.That(distanceA, Is.EqualTo(distanceB)); }
public void Distance_Symmetric() { // Given var bag1 = new SparseBag(1, "1", new[] { new Dictionary<int, float> {{1, 22.8529f}, {2, 22.6664f}}, new Dictionary<int, float> {{1, 59.1986f}, {2, 34.5393f}} }); var bag2 = new SparseBag(1, "1", new[] { new Dictionary<int, float> {{1, 0.0f}, {2, 22.6664f}}, new Dictionary<int, float> {{1, 59.1986f}, {2, 0.0f}} }); // When var distanceA = m_hausdorffDistance.Distance(bag1, bag2); var distanceB = m_hausdorffDistance.Distance(bag2, bag1); // Then Assert.That(distanceA, Is.EqualTo(distanceB)); }
public void PredictWinner_NotBinary() { // Given var bags = new[] { new BagTestData(-1, 2, new[] { new[] {26.4386, 25.5379} }), new BagTestData(1, 3, new[] { new[] {44.9671, 32.1093} }), new BagTestData(2, 4, new[] { new[] {20.7886, 17.2379} }) }; var bagToTest = new SparseBag(-1, "1", new[] { new Dictionary<int, float> {{1, 22.8529f}, {2, 22.6664f}}, new Dictionary<int, float> {{1, 59.1986f}, {2, 34.5393f}} }); PredictBagLabel(bags, bagToTest, 3); }
public void Equals_True() { // Given var bag1 = new SparseBag(1, "1", new[] { new Dictionary<int, float> {{1, 22.8529f}, {2, 22.6664f}}, new Dictionary<int, float> {{1, 59.1986f}, {2, 34.5393f}} }); var bag2 = new SparseBag(1, "1", new[] { new Dictionary<int, float> {{1, 22.8529f}, {2, 22.6664f}}, new Dictionary<int, float> {{1, 59.1986f}, {2, 34.5393f}} }); // When / Then Assert.That(bag1.Equals(bag2), Is.True); }
private static int PredictBagLabel(BagTestData[] bags, SparseBag bagToTest, int K) { var sparseBags = BagTestData.ToSparseBags(bags); // When var citationNearestNeighbor = ClassifierFactory.GetMultiInstanceClassifier("nn:distance=hausdorff,K=" + K); citationNearestNeighbor.Train(sparseBags); return citationNearestNeighbor.PredictWinner(bagToTest); }
public void EqualOperator_False() { // Given var bag1 = new SparseBag(1, "1", new[] { new Dictionary<int, float> {{1, 22.8529f}, {2, 22.6664f}}, new Dictionary<int, float> {{1, 59.1986f}, {2, 34.5393f}} }); var bag2 = new SparseBag(1, "1", new[] { new Dictionary<int, float> {{1, 22.8529f}, {2, 22.6664f}}, new Dictionary<int, float> {{1, 59.1386f}, {2, 34.5393f}} }); // When / Then Assert.That(bag1 != bag2); }
public void Test_Musk1Speed() { // Given const int K = 1; var citationNearestNeighbor = ClassifierFactory.GetMultiInstanceClassifier("nn:distance=hausdorff,K=" + K); const string datasetPath = "../../TestFiles/sparsebags/musk1.data"; var bagToTest = new SparseBag(-1, "1", new[] { new Dictionary<int, float> {{1, 22.8529f}, {2, 22.6664f}}, new Dictionary<int, float> {{1, 59.1986f}, {2, 34.5393f}} }); citationNearestNeighbor.Train(DatasetReaderFactory.GetDatasetReader<SparseBag>(datasetPath)); var start = DateTime.Now; citationNearestNeighbor.PredictWinner(bagToTest); Console.WriteLine("Total time: {0}", (DateTime.Now - start)); }
public void PredictWinner_NegativeWins_EmptyKnowledge() { // Given var bags = new BagTestData[0]; var bagToTest = new SparseBag(1, "1", new[] { new Dictionary<int, float> {{1, 22.8529f}, {2, 22.6664f}}, new Dictionary<int, float> {{1, 59.1986f}, {2, 34.5393f}} }); int predictedLabelForOne = PredictBagLabel(bags, bagToTest, 2); // Then Assert.That(predictedLabelForOne, Is.EqualTo(-1)); }
private static int PredictBagLabel(BagTestData[] bags, SparseBag bagToTest, int M) { var sparseBags = BagTestData.ToSparseBags(bags); var citationNearestNeighbor = ClassifierFactory.GetMultiInstanceClassifier("rbfn:distance=hausdorff,M=" + M+",sigma=1"); citationNearestNeighbor.Train(sparseBags); return citationNearestNeighbor.PredictWinner(bagToTest); }