classifier

package
v0.0.0-...-e3597af Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2016 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Example
package main

import (
	"fmt"
	"gopkg.in/sensorbee/sensorbee.v0/data"
	"math/rand"
)

type shogun struct {
	family string
	given  string
}

func unigram(s string) FeatureVector {
	fv := make(FeatureVector)
	for _, r := range s {
		fv[string(r)] = data.Float(1)
	}
	return fv
}

func main() {
	shogunList := []shogun{
		{"徳川", "家康"}, {"徳川", "秀忠"}, {"徳川", "家光"}, {"徳川", "家綱"},
		{"徳川", "綱吉"}, {"徳川", "家宣"}, {"徳川", "家継"}, {"徳川", "吉宗"},
		{"徳川", "家重"}, {"徳川", "家治"}, {"徳川", "家斉"}, {"徳川", "家慶"},
		{"徳川", "家定"}, {"徳川", "家茂"},

		{"足利", "尊氏"}, {"足利", "義詮"}, {"足利", "義満"}, {"足利", "義持"},
		{"足利", "義量"}, {"足利", "義教"}, {"足利", "義勝"}, {"足利", "義政"},
		{"足利", "義尚"}, {"足利", "義稙"}, {"足利", "義澄"}, {"足利", "義稙"},
		{"足利", "義晴"}, {"足利", "義輝"}, {"足利", "義栄"},

		{"北条", "時政"}, {"北条", "義時"}, {"北条", "泰時"}, {"北条", "経時"},
		{"北条", "時頼"}, {"北条", "長時"}, {"北条", "政村"}, {"北条", "時宗"},
		{"北条", "貞時"}, {"北条", "師時"}, {"北条", "宗宣"}, {"北条", "煕時"},
		{"北条", "基時"}, {"北条", "高時"}, {"北条", "貞顕"},
	}

	shuffledShogunList := make([]shogun, len(shogunList))
	perm := rand.Perm(len(shogunList))
	for i, v := range perm {
		shuffledShogunList[v] = shogunList[i]
	}

	var arow, _ = NewAROW(1)
	for _, s := range shuffledShogunList {
		fv := unigram(s.given)
		arow.Train(fv, Label(s.family))
	}

	scores, _ := arow.Classify(unigram("慶喜"))
	l, _ := scores.Max()
	fmt.Println(l)
	scores, _ = arow.Classify(unigram("義昭"))
	l, _ = scores.Max()
	fmt.Println(l)
	scores, _ = arow.Classify(unigram("守時"))
	l, _ = scores.Max()
	fmt.Println(l)

}
Output:

徳川
足利
北条

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AROWClassify

func AROWClassify(ctx *core.Context, stateName string, featureVector data.Map) (data.Map, error)

AROWClassify classifies the input using the given model having stateName.

func ClassifiedLabel

func ClassifiedLabel(scores data.Map) (string, error)

ClassifiedLabel returns the label having the highest score in a classification result.

func ClassifiedScore

func ClassifiedScore(scores data.Map) (float64, error)

ClassifiedScore returns the highest score in a classification result.

Types

type AROW

type AROW struct {
	// contains filtered or unexported fields
}

AROW holds a model for classification.

func LoadAROW

func LoadAROW(r io.Reader) (*AROW, error)

LoadAROW loads AROW from the saved data.

func NewAROW

func NewAROW(regWeight float32) (*AROW, error)

NewAROW creates an AROW model. regWeight means sensitivity for data. When regWeight is large, the model learns quickly but harms from noise. regWeight must be larger than zero.

func (*AROW) Classify

func (a *AROW) Classify(v FeatureVector) (LScores, error)

Classify classifies a feature vector. This function returns all labels and scores.

func (*AROW) Clear

func (a *AROW) Clear()

Clear clears a model.

func (*AROW) RegWeight

func (a *AROW) RegWeight() float32

RegWeight returns regularization weight.

func (*AROW) Save

func (a *AROW) Save(w io.Writer) error

Save saves the current state of AROW.

func (*AROW) Train

func (a *AROW) Train(v FeatureVector, label Label) error

Train trains a model with a feature vector and a label.

type AROWState

type AROWState struct {
	// contains filtered or unexported fields
}

AROWState is a state which support AROW classification algorithm.

func (*AROWState) Save

func (a *AROWState) Save(ctx *core.Context, w io.Writer, params data.Map) error

Save is provided as a part of core.SavableSharedState.

func (*AROWState) Terminate

func (*AROWState) Terminate(ctx *core.Context) error

Terminate terminates the state.

func (*AROWState) Write

func (a *AROWState) Write(ctx *core.Context, t *core.Tuple) error

Write trains the machine learning model the state has with a given tuple.

type AROWStateCreator

type AROWStateCreator struct {
}

AROWStateCreator is used by BQL to create or load AROWState as a UDS.

func (*AROWStateCreator) CreateState

func (c *AROWStateCreator) CreateState(ctx *core.Context, params data.Map) (core.SharedState, error)

CreateState creates a new state for AROW classifier.

func (*AROWStateCreator) LoadState

func (c *AROWStateCreator) LoadState(ctx *core.Context, r io.Reader, params data.Map) (core.SharedState, error)

LoadState loads a new state for AROW classifier.

type FeatureVector

type FeatureVector data.Map

FeatureVector is a type for feature vectors.

type LScores

type LScores data.Map

LScores is a type representing a map from labels to scores.

func (LScores) Max

func (s LScores) Max() (label Label, score float32)

Max returns a max scored label and the score

type Label

type Label string

Label represents labels for classification.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL