func ExtractParamAndConvertToFloat(params data.Map, key string) (float64, error) { v, ok := params[key] if !ok { return 0, fmt.Errorf("%s parameter is missing", key) } x, err := data.ToFloat(v) if err != nil { return 0, fmt.Errorf("%s parameter cannot be converted to float: %v", key, err) } return x, nil }
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 }
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 }
func (r *source) GenerateStream(ctx *core.Context, w core.Writer) error { numFieldNames := []string{ "家賃(万円)", "駅からの徒歩時間 (分)", "専有面積 (m*m)", "築年数 (年)", "階数", } defer r.file.Close() if r.training { for { line, err := r.readLine() if err != nil { if err == io.EOF { return nil } return err } if line[0] == '#' { continue } fields := strings.Split(line, ", ") if len(fields) != 6 { panic("hoge") } value, err := data.ToFloat(data.String(fields[0])) if err != nil { panic(err) } fv := make(data.Map) for i := 1; i < len(numFieldNames); i++ { x, err := data.ToFloat(data.String(fields[i])) if err != nil { panic(err) } fv[numFieldNames[i]] = data.Float(x) } fv[fields[len(fields)-1]] = data.Float(1) now := time.Now() w.Write(ctx, &core.Tuple{ Data: data.Map{ "value": data.Float(value), "feature_vector": fv, }, Timestamp: now, ProcTimestamp: now, }) } } else { fv := make(data.Map) i := 1 for { line, err := r.readLine() if err != nil { if err == io.EOF { return nil } return err } if line == "" || line[0] == '#' { continue } fields := strings.Split(line, ":") if len(fields) != 2 { panic("hoge") } for i := range fields { fields[i] = strings.TrimSpace(fields[i]) } if i < len(numFieldNames) { x, err := data.ToFloat(data.String(fields[1])) if err != nil { panic(err) } fv[numFieldNames[i]] = data.Float(x) i++ } else { if fields[0] != "aspect" { panic(fields) } aspect := strings.Trim(fields[1], "\"") fv[aspect] = data.Float(1) break } } now := time.Now() w.Write(ctx, &core.Tuple{ Data: data.Map{ "feature_vector": fv, }, Timestamp: now, ProcTimestamp: now, }) } return nil }