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 ¶
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 ¶
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).