modelfox

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2022 License: MIT Imports: 10 Imported by: 2

README

ModelFox for Go

The ModelFox Go module makes it easy to make predictions with your ModelFox machine learning model from Go.

Usage

$ go get -u github.com/modelfoxdotdev/modelfox-go
import "github.com/modelfoxdotdev/modelfox-go"

model, _ := modelfox.LoadModelFromPath("./heart_disease.modelfox", nil)
defer model.Destroy()

input := modelfox.PredictInput{
  "age":    63,
  "gender": "male",
  // ...
}

output := model.PredictOne(input, nil)

fmt.Println("Output:", output.ClassName)

For more information, read the docs.

Platform Support

ModelFox for Go is currently supported on the following combinations of $GOARCH and $GOOS:

  • amd64 linux
  • arm64 linux
  • amd64 darwin
  • arm64 darwin
  • amd64 windows

Are you interested in another platform? Open an issue or send us an email at help@modelfox.dev.

ModelFox for Go links to the modelfox C library, so cgo is required. The modelfox C library will be linked statically into your executable, so when you run go build you will still get a statically linked executable you can run anywhere without having to worry about dynamic linking errors.

Examples

The source for this package contains a number of examples in the examples directory. Each example has a README.md explaining how to run it.

Documentation

Index

Constants

View Source
const (
	RegressionTaskType = iota
	BinaryClassificationTaskType
	MulticlassClassificationTaskType
)
View Source
const (
	// IdentityFeatureContributionType is the feature contribution type of an identity feature group.
	IdentityFeatureContributionType = iota
	// NormalizedFeatureContributionType is the feature contribution type of a normalized feature group.
	NormalizedFeatureContributionType
	// OneHotEncodedFeatureContributionType is the feature contribution type of a one hot encoded feature group.
	OneHotEncodedFeatureContributionType
	// BagOfWordsFeatureContributionType is the feature contribution type of a bag of words feature group.
	BagOfWordsFeatureContributionType
	// BagOfWordsCosineSimilarityFeatureContributionType is the feature contribution type of a bag of words cosine similarity feature group.
	BagOfWordsCosineSimilarityFeatureContributionType
	// WordEmbeddingFeatureContributionType is the feature contribution type of a word embedding feature group.
	WordEmbeddingFeatureContributionType
)
View Source
const (
	UnigramType = iota
	BigramType
)

Variables

This section is empty.

Functions

func Version

func Version() string

This is the version of libmodelfox that is in use.

Types

type BagOfWordsCosineSimilarityFeatureContribution

type BagOfWordsCosineSimilarityFeatureContribution struct {
	// This is the name of the source column a for the feature group.
	ColumnNameA string
	// This is the name of the source column b for the feature group.
	ColumnNameB string
	// This is the value of the feature.
	FeatureValue float32
	// This is the amount that the feature contributed to the output.
	FeatureContributionValue float32
}

This describes the contribution of a feature from a bag of words cosine similarity feature group.

type BagOfWordsFeatureContribution

type BagOfWordsFeatureContribution struct {
	// This is the name of the source column for the feature group.
	ColumnName string
	// This is the ngram for the feature.
	NGram NGram
	// This is the value of the feature.
	FeatureValue float32
	// This is the amount that the feature contributed to the output.
	FeatureContributionValue float32
}

This describes the contribution of a feature from a bag of words feature group.

type Bigram

type Bigram struct {
	// This is the first token in the bigram.
	TokenA string
	// This is the second token in the bigram.
	TokenB string
}

This describes a bigram ngram.

type BinaryClassificationPredictOutput

type BinaryClassificationPredictOutput struct {
	// This is the name of the predicted class.
	ClassName string `json:"className"`
	// This is the probability the model assigned to the predicted class.
	Probability float32 `json:"probability"`
	// If computing feature contributions was enabled in the predict options, this value will explain the model's output, showing how much each feature contributed to the output.
	FeatureContributions FeatureContributions `json:"-"`
}

`Predict` outputs `BinaryClassificationPredictOutput` when the model's task is regression.

type FeatureContributionEntry

type FeatureContributionEntry interface {
	// contains filtered or unexported methods
}

FeatureContribution represents a feature contribution.

type FeatureContributionType

type FeatureContributionType int

This identifies the type of a feature contribution.

type FeatureContributions

type FeatureContributions struct {
	// This is the value the model would output if all features had baseline values.
	BaselineValue float32
	// This is the value the model output. Any difference from the `baseline_value` is because of the deviation of the features from their baseline values.
	OutputValue float32
	// This list will contain one entry for each of the model's features. Note that features are computed from columns, so there will likely be more features than columns.
	Entries []FeatureContributionEntry
}

This is a description of the feature contributions for the prediction if the task is regression or binary classification, or for a single class if the task is multiclass classification.

type IdentityFeatureContribution

type IdentityFeatureContribution struct {
	// This is the name of the source column for the feature group.
	ColumnName string
	// This is the value of the feature.
	FeatureValue float32
	// This is the amount that the feature contributed to the output.
	FeatureContributionValue float32
}

This describes the contribution of a feature from an identity feature group

type LoadModelOptions

type LoadModelOptions struct {
	// If you are running the app locally or on your own server, use this field to provide the url to it. If not specified, the default value is https://app.modelfox.dev.
	ModelFoxURL string
}

These are the options passed when loading a model.

type LogPredictionArgs

type LogPredictionArgs struct {
	// This is a unique identifier for the prediction, which will associate it with a true value event and allow you to look it up in the app.
	Identifier string
	// This is the same `PredictInput` value that you passed to `model.Predict`.
	Input PredictInput
	// This is the same `PredictOptions` value that you passed to `model.Predict`.
	Options PredictOptions
	// This is the output returned by `model.Predict`.
	Output PredictOutput
}

This is the type of the argument to `model.LogPrediction` and `model.EnqueueLogPrediction` which specifies the details of the prediction to log.

type LogTrueValueArgs

type LogTrueValueArgs struct {
	// This is a unique identifier for the prediction, which will associate it with a true value event and allow you to look it up in the app.
	Identifier string
	// This is the true value for the prediction.
	TrueValue interface{}
}

This is the type of the argument to `model.LogTrueValue` and `model.EnqueueLogTrueValue` which specifies the details of the true value to log.

type Model

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

Use this struct to load a model, make predictions, and log events to the app.

func LoadModelFromBytes

func LoadModelFromBytes(data []byte, options *LoadModelOptions) (*Model, error)

Load a model from bytes instead of a file. You should use this only if you already have a `.modelfox` loaded into memory. Otherwise, use `model.LoadModelFromPath`, which is faster because it memory maps the file.

func LoadModelFromPath

func LoadModelFromPath(path string, options *LoadModelOptions) (*Model, error)

Load a model from a `.modelfox` file at `path`.

func (Model) Destroy

func (m Model) Destroy()

Destroy frees up the memory used by the model. You should call this with defer after loading your model.

func (Model) EnqueueLogPrediction

func (m Model) EnqueueLogPrediction(args LogPredictionArgs)

Add a prediction event to the queue. Remember to call `model.FlushLogQueue` at a later point to send the event to the app.

func (Model) EnqueueLogTrueValue

func (m Model) EnqueueLogTrueValue(args LogTrueValueArgs)

Add a true value event to the queue. Remember to call `model.FlushLogQueue` at a later point to send the event to the app.

func (Model) FlushLogQueue

func (m Model) FlushLogQueue() error

Send all events in the queue to the app.

func (Model) ID

func (m Model) ID() string

Retrieve the model's id.

func (Model) LogPrediction

func (m Model) LogPrediction(args LogPredictionArgs) error

Send a prediction event to the app. If you want to batch events, you can use `model.EnqueueLogPrediction` instead.

func (Model) LogTrueValue

func (m Model) LogTrueValue(args LogTrueValueArgs) error

Send a true value event to the app. If you want to batch events, you can use `model.EnqueueLogTrueValue` instead.

func (Model) Predict

func (m Model) Predict(input []PredictInput, options *PredictOptions) []PredictOutput

Make a prediction with multiple inputs.

func (Model) PredictOne

func (m Model) PredictOne(input PredictInput, options *PredictOptions) PredictOutput

Make a prediction with a single input.

type MulticlassClassificationPredictOutput

type MulticlassClassificationPredictOutput struct {
	// This is the name of the predicted class.
	ClassName string `json:"className`
	// This is the probability the model assigned to the predicted class.
	Probability float32 `json:"probability"`
	// This value maps from class names to the probability the model assigned to each class.
	Probabilities map[string]float32 `json:"probabilities"`
	// If computing feature contributions was enabled in the predict options, this value will explain the model's output, showing how much each feature contributed to the output. This value maps from class names to `FeatureContributions` values for each class. The class with the `FeatureContributions` value with the highest `output_value` is the predicted class.
	FeatureContributions map[string]FeatureContributions `json:"-"`
}

`Predict` outputs `MulticlassClassificationPredictOutput` when the model's task is regression.

type NGram

type NGram interface {
	// contains filtered or unexported methods
}

NGram is the token type in a bag of words feature group.

type NormalizedFeatureContribution

type NormalizedFeatureContribution struct {
	// This is the name of the source column for the feature group.
	ColumnName string
	// This is the value of the feature.
	FeatureValue float32
	// This is the amount that the feature contributed to the output.
	FeatureContributionValue float32
}

This describes the contribution of a feature from a normalized feature group.

type OneHotEncodedFeatureContribution

type OneHotEncodedFeatureContribution struct {
	// This is the name of the source column for the feature group.
	ColumnName string
	// This is the enum variant the feature indicates the presence of.
	Variant string
	// This is the value of the feature.
	FeatureValue bool
	// This is the amount that the feature contributed to the output.
	FeatureContributionValue float32
}

This describes the contribution of a feature from a one hot encoded feature group.

type PredictInput

type PredictInput map[string]interface{}

This is the input type of `Predict`. A predict input is a map from strings to strings or floats. The keys should match the columns in the CSV file you trained your model with.

type PredictOptions

type PredictOptions struct {
	// If your model is a binary classifier, use this field to make predictions using a threshold chosen on the tuning page of the app. The default value is `0.5`.
	Threshold float32 `json:"threshold"`
	// Computing feature contributions is disabled by default. If you set this field to `true`, you will be able to access the feature contributions with the `feature_contributions` field of the predict output.
	ComputeFeatureContributions bool `json:"computeFeatureContributions"`
}

These are the options passed to `Predict`.

type PredictOutput

type PredictOutput interface {
	// contains filtered or unexported methods
}

This is the return type of `Predict`.

type RegressionPredictOutput

type RegressionPredictOutput struct {
	// This is the predicted value.
	Value float32 `json:"value"`
	// If computing feature contributions was enabled in the predict options, this value will explain the model's output, showing how much each feature contributed to the output.
	FeatureContributions FeatureContributions `json:"-"`
}

`Predict` outputs `RegressionPredictOutput` when the model's task is regression.

type TaskType

type TaskType int

TaskType is the type of the task corresponding to the model task, one of RegressionTaskType, BinaryClassificationTaskType, and MulticlassClassificationTaskType.

type Unigram

type Unigram struct {
	// This is the token.
	Token string
}

This describes a unigram ngram.

type WordEmbeddingFeatureContribution

type WordEmbeddingFeatureContribution struct {
	// This is the name of the source column for the feature group.
	ColumnName string
	// This is the index of the feature in the word embedding.
	ValueIndex int
	// This is the amount that the feature contributed to the output.
	FeatureContributionValue float32
}

This describes the contribution of a feature from a word embedding feature group.

Directories

Path Synopsis
examples
advanced Module
basic Module

Jump to

Keyboard shortcuts

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