Python's statistics.linear_regression in TypeScript
✓ Verified: Python 3.12
Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.
How to use
Install via yarn add locutus and import:
import { linear_regression } from 'locutus/python/statistics/linear_regression'.
Or with CommonJS: const { linear_regression } = require('locutus/python/statistics/linear_regression')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
#
code
expected result
1
linear_regression([1, 2, 3], [2, 4, 6])
{slope: 2, intercept: 0}
2
linear_regression([1, 2, 3], [1, 2, 2])
{slope: 0.5, intercept: 0.6666666666666667}
3
linear_regression([1, 2, 3], [2, 4, 6], true)
{slope: 2, intercept: 0}
Notes
Returns ordinary least-squares slope and intercept, mirroring Python’s named tuple as a plain object.
Dependencies
This function uses the following Locutus functions:
const n = xValues.length if (yValues.length !== n) { thrownewError('linear regression requires that both inputs have same number of data points') } if (n < 2) { thrownewError('linear regression requires at least two data points') }
const n = xValues.length if (yValues.length !== n) { thrownewError('linear regression requires that both inputs have same number of data points') } if (n < 2) { thrownewError('linear regression requires at least two data points') }
functionassertStatisticsArray(data: unknown, functionName: string): unknown[] { if (!Array.isArray(data)) { thrownewTypeError(`${functionName}() data must be an array`) }
return data }
functiontoStatisticNumber(value: unknown, functionName: string): number { if (typeof value === 'number') { return value }
if (typeof value === 'boolean') { return value ? 1 : 0 }
if (typeof value === 'bigint') { const numericValue = Number(value) if (!Number.isSafeInteger(numericValue)) { thrownewRangeError(`${functionName}() bigint values must fit within JS safe integers`) }
return numericValue }
thrownewTypeError(`${functionName}() data must contain only real numbers`) }
functionsumNumbers(values: number[]): number { return values.reduce((sum, value) => sum + value, 0) }
functionsumProducts(left: number[], right: number[]): number { let total = 0 for (let index = 0; index < left.length; index += 1) { total += (left[index] ?? 0) * (right[index] ?? 0) } return total }
functiondivideStatisticsValue(total: number, divisor: number, integral: boolean, preferFloat = false): number { if (!preferFloat && integral && Number.isSafeInteger(total) && total % divisor === 0) { return total / divisor }
return total / divisor }
// python/statistics/linear_regression (target function module) functionlinear_regression(x: unknown, y: unknown, proportional = false): { slope: number; intercept: number } { // discuss at: https://locutus.io/python/statistics/linear_regression/ // parity verified: Python 3.12 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Returns ordinary least-squares slope and intercept, mirroring Python's named tuple as a plain object. // example 1: linear_regression([1, 2, 3], [2, 4, 6]) // returns 1: {slope: 2, intercept: 0} // example 2: linear_regression([1, 2, 3], [1, 2, 2]) // returns 2: {slope: 0.5, intercept: 0.6666666666666667} // example 3: linear_regression([1, 2, 3], [2, 4, 6], true) // returns 3: {slope: 2, intercept: 0}
const n = xValues.length if (yValues.length !== n) { thrownewError('linear regression requires that both inputs have same number of data points') } if (n < 2) { thrownewError('linear regression requires at least two data points') }
// python/_helpers/_statistics (Locutus helper dependency) functionassertStatisticsArray(data, functionName) { if (!Array.isArray(data)) { thrownewTypeError(`${functionName}() data must be an array`) }
return data }
functiontoStatisticNumber(value, functionName) { if (typeof value === 'number') { return value }
if (typeof value === 'boolean') { return value ? 1 : 0 }
if (typeof value === 'bigint') { const numericValue = Number(value) if (!Number.isSafeInteger(numericValue)) { thrownewRangeError(`${functionName}() bigint values must fit within JS safe integers`) }
return numericValue }
thrownewTypeError(`${functionName}() data must contain only real numbers`) }
functionsumProducts(left, right) { let total = 0 for (let index = 0; index < left.length; index += 1) { total += (left[index] ?? 0) * (right[index] ?? 0) } return total }
functiondivideStatisticsValue(total, divisor, integral, preferFloat = false) { if (!preferFloat && integral && Number.isSafeInteger(total) && total % divisor === 0) { return total / divisor }
return total / divisor }
// python/statistics/linear_regression (target function module) functionlinear_regression(x, y, proportional = false) { // discuss at: https://locutus.io/python/statistics/linear_regression/ // parity verified: Python 3.12 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Returns ordinary least-squares slope and intercept, mirroring Python's named tuple as a plain object. // example 1: linear_regression([1, 2, 3], [2, 4, 6]) // returns 1: {slope: 2, intercept: 0} // example 2: linear_regression([1, 2, 3], [1, 2, 2]) // returns 2: {slope: 0.5, intercept: 0.6666666666666667} // example 3: linear_regression([1, 2, 3], [2, 4, 6], true) // returns 3: {slope: 2, intercept: 0}
const n = xValues.length if (yValues.length !== n) { thrownewError('linear regression requires that both inputs have same number of data points') } if (n < 2) { thrownewError('linear regression requires at least two data points') }
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.
Click "New file" in the appropriate folder
on GitHub.
This will fork the project to your account, directly add the file to it, and send a
Pull Request to us.
We will then review it. If it's useful to the project and in line with our
contributing guidelines
your work will become part of Locutus and you'll be automatically credited
in the authors
section accordingly.