package
0.0.0-20220715001353-00e0c845ae1c
Repository: https://github.com/cdipaolo/goml.git
Documentation: pkg.go.dev

# README

Generalized Linear Models

import "github.com/cdipaolo/goml/linear"

GoDoc

This part of the goml package implements varied generalized linear models using gradient descent (currently, though more options for optimization methods might be available in the future.)

implemented models

Linear Least Squares RegressionLogistic Regression Classification (Color is Ground Truth Class)
Linear Least Squares Regression ResultsLogistic Regression Results

example ordinary least squares

this is mostly from from the linear_test.go tests. You can find more examples from the testing files. The line given is z = 10 + (x/10) + (y/5)

// initialize data
threeDLineX = [][]float64{}
threeDLineY = []float64{}
// the line z = 10 + (x/10) + (y/5)
for i := -10; i < 10; i++ {
    for j := -10; j < 10; j++ {
        threeDLineX = append(threeDLineX, []float64{float64(i), float64(j)})
        threeDLineY = append(threeDLineY, 10+float64(i)/10+float64(j)/5)
    }
}

// initialize model
//
// use optimization method of Stochastic Gradient Ascent
// use α (learning rate) = .0001 / 1e-4
// use λ (regularization term) = 13.06
// set the max iteration cap for gradient
//     descent to be 1000/1e3 iterations
// and finally pass in the data
model := linear.NewLeastSquares(base.StochasticGA, 1e-4, 13.06, 1e3, threeDLineX, threeDLineY)

// learn
err = model.Learn()
if err != nil {
    panic("There was some error learning")
}

// predict based on model
guess, err = model.Predict([]float64{12.016, 6.523})
if err != nil {
    panic("There was some error in the prediction")
}

// persist the model to disk
//
// path to file will be '/tmp/.goml/LeastSquares'
// and it stores the parameter vector θ as a JSON
// array
err = model.PersistToFile("/tmp/.goml/LeastSquares")
if err != nil {
    panic("There was some error persisting the model to a file!")
}

// restore the model from file
//
// note that you could have a file with a JSON
// array of floats from some other tool and it
// would import correctly as well
err = model.RestoreFromFile("/tmp/.goml/LeastSquares")
if err != nil {
    panic("There was some error persisting the model to a file!")
}

gradient descent optimization

Here's some data relating the cost function J(θ) and the number of iterations of the data using a 3d model. Note that in this case the data modeled off of was perfectly linear, so obviously the cost function wouldn't and shouldn't bottom out at 0.000... for real world data!

Nice Looking Graph!

# Functions

NewLeastSquares returns a pointer to the linear model initialized with the learning rate alpha, the training set trainingSet, and the expected results upon which to use the dataset to train, expectedResults.
NewLocalLinear returns a pointer to the linear model initialized with the learning rate alpha, the training set trainingSet, and the expected results upon which to use the dataset to train, expectedResults.
NewLogistic takes in a learning rate alpha, a regularization parameter value (0 means no regularization, higher value means higher bias on the model,) the maximum number of iterations the data can go through in gradient descent, as well as a training set and expected results for that training set.
NewSoftmax takes in a learning rate alpha, a regularization parameter value (0 means no regularization, higher value means higher bias on the model,) the maximum number of iterations the data can go through in gradient descent, as well as a training set and expected results for that training set.

# Structs

LeastSquares implements a standard linear regression model with a Least Squares cost function.
LocalLinear implements a locally weighted linear least squares regression.
Logistic represents the logistic classification model with a sigmoidal hypothesis https://en.wikipedia.org/wiki/Logistic_regression The model is currently optimized using Gradient Ascent, not Newton's method, etc.
Softmax represents a softmax classification model in 'k' demensions.