cognitive-ts
Advanced tools
Comparing version 2.0.1 to 2.0.2
import * as _ from "lodash" | ||
import { compose, sFn, isSquare, swap, sFill } from "../lib/base" | ||
import { compose, sFn, isSquare, swap, sFill, transpose } from "../lib/base" | ||
import { add } from "../lib/data-frame" | ||
@@ -12,3 +12,3 @@ | ||
expect(sFn([[1, 2, 3]], (x: number[]) => _.map(x, (n) => n * 2))).toEqual([ | ||
expect(sFn([[1, 2, 3]])((x: number[]) => _.map(x, (n) => n * 2))).toEqual([ | ||
[2, 4, 6] | ||
@@ -19,2 +19,8 @@ ]) | ||
expect(transpose([[1, 2, 3], [1, 2, 3]])).toEqual([ | ||
[1, 1], | ||
[2, 2], | ||
[3, 3] | ||
]) | ||
expect( | ||
@@ -21,0 +27,0 @@ isSquare([ |
10
index.ts
@@ -1,6 +0,6 @@ | ||
import * as dfMath from "./lib/df/math" | ||
import * as sMath from "./lib/s/math" | ||
import * as base from "./lib/base/base" | ||
import * as rotate from "./lib/rotate" | ||
import * as shape from "./lib/shape" | ||
import * as dfMath from './lib/df/math' | ||
import * as sMath from './lib/s/math' | ||
import * as base from './lib/base/base' | ||
import * as rotate from './lib/rotate' | ||
import * as shape from './lib/shape' | ||
@@ -7,0 +7,0 @@ export default { |
@@ -1,5 +0,5 @@ | ||
import * as _ from "lodash" | ||
import { shape } from "../base" | ||
import { dot, sumSquares } from "../data-frame" | ||
import { sZeros } from "../series" | ||
import * as _ from 'lodash' | ||
import { shape } from '../base' | ||
import { dot, sumSquares } from '../data-frame' | ||
import { sZeros } from '../series' | ||
@@ -31,3 +31,3 @@ export const multiVariateLinearRegression = (x: { features: Matrix, labels: Series}, iterations: number) => { | ||
const [m, n] = shape(features) | ||
let weights = sZeros(n) | ||
const weights = sZeros(n) | ||
let bias = 0 | ||
@@ -34,0 +34,0 @@ |
@@ -0,8 +1,10 @@ | ||
/* eslint-disable @typescript-eslint/ban-types */ | ||
import * as _ from 'lodash' | ||
export const compose = (...fns: Function[]) => | ||
(args: Matrix) => | ||
_.reduce(fns, (arg, fn: Function) => fn(arg), args) | ||
export const compose = | ||
(...fns: Function[]) => | ||
(args: Matrix) => | ||
_.reduce(fns, (arg, fn: Function) => fn(arg), args) | ||
export const sFn = (x: Matrix, fn: (s: Series) => SeriesResult) => | ||
export const sFn = (x: Matrix) => (fn: (s: Series) => SeriesResult) => | ||
_.map( | ||
@@ -15,3 +17,3 @@ x, | ||
export const swap = (arr: Array<any>, i: any, j: any) => { | ||
export const swap = (arr: number[], i: number, j: number) => { | ||
const temp = arr[i] | ||
@@ -23,11 +25,14 @@ arr[i] = arr[j] | ||
export const transpose = _.memoize((x : Matrix) => | ||
_.map(_.first(x), (_v: Series, i: number) => _.reverse(_.map(x, (r) => r[i]))) | ||
) | ||
export const transpose = (x: Matrix) => | ||
_.map(_.first(x), (_v: Series, i: number): number[] => | ||
_.reverse(_.map(x, (r) => r[i])) | ||
) | ||
export const inverseTranspose = _.memoize((x : Matrix) => | ||
_.map(_.first(x), (_v: Series, i: number) => _.map(x, (r: number[]) => r[_.size(r) - 1 - i])) | ||
export const inverseTranspose = _.memoize((x: Matrix) => | ||
_.map(_.first(x), (_v: Series, i: number) => | ||
_.map(x, (r: number[]) => r[_.size(r) - 1 - i]) | ||
) | ||
) | ||
export const shape = _.memoize((x : Matrix) => [_.size(x), _.size(_.first(x))]) | ||
export const shape = _.memoize((x: Matrix) => [_.size(x), _.size(_.first(x))]) | ||
@@ -34,0 +39,0 @@ export const isSquare = _.memoize( |
@@ -1,50 +0,68 @@ | ||
import * as _ from "lodash" | ||
import * as b from "../base" | ||
import * as s from "../series" | ||
import * as _ from 'lodash' | ||
import { transpose, inverseTranspose, compose, sFn } from '../base' | ||
import { | ||
sAbs, | ||
sAdd, | ||
sCumSum, | ||
sDivide, | ||
sMean, | ||
sMultiply, | ||
sPow, | ||
sRandom, | ||
sSqrt, | ||
sStd, | ||
sSubtract, | ||
sZeros | ||
} from '../series' | ||
export const mean = (x : Matrix) => | ||
b.compose(b.transpose, (x : Matrix) => b.sFn(x, s.sMean), b.inverseTranspose)(x) | ||
export const mean = (x: Matrix) => | ||
compose(transpose, (x: Matrix) => sFn(x)(sMean), inverseTranspose)(x) | ||
export const sqrt = (x : Matrix) => | ||
b.compose(b.transpose, (x : Matrix) => b.sFn(x, s.sSqrt), b.inverseTranspose)(x) | ||
export const sqrt = (x: Matrix) => | ||
compose(transpose, (x: Matrix) => sFn(x)(sSqrt), inverseTranspose)(x) | ||
export const stddev = (x : Matrix) => b.compose(b.transpose, (x : Matrix) => b.sFn(x, s.sStd))(x) | ||
export const stddev = (x: Matrix) => | ||
compose(transpose, (x: Matrix) => sFn(x)(sStd))(x) | ||
export const cumSum = (x : Matrix) => | ||
b.compose(b.inverseTranspose, (x : Matrix) => b.sFn(x, s.sCumSum), b.transpose)(x) | ||
export const cumSum = (x: Matrix) => | ||
compose(inverseTranspose, (x: Matrix) => sFn(x)(sCumSum), transpose)(x) | ||
export const add = (x : Matrix) => b.compose(b.transpose, (x : Matrix) => b.sFn(x, s.sAdd))(x) | ||
export const add = (x: Matrix) => | ||
compose(transpose, (x: Matrix) => sFn(x)(sAdd))(x) | ||
export const subtract = (x : Matrix) => | ||
b.compose(b.transpose, (x : Matrix) => b.sFn(x, s.sSubtract))(x) | ||
export const subtract = (x: Matrix) => | ||
compose(transpose, (x: Matrix) => sFn(x)(sSubtract))(x) | ||
export const divide = (x : Matrix) => | ||
b.compose(b.transpose, (x : Matrix) => b.sFn(x, s.sDivide))(x) | ||
export const divide = (x: Matrix) => | ||
compose(transpose, (x: Matrix) => sFn(x)(sDivide))(x) | ||
export const multiply = (x : Matrix) => | ||
b.compose(b.transpose, (x : Matrix) => b.sFn(x, s.sMultiply))(x) | ||
export const multiply = (x: Matrix) => | ||
compose(transpose, (x: Matrix) => sFn(x)(sMultiply))(x) | ||
export const matMul = (x: Matrix, y: Matrix) => | ||
_.map(b.transpose(x), (r: number[], ri: number) => | ||
_.map(r, (n: number, i: number) => n * _.get(b.transpose(y), [ri, i])) | ||
_.map(transpose(x), (r: number[], ri: number) => | ||
_.map(r, (n: number, i: number) => n * _.get(transpose(y), [ri, i])) | ||
) | ||
export const abs = (x : Matrix) => b.sFn(x, s.sAbs) | ||
export const abs = (x: Matrix) => sFn(x)(sAbs) | ||
export const fill = (i: number, t: number) => _.fill(Array(t), i) | ||
export const zeros = (r: number, c: number) => _.map(_.times(r, () => s.sZeros(c))) | ||
export const zeros = (r: number, c: number) => | ||
_.map(_.times(r, () => sZeros(c))) | ||
export const random = (r: number, c: number, min?: number, max?: number) => | ||
_.map(_.times(r, () => s.sRandom(c, min, max))) | ||
_.map(_.times(r, () => sRandom(c, min, max))) | ||
export const pow = (x: Matrix, p: number) => b.sFn(x, (x : number[]) => s.sPow(x, p)) | ||
export const pow = (x: Matrix, p: number) => sFn(x)((x: number[]) => sPow(x, p)) | ||
export const dot = (a: number[], b: number[]): number => | ||
_.reduce(a, (acc: number, n: number, i: number) => _.add(acc, _.multiply(n, _.get(b, i))), 0) | ||
_.reduce( | ||
a, | ||
(acc: number, n: number, i: number) => | ||
_.add(acc, _.multiply(n, _.get(b, i))), | ||
0 | ||
) | ||
export const sumSquares = (x: number[], y: number[]) => | ||
_.divide( | ||
_.sum(_.map(x, (n, i) => (n - _.get(y, i)) ** 2)), | ||
_.size(x) | ||
) | ||
_.divide(_.sum(_.map(x, (n, i) => (n - _.get(y, i)) ** 2)), _.size(x)) |
{ | ||
"name": "cognitive-ts", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "", | ||
@@ -13,5 +13,4 @@ "main": "index.js", | ||
"test:debug": "jest --runInBand", | ||
"lint": "standard ./lib", | ||
"lint:fix": "standard --fix ./lib", | ||
"lint:fix:test": "standard --fix ./__test__" | ||
"lint": "eslint . --ext .ts", | ||
"lint:fix": "eslint --fix . --ext .ts" | ||
}, | ||
@@ -26,3 +25,6 @@ "author": "Richard Macarthy (rjmacarthy)", | ||
"@types/lodash": "^4.14.187", | ||
"@typescript-eslint/eslint-plugin": "^5.42.0", | ||
"@typescript-eslint/parser": "^5.42.0", | ||
"babel-jest": "^29.1.2", | ||
"eslint": "^8.26.0", | ||
"jest": "^29.2.2", | ||
@@ -29,0 +31,0 @@ "standard": "^17.0.0", |
Sorry, the diff of this file is not supported yet
20361
26
749
12