xgp

package module
v0.0.0-...-8433fb5 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2018 License: MIT Imports: 16 Imported by: 6

README



XGP is a machine learning library for performing symbolic regression. It can be used both for regression and classification tasks. Please refer to the documentation for an in-depth introduction to symbolic regression.

Interfaces

The core library is written in Go but it can be used in different ways:

Usage examples

Command-line interface (CLI)

>>> xgp fit train.csv
>>> xgp predict test.csv

Go

package main

import "github.com/MaxHalford/xgp"

func main() {
    config := xgp.NewDefaultGPConfig()
    estimator := config.NewGP()

    estimator.Fit(XTrain, YTrain)
    yPred := estimator.Predict()
}

Python

import xgp

model = xgp.XGPRegressor()

model.fit(X_train, y_train)
y_pred = model.predict(X_test)

Dependencies

The core of XGP has the following dependencies.

License

The MIT License (MIT). Please see the LICENSE file for more information.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Crossover

type Crossover interface {
	Apply(op1, op2 op.Operator, rng *rand.Rand) (op.Operator, op.Operator)
}

A Crossover takes two Operators and combines them in order to produce two new Operators.

type FullInit

type FullInit struct{}

FullInit can generate an Operator of height maxHeight.

func (FullInit) Apply

func (full FullInit) Apply(
	minHeight uint,
	maxHeight uint,
	newOp func(leaf bool, rng *rand.Rand) op.Operator,
	rng *rand.Rand,
) op.Operator

Apply FullInit returns an Operator of height maxHeight, hence minHeight is not taken into account.

type GP

type GP struct {
	GPConfig

	EvalMetric       metrics.Metric
	LossMetric       metrics.Metric
	Functions        []op.Operator
	Initializer      Initializer
	GA               *eaopt.GA
	PointMutation    PointMutation
	SubtreeMutation  SubtreeMutation
	HoistMutation    HoistMutation
	SubtreeCrossover SubtreeCrossover

	X    [][]float64
	Y    []float64
	W    []float64
	XVal [][]float64
	YVal []float64
	WVal []float64
	// contains filtered or unexported fields
}

An GP links all the different components together and can be used to train Programs on a dataset. You shouldn't instantiate this struct directly; instead you should use the GPConfig struct and call it's NewGP method.

func (GP) BestProgram

func (gp GP) BestProgram() (Program, error)

BestProgram returns the GP's best obtained Program.

func (*GP) Fit

func (gp *GP) Fit(

	X [][]float64,
	Y []float64,

	W []float64,
	XVal [][]float64,
	YVal []float64,
	WVal []float64,
	verbose bool,
) error

Fit an GP to a dataset.

func (GP) Predict

func (gp GP) Predict(X [][]float64, proba bool) ([]float64, error)

Predict makes predictions with the best obtained Program as so far.

func (GP) PredictPartial

func (gp GP) PredictPartial(x []float64, proba bool) (float64, error)

PredictPartial is a convenience function on top of Predict to make predictions on a single instance.

func (GP) String

func (gp GP) String() string

String representation of an GP.

type GPConfig

type GPConfig struct {
	// Learning parameters
	LossMetric     metrics.Metric
	EvalMetric     metrics.Metric
	ParsimonyCoeff float64
	PolishBest     bool
	// Function parameters
	Funcs     string
	ConstMin  float64
	ConstMax  float64
	PConst    float64
	PFull     float64
	PLeaf     float64
	MinHeight uint
	MaxHeight uint
	// Genetic algorithm parameters
	NPopulations      uint
	NIndividuals      uint
	NGenerations      uint
	PHoistMutation    float64
	PSubtreeMutation  float64
	PPointMutation    float64
	PointMutationRate float64
	PSubtreeCrossover float64
	// Other
	RNG *rand.Rand
}

A GPConfig contains all the information needed to instantiate an GP.

Example
var conf = NewDefaultGPConfig()
conf.LossMetric = metrics.F1{}
var est, err = conf.NewGP()
if err != nil {
	fmt.Println(err)
}
fmt.Println(est)
Output:

Loss metric: neg_f1
Evaluation metric: f1
Parsimony coefficient: 0
Polish best program: true
Functions: add,sub,mul,div
Constant minimum: -5
Constant maximum: 5
Constant probability: 0.5
Full initialization probability: 0.5
Terminal probability: 0.3
Minimum height: 3
Maximum height: 5
Number of populations: 1
Number of individuals per population: 100
Number of generations: 30
Hoist mutation probability: 0.1
Subtree mutation probability: 0.1
Point mutation probability: 0.1
Point mutation rate: 0.3
Subtree crossover probability: 0.5

func NewDefaultGPConfig

func NewDefaultGPConfig() GPConfig

NewDefaultGPConfig returns a GPConfig with default values.

func (GPConfig) NewGP

func (c GPConfig) NewGP() (*GP, error)

NewGP returns an GP from a GPConfig.

func (GPConfig) String

func (c GPConfig) String() string

String representation of a GPConfig. It returns a string containing the parameters line by line.

type GrowInit

type GrowInit struct {
	PLeaf float64
}

GrowInit can generate an Operator who's depth lies in [MinHeight, MaxHeight].

func (GrowInit) Apply

func (grow GrowInit) Apply(
	minHeight uint,
	maxHeight uint,
	newOp func(leaf bool, rng *rand.Rand) op.Operator,
	rng *rand.Rand,
) op.Operator

Apply GrowInit.

type HoistMutation

type HoistMutation struct {
	Weight1, Weight2 func(operator op.Operator, depth uint, rng *rand.Rand) float64
}

HoistMutation replaces an Operator by of it's operands.

func (HoistMutation) Apply

func (hm HoistMutation) Apply(operator op.Operator, rng *rand.Rand) op.Operator

Apply HoistMutation.

type Initializer

type Initializer interface {
	Apply(
		minHeight uint,
		maxHeight uint,
		newOp func(leaf bool, rng *rand.Rand) op.Operator,
		rng *rand.Rand,
	) op.Operator
}

An Initializer generates a random Operator with random operands.

type Mutator

type Mutator interface {
	Apply(operator op.Operator, rng *rand.Rand) op.Operator
}

A Mutator takes an Operator and returns a modified version of it.

type PointMutation

type PointMutation struct {
	Rate   float64
	Mutate func(op op.Operator, rng *rand.Rand) op.Operator
}

PointMutation randomly replaces Operators.

func (PointMutation) Apply

func (pm PointMutation) Apply(operator op.Operator, rng *rand.Rand) op.Operator

Apply PointMutation.

type Program

type Program struct {
	*GP
	Op op.Operator
}

A Program is a thin layer on top of an Operator.

func (Program) Clone

func (prog Program) Clone() eaopt.Genome

Clone is required to implement eaopt.Genome.

func (*Program) Crossover

func (prog *Program) Crossover(prog2 eaopt.Genome, rng *rand.Rand)

Crossover is required to implement eaopt.Genome.

func (Program) Evaluate

func (prog Program) Evaluate() (float64, error)

Evaluate is required to implement eaopt.Genome.

func (Program) MarshalJSON

func (prog Program) MarshalJSON() ([]byte, error)

MarshalJSON serializes a Program.

func (*Program) Mutate

func (prog *Program) Mutate(rng *rand.Rand)

Mutate is required to implement eaopt.Genome.

func (Program) Predict

func (prog Program) Predict(X [][]float64, proba bool) ([]float64, error)

Predict predicts the output of a slice of features.

func (Program) PredictPartial

func (prog Program) PredictPartial(x []float64, proba bool) (float64, error)

PredictPartial is a convenience function on top of Predict to make predictions on a single instance.

func (Program) String

func (prog Program) String() string

String formatting.

func (*Program) UnmarshalJSON

func (prog *Program) UnmarshalJSON(bytes []byte) error

UnmarshalJSON parses a Program.

type RampedHaldAndHalfInit

type RampedHaldAndHalfInit struct {
	PFull    float64
	FullInit FullInit
	GrowInit GrowInit
}

RampedHaldAndHalfInit randomly uses GrowInit and FullInit. If it uses FullInit then it uses a random height.

func (RampedHaldAndHalfInit) Apply

func (rhah RampedHaldAndHalfInit) Apply(
	minHeight uint,
	maxHeight uint,
	newOp func(leaf bool, rng *rand.Rand) op.Operator,
	rng *rand.Rand,
) op.Operator

Apply RampedHaldAndHalfInit.

type SubtreeCrossover

type SubtreeCrossover struct {
	Weight func(operator op.Operator, depth uint, rng *rand.Rand) float64
}

SubtreeCrossover applies subtree crossover to two Operators.

func (SubtreeCrossover) Apply

func (sc SubtreeCrossover) Apply(op1, op2 op.Operator, rng *rand.Rand) (op.Operator, op.Operator)

Apply SubtreeCrossover.

type SubtreeMutation

type SubtreeMutation struct {
	Weight      func(operator op.Operator, depth uint, rng *rand.Rand) float64
	NewOperator func(rng *rand.Rand) op.Operator
}

SubtreeMutation selects a suboperator at random and replaces it with a new Operator.

func (SubtreeMutation) Apply

func (sm SubtreeMutation) Apply(operator op.Operator, rng *rand.Rand) op.Operator

Apply SubtreeMutation.

Directories

Path Synopsis
cmd
xgp

Jump to

Keyboard shortcuts

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