array-normalize
Advanced tools
Comparing version 1.1.1 to 1.1.2
12
index.js
'use strict' | ||
var getBounds = require('array-bounds') | ||
module.exports = normalize; | ||
function normalize (arr, dim) { | ||
function normalize (arr, dim, bounds) { | ||
if (!arr || arr.length == null) throw Error('Argument should be an array') | ||
if (dim == null) dim = 1 | ||
if (bounds == null) bounds = getBounds(arr, dim) | ||
for (var offset = 0; offset < dim; offset++) { | ||
var max = -Infinity, min = Infinity, i = offset, l = arr.length; | ||
var max = bounds[dim + offset], min = bounds[offset], i = offset, l = arr.length; | ||
for (; i < l; i+=dim) { | ||
if (arr[i] > max) max = arr[i]; | ||
if (arr[i] < min) min = arr[i]; | ||
} | ||
if (max === Infinity && min === -Infinity) { | ||
@@ -19,0 +17,0 @@ for (i = offset; i < l; i+=dim) { |
{ | ||
"name": "array-normalize", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Normalize array (possibly n-dimensional) to zero mean and unit variance", | ||
@@ -25,3 +25,6 @@ "main": "index.js", | ||
}, | ||
"homepage": "https://github.com/dfcreative/array-normalize#readme" | ||
"homepage": "https://github.com/dfcreative/array-normalize#readme", | ||
"dependencies": { | ||
"array-bounds": "^1.0.0" | ||
} | ||
} |
# array-normalize [](http://github.com/badges/stability-badges) [](https://travis-ci.org/dfcreative/array-normalize) | ||
Normalize array in-place to zero mean and unit variance. | ||
Normalize array to unit length, that is 0..1 range. See [feature scaling](https://en.wikipedia.org/wiki/Feature_scaling). | ||
@@ -15,4 +15,4 @@ [](https://npmjs.org/package/array-normalize/) | ||
### array = normalize(array, n=1) | ||
### array = normalize(array, n=1, bounds?) | ||
Normalizes n-dimensional array in-place using dimensions `n` as stride, ie. for 1d array the expected data layout is `[x, x, x, ...]` for 2d is `[x, y, x, y, ...]`, etc. Every dimension is normalized independently, so 2d array is normalized to unit square `[0, 0, 1, 1]`. | ||
Normalizes n-dimensional array in-place using dimensions `n` as stride, ie. for 1d array the expected data layout is `[x, x, x, ...]` for 2d is `[x, y, x, y, ...]`, etc. Every dimension is normalized independently, so 2d array is normalized to unit square `[0, 0, 1, 1]`. Optionally pass `bounds` box if you know min/max values to optimize calculations. |
@@ -5,2 +5,3 @@ 'use strict' | ||
const assert = require('assert') | ||
const getBounds = require('array-bounds') | ||
@@ -18,1 +19,4 @@ let a = [-Infinity, -1, 0, 1, Infinity] | ||
assert.deepEqual(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]) |
4704
7
91
1
+ Addedarray-bounds@^1.0.0
+ Addedarray-bounds@1.0.1(transitive)