array-normalize
Advanced tools
Comparing version 1.1.4 to 2.0.0
27
index.js
@@ -14,26 +14,21 @@ 'use strict' | ||
for (var offset = 0; offset < dim; offset++) { | ||
var max = bounds[dim + offset], min = bounds[offset], i = offset, l = arr.length; | ||
var max = bounds[dim + offset], min = bounds[offset], i = offset, l = arr.length, range; | ||
if (max === Infinity && min === -Infinity) { | ||
for (i = offset; i < l; i+=dim) { | ||
arr[i] = arr[i] === max ? 1 : arr[i] === min ? 0 : .5 | ||
} | ||
for (i = offset; i < l; i+=dim) arr[i] = arr[i] === max ? 1 : arr[i] === min ? 0 : .5 | ||
} | ||
else if (max === Infinity) { | ||
for (i = offset; i < l; i+=dim) { | ||
arr[i] = arr[i] === max ? 1 : 0 | ||
} | ||
for (i = offset; i < l; i+=dim) arr[i] = arr[i] === max ? 1 : 0 | ||
} | ||
else if (min === -Infinity) { | ||
for (i = offset; i < l; i+=dim) { | ||
arr[i] = arr[i] === min ? 0 : 1 | ||
} | ||
for (i = offset; i < l; i+=dim) arr[i] = arr[i] === min ? 0 : 1 | ||
} | ||
else if (min === max) { | ||
for (i = offset; i < l; i+=dim) if (!isNaN(arr[i])) arr[i] = .5 | ||
} | ||
else if (min === 0) { | ||
for (i = offset; i < l; i+=dim) arr[i] = arr[i] / max | ||
} | ||
else { | ||
var range = max - min | ||
for (i = offset; i < l; i+=dim) { | ||
if (!isNaN(arr[i])) { | ||
arr[i] = range === 0 ? .5 : (arr[i] - min) / range | ||
} | ||
} | ||
for (i = offset, range = max - min; i < l; i+=dim) arr[i] = (arr[i] - min) / range | ||
} | ||
@@ -40,0 +35,0 @@ } |
{ | ||
"name": "array-normalize", | ||
"version": "1.1.4", | ||
"version": "2.0.0", | ||
"description": "Normalize array (possibly n-dimensional) to zero mean and unit variance", | ||
"main": "index.js", | ||
"browser": "index.js", | ||
"module": "./index.mjs", | ||
"exports": { | ||
"require": "./index.js", | ||
"import": "./index.mjs" | ||
}, | ||
"scripts": { | ||
@@ -11,3 +17,3 @@ "test": "node test" | ||
"type": "git", | ||
"url": "git+https://github.com/dfcreative/array-normalize.git" | ||
"url": "git+https://github.com/dy/array-normalize.git" | ||
}, | ||
@@ -21,8 +27,8 @@ "keywords": [ | ||
], | ||
"author": "Dima Yv <dfcreative@gmail.com>", | ||
"author": "Dima Iv <dfcreative@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/dfcreative/array-normalize/issues" | ||
"url": "https://github.com/dy/array-normalize/issues" | ||
}, | ||
"homepage": "https://github.com/dfcreative/array-normalize#readme", | ||
"homepage": "https://github.com/dy/array-normalize#readme", | ||
"dependencies": { | ||
@@ -29,0 +35,0 @@ "array-bounds": "^1.0.0" |
@@ -1,2 +0,2 @@ | ||
# array-normalize [![experimental](https://img.shields.io/badge/stability-unstable-yellow.svg)](http://github.com/badges/stability-badges) [![Build Status](https://img.shields.io/travis/dfcreative/array-normalize.svg)](https://travis-ci.org/dfcreative/array-normalize) | ||
# array-normalize [![experimental](https://img.shields.io/badge/stability-unstable-yellow.svg)](http://github.com/badges/stability-badges) [![Build Status](https://img.shields.io/travis/dy/array-normalize.svg)](https://travis-ci.com/dy/array-normalize) | ||
@@ -17,8 +17,10 @@ Normalize array to unit length, that is 0..1 range. See [feature scaling](https://en.wikipedia.org/wiki/Feature_scaling). | ||
### array = normalize(array, dimensions=1, bounds?) | ||
### array = normalize(array, stride=1, bounds?) | ||
Normalizes n-dimensional array in-place using `dimensions` as stride, ie. for 1d array the expected data layout is `[x, x, x, ...]` for 2d is `[x, y, x, y, ...]`, etc. | ||
Normalizes n-dimensional array in-place using optional stride for n-dimensions, ie. for 2d data layout is `[x, y, x, y, ...]`. | ||
Every dimension is normalized independently, eg. 2d array is normalized to unit square `[0, 0, 1, 1]`. | ||
Optional `bounds` box can predefine min/max to optimize calculations. | ||
Optional `bounds` box can predefine min/max to skip bounds detection. | ||
<p align="center">ॐ</p> |
16
test.js
'use strict' | ||
const norm = require('./') | ||
const norm = require('.') | ||
const assert = require('assert') | ||
@@ -8,20 +8,20 @@ const getBounds = require('array-bounds') | ||
let a = [-Infinity, -1, 0, 1, Infinity] | ||
assert.deepEqual(norm(a.slice()), [0, 0.5, 0.5, 0.5, 1]) | ||
assert.deepStrictEqual(norm(a.slice()), [0, 0.5, 0.5, 0.5, 1]) | ||
let b = [0, .5, 1] | ||
assert.deepEqual(norm(b.slice()), b) | ||
assert.deepStrictEqual(norm(b.slice()), b) | ||
let c = [0, 50, 100] | ||
assert.deepEqual(norm(c.slice()), [0, .5, 1]) | ||
assert.deepStrictEqual(norm(c.slice()), [0, .5, 1]) | ||
let d = [0, 0, .1, .2, 1, 2] | ||
assert.deepEqual(norm(d.slice(), 2), [0, 0, .1, .1, 1, 1]) | ||
assert.deepStrictEqual(norm(d.slice(), 2), [0, 0, .1, .1, 1, 1]) | ||
let e = [0, 0, .1, .2, 1, 2] | ||
assert.deepEqual(norm(e.slice(), 2, getBounds(e, 2)), [0, 0, .1, .1, 1, 1]) | ||
assert.deepStrictEqual(norm(e.slice(), 2, getBounds(e, 2)), [0, 0, .1, .1, 1, 1]) | ||
let f = [0, .25, 1, .25] | ||
assert.deepEqual(norm(f, 2, [0, .5, 1, .5]), [0, .5, 1, .5]) | ||
assert.deepStrictEqual(norm(f, 2, [0, .5, 1, .5]), [0, .5, 1, .5]) | ||
let g = [0, 0, NaN, NaN, 1, 1] | ||
assert.deepEqual(norm(g, 2).map(v => isNaN(v) ? -1 : v), [0, 0, -1, -1, 1, 1]) | ||
assert.deepStrictEqual(norm(g, 2).map(v => isNaN(v) ? -1 : v), [0, 0, -1, -1, 1, 1]) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7192
8
138
26
1