Example #1
0
func featureFactory(s string) db.DataFrameFeature {
	var feature db.Feature
	switch s[0] {
	case 'C':
		feature = db.NewCategoricalFeature(db.NewStringTable())
	default:
		feature = db.NewNumericFeature(nil)
	}
	return db.NewDataFrameFeature(s, feature)
}
Example #2
0
func TestCategoricalFeature(t *testing.T) {
	// Assign to Feature to ensure CategoricalFeature implements Feature
	cf := db.NewCategoricalFeature(db.NewStringTable())

	testStrings := []string{"alpha", "beta", "delta", "beta", "alpha"}

	for _, s := range testStrings {
		cf.AddFromString(s)
	}

	check := func(index int, expected string) {
		actual := cf.Value(index).(string)
		altActual := cf.Decode(cf.NumericValue(index)).(string)
		if actual != altActual {
			t.Errorf("Value(%d) returned %v; Decode(NumericValue(%d) returned %v", index, actual, index, altActual)
		}
		if actual != expected {
			t.Errorf("Get(%d) got %v; expecting %v", index, actual, expected)
		}
	}

	for i, s := range testStrings {
		check(i, s)
	}

	if cf.Len() != len(testStrings) {
		t.Errorf("Length() returned %d; expecting %d", cf.Len(), 5)
	}

	ordercheck := func(index int, expected string) {
		orderedString := cf.Decode(cf.NumericValue(cf.InOrder(index)))
		if orderedString != expected {
			t.Errorf("Get(%d) got %s; expecting %s", index, orderedString, expected)
		}
	}

	cf.Prepare()

	ordercheck(0, "alpha")
	ordercheck(1, "alpha")
	ordercheck(2, "beta")
	ordercheck(3, "beta")
	ordercheck(4, "delta")

	if n := cf.Categories(); n != 3 {
		t.Errorf("Expected %d categories; got %d\n", 3, n)
	}
}