linear

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package linear provides linear regression models.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lasso

type Lasso struct {
	// Alpha is the regularization strength.
	// Larger values specify stronger regularization.
	Alpha float64

	// MaxIter is the maximum number of iterations.
	MaxIter int

	// Tol is the tolerance for convergence.
	Tol float64

	// FitIntercept determines whether to calculate the intercept.
	FitIntercept bool
	// contains filtered or unexported fields
}

Lasso implements Lasso Regression with L1 regularization. Lasso adds a penalty term α * ||w||₁ to the loss function, which encourages sparsity (some coefficients become exactly zero). Uses coordinate descent algorithm for optimization.

func NewLasso

func NewLasso(alpha float64, maxIter int, fitIntercept bool) *Lasso

NewLasso creates a new Lasso regression model.

func (*Lasso) Coef

func (l *Lasso) Coef() []float64

Coef returns the coefficients.

func (*Lasso) Fit

func (l *Lasso) Fit(X *dataframe.DataFrame, y *seriesPkg.Series[any]) error

Fit trains the Lasso regression model using coordinate descent.

func (*Lasso) Intercept

func (l *Lasso) Intercept() float64

Intercept returns the intercept.

func (*Lasso) NIter

func (l *Lasso) NIter() int

NIter returns the number of iterations performed.

func (*Lasso) Predict

func (l *Lasso) Predict(X *dataframe.DataFrame) (*seriesPkg.Series[any], error)

Predict makes predictions on new data.

func (*Lasso) Score

func (l *Lasso) Score(X *dataframe.DataFrame, y *seriesPkg.Series[any]) (float64, error)

Score returns the R² score on test data.

type LinearRegression

type LinearRegression struct {
	// FitIntercept determines whether to calculate the intercept.
	// If false, the data is expected to be centered.
	FitIntercept bool
	// contains filtered or unexported fields
}

LinearRegression implements Ordinary Least Squares (OLS) regression. Fits a linear model with coefficients w = (w_1, ..., w_p) to minimize the residual sum of squares between the observed targets and the targets predicted by the linear approximation.

func NewLinearRegression

func NewLinearRegression(fitIntercept bool) *LinearRegression

NewLinearRegression creates a new LinearRegression model.

func (*LinearRegression) Coef

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

Coef returns the coefficients of the model.

func (*LinearRegression) Fit

Fit trains the linear regression model on data X with target y. Solves the normal equation: β = (X^T X)^-1 X^T y

func (*LinearRegression) Intercept

func (lr *LinearRegression) Intercept() float64

Intercept returns the intercept of the model.

func (*LinearRegression) Predict

Predict makes predictions on new data X. Returns y_pred = X * coef + intercept

func (*LinearRegression) Score

Score returns the R² score of the model on test data.

type LogisticRegression

type LogisticRegression struct {
	// Penalty specifies the regularization type: "l1", "l2", or "none"
	Penalty string

	// C is the inverse of regularization strength (smaller values = stronger regularization)
	C float64

	// MaxIter is the maximum number of iterations for gradient descent
	MaxIter int

	// Tol is the tolerance for convergence
	Tol float64

	// FitIntercept determines whether to calculate the intercept
	FitIntercept bool

	// LearningRate for gradient descent
	LearningRate float64
	// contains filtered or unexported fields
}

LogisticRegression implements logistic regression for binary classification. Uses gradient descent with logistic loss function. Supports L1, L2, or no regularization.

func NewLogisticRegression

func NewLogisticRegression(penalty string, C float64, maxIter int) *LogisticRegression

NewLogisticRegression creates a new logistic regression model.

func (*LogisticRegression) Classes

func (lr *LogisticRegression) Classes() []string

Classes returns the class labels.

func (*LogisticRegression) Coef

func (lr *LogisticRegression) Coef() []float64

Coef returns the coefficients.

func (*LogisticRegression) Fit

Fit trains the logistic regression model using gradient descent.

func (*LogisticRegression) Intercept

func (lr *LogisticRegression) Intercept() float64

Intercept returns the intercept.

func (*LogisticRegression) NIter

func (lr *LogisticRegression) NIter() int

NIter returns the number of iterations performed.

func (*LogisticRegression) Predict

Predict predicts class labels for samples in X.

func (*LogisticRegression) PredictProba

PredictProba returns probability estimates for each class.

type Ridge

type Ridge struct {
	// Alpha is the regularization strength (λ or α).
	// Larger values specify stronger regularization.
	// Alpha must be >= 0. When alpha = 0, Ridge is equivalent to LinearRegression.
	Alpha float64

	// FitIntercept determines whether to calculate the intercept.
	FitIntercept bool
	// contains filtered or unexported fields
}

Ridge implements Ridge Regression with L2 regularization. Ridge regression adds a penalty term α * ||w||² to the loss function, which helps prevent overfitting by shrinking coefficients. Solves: β = (X^T X + α I)^-1 X^T y

func NewRidge

func NewRidge(alpha float64, fitIntercept bool) *Ridge

NewRidge creates a new Ridge regression model.

func (*Ridge) Coef

func (r *Ridge) Coef() []float64

Coef returns the coefficients.

func (*Ridge) Fit

func (r *Ridge) Fit(X *dataframe.DataFrame, y *seriesPkg.Series[any]) error

Fit trains the Ridge regression model. Solves: β = (X^T X + α I)^-1 X^T y

func (*Ridge) Intercept

func (r *Ridge) Intercept() float64

Intercept returns the intercept.

func (*Ridge) Predict

func (r *Ridge) Predict(X *dataframe.DataFrame) (*seriesPkg.Series[any], error)

Predict makes predictions on new data.

func (*Ridge) Score

func (r *Ridge) Score(X *dataframe.DataFrame, y *seriesPkg.Series[any]) (float64, error)

Score returns the R² score on test data.

Jump to

Keyboard shortcuts

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