CLOUDLESS OPEN LABS - MACHINE LEARNING
This library implements common machine learning and linear algebra algorithms. It is built upon the following dependencies:
- svd-js: A lightweight implementation of the SVD decomposition.
- mathjs: A general Math library. It is used for its FFT and IFFT APIs.
- @rayyamhk/matrix (NOT EXPLICIT): A lightweight implementation of matrix operations such as QR, LU, eigenvalues, rank... This library is not explicitly used, but bits and pieces of its codebase was used. The reason it could not be used as a dependency is that it did not support ES6 import/export APIs.
npm i @cloudlessopenlabs/ml
ES6:
import { backward, det, inverse, lu, qr, rank, svd, matrix } from '@cloudlessopenlabs/ml/linalg'
import { bilinear } from '@cloudlessopenlabs/ml/interpolation'
import { nonlinear } from '@cloudlessopenlabs/ml/regression'
import { spectrum, filter } from '@cloudlessopenlabs/ml/signal'
const interpolate = bilinear([
{ x:0, y:0, z:0 },
{ x:1, y:0, z:0 },
{ x:1, y:1, z:1 },
{ x:0, y:1, z:1 }
])
console.log(interpolate({ x:0.5, y:0.5 }))
Table of contents
APIs
linalg
ES6:
import { backward, det, inverse, lu, qr, rank, svd } from '@cloudlessopenlabs/ml/linalg'
CommonJS:
const { linalg } = require('@cloudlessopenlabs/ml')
const { backward, det, inverse, lu, qr, rank, svd } = linalg
backward
import { backward } from '@cloudlessopenlabs/ml/linalg'
det
import { det } from '@cloudlessopenlabs/ml/linalg'
const C = [
[1 , 2 , 3 , 4 , 5 , 6],
[11, 12, 33, 54, 3 , 4],
[3 , 9 , 17, 43, 61, 2],
[7 , 21, 21, 7 , 23, 2],
[21, 8 , 87, 3 , 34, 3],
[14, 5 , 0 , 1 , 9 , 18]
]
console.log(det(C))
inverse
import { inverse, dot } from '@cloudlessopenlabs/ml/linalg'
const C = [
[1 , 2 , 3 , 4 , 5 , 6],
[11, 12, 33, 54, 3 , 4],
[3 , 9 , 17, 43, 61, 2],
[7 , 21, 21, 7 , 23, 2],
[21, 8 , 87, 3 , 34, 3],
[14, 5 , 0 , 1 , 9 , 18]
]
const C_1 = inverse(C)
console.log(dot(C_1,C))
lu
import { lu } from '@cloudlessopenlabs/ml/linalg'
matrix
import { matrix } from '@cloudlessopenlabs/ml/linalg'
qr
import { qr, dot } from '@cloudlessopenlabs/ml/linalg'
const A = [
[1,2],
[3,4]
]
const [Q,R] = qr(A)
console.log(dot(Q,R))
rank
import { rank } from '@cloudlessopenlabs/ml/linalg'
const B = [
[1,2,3],
[4,5,6],
[7,8,9]
]
console.log(rank(B))
svd
import { svd } from '@cloudlessopenlabs/ml/linalg'
interpolation
ES6:
import { bilinear } from '@cloudlessopenlabs/ml/interpolation'
CommonJS:
const { interpolation } = require('@cloudlessopenlabs/ml')
const { bilinear } = interpolation
bilinear
import { bilinear } from '@cloudlessopenlabs/ml/interpolation'
const interpolate = bilinear([
{ x:0, y:0, z:0 },
{ x:1, y:0, z:0 },
{ x:1, y:1, z:1 },
{ x:0, y:1, z:1 }
])
console.log(interpolate({ x:0.5, y:0.5 }))
regression
ES6:
import { nonlinear } from '@cloudlessopenlabs/ml/regression'
CommonJS:
const { regression } = require('@cloudlessopenlabs/ml')
const { nonlinear } = regression
nonlinear
import { nonlinear } from '@cloudlessopenlabs/ml/regression'
signal
spectrum
import { spectrum } from '@cloudlessopenlabs/ml/signal'
import { range } from 'mathjs'
const SAMPLE_RATE_HZ = 10
const TIME_INTERVAL_SEC = 2
const SIGNAL_FREQ_HZ = 2
const timeSeries = range(0, TIME_INTERVAL_SEC, 1/SAMPLE_RATE_HZ).toArray()
const sinusoid = timeSeries.map(t => Math.sin((t/SIGNAL_FREQ_HZ)*2*Math.PI))
const [errors, spect] = spectrum(sinusoid, SAMPLE_RATE_HZ)
console.log(spect)
Where:
spect
is an array of all the phasors including complex and their conjugate (1).phasor
: Complex number. The real part is the amplitude of the cosine component, while the imaginary part is the amplitude of the sine component.magnitude
is the the magnitude of the frequency.frequency
is the frequency.fftFrequency
is the frequency represented by the FFT (2).
(1) The FFT represents a signal in the frequency space using complex numbers (phasors). A single frequency is represented by two complex numbers (a complex number and its conjugate).
(2) The FFT associates phasor's conjugate with a ever growing frequency number when it goes above the Nyquist value. In reality, those frequency are the same as the lower frequency associated with their conjugate.
filter
lowpass
import { filter } from '@cloudlessopenlabs/ml/signal'
const SAMPLE_RATE_HZ = 10
const TIME_INTERVAL_SEC = 2
const SIGNAL_FREQ_HZ = 2
const CUT_OFF_FREQ = 0.5
const timeSeries = range(0, TIME_INTERVAL_SEC, 1/SAMPLE_RATE_HZ).toArray()
const sinusoid = timeSeries.map(t => Math.sin((t/SIGNAL_FREQ_HZ)*2*Math.PI))
const [errors, filteredSignal] = filter.lowpass(sinusoid, SAMPLE_RATE_HZ, CUT_OFF_FREQ)
console.log(filteredSignal)