goscore

package module
v0.0.0-...-6e6e174 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2019 License: MIT Imports: 10 Imported by: 0

README

Build Status Go Report Card Coverage Status GoDoc

Goscore

Go scoring API for Predictive Model Markup Language (PMML).

Currently supports Neural Network, Decision Tree, Random Forest and Gradient Boosted Models

Will be happy to implement new models by demand, or assist with any other issue.

Contact me here or at aschers@gmail.com.

Tutorial - Deploy Machine Learning Models from R Research to Go Production with PMML

Installation

go get github.com/asafschers/goscore

Usage

Random Forest / Gradient Boosted Model

Generate PMML - R

Fetch model from PMML file -

modelXml, _ := ioutil.ReadFile("fixtures/model.pmml")
var model goscore.RandomForest // or goscore.GradientBoostedModel
xml.Unmarshal([]byte(modelXml), &model)

Set features -

features := map[string]interface{}{
  "Sex": "male",
  "Parch": 0,
  "Age": 30,
  "Fare": 9.6875,
  "Pclass": 2,
  "SibSp": 0,
  "Embarked": "Q",
}

Score features by model -

score, _ := model.Score(features, "1") // gbm.score doesn't recieve label

Score faster -

runtime.GOMAXPROCS(runtime.NumCPU()) // use all cores
score, _ := model.ScoreConcurrently(features, "1") // parallelize tree traversing  

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/asafschers/goscore. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Documentation

Overview

Package goscore is a go scoring API for PMML files

Index

Constants

This section is empty.

Variables

View Source
var ActivationFunctionNotImplemented = errors.New("Activation Function Not Implemented Yet")
View Source
var ActivationFunctions map[string]ActivationFunction
View Source
var NormalizationMethodMaps map[string]NormalizationMethodMap
View Source
var NormalizationMethodNotImplemented = errors.New("Normalization Method Not Implemented Yet")
View Source
var NormalizationMethods map[string]NormalizationMethod

Functions

func ArctanActivationFunction

func ArctanActivationFunction(Z float64) float64

func CosineActivationFunction

func CosineActivationFunction(Z float64) float64

func ElliottActivationFunction

func ElliottActivationFunction(Z float64) float64

func ExponentialActivationFunction

func ExponentialActivationFunction(Z float64) float64

func GaussActivationFunction

func GaussActivationFunction(Z float64) float64

func IdentityActivationFunction

func IdentityActivationFunction(b float64) float64

func LogisticActivationFunction

func LogisticActivationFunction(b float64) float64

func ReciprocalActivationFunction

func ReciprocalActivationFunction(Z float64) float64

func RectifierActivationFunction

func RectifierActivationFunction(Z float64) float64

func SineActivationFunction

func SineActivationFunction(Z float64) float64

func SoftmaxNormalizationMethod

func SoftmaxNormalizationMethod(input ...float64) []float64

func SoftmaxNormalizationMethods

func SoftmaxNormalizationMethods(confidence map[string]float64) (map[string]float64, error)

function for compute confidence value into probability using softMax function input : map of confidence value with float64 type output : map of probability each class with float64 type

func SquareActivationFunction

func SquareActivationFunction(Z float64) float64

func TanhActivationFunction

func TanhActivationFunction(b float64) float64

Types

type ActivationFunction

type ActivationFunction func(float64) float64

func NewThresHoldFunction

func NewThresHoldFunction(a float64) ActivationFunction

type Contribution

type Contribution struct {
	From   string  `xml:"from,attr"`
	Weight float64 `xml:"weight,attr"`
}

type DerivedField

type DerivedField struct {
	DataType     string `xml:"dataType,attr"`
	FieldRef     FieldRef
	NormDiscrete NormDiscrete
}

func (*DerivedField) GetInputName

func (d *DerivedField) GetInputName() string

type FieldRef

type FieldRef struct {
	Field string `xml:"field,attr"`
}

type GradientBoostedModel

type GradientBoostedModel struct {
	Version  string  `xml:"version,attr"`
	Trees    []Node  `xml:"MiningModel>Segmentation>Segment>MiningModel>Segmentation>Segment>TreeModel"`
	Target   target  `xml:"MiningModel>Segmentation>Segment>MiningModel>Targets>Target"`
	Constant float64 `xml:"MiningModel>Segmentation>Segment>MiningModel>Output>OutputField>Apply>Constant"`
}

GradientBoostedModel - GradientBoostedModel PMML

func LoadGradientBoostedModel

func LoadGradientBoostedModel(fileName string) (gbm GradientBoostedModel, err error)

LoadGradientBoostedModel - Load Gradient Boosted Model PMML file to GradientBoostedModel struct

func (*GradientBoostedModel) Score

func (gbm *GradientBoostedModel) Score(features map[string]interface{}) (float64, error)

Score - traverses all trees in GradientBoostedModel with features and returns exp(sum) / (1 + exp(sum)) where sum is sum of trees + rescale constant

func (*GradientBoostedModel) ScoreConcurrently

func (gbm *GradientBoostedModel) ScoreConcurrently(features map[string]interface{}) (float64, error)

ScoreConcurrently - same as Score but concurrent

type LogisticRegression

type LogisticRegression struct {
	// struct xml:PMML>RegressionModel
	NormalizationMethod string            `xml:"normalizationMethod,attr"`
	Fields              []MiningField     `xml:"MiningSchema>MiningField"`
	RegressionTable     []RegressionTable `xml:"RegressionTable"`
}

func NewLogisticRegression

func NewLogisticRegression(source []byte) (*LogisticRegression, error)

func (*LogisticRegression) RegressionFunctionContinuous

func (lr *LogisticRegression) RegressionFunctionContinuous(features map[string]float64) map[string]float64

method for calculate feature using logistic regression function for countinous independent variable

func (*LogisticRegression) Score

func (lr *LogisticRegression) Score(args ...interface{}) (string, map[string]float64, error)

method for score test data input : independent variable with map["var name"]value

voting with boolean type
	true (default)  -> using normalization
	false			-> without normalization

return : -label with string type

-confident/prob with map type
-errors

func (*LogisticRegression) SetupNumbericPredictorMap

func (lr *LogisticRegression) SetupNumbericPredictorMap()

create map for containing numeric predictor

type MiningField

type MiningField struct {
	Name string `xml:"name,attr"`
}

type NeuralInputs

type NeuralInputs struct {
	Input []Neuron `xml:"NeuralInput"`
}

type NeuralLayer

type NeuralLayer struct {
	Neuron              []Neuron `xml:"Neuron"`
	ActivationFunction  string   `xml:"activationFunction,attr"`
	NormalizationMethod string   `xml:"normalizationMethod,attr"`
	Threshold           float64  `xml:"threshold,attr"`
}

type NeuralNetWorkStructure

type NeuralNetWorkStructure struct {
}

type NeuralNetwork

type NeuralNetwork struct {
	XMLName xml.Name
	//Struct  NeuralNetWorkStructure `xml:"PMML>NeuralNetwork"`
	InputLayer          NeuralInputs  `xml:"NeuralInputs"`
	NeuralOutputs       NeuralOutputs `xml:"NeuralOutputs"`
	OutputLayer         Outputs       `xml:"Output"`
	Fields              []MiningField `xml:"MiningSchema>MiningField"`
	Layers              []NeuralLayer `xml:"NeuralLayer"`
	ActivationFunction  string        `xml:"activationFunction,attr"`
	NormalizationMethod string        `xml:"normalizationMethod,attr"`
	Threshold           float64       `xml:"threshold,attr"`
}

func NewNeuralNetwork

func NewNeuralNetwork(source []byte) (*NeuralNetwork, error)

func NewNeuralNetworkFromReader

func NewNeuralNetworkFromReader(source io.Reader) (*NeuralNetwork, error)

func (*NeuralNetwork) Score

func (nn *NeuralNetwork) Score(feature map[string]interface{}, outputName string) (float64, error)

type NeuralOutput

type NeuralOutput struct {
	OutputNeuron string       `xml:"outputNeuron,attr"`
	DerivedField DerivedField `xml:"DerivedField"`
}

type NeuralOutputs

type NeuralOutputs struct {
	NeuralOutput []NeuralOutput `xml:"NeuralOutput"`
}

type Neuron

type Neuron struct {
	Id               string         `xml:"id,attr"`
	DerivedFieldType DerivedField   `xml:"DerivedField"`
	Bias             float64        `xml:"bias,attr"`
	Contribution     []Contribution `xml:"Con"`
}

type Node

type Node struct {
	XMLName            xml.Name
	Attrs              []xml.Attr         `xml:",any,attr"`
	Content            []byte             `xml:",innerxml"`
	Nodes              []Node             `xml:",any"`
	True               truePredicate      `xml:"True"`
	DummyMiningSchema  dummyMiningSchema  `xml:"MiningSchema"`
	SimplePredicate    SimplePredicate    `xml:"SimplePredicate"`
	SimpleSetPredicate SimpleSetPredicate `xml:"SimpleSetPredicate"`
}

Node - PMML tree node

func (Node) TraverseTree

func (n Node) TraverseTree(features map[string]interface{}) (score float64, err error)

TraverseTree - traverses Node predicates with features and returns score by terminal node

type NormDiscrete

type NormDiscrete struct {
	Value string `xml:"value,attr"`
}

type NormalizationMethod

type NormalizationMethod func(...float64) []float64

type NormalizationMethodMap

type NormalizationMethodMap func(map[string]float64) (map[string]float64, error)

type NumericPredictor

type NumericPredictor struct {
	// struct xml:PMML>RegressionModel>RegressionTable>NumbericPredictor
	Name        string  `xml:"name,attr"`
	Coefficient float64 `xml:"coefficient,attr"`
}

type OutputField

type OutputField struct {
	Name  string `xml:"name,attr"`
	Value string `xml:"value,attr"`
}

type Outputs

type Outputs struct {
	OutputField []OutputField `xml:"OutputField"`
}

type PMMLLR

type PMMLLR struct {
	// struct xml:PMML
	LogisticRegression LogisticRegression `xml:"RegressionModel"`
}

type PMMLNN

type PMMLNN struct {
	NeuralNetwork NeuralNetwork `xml:"NeuralNetwork"`
}

type RandomForest

type RandomForest struct {
	XMLName xml.Name
	Trees   []Node `xml:"MiningModel>Segmentation>Segment>TreeModel"`
}

RandomForest - PMML Random Forest

func LoadRandomForest

func LoadRandomForest(fileName string) (rf RandomForest, err error)

LoadRandomForest - Load Random Forest PMML file to RandomForest struct

func (RandomForest) LabelScores

func (rf RandomForest) LabelScores(features map[string]interface{}) (map[string]float64, error)

LabelScores - traverses all trees in RandomForest with features and maps result labels to how many trees returned those label

func (RandomForest) LabelScoresConcurrently

func (rf RandomForest) LabelScoresConcurrently(features map[string]interface{}) (map[string]float64, error)

LabelScoresConcurrently - same as LabelScores but concurrent

func (RandomForest) Score

func (rf RandomForest) Score(features map[string]interface{}, label string) (float64, error)

Score - traverses all trees in RandomForest with features and returns ratio of given label results count to all results count

func (RandomForest) ScoreConcurrently

func (rf RandomForest) ScoreConcurrently(features map[string]interface{}, label string) (float64, error)

ScoreConcurrently - same as Score but concurrent

type RegressionTable

type RegressionTable struct {
	// struct xml:PMML>RegressionModel>RegressionTable
	Intercept           float64            `xml:"intercept,attr"`
	TargetCategory      string             `xml:"targetCategory,attr"`
	NumericPredictor    []NumericPredictor `xml:"NumericPredictor"`
	NumericPredictorMap *map[string]float64
}

type SimplePredicate

type SimplePredicate struct {
	Field    string `xml:"field,attr"`
	Operator string `xml:"operator,attr"`
	Value    string `xml:"value,attr"`
}

SimplePredicate - PMML simple predicate

func (SimplePredicate) True

func (p SimplePredicate) True(features map[string]interface{}) bool

True - Evaluates to true if features input is true for SimplePredicate

type SimpleSetPredicate

type SimpleSetPredicate struct {
	Field    string `xml:"field,attr"`
	Operator string `xml:"booleanOperator,attr"`
	Values   string `xml:"Array"`
}

SimpleSetPredicate - PMML simple set predicate

func (SimpleSetPredicate) True

func (p SimpleSetPredicate) True(features map[string]interface{}) bool

True - Evaluates to true if features input is true for SimpleSetPredicate

Jump to

Keyboard shortcuts

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