Ejemplo n.º 1
0
func (l *lightLOFState) Write(ctx *core.Context, t *core.Tuple) error {
	vfv, ok := t.Data[l.featureVectorField]
	if !ok {
		return fmt.Errorf("%s field is missing", l.featureVectorField)
	}
	fv, err := data.AsMap(vfv)
	if err != nil {
		return fmt.Errorf("%s value is not a map: %v", l.featureVectorField, err)
	}

	err = l.lightLOF.AddWithoutCalcScore(FeatureVector(fv))
	return err
}
Ejemplo n.º 2
0
// Write trains the machine learning model the state has with a given tuple.
func (a *AROWState) Write(ctx *core.Context, t *core.Tuple) error {
	vlabel, ok := t.Data[a.labelField]
	if !ok {
		return fmt.Errorf("%s field is missing", a.labelField)
	}
	label, err := data.AsString(vlabel)
	if err != nil {
		return fmt.Errorf("%s value is not a string: %v", a.labelField, err)
	}

	vfv, ok := t.Data[a.featureVectorField]
	if !ok {
		return fmt.Errorf("%s field is missing", a.featureVectorField)
	}
	fv, err := data.AsMap(vfv)
	if err != nil {
		return fmt.Errorf("%s value is not a map: %v", a.labelField, err)
	}

	err = a.train(fv, label)
	return err
}
Ejemplo n.º 3
0
func (pa *PassiveAggressiveState) Write(ctx *core.Context, t *core.Tuple) error {
	vval, ok := t.Data[pa.valueField]
	if !ok {
		return fmt.Errorf("%s field is missing", pa.valueField)
	}
	val64, err := data.ToFloat(vval)
	if err != nil {
		return fmt.Errorf("%s cannot be converted to float: %v", pa.valueField, err)
	}
	val := float32(val64)

	vfv, ok := t.Data[pa.featureVectorField]
	if !ok {
		return fmt.Errorf("%s field is missing", pa.featureVectorField)
	}
	fv, err := data.AsMap(vfv)
	if err != nil {
		return fmt.Errorf("%s value is not a map: %v", pa.featureVectorField, err)
	}

	err = pa.pa.Train(FeatureVector(fv), val)
	return err
}
Ejemplo n.º 4
0
func flattenImpl(keyPrefix string, v data.Value, ap Appender) error {
	switch v.Type() {
	case data.TypeArray:
		keyPrefix += "\x00"
		a, _ := data.AsArray(v)
		for i, v := range a {
			err := flattenImpl(fmt.Sprintf(keyPrefix, i), v, ap)
			if err != nil {
				return err
			}
		}

	case data.TypeMap:
		if keyPrefix != "" {
			keyPrefix += "\x00"
		}

		m, _ := data.AsMap(v)
		for f, v := range m {
			err := flattenImpl(keyPrefix+f, v, ap)
			if err != nil {
				return err
			}
		}

	default:
		xx, err := data.ToFloat(v)
		if err != nil {
			// TODO: return better error
			return err
		}
		x := float32(xx)
		ap(keyPrefix, x)
	}
	return nil
}