SGDRegressor
#include <Skigen/LinearModel>
template <typename Scalar = double>
class Skigen::SGDRegressor(alpha=1e-4, max_iter=1000, tol=1e-3, eta0=0.01, random_state=42)
Linear regressor fitted by minimizing a regularized empirical loss with SGD.
SGDRegressor implements regularized linear regression with stochastic gradient descent (squared error loss, L2 penalty).
Mirrors sklearn.linear_model.SGDRegressor.
Parameters:
-
alpha : Scalar, default=1e-4 Regularization constant (
Scalar, default1e-4). -
max_iter : int, default=1000 Maximum number of epochs (
int, default1000). -
tol : Scalar, default=1e-3 Stopping tolerance (
Scalar, default1e-3). -
eta0 : Scalar, default=0.01 Initial learning rate (
Scalar, default0.01). -
random_state : unsigned int, default=42 RNG seed (
unsigned int, default42).
Attributes:
-
is_fitted : bool Whether the estimator has been fitted.
-
coef : RowVectorType Parameter vector (1 × n_features).
-
intercept : Scalar Independent term in the decision function.
Methods
fit(X, y)
Fit the linear model with SGD.
Parameters:
-
X : MatrixType Training matrix of shape (n_samples, n_features).
-
y : VectorType Target vector of shape (n_samples,).
Returns:
- result : SGDRegressor
Reference to the fitted estimator (
*this).
Throws:
std::invalid_argument— if X and y have inconsistent lengths.
predict(X)
Predict using the linear model.
Parameters:
- X : MatrixType Sample matrix of shape (n_samples, n_features).
Returns:
- result : VectorType Predicted values of shape (n_samples,).
Throws:
std::runtime_error— if the model has not been fitted.
score(X, y)
Return the coefficient of determination on test data.
Parameters:
-
X : MatrixType Test samples of shape (n_samples, n_features).
-
y : VectorType True values of shape (n_samples,).
Returns:
- result : Scalar score.
Example
Skigen::SGDRegressor<double> regressor;
regressor.fit(split_reg.X_train, split_reg.y_train);
std::cout << "=== SGD Regressor ===\n";
std::cout << "R²: " << regressor.score(split_reg.X_test, split_reg.y_test) << "\n";
std::cout << "Coef: " << regressor.coef() << "\n";