ml

package module
v0.0.0-...-6756cf3 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2016 License: MIT Imports: 5 Imported by: 0

README

ml

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadDim              = errors.New("bad dimenstion for matrix")
	ErrInconsistentData    = errors.New("matrix has different y dimension per x")
	ErrUninitialized       = errors.New("matrix not initialized")
	ErrIdentityInvalidSize = errors.New("current dimension of matrix does not have an identity")
	ErrNegativeIndex       = errors.New("negative index are not supported")
	ErrOutOfBound          = errors.New("index out of bound")
	ErrNotAVector          = errors.New("the current vector has invalid dimension for a vector")
	ErrSingularMatrix      = errors.New("the matrix is singuler")
)

Common erros.

Functions

This section is empty.

Types

type Dataset

type Dataset struct {
	X Matrix `json:"x"`
	Y Vector `json:"y"`
}

Dataset .

type Hypothesis

type Hypothesis interface {
	Fct(Vector) float64                   // h.Fct(x) = y.
	SquaredError(dataset Dataset) float64 // Cost function.
}

Hypothesis .

type LinearRegression

type LinearRegression struct {
	Θ Vector
}

LinearRegression is the linar regression algorithm.

  • Hypothesis: h(x) = Θ[0][0] + Θ[1][0]*x

func (LinearRegression) Fct

func (b LinearRegression) Fct(x Vector) float64

Fct implements the hypothesis function.

func (*LinearRegression) GradientDescent

func (b *LinearRegression) GradientDescent(dataset Dataset, alpha float64, plotData bool) <-chan string

GradientDescent .

func (LinearRegression) PartialDerivative

func (b LinearRegression) PartialDerivative(dataset Dataset, j int) float64

PartialDerivative .

func (LinearRegression) SquaredError

func (b LinearRegression) SquaredError(dataset Dataset) float64

SquaredError process the squared error of the given hypothesis on the given dataset. The squared error equation is: $$\frac{1}{2m}\sum_{i=1}^{m} (h(x^{(i)})-y^{(i)})^2 $$ - 1/(2m) * Sum from i=1 to m of square h(x(i))-y(i). TODO: rename CostFunction() ?

func (LinearRegression) String

func (b LinearRegression) String() string

type MRow

type MRow []float64

MRow is the row type for matrix.

func (MRow) Add

func (mr MRow) Add(mr2 MRow) MRow

Add adds the given matrix to the current one and return the result. NOTE: Does not change current matrix state.

func (MRow) Scale

func (mr MRow) Scale(n float64) MRow

Scale returns the result of the scalar multiplication of the given scalar and the current matrix row. NOTE: Does not change current row state.

func (MRow) ToVector

func (mr MRow) ToVector() Vector

ToVector returns the current row as a vector. Note: Not a copy, changes to the vector affect the row.

type Matrix

type Matrix []MRow

Matrix .

func NewMatrix

func NewMatrix(m, n int) Matrix

NewMatrix instantiates a new matrix of (n,m) dimension.

func (Matrix) Add

func (ma Matrix) Add(ma2 Matrix) Matrix

Add adds the given matrix to the current one and return the result. NOTE: Does not change current matrix state.

func (Matrix) Copy

func (ma Matrix) Copy() Matrix

Copy returns a copy of the current matrix.

func (Matrix) Dim

func (ma Matrix) Dim() (int, int)

Dim returns the dimension of the matrix.

func (Matrix) DimMatch

func (ma Matrix) DimMatch(ma2 Matrix) bool

DimMatch checks if the given matrice has the same dimension as the current one.

func (Matrix) Equal

func (ma Matrix) Equal(ma2 Matrix) bool

Equal compares the given matrix to the current one.

func (Matrix) Extend

func (ma Matrix) Extend(m1, n1 int) Matrix

Extend returns a copy of the current matrix with m more rows and n more cols. The extended rows/cols are set to 0. Extend(0, 0) creates an identical copy of the matrix. NOTE: Does not change state of current matrix.

func (Matrix) Identity

func (ma Matrix) Identity() Matrix

Identity returns the identify matrix for the current one. NOTE: Does not change current matrix state.

func (Matrix) Inverse

func (ma Matrix) Inverse() Matrix

Inverse returns the inverted copy of the current matrix. NOTE: Does not change current matrix state.

func (Matrix) Mul

func (ma Matrix) Mul(ma2 Matrix) Matrix

Mul returns the result of the current matrix multiplied by the given one. NOTE: Does not change current matrix state.

func (Matrix) MulV

func (ma Matrix) MulV(v Vector) Vector

MulV multiplies the current matrix with the given vector. Returns a Vector. NOTE: Does not change current matrix state.

func (Matrix) Scale

func (ma Matrix) Scale(n float64) Matrix

Scale returns the result of the scalar multiplication of the given scalar and the current matrix. NOTE: Does not change current matrix state.

func (Matrix) SetSubMatrix

func (ma Matrix) SetSubMatrix(ma2 Matrix, m, n int) Matrix

SetSubMatrix updates the current matrix with the given submatrix starting at m,n index. NOTE: Changes the state of the current matrix. NOTE: Overflowing submatrix produce an error.

func (Matrix) String

func (ma Matrix) String() string

String pretty prints the matrix.

func (Matrix) Sub

func (ma Matrix) Sub(ma2 Matrix) Matrix

Sub substracts the given matrix to the current one and return the result. NOTE: Does not change current matrix state.

func (Matrix) SubMatrix

func (ma Matrix) SubMatrix(m, n, m1, n1 int) Matrix

SubMatrix return a sub matrix part of the current matrix. Starts at (m,n) index (0 indexed) and of dimension (m1,n1) NOTE: Changes to the sub matrix will change the parent one.

func (Matrix) ToVector

func (ma Matrix) ToVector() Vector

ToVector asserts the current matrix as a vector and returns it.

func (Matrix) Transpose

func (ma Matrix) Transpose() Matrix

Transpose returns a transposed copy of the current matrix. NOTE: Does not change current matrix state.

func (Matrix) Validate

func (ma Matrix) Validate() error

Validate checks if the current matrix is valid. NOTE: When instantiating a matrix outside ml.NewMatrix, Validate should be called.

type Vector

type Vector Matrix

Vector is a matrix with 1 column.

func NewVector

func NewVector(n int) Vector

NewVector instantiate a new vector of dimensin n.

func ToVector

func ToVector(m Matrix) Vector

ToVector converts the matrix type to vector and validates the resulting vector. panic if the given matrix is not of (1,n) dimension.

func (Vector) Dim

func (v Vector) Dim() (int, int)

Dim returns the size of the vector. dim (1,n)

func (Vector) Scale

func (v Vector) Scale(n float64) Vector

Scale .

func (Vector) String

func (v Vector) String() string

func (Vector) SubV

func (v Vector) SubV(v2 Vector) Vector

SubV returns the result of v - v2 as a copy. NOTE: Does not change cureent vector state.

func (Vector) Sum

func (v Vector) Sum() float64

Sum computes the sum of all the vector elements.

func (Vector) Transpose

func (v Vector) Transpose() Matrix

Transpose return a transposed copy of the vector as a matrix (n,1). NOTE: Does not change state of current vector.

func (Vector) Validate

func (v Vector) Validate() error

Validate checks if the current matrix is valid. NOTE: When instantiating a matrix outside ml.NewVector, Validate should be called.

Directories

Path Synopsis
web

Jump to

Keyboard shortcuts

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