---
title: "Linear Regression Using ClickHouse Machine Learning Functions"
description: "Delivery times can be predicted with a linear regression trained entirely inside ClickHouse using stochasticLinearRegressionState and evalMLMethod, with no Python or Spark involved."
url: "https://swe.quest/articles/linear-regression-using-clickhouse-machine-learning-functions-74857eccf0056a32"
canonical: "https://clickhouse.com/blog/clickhouse-linear-regression-machine-learning-functions"
source: "ClickHouse"
published: "2023-12-13"
difficulty: "medium"
kind: "How-to"
reading_minutes: 7
tags: ["machine-learning","analytics","sql","geospatial"]
---

# Linear Regression Using ClickHouse Machine Learning Functions

A post on [ClickHouse](https://swe.quest/sources/clickhouse.md), published 2023-12-13, 7 min read, How-to. Original: [https://clickhouse.com/blog/clickhouse-linear-regression-machine-learning-functions](https://clickhouse.com/blog/clickhouse-linear-regression-machine-learning-functions)

## Summary

Delivery times can be predicted with a linear regression trained entirely inside ClickHouse using stochasticLinearRegressionState and evalMLMethod, with no Python or Spark involved. The worked example uses 2,293 last-mile orders from one courier in Jilin. Features are built in SQL: geoDistance for pickup-to-delivery distance, one-hot pickup-hour columns, and a randUniform 80/20 train/test split. Distance is standardized for gradient descent and the target is log-transformed to keep predictions positive. The model reaches about 58 min MAE and 78 min RMSE overall, improving to roughly 30 and 42 minutes for deliveries under two hours, while accuracy degrades for longer journeys.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)
- [Analytics & OLAP](https://swe.quest/tags/analytics.md) (Data & databases)
- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Maps & geospatial](https://swe.quest/tags/geospatial.md) (Domains)

## Prerequisites

- SQL basics ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- linear regression
- gradient descent basics

## Keywords

ClickHouse, stochasticLinearRegression, evalMLMethod, geoDistance, Hex

## Related posts

- [Forecasting Using ClickHouse Machine Learning Functions](https://swe.quest/articles/forecasting-using-clickhouse-machine-learning-functions-69c536e4a757b31c.md): ClickHouse · 2023-10-26 · Medium · 9 min read · Analytics & OLAP, Machine learning, SQL
  ClickHouse's built-in stochasticLinearRegression and evalMLMethod functions can fit and apply time-series forecasting models entirely in SQL, with no Python or R. Using monthly flight passenger data, a log-transformed target is regressed on a linear trend plus 12 month dummy variables, with no intercept to avoid the dummy variable trap.
- [Variance Reduction Below the Randomization Grain](https://swe.quest/articles/variance-reduction-below-the-randomization-grain-0cacd97bb1a5531d.md): Instacart Tech · 2026-07-01 · Hard · 7 min read · Machine learning, E-commerce
  Instacart improves CUPED variance reduction for region-day switchback experiments by training an order-level model (gradient boosted trees) on pre-treatment features and aggregating its predictions to the region-day grain as the CUPED covariate.
- [Reducing experiment duration with predicted control variates](https://swe.quest/articles/reducing-experiment-duration-with-predicted-control-variates-cf5f4d721df76a4d.md): Etsy Code as Craft · 2025-11-25 · Medium · 8 min read · Machine learning, Data pipelines, E-commerce
  Etsy upgraded its experimentation platform's variance reduction from CUPED to CUPAC, which feeds pre-experiment features (over 100, up from 3) into LightGBM models whose predictions serve as the covariate in a linear CUPED-style adjustment.
- [ClickHouse forecasting and anomaly detection with time series foundation models](https://swe.quest/articles/clickhouse-forecasting-and-anomaly-detection-with-time-series-foundation-models-71a24c218275d2ec.md): ClickHouse · 2026-11-17 · Medium · 11 min read · Analytics & OLAP, Machine learning, Inference & serving, SQL
  TimesFM 3.0, a pretrained time-series foundation model, can be called from ClickHouse SQL via executable Python UDFs that forward arrays to an HTTP model server, giving zero-shot forecasts and anomaly scores without per-series training.
- [How to Scale K-Means Clustering with just ClickHouse SQL](https://swe.quest/articles/how-to-scale-k-means-clustering-with-just-clickhouse-sql-72a89ac76ba9cbb9.md): ClickHouse · 2024-04-11 · Medium · 21 min read · Analytics & OLAP, Machine learning, SQL, Performance engineering
  K-Means clustering can be implemented entirely in ClickHouse SQL by computing centroids incrementally with avgForEachState aggregate states stored in an AggregatingMergeTree table, fed by a Materialized View over a Null-engine table.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
