Skip to main content

stochasticLinearRegression

Introduced in: v20.1.0 This function implements stochastic linear regression. It supports custom parameters for:
  • learning rate
  • L2 regularization coefficient
  • mini-batch size
It also has a few methods for updating weights:
  • Adam (used by default)
  • simple SGD
  • Momentum
  • Nesterov
Usage The function is used in two steps: fitting the model and predicting on new data.
  1. Fitting
For fitting a query like this can be used:
CREATE TABLE IF NOT EXISTS train_data
(
    param1 Float64,
    param2 Float64,
    target Float64
) ENGINE = Memory;

CREATE TABLE your_model ENGINE = Memory AS SELECT
stochasticLinearRegressionState(0.1, 0.0, 5, 'SGD')(target, x1, x2)
AS state FROM train_data;
Here, we also need to insert data into the train_data table. The number of parameters is not fixed, it depends only on the number of arguments passed into linearRegressionState. They all must be numeric values. Note that the column with target value (which we would like to learn to predict) is inserted as the first argument.
  1. Predicting
After saving a state into the table, we may use it multiple times for prediction or even merge with other states and create new, even better models.
WITH (SELECT state FROM your_model) AS model SELECT
evalMLMethod(model, x1, x2) FROM test_data
The query will return a column of predicted values. Note that first argument of evalMLMethod is AggregateFunctionState object, next are columns of features. test_data is a table like train_data but may not contain target value. Notes
  1. To merge two models user may create such query:
SELECT state1 + state2 FROM your_models
where the your_models table contains both models. This query will return a new AggregateFunctionState object.
  1. You may fetch weights of the created model for its own purposes without saving the model if no -State combinator is used.
SELECT stochasticLinearRegression(0.01)(target, param1, param2)
FROM train_data
A query like this will fit the model and return its weights - first are weights, which correspond to the parameters of the model, the last one is bias. So in the example above the query will return a column with 3 values. Syntax
stochasticLinearRegression([learning_rate, l2_regularization_coef, mini_batch_size, method])(target, x1, x2, ...)
Arguments
  • learning_rate — The coefficient on step length when gradient descent step is performed. A learning rate that is too big may cause infinite weights of the model. Default is 0.00001. Float64
  • l2_regularization_coef — L2 regularization coefficient which may help to prevent overfitting. Default is 0.1. Float64
  • mini_batch_size — Sets the number of elements which gradients will be computed and summed to perform one step of gradient descent. Pure stochastic descent uses one element, however having small batches (about 10 elements) makes gradient steps more stable. Default is 15. UInt64
  • method — Method for updating weights: Adam (by default), SGD, Momentum, Nesterov. Momentum and Nesterov require slightly more computations and memory, however they happen to be useful in terms of speed of convergence and stability of stochastic gradient methods. const String
  • target — Target value (dependent variable) to learn to predict. Must be numeric. Float*
  • x1, x2, ... — Feature values (independent variables). All must be numeric. Float*
Returned value Returns the trained linear regression model weights. First values correspond to the parameters of the model, the last one is bias. Use evalMLMethod for predictions. Array(Float64) Examples Training a model
Query
CREATE TABLE your_model
ENGINE = Memory
AS SELECT
stochasticLinearRegressionState(0.1, 0.0, 5, 'SGD')(target, x1, x2)
AS state FROM train_data
Response
Saves trained model state to table
Making predictions
Query
WITH (SELECT state FROM your_model) AS model SELECT
evalMLMethod(model, x1, x2) FROM test_data
Response
Returns predicted values for test data
Getting model weights
Query
SELECT stochasticLinearRegression(0.01)(target, x1, x2) FROM train_data
Response
Returns model weights without saving state
See Also
Last modified on June 8, 2026