@cloudlessopenlabs/ml
v0.1.1
Published
Set of machine learning and linear algebra tools.
Readme
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/mlES6:
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'
// CommonJS:
// const { linalg, interpolation, regression, signal } = require('@cloudlessopenlabs/ml')
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 })) // 0.5Table 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 } = linalgbackward
import { backward } from '@cloudlessopenlabs/ml/linalg'
// import backward from '@cloudlessopenlabs/ml/linalg/backward'
// // CommonJS
// const { linalg:{ backward } } = require('@cloudlessopenlabs/ml')det
import { det } from '@cloudlessopenlabs/ml/linalg'
// import det from '@cloudlessopenlabs/ml/linalg/det'
// // CommonJS
// const { linalg:{ det } } = require('@cloudlessopenlabs/ml')
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)) // -387953848inverse
import { inverse, dot } from '@cloudlessopenlabs/ml/linalg'
// import inverse from '@cloudlessopenlabs/ml/linalg/inverse'
// // CommonJS
// const { linalg:{ inverse } } = require('@cloudlessopenlabs/ml')
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))
//[
// [1 , 0, 0, 0, 0, 0],
// [0 , 1, 0, 0, 0, 0],
// [0 , 0, 1, 0, 0, 0],
// [0 , 0, 0, 1, 0, 0],
// [0 , 0, 0, 0, 1, 0],
// [0 , 0, 0, 0, 0, 1]
//]lu
import { lu } from '@cloudlessopenlabs/ml/linalg'
// import lu from '@cloudlessopenlabs/ml/linalg/lu'
// // CommonJS
// const { linalg:{ lu } } = require('@cloudlessopenlabs/ml')matrix
import { matrix } from '@cloudlessopenlabs/ml/linalg'
// import matrix from '@cloudlessopenlabs/ml/linalg/matrix'
// // CommonJS
// const { linalg:{ matrix } } = require('@cloudlessopenlabs/ml')
qr
import { qr, dot } from '@cloudlessopenlabs/ml/linalg'
// import qr from '@cloudlessopenlabs/ml/linalg/qr'
// // CommonJS
// const { linalg:{ qr } } = require('@cloudlessopenlabs/ml')
const A = [
[1,2],
[3,4]
]
const [Q,R] = qr(A) // Where R is an upper-triangular matrix and Q is orthonormal (Q^T = Q^-1)
console.log(dot(Q,R))
//[
// [1,2],
// [3,4]
//]rank
import { rank } from '@cloudlessopenlabs/ml/linalg'
// import rank from '@cloudlessopenlabs/ml/linalg/rank'
// // CommonJS
// const { linalg:{ rank } } = require('@cloudlessopenlabs/ml')
const B = [
[1,2,3],
[4,5,6],
[7,8,9]
]
console.log(rank(B)) // 2svd
import { svd } from '@cloudlessopenlabs/ml/linalg'
// import svd from '@cloudlessopenlabs/ml/linalg/svd'
// // CommonJS
// const { linalg:{ svd } } = require('@cloudlessopenlabs/ml')interpolation
ES6:
import { bilinear } from '@cloudlessopenlabs/ml/interpolation'CommonJS:
const { interpolation } = require('@cloudlessopenlabs/ml')
const { bilinear } = interpolationbilinear
import { bilinear } from '@cloudlessopenlabs/ml/interpolation'
// import bilinear from '@cloudlessopenlabs/ml/interpolation/bilinear'
// // CommonJS
// const { interpolation:{ bilinear } } = require('@cloudlessopenlabs/ml')
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 })) // 0.5regression
ES6:
import { nonlinear } from '@cloudlessopenlabs/ml/regression'CommonJS:
const { regression } = require('@cloudlessopenlabs/ml')
const { nonlinear } = regressionnonlinear
import { nonlinear } from '@cloudlessopenlabs/ml/regression'
// import nonlinear from '@cloudlessopenlabs/ml/regression/nonlinear'
// // CommonJS
// const { regression:{ nonlinear } } = require('@cloudlessopenlabs/ml')signal
spectrum
import { spectrum } from '@cloudlessopenlabs/ml/signal'
// import spectrum from '@cloudlessopenlabs/ml/signal/spectrum'
// // CommonJS
// const { signal: { spectrum } } = require('@cloudlessopenlabs/ml')
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()
// Creates 2Hz sinusoid
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)
// [{
// idx: 0,
// phasor: { re: 5.88418203051333e-15, im: 1.4660299807724221e-15 },
// magnitude: 6.064061516239719e-15,
// frequency: 0,
// fftFrequency: 0
// }, ..., {
// idx: 10,
// phasor: Complex { re: -7.771561172376096e-16, im: -3.370020422758625e-17 },
// magnitude: 7.778864533624546e-16,
// frequency: 5,
// fftFrequency: 5
//}, ..., {
// idx: 19,
// phasor: Complex { re: -4.6629367034256575e-15, im: 9.999999999999993 },
// magnitude: 9.999999999999993,
// frequency: 0.5,
// fftFrequency: 9.5
//}]Where:
spectis 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.magnitudeis the the magnitude of the frequency.frequencyis the frequency.fftFrequencyis 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'
// import filter from '@cloudlessopenlabs/ml/signal/filter'
// import lowpass from '@cloudlessopenlabs/ml/signal/filter/lowpass'
// // CommonJS
// const { signal: { filter: { lowpass } } } = require('@cloudlessopenlabs/ml')
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()
// Creates 2Hz sinusoid
const sinusoid = timeSeries.map(t => Math.sin((t/SIGNAL_FREQ_HZ)*2*Math.PI))
// Removes the frequencies higher than 0.5Hz
const [errors, filteredSignal] = filter.lowpass(sinusoid, SAMPLE_RATE_HZ, CUT_OFF_FREQ)
console.log(filteredSignal)