Documentation Index Fetch the complete documentation index at: https://mintlify.com/Seyamalam/bun-scikit/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Ridge implements linear regression with L2 regularization. The regularization term penalizes large coefficients, which helps prevent overfitting and improves model generalization. Ridge uses a closed-form solution for efficient fitting.
Constructor
import { Ridge } from "bun-scikit" ;
const model = new Ridge ( options );
Parameters
Configuration options for the model Regularization strength. Higher values specify stronger regularization:
alpha = 0: Equivalent to ordinary least squares
alpha > 0: Adds L2 penalty (sum of squared coefficients)
Whether to calculate the intercept for this model
Methods
fit
Fit the ridge regression model using training data.
fit ( X : Matrix , y : Vector , sampleWeight ?: Vector ): this
Parameters:
X: Training data matrix of shape [nSamples, nFeatures]
y: Target values vector of length nSamples
sampleWeight: Optional sample weights (not currently used)
Returns: The fitted model instance
Example:
const X = [[ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ]];
const y = [ 0 , 1 , 2 , 3 , 4 , 5 ];
const ridge = new Ridge ({ alpha: 1 });
ridge . fit ( X , y );
predict
Predict using the ridge regression model.
predict ( X : Matrix ): Vector
Parameters:
X: Samples matrix of shape [nSamples, nFeatures]
Returns: Predicted values vector of length nSamples
Example:
const predictions = ridge . predict ([[ 2.5 ]]);
console . log ( predictions [ 0 ]); // Predicted value
score
Return the coefficient of determination (R² score) of the prediction.
score ( X : Matrix , y : Vector ): number
Parameters:
X: Test samples matrix
y: True target values
Returns: R² score (coefficient of determination)
Attributes
After fitting, the following attributes are available:
Estimated coefficients for the ridge regression problem
Independent term in the linear model
Complete Example
import { Ridge } from "bun-scikit" ;
// Training data
const X = [[ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ]];
const y = [ 0 , 1 , 2 , 3 , 4 , 5 ];
// Create and fit the model
const ridge = new Ridge ({ alpha: 1 });
ridge . fit ( X , y );
// Evaluate the model
const r2 = ridge . score ( X , y );
console . log ( "R² score:" , r2 ); // >0.98
// Make predictions
const predictions = ridge . predict ( X );
console . log ( "Predictions:" , predictions );
// Check coefficients
console . log ( "Coefficients:" , ridge . coef_ );
console . log ( "Intercept:" , ridge . intercept_ );
Comparison Example
import { Ridge , LinearRegression } from "bun-scikit" ;
// Compare Ridge with different alpha values
const X = [[ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ]];
const y = [ 0 , 1.1 , 1.9 , 3.1 , 4.1 , 4.9 ];
const models = [
new LinearRegression (),
new Ridge ({ alpha: 0.1 }),
new Ridge ({ alpha: 1.0 }),
new Ridge ({ alpha: 10.0 }),
];
models . forEach ( model => {
model . fit ( X , y );
const r2 = model . score ( X , y );
console . log ( ` ${ model . constructor . name } R²: ${ r2 . toFixed ( 4 ) } ` );
});
Notes
Ridge regression uses a closed-form solution, making it very fast to fit.
The alpha parameter controls the trade-off between fitting the training data and keeping the coefficients small.
Ridge never sets coefficients exactly to zero (unlike Lasso). For feature selection, consider Lasso or ElasticNet .
For automatic selection of the best alpha value, see RidgeCV.