linear

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package linear provides linear machine learning algorithms and models.

This package implements various linear models for regression and classification tasks:

  • LinearRegression: Ordinary least squares regression with L2 regularization support
  • scikit-learn compatibility: Import/export linear models trained in Python
  • High-performance matrix operations using gonum/mat
  • Production-ready with comprehensive error handling and validation

Linear models are fundamental building blocks in machine learning, offering:

  • Fast training and prediction
  • Interpretable coefficients and feature importance
  • Memory efficient implementation
  • Robust numerical stability using QR decomposition

Example usage:

lr := linear.NewLinearRegression()
err := lr.Fit(X, y) // X: features, y: target values
if err != nil {
	log.Fatal(err)
}
predictions, err := lr.Predict(XTest)

The package supports model persistence and scikit-learn interoperability:

// Save trained model
err = lr.ExportToSKLearn("model.json")

// Load Python-trained model
err = lr.LoadFromSKLearn("sklearn_model.json")

All algorithms follow the standard estimator interface with Fit/Predict methods and integrate seamlessly with preprocessing pipelines and model evaluation tools.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LinearRegression

type LinearRegression struct {
	State     *model.StateManager // State manager (composition instead of embedding) - Public for gob encoding
	Weights   *mat.VecDense       // Model weights (coefficients)
	Intercept float64             // Model intercept
	NFeatures int                 // Number of features
	// contains filtered or unexported fields
}

LinearRegression is a linear regression model

Example

ExampleLinearRegression demonstrates basic linear regression usage

package main

import (
	"fmt"
	"log"

	"github.com/YuminosukeSato/scigo/linear"
	"gonum.org/v1/gonum/mat"
)

func main() {
	// Create simple training data: y = 2*x + 1
	X := mat.NewDense(4, 1, []float64{1.0, 2.0, 3.0, 4.0})
	y := mat.NewDense(4, 1, []float64{3.0, 5.0, 7.0, 9.0})

	// Create and train model
	lr := linear.NewLinearRegression()
	err := lr.Fit(X, y)
	if err != nil {
		log.Fatal(err)
	}

	// Make predictions
	testX := mat.NewDense(2, 1, []float64{5.0, 6.0})
	predictions, err := lr.Predict(testX)
	if err != nil {
		log.Fatal(err)
	}

	// Print predictions
	fmt.Printf("Input: %.1f, Prediction: %.1f\n", testX.At(0, 0), predictions.At(0, 0))
	fmt.Printf("Input: %.1f, Prediction: %.1f\n", testX.At(1, 0), predictions.At(1, 0))

}
Output:

Input: 5.0, Prediction: 11.0
Input: 6.0, Prediction: 13.0
Example (ModelEvaluation)

ExampleLinearRegression_modelEvaluation demonstrates model evaluation

package main

import (
	"fmt"
	"log"

	"github.com/YuminosukeSato/scigo/linear"
	"gonum.org/v1/gonum/mat"
)

func main() {
	// Create training data
	X := mat.NewDense(5, 1, []float64{1.0, 2.0, 3.0, 4.0, 5.0})
	y := mat.NewDense(5, 1, []float64{2.0, 4.0, 6.0, 8.0, 10.0})

	// Train model
	lr := linear.NewLinearRegression()
	err := lr.Fit(X, y)
	if err != nil {
		log.Fatal(err)
	}

	// Evaluate on training data
	score, err := lr.Score(X, y)
	if err != nil {
		log.Fatal(err)
	}

	// Check if model is fitted
	fmt.Printf("Model fitted: %t\n", lr.IsFitted())
	fmt.Printf("R² Score: %.3f\n", score)

}
Output:

Model fitted: true
R² Score: 1.000
Example (MultipleFeatures)

ExampleLinearRegression_multipleFeatures demonstrates multiple feature regression

package main

import (
	"fmt"
	"log"

	"github.com/YuminosukeSato/scigo/linear"
	"gonum.org/v1/gonum/mat"
)

func main() {
	// Create training data with 2 features
	X := mat.NewDense(4, 2, []float64{
		1.0, 1.0, // Sample 1: [1, 1]
		2.0, 1.0, // Sample 2: [2, 1]
		1.0, 2.0, // Sample 3: [1, 2]
		2.0, 2.0, // Sample 4: [2, 2]
	})

	// Target: y = x1 + 2*x2
	y := mat.NewDense(4, 1, []float64{3.0, 4.0, 5.0, 6.0})

	// Train model
	lr := linear.NewLinearRegression()
	err := lr.Fit(X, y)
	if err != nil {
		log.Fatal(err)
	}

	// Get model parameters
	weights := lr.GetWeights()
	intercept := lr.GetIntercept()

	fmt.Printf("Weights: [%.1f, %.1f]\n", weights[0], weights[1])
	fmt.Printf("Intercept: %.1f\n", intercept)

}
Output:

Weights: [1.0, 2.0]
Intercept: 0.0
Example (Persistence)

ExampleLinearRegression_persistence demonstrates model saving/loading

package main

import (
	"fmt"
	"log"

	"github.com/YuminosukeSato/scigo/linear"
	"gonum.org/v1/gonum/mat"
)

func main() {
	// Create and train model
	X := mat.NewDense(3, 1, []float64{1.0, 2.0, 3.0})
	y := mat.NewDense(3, 1, []float64{1.0, 2.0, 3.0})

	lr := linear.NewLinearRegression()
	err := lr.Fit(X, y)
	if err != nil {
		log.Fatal(err)
	}

	// Get weights before export
	originalWeights := lr.GetWeights()
	originalIntercept := lr.GetIntercept()

	// Format intercept to avoid -0 display
	interceptStr := "0"
	if originalIntercept > 0.001 || originalIntercept < -0.001 {
		interceptStr = fmt.Sprintf("%.3f", originalIntercept)
	}

	fmt.Printf("Original model - Weight: %.3f, Intercept: %s\n",
		originalWeights[0], interceptStr)

	// In a real scenario, you would save to file and load in another process
	// This example shows the model parameters are preserved
	fmt.Printf("Model can be exported and imported using ExportToSKLearn/LoadFromSKLearn methods\n")

}
Output:

Original model - Weight: 1.000, Intercept: 0
Model can be exported and imported using ExportToSKLearn/LoadFromSKLearn methods

func NewLinearRegression

func NewLinearRegression() *LinearRegression

NewLinearRegression creates a new linear regression model for ordinary least squares regression.

The model uses QR decomposition for numerical stability and supports both single and multiple linear regression tasks. The returned model must be trained using the Fit method before making predictions.

Returns:

  • *LinearRegression: A new untrained linear regression model

Example:

lr := linear.NewLinearRegression()
err := lr.Fit(X, y)
predictions, err := lr.Predict(X_test)

func (*LinearRegression) ExportToSKLearn

func (lr *LinearRegression) ExportToSKLearn(filename string) (err error)

ExportToSKLearn exports the model in scikit-learn compatible JSON format

Parameters:

  • filename: Output filename

Returns:

  • error: Error if export fails

func (*LinearRegression) ExportToSKLearnWriter

func (lr *LinearRegression) ExportToSKLearnWriter(w io.Writer) (err error)

ExportToSKLearnWriter exports the model to a Writer in scikit-learn compatible format

Parameters:

  • w: Output Writer

Returns:

  • error: Error if export fails

func (*LinearRegression) Fit

func (lr *LinearRegression) Fit(X, y mat.Matrix) (err error)

Fit trains the linear regression model using the provided training data.

The method uses QR decomposition to solve the normal equation (X^T * X)w = X^T * y for numerical stability and handles both overdetermined and underdetermined systems. After successful training, the model's fitted state is set to true.

Parameters:

  • X: Feature matrix of shape (n_samples, n_features)
  • y: Target vector of shape (n_samples, 1) or (n_samples, n_targets)

Returns:

  • error: nil if training succeeds, otherwise an error describing the failure

Errors:

  • ErrEmptyData: if X or y are empty
  • ErrDimensionMismatch: if the number of samples in X and y don't match
  • ErrSingularMatrix: if X^T * X is singular and cannot be inverted

Example:

X := mat.NewDense(100, 5, nil) // 100 samples, 5 features
y := mat.NewVecDense(100, nil) // 100 target values
err := lr.Fit(X, y)
if err != nil {
    log.Fatal(err)
}

func (*LinearRegression) GetIntercept

func (lr *LinearRegression) GetIntercept() float64

GetIntercept returns the learned intercept

func (*LinearRegression) GetParams added in v0.4.0

func (lr *LinearRegression) GetParams() map[string]interface{}

GetParams returns the model's hyperparameters.

func (*LinearRegression) GetWeights

func (lr *LinearRegression) GetWeights() []float64

GetWeights returns the learned weights (coefficients)

func (*LinearRegression) IsFitted added in v0.4.0

func (lr *LinearRegression) IsFitted() bool

IsFitted returns whether the model has been fitted.

func (*LinearRegression) LoadFromSKLearn

func (lr *LinearRegression) LoadFromSKLearn(filename string) (err error)

LoadFromSKLearn loads a model from a JSON file exported from scikit-learn

Parameters:

  • filename: Path to the JSON file

Returns:

  • error: Loading error

Example:

lr := NewLinearRegression()
err := lr.LoadFromSKLearn("sklearn_model.json")

func (*LinearRegression) LoadFromSKLearnReader

func (lr *LinearRegression) LoadFromSKLearnReader(r io.Reader) (err error)

LoadFromSKLearnReader loads a scikit-learn model from a Reader

Parameters:

  • r: Reader containing JSON data

Returns:

  • error: Loading error

func (*LinearRegression) Predict

func (lr *LinearRegression) Predict(X mat.Matrix) (_ mat.Matrix, err error)

Predict generates predictions for the input feature matrix using the trained model.

The method computes predictions using the learned weights and intercept: y_pred = X * weights + intercept. The model must be fitted before calling this method, otherwise it returns an error.

Parameters:

  • X: Feature matrix of shape (n_samples, n_features) for prediction

Returns:

  • mat.Matrix: Prediction matrix of shape (n_samples, 1) containing predicted values
  • error: nil if prediction succeeds, otherwise an error describing the failure

Errors:

  • ErrNotFitted: if the model hasn't been trained yet
  • ErrDimensionMismatch: if X has different number of features than training data

Example:

predictions, err := lr.Predict(X_test)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("First prediction: %.2f\n", predictions.At(0, 0))

func (*LinearRegression) Score

func (lr *LinearRegression) Score(X, y mat.Matrix) (_ float64, err error)

Score calculates the coefficient of determination (R²) of the model

func (*LinearRegression) SetParams added in v0.4.0

func (lr *LinearRegression) SetParams(params map[string]interface{}) error

SetParams sets the model's hyperparameters.

Jump to

Keyboard shortcuts

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