lasso

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2025 License: MIT Imports: 7 Imported by: 0

README

LASSO Regression in Go

Pure Go implementation of LASSO and Elastic Net regression - sklearn-compatible API

GitHub Release Go Version Go Reference GitHub Actions Go Report Card codecov License GitHub Stars GitHub Issues


Efficient implementation of LASSO (Least Absolute Shrinkage and Selection Operator) and Elastic Net regression using sequential coordinate descent optimization. Supports pure LASSO (L1), pure Ridge (L2), and balanced Elastic Net regularization. Optimized for cache locality and numerical stability.

Features ✨

  • Sequential coordinate descent - Optimized for cache locality and performance
  • 📉 L1 regularization - Automatic feature selection and model simplification
  • 🔀 Elastic Net support - Combine L1 (LASSO) and L2 (Ridge) regularization
  • 🎯 Early stopping - Terminates training when convergence is detected
  • 📊 Metrics tracking - Records MSE, R², and weight deltas during training
  • 🔧 Feature standardization - Automatic data preprocessing
  • 📈 Comprehensive evaluation - Supports R², MSE, and MAE metrics
  • 📝 Training history - Access detailed logs of each iteration
  • ⚙️ Configurable parameters - Tune lambda, alpha, tolerance, and more
  • 🔄 Cross-validation - K-fold CV for automatic lambda selection
  • 💾 Model persistence - Save/Load models to JSON

Requirements

  • Go 1.25+

Installation 📦

go get github.com/causalgo/lasso

Quick Start 🚀

package main

import (
	"fmt"
	
	"github.com/causalgo/lasso"
	"gonum.org/v1/gonum/mat"
)

func main() {
	// Training data
	X := mat.NewDense(4, 2, []float64{
		1, 2,
		3, 4,
		5, 6,
		7, 8,
	})
	y := []float64{3, 7, 11, 15}

	// Configure training
	cfg := lasso.NewDefaultConfig()
	cfg.Lambda = 0.1      // Regularization strength
	cfg.Verbose = true    // Enable training logs

	// Train model
	model, err := lasso.Fit(X, y, cfg)
	if err != nil {
		panic(err)
	}

	// Make predictions
	newX := mat.NewDense(2, 2, []float64{
		2, 3,
		4, 5,
	})
	predictions := model.Predict(newX)
	fmt.Println("Predictions:", predictions) // [5.0001, 9.0000]
	
	// Evaluate model
	score := model.Score(X, y)
	fmt.Printf("R² score: %.4f\n", score) // 1.0000
}

Advanced Usage 🧠

Custom Configuration
cfg := &lasso.Config{
	Lambda:      0.05,    // Regularization parameter
	Alpha:       1.0,     // Elastic Net mixing: 1.0=LASSO, 0.0=Ridge, 0.5=balanced
	MaxIter:     2000,    // Maximum iterations
	Tol:         1e-5,    // Convergence tolerance
	Standardize: true,    // Standardize features
	Verbose:     true,    // Show training logs
	LogStep:     50,      // Log every 50 iterations
	EarlyStop:   true,    // Enable early stopping
	StopAfter:   15,      // Stop after 15 iterations without improvement
	MinDelta:    1e-5,    // Minimum improvement for early stopping
}
Elastic Net Regularization
// Pure LASSO (L1 only) - produces sparse solutions
cfg := lasso.NewDefaultConfig()
cfg.Lambda = 0.1
cfg.Alpha = 1.0  // Default: pure LASSO

// Elastic Net (L1 + L2 mix) - balanced regularization
cfg.Alpha = 0.5  // 50% L1, 50% L2

// Pure Ridge (L2 only) - no sparsity, all features active
cfg.Alpha = 0.0  // Pure Ridge regularization

model, err := lasso.Fit(X, y, cfg)

The Elastic Net objective function:

minimize: (1/2n) * ||y - Xw||² + λ * (α * ||w||₁ + (1-α) * ||w||²/2)

Where:

  • α = 1.0: Pure LASSO - encourages sparsity (feature selection)
  • α = 0.0: Pure Ridge - shrinks coefficients without sparsity
  • 0 < α < 1: Elastic Net - balances sparsity and stability
Accessing Training History
model, err := lasso.Fit(X, y, cfg)
if err != nil {
	panic(err)
}

// Analyze training progress
for _, log := range model.History {
	if log.Iteration%100 == 0 {
		fmt.Printf("Iter %d: MSE=%.4f R²=%.4f\n", 
			log.Iteration, log.MSE, log.R2)
	}
}
Saving and Loading Models
// Save model to JSON
err := model.Save("model.json")
if err != nil {
	panic(err)
}

// Load model from JSON
loadedModel, err := lasso.Load("model.json")
if err != nil {
	panic(err)
}
Cross-Validation for Lambda Selection
// Automatic lambda selection via k-fold cross-validation
result, err := lasso.CrossValidate(X, y, &lasso.CVConfig{
	Lambdas: []float64{0.001, 0.01, 0.1, 1.0}, // Or nil for auto-generation
	NFolds:  5,                                  // 5-fold CV
	Scoring: "mse",                              // "mse", "r2", or "mae"
	Seed:    42,                                 // For reproducibility
	Config:  lasso.NewDefaultConfig(),           // Base training config
})
if err != nil {
	panic(err)
}

fmt.Printf("Best lambda: %.4f\n", result.BestLambda)
fmt.Printf("Best score: %.4f\n", result.BestScore)

// Use the best model (trained on full data with best lambda)
predictions := result.Model.Predict(newX)

// Access detailed CV results
for lambda, scores := range result.CVScores {
	fmt.Printf("Lambda %.4f: mean=%.4f, scores=%v\n",
		lambda, result.MeanScores[lambda], scores)
}

Performance Benchmarks ⏱️

Run benchmarks locally:

go test -bench=. -run=^Benchmark -benchmem ./...

Key optimizations:

  • Sequential coordinate descent - better cache locality than parallel
  • RawMatrix() access - direct slice operations, no bounds checking overhead
  • Minimal allocations - reuses buffers via predictInto()

Documentation 📚

Contributing 🤝

Contributions are welcome! Please read our Contributing Guide for details on:

  • Git-Flow branching model
  • Commit message conventions
  • Code quality standards
  • Pull request requirements

Quick start:

git checkout develop
git checkout -b feature/amazing-feature
# Make changes...
go fmt ./... && golangci-lint run && go test -race ./...
git commit -m "feat: add amazing feature"
git push origin feature/amazing-feature

License 📄

This project is licensed under the MIT License - see the LICENSE file for details.


causalgo - Machine learning tools for causal analysis in Go

Documentation

Overview

Package lasso implements LASSO (Least Absolute Shrinkage and Selection Operator) regression with sequential coordinate descent optimization. This implementation prioritizes cache locality and numerical stability over parallelism for small-to-medium datasets.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CVConfig

type CVConfig struct {
	Lambdas []float64 // Lambda values to try (nil = auto-generate)
	NFolds  int       // Number of folds (default 5)
	Seed    int64     // Random seed for reproducibility
	Scoring string    // "mse", "r2", or "mae" (default "mse")
	Config  *Config   // Base training config
}

CVConfig holds cross-validation parameters.

type CVResult

type CVResult struct {
	BestLambda float64
	BestScore  float64
	Model      *LassoModel           // Best model trained on full data
	CVScores   map[float64][]float64 // Lambda -> scores per fold
	MeanScores map[float64]float64   // Lambda -> mean score
}

CVResult holds cross-validation results.

func CrossValidate

func CrossValidate(X *mat.Dense, y []float64, cvCfg *CVConfig) (*CVResult, error)

CrossValidate performs k-fold cross-validation to find the best lambda value. It trains models for each lambda on k-1 folds and validates on the remaining fold. Returns the best model trained on the full dataset with the optimal lambda.

type Config

type Config struct {
	Lambda      float64 // Regularization strength (λ)
	Alpha       float64 // Elastic Net mixing parameter: 1.0 = LASSO (L1), 0.0 = Ridge (L2), 0.5 = balanced mix
	MaxIter     int     // Maximum number of iterations
	Tol         float64 // Convergence tolerance
	NJobs       int     // Deprecated: kept for API compatibility, not used in sequential implementation
	Standardize bool    // Standardize features
	Verbose     bool    // Enable training logs
	LogStep     int     // Logging frequency
	EarlyStop   bool    // Enable early stopping
	StopAfter   int     // Stop after N iterations without improvement
	MinDelta    float64 // Minimum improvement for early stopping
}

Config holds training parameters for LASSO regression.

func NewDefaultConfig

func NewDefaultConfig() *Config

NewDefaultConfig returns recommended default parameters.

type IterationLog

type IterationLog struct {
	Iteration int
	Timestamp time.Time
	MaxDelta  float64 // Maximum weight change
	MSE       float64 // Mean Squared Error
	R2        float64 // R-squared coefficient
}

IterationLog contains training metrics for a single iteration.

type LassoModel

type LassoModel struct {
	Weights   []float64      // Regression coefficients
	Intercept float64        // Bias term
	Lambda    float64        // Regularization parameter
	History   []IterationLog // Training history
}

LassoModel represents a trained LASSO regression model.

func Fit

func Fit(X *mat.Dense, y []float64, cfg *Config) (*LassoModel, error)

Fit trains a LASSO regression model using sequential coordinate descent. Returns (*LassoModel, error) where error is non-nil if validation fails.

func Load

func Load(filepath string) (*LassoModel, error)

Load deserializes a model from a JSON file. Returns the loaded model or an error if the file cannot be read or parsed.

func (*LassoModel) MAE

func (m *LassoModel) MAE(X *mat.Dense, y []float64) float64

MAE returns the mean absolute error for given data.

func (*LassoModel) MSE

func (m *LassoModel) MSE(X *mat.Dense, y []float64) float64

MSE returns the mean squared error for given data.

func (*LassoModel) Predict

func (m *LassoModel) Predict(X *mat.Dense) []float64

Predict returns predictions for input samples.

func (*LassoModel) Save

func (m *LassoModel) Save(filepath string) error

Save serializes the model to JSON and writes it to a file. The model is saved with all its parameters (weights, intercept, lambda, history).

func (*LassoModel) Score

func (m *LassoModel) Score(X *mat.Dense, y []float64) float64

Score returns the R² score for given data.

Directories

Path Synopsis
cmd
example command

Jump to

Keyboard shortcuts

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