regression

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2022 License: MIT Imports: 1 Imported by: 1

README

regression

regression is a simple, written from scratch Go library for basic variants of two the most popular models from the Generalized Linear Models (GLM) family.

regression/linear provides implementation of the linear regression model.

regression/logistic provides implementation of the logistic model.

Why?

Do you need another not fancy machine learning library? Actually, you don't. The main puropose of regression library is learning. Machine learning and AI algorithms remains mystified and magic. So here it is a simple, written from scratch implementation of one of the most popular algorithms solving regression and classification problem. It doesn't throw the responsibility for underyling math (like matrix calculus, iterative optimisation) to external packages. Everything is embedded in this repository.

Install

Examples

Documentation

Overview

Package regression defines interfaces, structures and errors shared by other packages that implement a concrete regression algorithm.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCannotConverge is returned if gradient descent cannot converge.
	// It usually means that the learning rate is too large.
	ErrCannotConverge = errors.New("cannot converge")
	// ErrUnsupportedGradientDescentVariant is returned if unsupported gradient descent variant was chosen.
	ErrUnsupportedGradientDescentVariant = errors.New("unsupported gradient descent variant")
	// ErrUnsupportedConverganceType is returned if unsupported convergance type was chosen.
	ErrUnsupportedConverganceType = errors.New("unsupported convergance type")
	// ErrInvalidTrainingSet is returned if features vectors included in the set are not consistent.
	ErrInvalidTrainingSet = errors.New("invalid training set")
	// ErrInvalidFeatureVector is returned if feature vector is invalid.
	ErrInvalidFeatureVector = errors.New("invalid feature vector")
)

Functions

This section is empty.

Types

type Model

type Model[T TargetType] interface {
	// Predict returns the predicated target value for the given input.
	Predict(x []float64) (T, error)
	// Coefficients returns the trained regression model's coefficients.
	Coefficients() []float64
	// Accuracy returns calculated accuracy for trained model.
	Accuracy() float64
}

A Model is a trained regression model.

type Regression

type Regression[T TargetType] interface {
	// Run runs regression against input training set.
	// It returns trained Model if succeded, otherwise returns an error.
	Run(s TrainingSet) (Model[T], error)
}

A Regression is a regression runner. It provides an abstraction for model training.

type RegressionFunc

type RegressionFunc[T TargetType] func(s TrainingSet) (Model[T], error)

RegressionFunc is an adapter to allow the use of plain functions as regressions.

func (RegressionFunc[T]) Run

func (f RegressionFunc[T]) Run(s TrainingSet) (Model[T], error)

Run calls f(s).

type TargetType

type TargetType interface {
	~float64 | ~int
}

TargetType is a constraint that permits two types (float64 or integer) for target value. Floating point numbers are used for continous value of y, while integer corresponds to the discrete one.

type TrainingExample

type TrainingExample struct {
	Features []float64
	Target   float64
}

TrainingExample represents a single (features, target) example.

type TrainingSet

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

TrainingSet represents set of traning examples.

func NewTrainingSet

func NewTrainingSet(exs []TrainingExample) (*TrainingSet, error)

NewTrainingSet creates a new training set if examples are consistent. It doesn't check for correctness of values. If examples are not consistent, returns an error.

func (*TrainingSet) AddDummyFeatures

func (s *TrainingSet) AddDummyFeatures()

AddDummyFeatures puts dummy feature (equals 1) for each training example being a part of the training set.

func (*TrainingSet) Examples

func (s *TrainingSet) Examples() []TrainingExample

Examples returns all training examples from training set.

func (*TrainingSet) GetDesignMatrix

func (s *TrainingSet) GetDesignMatrix() [][]float64

GetDesignMatrix returns the design matrix from the training set.

func (*TrainingSet) GetTargetVector

func (s *TrainingSet) GetTargetVector() []float64

GetTargetVector returns the target vector from the training set.

Directories

Path Synopsis
internal
gd
Package gd provides gradient descent implementation.
Package gd provides gradient descent implementation.
Package linear provides linear regression model implementation.
Package linear provides linear regression model implementation.
Package options defines training options for specific regression variants.
Package options defines training options for specific regression variants.

Jump to

Keyboard shortcuts

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