Ejemplo n.º 1
0
func makeSingleFloatTargets(floats ...float64) []slice.Slice {
	columnTypes, columnTypesError := columntype.StringsToColumnTypes([]string{"0"})
	Ω(columnTypesError).ShouldNot(HaveOccurred())

	targets := make([]slice.Slice, len(floats))
	for i, f := range floats {
		target, err := slice.SliceFromRawValues(true, []int{0}, columnTypes, []float64{f})
		Ω(err).ShouldNot(HaveOccurred())
		targets[i] = target
	}
	return targets
}
Ejemplo n.º 2
0
func (dataset *inMemoryDataset) AddRowFromStrings(strings []string) error {
	actualLength := len(strings)
	expectedLength := dataset.numColumns

	if actualLength != expectedLength {
		return newRowLengthMismatchError(actualLength, expectedLength)
	}

	rawValues := make([]float64, actualLength)

	for i, s := range strings {
		value, err := dataset.columnTypes[i].PersistRawFromString(s)
		if err != nil {
			return err
		}

		rawValues[i] = value
	}

	features, _ := slice.SliceFromRawValues(
		dataset.allFeaturesFloats,
		dataset.featureColumnIndices,
		dataset.columnTypes,
		rawValues,
	)

	target, _ := slice.SliceFromRawValues(
		dataset.allTargetsFloats,
		dataset.targetColumnIndices,
		dataset.columnTypes,
		rawValues,
	)

	dataset.rows = append(dataset.rows, row.NewRow(features, target, dataset.numFeatures))

	return nil
}
	"math"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("SortedTargetCollection", func() {
	Describe("Insert and MaxDistance", func() {
		var stc knnutilities.SortedTargetCollection
		var target slice.Slice
		var err error

		BeforeEach(func() {
			stc = knnutilities.NewKNNTargetCollection(2)
			target, err = slice.SliceFromRawValues(true, []int{}, []columntype.ColumnType{}, []float64{})
			Ω(err).ShouldNot(HaveOccurred())
		})

		Context("Before the collection is full", func() {
			It("The MaxDistance should be +Inf", func() {
				Ω(stc.MaxDistance()).Should(Equal(math.MaxFloat64))

				stc.Insert(target, 1.0)
				Ω(stc.MaxDistance()).Should(Equal(math.MaxFloat64))
			})
		})

		Context("When the collection is full", func() {
			initialMax := 3.0
			initialMin := initialMax - 3
Ejemplo n.º 4
0
	})

	Describe("Classify", func() {
		var testRow row.Row
		var emptyTarget slice.Slice
		var columnTypes []columntype.ColumnType
		var err error
		var helloRaw float64

		BeforeEach(func() {
			kNNClassifier, _ = knn.NewKNNClassifier(1)

			columnTypes, err = columntype.StringsToColumnTypes([]string{"hi", "0", "0"})
			Ω(err).ShouldNot(HaveOccurred())

			emptyTarget, err = slice.SliceFromRawValues(true, []int{}, columnTypes, []float64{})
			Ω(err).ShouldNot(HaveOccurred())

			helloRaw, err = columnTypes[0].PersistRawFromString("hello")
			Ω(err).ShouldNot(HaveOccurred())
		})

		Context("When the classifier hasn't been trained", func() {
			BeforeEach(func() {
				features, err := slice.SliceFromRawValues(true, []int{1}, columnTypes, []float64{helloRaw, 1, 2})
				Ω(err).ShouldNot(HaveOccurred())

				testRow = row.NewRow(features, emptyTarget, 1)
			})

			It("Returns an error", func() {
Ejemplo n.º 5
0
		var columnTypes []columntype.ColumnType
		var err error
		var s slice.Slice

		BeforeEach(func() {
			columnIndices = []int{1, 4, 3}
		})

		Context("When all the relevant columns store float data", func() {
			BeforeEach(func() {
				columnTypes, err = columntype.StringsToColumnTypes([]string{"x", "1.0", "1.0", "1.0", "1.0", "1.0"})
			})

			Describe("When told all entires are floats", func() {
				BeforeEach(func() {
					s, err = slice.SliceFromRawValues(true, columnIndices, columnTypes, []float64{1.2, 0, 0, 1, 4.9, 2.2})
				})

				It("Does not return an error", func() {
					Ω(err).ShouldNot(HaveOccurred())
				})

				It("Returns a float slice with the correct values", func() {
					floatSlice, ok := s.(slice.FloatSlice)
					Ω(ok).Should(BeTrue())

					Ω(floatSlice.Values()).Should(Equal([]float64{0.0, 4.9, 1.0}))
				})
			})

			Describe("When told some entires are not floats", func() {
Ejemplo n.º 6
0
		})
	})

	Describe("Predict", func() {
		var testRow row.Row
		var emptyTarget slice.Slice
		var columnTypes []columntype.ColumnType
		var err error

		BeforeEach(func() {
			linearRegressor = linear.NewLinearRegressor()

			columnTypes, err = columntype.StringsToColumnTypes([]string{"0", "0", "0"})
			Ω(err).ShouldNot(HaveOccurred())

			emptyTarget, err = slice.SliceFromRawValues(true, []int{}, columnTypes, []float64{})
			Ω(err).ShouldNot(HaveOccurred())
		})

		Context("When the regressor hasn't been trained", func() {
			BeforeEach(func() {
				features, err := slice.SliceFromRawValues(true, []int{1}, columnTypes, []float64{0, 1, 2})
				Ω(err).ShouldNot(HaveOccurred())

				testRow = row.NewRow(features, emptyTarget, 1)
			})

			It("Returns an error", func() {
				_, err := linearRegressor.Predict(testRow)
				Ω(err).Should(HaveOccurred())
				Ω(err).Should(BeAssignableToTypeOf(linearerrors.UntrainedRegressorError{}))