BeforeEach(func() { trainingRatio = 0.80 }) Context("when the dataset has no rows", func() { It("errors", func() { Ω(err).Should(HaveOccurred()) }) }) Context("when the dataset has rows and the ratio is valid", func() { var expectedTrainingTargets, expectedTestTargets []slice.Slice BeforeEach(func() { for i := 0; i < 20; i++ { originalSet.AddRowFromStrings([]string{strconv.Itoa(i)}) } expectedTrainingTargets = makeSingleFloatTargets(16, 13, 1, 2, 19, 6, 5, 14, 10, 11, 4, 15, 8, 3, 9) expectedTestTargets = makeSingleFloatTargets(12, 7, 17, 0, 18) }) It("splits the dataset into two according to the given ratio", func() { Ω(err).ShouldNot(HaveOccurred()) Ω(trainingSet.NumRows()).Should(Equal(len(expectedTrainingTargets))) for i := 0; i < trainingSet.NumRows(); i++ { trainingRow, rowErr := trainingSet.Row(i) Ω(rowErr).ShouldNot(HaveOccurred()) Ω(trainingRow.Target().Equals(expectedTrainingTargets[i])).Should(BeTrue()) }
_, err = ds.Row(-1) Ω(err).Should(HaveOccurred()) _, err = ds.Row(1) Ω(err).Should(HaveOccurred()) }) }) Context("When adding a row", func() { var newRow row.Row var err error Context("When the row's length is incorrect", func() { BeforeEach(func() { err = ds.AddRowFromStrings([]string{"0.0", "hi", "mom", "94"}) }) It("Returns an error", func() { Ω(err).Should(HaveOccurred()) }) It("Has 0 rows", func() { Ω(ds.NumRows()).To(BeZero()) }) Context("When getting a row", func() { It("Returns an error", func() { _, err = ds.Row(0) Ω(err).Should(HaveOccurred())
0.3, 0.3, 100, lossGradient, ) Ω(err).ShouldNot(HaveOccurred()) }) Context("Given a dataset with non-float features", func() { BeforeEach(func() { columnTypes, err := columntype.StringsToColumnTypes([]string{"x", "1.0"}) Ω(err).ShouldNot(HaveOccurred()) trainingSet = dataset.NewDataset([]int{0}, []int{1}, columnTypes) err = trainingSet.AddRowFromStrings([]string{"hi", "24"}) Ω(err).ShouldNot(HaveOccurred()) }) It("Returns an error", func() { err := estimator.Train(trainingSet) Ω(err).Should(BeAssignableToTypeOf(gdeErrors.NonFloatFeaturesError{})) }) }) Context("Given a dataset with a non-float target", func() { BeforeEach(func() { columnTypes, err := columntype.StringsToColumnTypes([]string{"x", "1.0"}) Ω(err).ShouldNot(HaveOccurred()) trainingSet = dataset.NewDataset([]int{1}, []int{0}, columnTypes)
It("Returns an error", func() { err := kNNClassifier.Train(trainingData) Ω(err).Should(HaveOccurred()) Ω(err).Should(BeAssignableToTypeOf(knnerrors.NonFloatFeaturesTrainingSetError{})) }) }) Context("When the dataset is valid", func() { BeforeEach(func() { columnTypes, err := columntype.StringsToColumnTypes([]string{"hi", "0", "0"}) Ω(err).ShouldNot(HaveOccurred()) trainingData = dataset.NewDataset([]int{1, 2}, []int{0}, columnTypes) err = trainingData.AddRowFromStrings([]string{"hi", "0", "0"}) Ω(err).ShouldNot(HaveOccurred()) }) It("Doesn't return an error", func() { err := kNNClassifier.Train(trainingData) Ω(err).ShouldNot(HaveOccurred()) }) }) }) Describe("Classify", func() { var testRow row.Row var emptyTarget slice.Slice var columnTypes []columntype.ColumnType var err error
It("Returns an error", func() { err := linearRegressor.Train(trainingData) Ω(err).Should(HaveOccurred()) Ω(err).Should(BeAssignableToTypeOf(linearerrors.NoFeaturesError{})) }) }) Context("When the dataset is valid", func() { BeforeEach(func() { columnTypes, err := columntype.StringsToColumnTypes([]string{"1.2", "3.4", "5.6"}) Ω(err).ShouldNot(HaveOccurred()) trainingData = dataset.NewDataset([]int{1, 2}, []int{0}, columnTypes) err = trainingData.AddRowFromStrings([]string{"1.2", "3.0", "0.5"}) Ω(err).ShouldNot(HaveOccurred()) }) It("Doesn't return an error", func() { err := linearRegressor.Train(trainingData) Ω(err).ShouldNot(HaveOccurred()) }) }) }) Describe("Predict", func() { var testRow row.Row var emptyTarget slice.Slice var columnTypes []columntype.ColumnType var err error