Example #1
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
}
Example #2
0
			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() {
				_, err := kNNClassifier.Classify(testRow)
				Ω(err).Should(HaveOccurred())
				Ω(err).Should(BeAssignableToTypeOf(knnerrors.UntrainedClassifierError{}))
			})
		})

		Context("When the classifier has been trained", func() {
			BeforeEach(func() {
				trainingData := dataset.NewDataset([]int{1, 2}, []int{0}, columnTypes)

				err = trainingData.AddRowFromStrings([]string{"hello", "0", "0"})
				Ω(err).ShouldNot(HaveOccurred())