Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
GH-802: Manipulated array throws “Index was outside the bounds of the…
Browse files Browse the repository at this point in the history
… array” exception
  • Loading branch information
cesarsouza committed Aug 24, 2017
1 parent 50261a8 commit 990fcb7
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public virtual TModel Learn(TInput[][] x, int[] y, double[] weight = null)
Accord.MachineLearning.Tools.CheckArgs(x, y, weight, () =>
{
if (Model == null)
Model = Create(x, y.DistinctCount());
Model = Create(x, y.Max() + 1);
return Model;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace Accord.MachineLearning.Bayes
using System;
using Accord.Compat;
using System.Threading;
using System.Diagnostics;

/// <summary>
/// Naïve Bayes learning algorithm for discrete distribution models.
Expand Down Expand Up @@ -60,7 +61,8 @@ public class NaiveBayesLearning :
/// </summary>
protected override NaiveBayes Create(int[][] x, int y)
{
int[] inputs = x.DistinctCount();
int[] inputs = x.Max(dimension: 0).Add(1);
Debug.Assert(inputs.Length == x.Columns());
return new NaiveBayes(classes: y, symbols: inputs);
}

Expand Down
188 changes: 188 additions & 0 deletions Unit Tests/Accord.Tests.MachineLearning/Bayes/NaiveBayesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,194 @@ public void CrossValidationTest()
Assert.AreEqual(10, result.Models.Length);

}


[Test]
public void gh_802()
{
int[][] inputs = new int[][]
{
new int[] { 0, 2, 0, 10 },
new int[] { 0, 2, 0, 10 },
new int[] { 0, 2, 0, 10 },
new int[] { 0, 0, 0, 1 },
new int[] { 0, 0, 0, 0 },
new int[] { 0, 0, 0, 1 },
new int[] { 0, 0, 0, 0 },
new int[] { 0, 0, 0, 1 },
new int[] { 1, 1, 3, 3 },
new int[] { 1, 1, 3, 2 },
new int[] { 1, 1, 3, 4 },
new int[] { 1, 1, 3, 8 },
new int[] { 1, 1, 3, 6 },
new int[] { 1, 1, 3, 3 },
new int[] { 1, 1, 3, 2 },
new int[] { 1, 1, 3, 4 },
new int[] { 1, 1, 3, 8 },
new int[] { 1, 1, 3, 11 },
new int[] { 1, 1, 2, 3 },
new int[] { 1, 1, 2, 2 },
new int[] { 1, 1, 2, 4 },
new int[] { 1, 1, 2, 5 },
new int[] { 1, 1, 2, 6 },
new int[] { 1, 1, 3, 3 },
new int[] { 1, 1, 3, 2 },
new int[] { 1, 1, 3, 4 },
new int[] { 1, 1, 3, 8 },
new int[] { 1, 1, 3, 6 },
new int[] { 1, 1, 4, 2 },
new int[] { 1, 1, 4, 5 },
new int[] { 1, 1, 3, 3 },
new int[] { 1, 1, 3, 2 },
new int[] { 1, 1, 3, 4 },
new int[] { 1, 1, 3, 8 },
new int[] { 1, 1, 3, 6 },
new int[] { 0, 2, 0, 10 },
new int[] { 0, 2, 0, 10 },
new int[] { 0, 2, 0, 10 },
new int[] { 0, 2, 0, 10 },
new int[] { 0, 2, 0, 10 },
new int[] { 1, 3, 5, 12 },
new int[] { 1, 3, 5, 13 },
new int[] { 1, 3, 5, 13 },
new int[] { 0, 0, 0, 1 },
new int[] { 1, 1, 2, 3 },
new int[] { 1, 1, 2, 5 },
new int[] { 1, 1, 2, 6 },
new int[] { 1, 1, 2, 9 },
new int[] { 1, 1, 2, 3 },
new int[] { 1, 1, 2, 2 },
new int[] { 1, 1, 2, 4 },
new int[] { 1, 1, 2, 5 },
new int[] { 1, 1, 2, 6 },
new int[] { 1, 1, 3, 3 },
new int[] { 1, 1, 3, 2 },
new int[] { 1, 1, 3, 4 },
new int[] { 1, 1, 3, 8 },
new int[] { 1, 1, 3, 6 },
new int[] { 1, 1, 3, 3 },
new int[] { 1, 1, 3, 2 },
new int[] { 1, 1, 3, 4 },
new int[] { 1, 1, 3, 8 },
new int[] { 1, 1, 3, 6 },
new int[] { 1, 1, 2, 3 },
new int[] { 1, 1, 2, 2 },
new int[] { 1, 1, 2, 4 },
new int[] { 1, 1, 2, 5 },
new int[] { 1, 1, 2, 6 },
new int[] { 1, 1, 3, 3 },
new int[] { 1, 1, 3, 2 },
new int[] { 1, 1, 3, 4 },
new int[] { 1, 1, 3, 8 },
new int[] { 1, 1, 3, 6 },
new int[] { 1, 1, 3, 3 },
new int[] { 1, 1, 3, 2 },
new int[] { 1, 1, 3, 4 },
new int[] { 1, 1, 3, 8 },
new int[] { 1, 1, 3, 6 },
new int[] { 0, 2, 0, 10 },
new int[] { 0, 2, 0, 10 },
new int[] { 0, 2, 0, 10 },
new int[] { 0, 2, 0, 10 },
new int[] { 0, 2, 0, 10 },
new int[] { 0, 2, 0, 10 },
new int[] { 0, 2, 0, 14 },
new int[] { 0, 4, 6, 15 },
new int[] { 0, 4, 6, 15 },
new int[] { 0, 4, 6, 15 },
new int[] { 0, 5, 0, 13 },
new int[] { 0, 0, 0, 0 },
new int[] { 0, 0, 0, 0 },
new int[] { 0, 0, 0, 1 },
new int[] { 0, 0, 0, 0 },
new int[] { 0, 0, 0, 1 },
new int[] { 1, 1, 3, 6 },
new int[] { 1, 1, 1, 3 },
new int[] { 1, 1, 1, 2 },
new int[] { 1, 1, 1, 8 },
new int[] { 1, 1, 1, 5 },
new int[] { 1, 1, 1, 6 },
new int[] { 1, 1, 1, 3 },
new int[] { 1, 1, 1, 2 },
new int[] { 1, 1, 1, 8 },
new int[] { 1, 1, 1, 5 },
new int[] { 1, 1, 1, 6 },
new int[] { 1, 1, 4, 2 },
new int[] { 1, 1, 4, 4 },
new int[] { 1, 1, 4, 8 },
new int[] { 1, 1, 4, 5 },
new int[] { 1, 1, 4, 6 },
new int[] { 0, 2, 0, 10 },
new int[] { 0, 2, 0, 10 },
new int[] { 0, 2, 0, 10 },
new int[] { 0, 2, 0, 10 },
new int[] { 0, 6, 0, 16 },
new int[] { 0, 4, 6, 15 },
new int[] { 0, 0, 0, 0 },
new int[] { 0, 0, 0, 1 },
new int[] { 0, 0, 0, 0 },
new int[] { 0, 0, 0, 1 },
new int[] { 0, 0, 0, 0 },
new int[] { 0, 0, 0, 1 },
new int[] { 1, 1, 3, 6 },
new int[] { 1, 1, 3, 6 },
new int[] { 1, 1, 4, 2 },
new int[] { 1, 1, 4, 4 },
new int[] { 1, 1, 4, 8 },
new int[] { 1, 1, 4, 5 },
new int[] { 1, 1, 4, 6 },
new int[] { 1, 1, 3, 2 },
new int[] { 1, 1, 3, 4 },
new int[] { 1, 1, 3, 6 },
new int[] { 0, 2, 0, 10 },
new int[] { 0, 2, 0, 10 },
new int[] { 1, 2, 0, 10 },
new int[] { 1, 2, 0, 10 },
new int[] { 0, 0, 0, 0 },
new int[] { 0, 0, 0, 1 },
new int[] { 0, 0, 0, 0 },
new int[] { 0, 0, 0, 1 },
new int[] { 1, 1, 7, 5 },
new int[] { 1, 1, 1, 3 },
new int[] { 1, 1, 1, 2 },
new int[] { 1, 1, 1, 8 },
new int[] { 1, 1, 1, 5 },
new int[] { 1, 1, 1, 6 },
new int[] { 0, 2, 0, 10 },
new int[] { 1, 2, 0, 10 },
new int[] { 0, 6, 0, 16 },
new int[] { 0, 0, 0, 0 },
new int[] { 0, 0, 0, 0 },
new int[] { 0, 0, 0, 1 },
new int[] { 1, 1, 1, 3 },
new int[] { 1, 1, 1, 2 },
new int[] { 1, 1, 1, 8 },
new int[] { 1, 1, 1, 5 },
new int[] { 1, 1, 1, 6 },
new int[] { 0, 2, 0, 10 }
};


int[] outputs = new int[] { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 };

// Create a new Naive Bayes teacher
var teacher = new NaiveBayesLearning()
{
Model = new NaiveBayes(2, 2, 7, 8, 17)
};

teacher.ParallelOptions.MaxDegreeOfParallelism = 1;

var bayes = teacher.Learn(inputs, outputs);

double[][] prediction = bayes.Probabilities(inputs);

// Teach the Naive Bayes model. The error should be zero:
double error = new BinaryCrossEntropyLoss(outputs).Loss(prediction);

Assert.AreEqual(53.346248364202573, error, 1e-8);

}
}
}
#endif

0 comments on commit 990fcb7

Please sign in to comment.