public void ThetasPropertyAfterTrainingReturnsTheSameThetasAsTrainerReturns() { // arrange var dataSetMock = new Mock <IDataSet <bool, double> >(); dataSetMock.Setup(ds => ds.GetTrainingSamplesCount()).Returns(2); dataSetMock.Setup(ds => ds.GetData()).Returns(new[] { TrainingSample.Create(true, new[] { 0.0 }), TrainingSample.Create(false, new[] { 1.0 }) }); const int FeaturesCount = 1; const double RegularizationParameter = 0.0; var classifier = new LogisticRegressionClassifier(FeaturesCount, RegularizationParameter); classifier.Train(dataSetMock.Object); var classifierTraining = new LogisticRegressionClassifierTraining(FeaturesCount, RegularizationParameter, dataSetMock.Object); var expectedThetas = classifierTraining.Train(); // act var thetas = classifier.Thetas; // assert Assert.IsTrue(expectedThetas.SequenceEqual(thetas)); }
public static IDataSet <T, double> MapTwoFeaturesDataSetToDegree <T>(IDataSet <T, double> dataSet, int degree) where T : IEquatable <T> { return(InMemoryDataSet.Create((from data in dataSet.GetData() let x1 = data.Attributes[0] let x2 = data.Attributes[1] select TrainingSample.Create(data.Category, MapTwoFeaturesToDegrees(x1, x2, degree), data.Count)) .ToArray())); }
private static IDataSet <bool, double> GetDataSet(string resourceName) { var dataLines = GetResourceLines(resourceName); var parsedDataLines = ParseDoublesLines(dataLines, new[] { ',' }); var dataSet1 = InMemoryDataSet.Create(parsedDataLines .Select(pl => TrainingSample.Create(pl[2] != 0, new[] { pl[0], pl[1] })) .ToArray()); return(dataSet1); }
public void ConstructorDoesNotThrowExceptionIfAllParametersIsGood() { // arrange var dataSetMock = new Mock <IDataSet <bool, double> >(); dataSetMock.Setup(ds => ds.GetTrainingSamplesCount()).Returns(1); dataSetMock.Setup(ds => ds.GetData()).Returns(new[] { TrainingSample.Create(true, new double[1]) }); // act var trainer = new LogisticRegressionClassifierTraining(1, 0.0, dataSetMock.Object); // assert }
public void CostFunctionThrowsArgumentNullExceptionIfThetasIsNull() { // arrange var dataSetMock = new Mock <IDataSet <bool, double> >(); dataSetMock.Setup(ds => ds.GetTrainingSamplesCount()).Returns(1); dataSetMock.Setup(ds => ds.GetData()).Returns(new[] { TrainingSample.Create(true, new double[1]) }); var trainer = new LogisticRegressionClassifierTraining(1, 0.0, dataSetMock.Object); // act var cost = trainer.CostFunction(null, new double[0]); // assert Assert.Fail(); }
public void CostFunctionThrowsArgumentOutOfRangeExceptionIfOneOfTheTrainingSamplesAttributesLengthIsNotEqualToFeaturesCount() { // arrange var dataSetMock = new Mock <IDataSet <bool, double> >(); dataSetMock.Setup(ds => ds.GetTrainingSamplesCount()).Returns(1); dataSetMock.Setup(ds => ds.GetData()).Returns(new[] { TrainingSample.Create(true, new double[0]) }); var trainer = new LogisticRegressionClassifierTraining(1, 0.0, dataSetMock.Object); // act var cost = trainer.CostFunction(new double[2], new double[2]); // assert Assert.Fail(); }
public void CostFunctionThrowsNullReferenceExceptionIfOneOfTheTrainingSamplesAttributesIsNull() { // arrange var dataSetMock = new Mock <IDataSet <bool, double> >(); dataSetMock.Setup(ds => ds.GetTrainingSamplesCount()).Returns(1); dataSetMock.Setup(ds => ds.GetData()).Returns(new[] { TrainingSample.Create(true, (double[])null) }); var trainer = new LogisticRegressionClassifierTraining(1, 0.0, dataSetMock.Object); // act var cost = trainer.CostFunction(new double[2], new double[2]); // assert Assert.Fail(); }
public void TrainDoesNotThrowExceptionOnValidDataSet() { // arrange var dataSetMock = new Mock <IDataSet <bool, double> >(); dataSetMock.Setup(ds => ds.GetTrainingSamplesCount()).Returns(1); dataSetMock.Setup(ds => ds.GetData()).Returns(new[] { TrainingSample.Create(true, new double[1]) }); var classifier = new LogisticRegressionClassifier(1, 0.0); // act classifier.Train(dataSetMock.Object); // assert }
public void GetCategoryProbabilityThrowsInvalidOperationExceptionIfClassifierIsNotTrained() { // arrange var dataSetMock = new Mock <IDataSet <bool, double> >(); dataSetMock.Setup(ds => ds.GetTrainingSamplesCount()).Returns(1); dataSetMock.Setup(ds => ds.GetData()).Returns(new[] { TrainingSample.Create(true, new double[1]) }); var classifier = new LogisticRegressionClassifier(1, 0.0); // act classifier.GetCategoryProbability(true, new double[1]); // assert Assert.Fail(); }
public void CorrectlyClassifiesTestDataSetFromExample13Dot1() { var trainingData = InMemoryDataSet.Create(new[] { TrainingSample.Create("China", new[] { "Chinese", "Beijing", "Chinese" }), TrainingSample.Create("China", new[] { "Chinese", "Chinese", "Shanghai" }), TrainingSample.Create("China", new[] { "Chinese", "Makao" }), TrainingSample.Create("Not China", new[] { "Tokio", "Japan", "Chinese" }), }); var classifier = MultinomialNaiveBayesClassifier.Create(trainingData); Assert.AreEqual("China", classifier.Classify(new[] { "Chinese", "Chinese", "Chinese", "Tokio", "Japan" })); Assert.AreEqual("Not China", classifier.Classify(new[] { "Tokio" })); Assert.AreEqual("China", classifier.Classify(new[] { "Chinese", "Tokio" })); Assert.AreEqual("China", classifier.Classify(new[] { "Unknown", "Chinese", "Tokio" })); Assert.AreEqual("Not China", classifier.Classify(new[] { "Chinese", "Tokio", "Japan" })); }
public void ClassifyThrowsArgumentOutOfRangeExceptionIfAttributesCountIsNotEqualToFeaturesCount() { // arrange var dataSetMock = new Mock <IDataSet <bool, double> >(); dataSetMock.Setup(ds => ds.GetTrainingSamplesCount()).Returns(1); dataSetMock.Setup(ds => ds.GetData()).Returns(new[] { TrainingSample.Create(true, new double[1]) }); var classifier = new LogisticRegressionClassifier(1, 0.0); classifier.Train(dataSetMock.Object); // act classifier.Classify(new double[0]); // assert Assert.Fail(); }
public void GetCategoryProbabilityThrowsArgumentNullExceptionIfAttributesIsNull() { // arrange var dataSetMock = new Mock <IDataSet <bool, double> >(); dataSetMock.Setup(ds => ds.GetTrainingSamplesCount()).Returns(1); dataSetMock.Setup(ds => ds.GetData()).Returns(new[] { TrainingSample.Create(true, new double[1]) }); var classifier = new LogisticRegressionClassifier(1, 0.0); classifier.Train(dataSetMock.Object); // act classifier.GetCategoryProbability(true, null); // assert Assert.Fail(); }