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 ¶
- type LinearRegression
- func (lr *LinearRegression) ExportToSKLearn(filename string) (err error)
- func (lr *LinearRegression) ExportToSKLearnWriter(w io.Writer) (err error)
- func (lr *LinearRegression) Fit(X, y mat.Matrix) (err error)
- func (lr *LinearRegression) GetIntercept() float64
- func (lr *LinearRegression) GetWeights() []float64
- func (lr *LinearRegression) LoadFromSKLearn(filename string) (err error)
- func (lr *LinearRegression) LoadFromSKLearnReader(r io.Reader) (err error)
- func (lr *LinearRegression) Predict(X mat.Matrix) (_ mat.Matrix, err error)
- func (lr *LinearRegression) Score(X, y mat.Matrix) (_ float64, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LinearRegression ¶
type LinearRegression struct {
model.BaseEstimator // BaseEstimatorを埋め込み
Weights *mat.VecDense // 重み(係数)
Intercept float64 // 切片
NFeatures int // 特徴量の数
}
LinearRegression は線形回帰モデル
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 はモデルをscikit-learn互換のJSON形式でエクスポート
パラメータ:
- filename: 出力ファイル名
戻り値:
- error: エクスポート失敗時のエラー
func (*LinearRegression) ExportToSKLearnWriter ¶
func (lr *LinearRegression) ExportToSKLearnWriter(w io.Writer) (err error)
ExportToSKLearnWriter はモデルをWriterにscikit-learn互換形式でエクスポート
パラメータ:
- w: 出力先Writer
戻り値:
- error: エクスポート失敗時のエラー
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 は学習された切片を返す
func (*LinearRegression) GetWeights ¶
func (lr *LinearRegression) GetWeights() []float64
GetWeights は学習された重み(係数)を返す
func (*LinearRegression) LoadFromSKLearn ¶
func (lr *LinearRegression) LoadFromSKLearn(filename string) (err error)
LoadFromSKLearn はscikit-learnからエクスポートされたJSONファイルからモデルを読み込む
パラメータ:
- filename: JSONファイルのパス
戻り値:
- error: 読み込みエラー
使用例:
lr := NewLinearRegression()
err := lr.LoadFromSKLearn("sklearn_model.json")
func (*LinearRegression) LoadFromSKLearnReader ¶
func (lr *LinearRegression) LoadFromSKLearnReader(r io.Reader) (err error)
LoadFromSKLearnReader はReaderからscikit-learnモデルを読み込む
パラメータ:
- r: JSONデータを含むReader
戻り値:
- error: 読み込みエラー
func (*LinearRegression) Predict ¶
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))