Function
Static Public Summary | ||
public |
Get a copy of an array with absolute values of the original array entries. |
|
public |
Evaluate the accuracy of a set of predictions. |
|
public |
argFilter(array: Array<mixed>, callback: function(element: mixed, !index: Number): boolean): Array<number> Filter an array and return the array indices where the filter was matched. |
|
public |
Get array key corresponding to largest element in the array. |
|
public |
Sort an array and return the array indices of the sorted elements. |
|
public |
Calculate the area under the receiver-operator characteristic curve (AUROC) for a set of predictions. |
|
public |
binaryIntervalSearch(array: Array<number>, value: number): number Perform a binary search in a sorted array A to find the index i such that the search value is larger than or equal to A[i], and strictly smaller than A[i+1]. |
|
public |
concatenate(axis: number, S: ...Array<mixed>): Array Concatenate two or more n-dimensional arrays. |
|
public |
Calculate dot product of two vectors. |
|
public |
Deep check whether two arrays are equal: sub-arrays will be traversed, and strong type checking is enabled. |
|
public |
Set all entries in an array to a specific value and return the resulting array. |
|
public |
Recursively flatten an array. |
|
public |
Initialize an n-dimensional array of a certain value. |
|
public |
getArrayElement(A: Array<mixed>, index: Array<number>): mixed Get an arbitrary element from an array, using another array to determine the index inside the first array. |
|
public |
Find the shape of an array, i.e. |
|
public |
getTouchCoordinate(e: object, coordinate: string): * Get touch coordinate (x or y) from touchpad input. |
|
public |
internalSum(A: Array<number>): number Sum all elements of an array. |
|
public |
Generate n points on the interval (a,b), with intervals (b-a)/(n-1). |
|
public |
Load a dataset (features and target) from some CSV input string. |
|
public |
loadDatasetFromRemoteCSV(url: string, callback: function(X: Array<Array<number>>, y: Array<number>)) Load a dataset from a remote CSV file. |
|
public |
Load the iris dataset. |
|
public |
Generate a mesh grid, i.e. |
|
public |
Calculate the Euclidian norm of a vector. |
|
public |
pad(A: Array<mixed>, paddingLengths: Array<number>|Array<Array.<number>>, paddingValues: Array<number>|Array<Array.<number>>, axes: Array<number>): Array<mixed> Pad an array along one or multiple axes. |
|
public |
permuteAxes(A: Array<mixed>, newAxes: Array<number>): Array<mixed> Permute the axes of an input array. |
|
public |
Raise all elements in an array to some power. |
|
public |
Generate a random number between a lower bound (inclusive) and an upper bound (exclusive). |
|
public |
Generate a random integer between a lower bound (inclusive) and an upper bound (exclusive). |
|
public |
Repeat an array multiple times along an axis. |
|
public |
Reshape an array into a different shape. |
|
public |
sample(input: Array<mixed>, number: number, withReplacement: boolean, weights: Array<number> | string): Array<mixed> Take a random sample with or without replacement from an array. |
|
public |
sampleFisherYates(input: Array<mixed>, number: number): Array<mixed> Take a random sample without replacement from an array. |
|
public |
Multiply each element of an array by a scalar (i.e. |
|
public |
setArrayElement(A: Array<mixed>, index: Array<number>, value: mixed): * Set an arbitrary element in an array, using another array to determine the index inside the array. |
|
public |
Randomly shuffle multiple arrays in the primary (first) axis. |
|
public |
Take a slice out of an input array. |
|
public |
this function was deprecated. Use slice() instead
Extract a sub-block of a matrix of a particular shape at a particular position. |
|
public |
Calculate element-wise sum of two or more arrays. |
|
public |
trainTestSplit(input: Array<Array<mixed>>, optionsUser: Object): Array Split a dataset into a training and a test set. |
|
public |
Get the transpose of a matrix or vector. |
|
public |
Get unique elements in array |
|
public |
valueCounts(array: Array<mixed>): Array<Array<mixed>> Count the occurrences of the unique values in an array |
|
public |
valueVector(n: number, value: mixed): * Initialize a vector of a certain length with a specific value in each entry. |
|
public |
Take a slice out of an array, but wrap around the beginning an end of the array. |
|
public |
Initialize an n-dimensional array of zeros. |
|
public |
zipWithIndex(array: Array<mixed>): Array<Array<mixed>> From an input array, create a new array where each element is comprised of a 2-dimensional array where the first element is the original array entry and the second element is its index |
Static Public
public abs(A: Array<mixed>): Array<mixed> source
import {abs} from '@jsmlt/jsmlt/src/arrays/index.js'
Get a copy of an array with absolute values of the original array entries.
Params:
Name | Type | Attribute | Description |
A | Array<mixed> | Array to get absolute values array from |
public accuracy(yTrue: Array<mixed>, yPred: Array<mixed>, normalize: boolean): number source
import {accuracy} from '@jsmlt/jsmlt/src/validation/metrics/index.js'
Evaluate the accuracy of a set of predictions.
Params:
Name | Type | Attribute | Description |
yTrue | Array<mixed> | True labels |
|
yPred | Array<mixed> | Predicted labels |
|
normalize | boolean |
|
Whether to normalize the accuracy to a range between 0 and 1. In this context, 0 means no predictions were correct, and 1 means all predictions were correct. If set to false, the integer number of correct predictions is returned |
Return:
number | Proportion of correct predictions (if normalize=true) or integer number of correct predictions (if normalize=false) |
public argFilter(array: Array<mixed>, callback: function(element: mixed, !index: Number): boolean): Array<number> source
import {argFilter} from '@jsmlt/jsmlt/src/arrays/index.js'
Filter an array and return the array indices where the filter was matched. Corresponds to JavaScript's native Array.filter(), but instead of returning the elements that match the filter criteria, it returns the indices of the elements matching the filter.
Params:
Name | Type | Attribute | Description |
array | Array<mixed> | Array to be filtered |
|
callback | function(element: mixed, !index: Number): boolean | Callback function to be used for filtering. This function takes an array element and possibly its index as its input and should return true when the index should be used (filtered) and false when it shouldn't |
public argMax(array: Array<number>): number source
import {argMax} from '@jsmlt/jsmlt/src/arrays/index.js'
Get array key corresponding to largest element in the array.
public argSort(array: Array<mixed>, compareFunction: function(a: mixed, b: mixed): Number): Array<number> source
import {argSort} from '@jsmlt/jsmlt/src/arrays/index.js'
Sort an array and return the array indices of the sorted elements. Corresponds to JavaScript's native Array.sort(), but instead of returning the sorted elements, it returns the indices of the sorted elements.
Params:
Name | Type | Attribute | Description |
array | Array<mixed> | Array to be sorted |
|
compareFunction | function(a: mixed, b: mixed): Number |
|
Callback function be used for sorting. This function takes two array elements and returns an integer indicating sort order of the two elements a and b: < 0 : a before b == 0 : leave order of a and b unchanged with respect to each other > 0 : b after a Defaults to numeric sorting. |
public auroc(yTrue: Array<number>, yPred: Array<mixed>): number source
import {auroc} from '@jsmlt/jsmlt/src/validation/metrics/index.js'
Calculate the area under the receiver-operator characteristic curve (AUROC) for a set of predictions. Area is calculated using the Trapezoidal rule.
public binaryIntervalSearch(array: Array<number>, value: number): number source
import {binaryIntervalSearch} from '@jsmlt/jsmlt/src/util/search/index.js'
Perform a binary search in a sorted array A to find the index i such that the search value is larger than or equal to A[i], and strictly smaller than A[i+1]. Informally, this corresponds with finding the interval in which the search value lies. Throws an error when the input value is outside of the array range. Does not check whether the array is actually sorted.
public concatenate(axis: number, S: ...Array<mixed>): Array source
import {concatenate} from '@jsmlt/jsmlt/src/arrays/index.js'
Concatenate two or more n-dimensional arrays.
Params:
Name | Type | Attribute | Description |
axis | number | Axis to perform concatenation on |
|
S | ...Array<mixed> | Arrays to concatenate. They must have the same shape, except in the dimension corresponding to axis (the first, by default) |
public dot(x: Array<number>, y: Array<number>): number source
import {dot} from '@jsmlt/jsmlt/src/arrays/index.js'
Calculate dot product of two vectors. Vectors should have same size.
public equal(array1: Array<mixed> | mixed, array2: Array<mixed> | mixed): boolean source
import {equal} from '@jsmlt/jsmlt/src/arrays/index.js'
Deep check whether two arrays are equal: sub-arrays will be traversed, and strong type checking is enabled.
public fill(A: Array<mixed>, value: mixed): Array<mixed> source
import {fill} from '@jsmlt/jsmlt/src/arrays/index.js'
Set all entries in an array to a specific value and return the resulting array. Original array is not modified.
Params:
Name | Type | Attribute | Description |
A | Array<mixed> | Array of which entries should be changed |
|
value | mixed | Value the array entries should be changed to |
public flatten(A: Array<mixed>): Array<mixed> source
import {flatten} from '@jsmlt/jsmlt/src/arrays/index.js'
Recursively flatten an array.
Params:
Name | Type | Attribute | Description |
A | Array<mixed> | Array to be flattened |
public full(shape: Array<number>, value: mixed): Array<mixed> source
import {full} from '@jsmlt/jsmlt/src/arrays/index.js'
Initialize an n-dimensional array of a certain value.
public getArrayElement(A: Array<mixed>, index: Array<number>): mixed source
import {getArrayElement} from '@jsmlt/jsmlt/src/arrays/index.js'
Get an arbitrary element from an array, using another array to determine the index inside the first array.
Return:
mixed | Array element value at index |
public getShape(A: Array<mixed>): Array<number> source
import {getShape} from '@jsmlt/jsmlt/src/arrays/index.js'
Find the shape of an array, i.e. the number of elements per dimension of the array.
Params:
Name | Type | Attribute | Description |
A | Array<mixed> | Arbitrarily nested array to find shape of. |
public getTouchCoordinate(e: object, coordinate: string): * source
import {getTouchCoordinate} from '@jsmlt/jsmlt/src/util/input-devices/index.js'
Get touch coordinate (x or y) from touchpad input.
Return:
* |
public internalSum(A: Array<number>): number source
import {internalSum} from '@jsmlt/jsmlt/src/arrays/index.js'
Sum all elements of an array.
public linspace(a: number, b: number, n: number): Array<number> source
import {linspace} from '@jsmlt/jsmlt/src/arrays/index.js'
Generate n points on the interval (a,b), with intervals (b-a)/(n-1).
Example:
var list = linspace(1, 3, 0.5);
// list now contains [1, 1.5, 2, 2.5, 3]
public loadDatasetFromCSV(input: string, callback: function(X: Array<Array<number>>, y: Array<number>)) source
import {loadDatasetFromCSV} from '@jsmlt/jsmlt/src/datasets/index.js'
Load a dataset (features and target) from some CSV input string. Extracts the data from the CSV and uses all but the last column as the features and the last column as the target. This function is asynchronous, and needs a user callback for when the file is successfully parsed.
public loadDatasetFromRemoteCSV(url: string, callback: function(X: Array<Array<number>>, y: Array<number>)) source
import {loadDatasetFromRemoteCSV} from '@jsmlt/jsmlt/src/datasets/index.js'
Load a dataset from a remote CSV file. Fetches the CSV file and calls loadDatasetFromCSV. This function is asynchronous, and needs a user callback for when the remote CSV file is successfully loaded and parsed.
public loadIris(callback: function(X: Array<Array<number>>, y: Array<number>)) source
import {loadIris} from '@jsmlt/jsmlt/src/datasets/index.js'
Load the iris dataset. This is an asynchronous function: when the Iris dataset is loaded, a user-specified callback function is invoked, with the data set features array and the targets array as the first and second parameter, respectively.
For more information, see https://github.com/jsmlt/datasets/tree/master/iris
Example:
var datasets = require('@jsmlt/jsmlt/datasets');
var Perceptron = require('@jsmlt/jsmlt/supervised/linear/perceptron');
datasets.loadIris(function(X, y) {
var clf = new Perceptron();
clf.train(X, y);
});
public meshGrid(x: Array<number>, y: Array<number>): Array<Array<Array<number>>> source
import {meshGrid} from '@jsmlt/jsmlt/src/arrays/index.js'
Generate a mesh grid, i.e. two m-by-n arrays where m=|y| and n=|x|, from two vectors. The mesh grid generates two grids, where the first grid repeats x row-wise m times, and the second grid repeats y column-wise n times. Can be used to generate coordinate grids.
Example input: x=[0, 1, 2], y=[2, 4, 6, 8] Corresponding output: matrix 1: [[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]] matrix 2: [[2, 2, 2], [4, 4, 4], [6, 6, 6], [8, 8, 8]]
public norm(x: Array<number>): * source
import {norm} from '@jsmlt/jsmlt/src/arrays/index.js'
Calculate the Euclidian norm of a vector.
Return:
* |
public pad(A: Array<mixed>, paddingLengths: Array<number>|Array<Array.<number>>, paddingValues: Array<number>|Array<Array.<number>>, axes: Array<number>): Array<mixed> source
import {pad} from '@jsmlt/jsmlt/src/arrays/index.js'
Pad an array along one or multiple axes.
Params:
Name | Type | Attribute | Description |
A | Array<mixed> | Array to be padded |
|
paddingLengths | Array<number>|Array<Array.<number>> | Amount of padding for each axis
that should be padded. Each element in this array should be a two-dimensional array, where the
first element specifies the padding at the start (front) of the axis, and the second element
specifies the padding at the end (back) of the axis. The nth element of |
|
paddingValues | Array<number>|Array<Array.<number>> | The values to pad each axis
with. See the specification of the |
|
axes | Array<number> |
|
Indices of axes to be padded. Defaults to the first n axes,
where n is the number of elements in |
public permuteAxes(A: Array<mixed>, newAxes: Array<number>): Array<mixed> source
import {permuteAxes} from '@jsmlt/jsmlt/src/arrays/index.js'
Permute the axes of an input array. In other words, you can interchange the axes of an n-dimensional input array.
public power(A: Array<mixed>, y: number | Array<number>): Array<mixed> source
import {power} from '@jsmlt/jsmlt/src/arrays/index.js'
Raise all elements in an array to some power. The power to raise the elements to can either be the same number for all elements, in which case it should be passed as a number, or an individual number for all elements, in which case it should be passed as an array of the same shape as the input array.
Params:
Name | Type | Attribute | Description |
A | Array<mixed> | Input array |
|
y | number | Array<number> | The power to raise all elements to. Either a {number} (all elements will be raised to this power) or an array (elements in the input array will be raised to the power specified at the same position in the powers array) |
public rand(a: number, b: number): number source
import {rand} from '@jsmlt/jsmlt/src/random/index.js'
Generate a random number between a lower bound (inclusive) and an upper bound (exclusive).
public randint(a: number, b: number, shape: number | Array<number>): number | Array<mixed> source
import {randint} from '@jsmlt/jsmlt/src/random/index.js'
Generate a random integer between a lower bound (inclusive) and an upper bound (exclusive).
Params:
Name | Type | Attribute | Description |
a | number | Lower bound (inclusive) |
|
b | number | Upper bound (exclusive) |
|
shape | number | Array<number> |
|
If null, a single element is returned. If integer, an array of {shape} elements is returned. If an Array, random numbers are returned in a shape specified by this array. n-th element corresponds to the number of elements in the n-th dimension. |
public repeat(axis: number, numRepeats: number, A: Array<mixed>): Array<mixed> source
import {repeat} from '@jsmlt/jsmlt/src/arrays/index.js'
Repeat an array multiple times along an axis. This is essentially one or more concatenations of an array with itself.
public reshape(A: Array<mixed>, shape: Array<number>): Array<mixed> source
import {reshape} from '@jsmlt/jsmlt/src/arrays/index.js'
Reshape an array into a different shape.
public sample(input: Array<mixed>, number: number, withReplacement: boolean, weights: Array<number> | string): Array<mixed> source
import {sample} from '@jsmlt/jsmlt/src/random/index.js'
Take a random sample with or without replacement from an array. Supports using sampling weights, governing the probability of an item in the input array being selected.
Params:
Name | Type | Attribute | Description |
input | Array<mixed> | Input array |
|
number | number | Number of elements to sample from the input array |
|
withReplacement | boolean |
|
Whether to sample with (set to true) or without replacement (false) |
weights | Array<number> | string |
|
Weights to use for sampling. Defaults to 'uniform', which means all samples have equal probability of being selected. Alternatively, you can pass an array of weights, containing a single weight for each element in the input array |
public sampleFisherYates(input: Array<mixed>, number: number): Array<mixed> source
import {sampleFisherYates} from '@jsmlt/jsmlt/src/random/index.js'
Take a random sample without replacement from an array. Uses the Fisher-Yates shuffling, algorithm, modified to accomodate sampling.
public scale(A: Array<mixed>, c: number): Array<mixed> source
import {scale} from '@jsmlt/jsmlt/src/arrays/index.js'
Multiply each element of an array by a scalar (i.e. scale the array).
public setArrayElement(A: Array<mixed>, index: Array<number>, value: mixed): * source
import {setArrayElement} from '@jsmlt/jsmlt/src/arrays/index.js'
Set an arbitrary element in an array, using another array to determine the index inside the array.
Return:
* |
public shuffle(S: ...Array<mixed>): Array<Array<mixed>> source
import {shuffle} from '@jsmlt/jsmlt/src/arrays/index.js'
Randomly shuffle multiple arrays in the primary (first) axis. All input arrays are shuffled simultaneously, i.e., if an element with index i is moved to index j for the first array, the the same happens for the second (and third, etc.) array.
Params:
Name | Type | Attribute | Description |
S | ...Array<mixed> | Arrays to shuffle. They must have the same size in the primary axis |
public slice(A: Array<mixed>, start: Array<number>, stop: Array<number>): Array<mixed> source
import {slice} from '@jsmlt/jsmlt/src/arrays/index.js'
Take a slice out of an input array. Negative indices can be used in both the starting indices and the stopping indices. Negative indices: the negative stopping index is used as the negative offset relative to the last index in the particular dimension.
Params:
Name | Type | Attribute | Description |
A | Array<mixed> | Array to extract block from |
|
start | Array<number> | Array specifying the starting index per dimension. n-th element corresponds to the number of elements to skip, before extracting the block, in the n-th dimension. Negative indices are supported. |
|
stop | Array<number> | Array specifying the index to stop at (exclusive) per dimension. n-th element corresponds to the stopping index in the n-th dimension. Negative indices are supported. Use null for unlimited offset. |
public subBlock(A: Array<mixed>, offset: Array<number>, shape: Array<number>): Array<mixed> source
import {subBlock} from '@jsmlt/jsmlt/src/arrays/index.js'
Extract a sub-block of a matrix of a particular shape at a particular position.
Params:
Name | Type | Attribute | Description |
A | Array<mixed> | Array to extract block from |
|
offset | Array<number> | Array specifying the offset per dimension. n-th element corresponds to the number of elements to skip, before extracting the block, in the n-th dimension. |
|
shape | Array<number> | Array specifying the number of elements per dimension. n-th element corresponds to the number of elements in the n-th dimension. |
public sum(S: ...Array<mixed>): Array<mixed> source
import {sum} from '@jsmlt/jsmlt/src/arrays/index.js'
Calculate element-wise sum of two or more arrays. Arrays should have the same shape.
Params:
Name | Type | Attribute | Description |
S | ...Array<mixed> | Arrays to concatenate. They must have the same shape |
public trainTestSplit(input: Array<Array<mixed>>, optionsUser: Object): Array source
import {trainTestSplit} from '@jsmlt/jsmlt/src/model-selection/index.js'
Split a dataset into a training and a test set.
Params:
Name | Type | Attribute | Description |
input | Array<Array<mixed>> | List of input arrays. The input arrays should have the same length (i.e., they should have the same first dimension size) |
|
optionsUser | Object | User-defined options. See method implementation for details |
|
optionsUser.trainSize | number |
|
Size of the training set. If int, this exact number of training samples is used. If float, the total number of elements times the float number is used as the number of training elements |
Return:
Array | List of output arrays. The number of elements is 2 times the number of input elements. For each input element, a pair of output elements is returned. |
Example:
// n x d array of features
var X = [[0, 0], [0.5, 0.2], [0.3, 2.5], [0.8, 0.9], [0.7, 0.2]];
// n-dimensional array of labels
var y = [1, 0, 0, 1, 1]; // n-dimensional array of labels
// Split into training and test set
var [X_train, y_train, X_test, y_test] = trainTestSplit([X, y], {trainSize: 0.8});
// Now, X_train and y_train will contain the features and labels of the training set,
// respectively, and X_test and y_test will contain the features and labels of the test set.
// Depending on the random seed, the result might be the following
X_train: [[0, 0], [0.5, 0.2], [0.3, 2.5], [0.7, 0.2]]
y_train: [1, 0, 0, 1]
X_test: [[0.8, 0.9]]
y_test: [1]
public transpose(A: Array<Array<number>>): Array<Array<number>> source
import {transpose} from '@jsmlt/jsmlt/src/arrays/index.js'
Get the transpose of a matrix or vector.
public unique(array: Array<mixed>): Array<mixed> source
import {unique} from '@jsmlt/jsmlt/src/arrays/index.js'
Get unique elements in array
Params:
Name | Type | Attribute | Description |
array | Array<mixed> | Input array |
public valueCounts(array: Array<mixed>): Array<Array<mixed>> source
import {valueCounts} from '@jsmlt/jsmlt/src/arrays/index.js'
Count the occurrences of the unique values in an array
Params:
Name | Type | Attribute | Description |
array | Array<mixed> | Input array |
public valueVector(n: number, value: mixed): * source
import {valueVector} from '@jsmlt/jsmlt/src/arrays/index.js'
Initialize a vector of a certain length with a specific value in each entry.
Params:
Name | Type | Attribute | Description |
n | number | Number of elements in the vector |
|
value | mixed | Value to initialize entries at |
Return:
* | Array Vector of n elements of the specified value |
public wrapSlice(array: Array<mixed>, begin: number, end: number): Array<mixed> source
import {wrapSlice} from '@jsmlt/jsmlt/src/arrays/index.js'
Take a slice out of an array, but wrap around the beginning an end of the array. For example,
if begin
is -1, the last element of the input array is used as the first output element.
public zeros(shape: Array<number>): Array<mixed> source
import {zeros} from '@jsmlt/jsmlt/src/arrays/index.js'
Initialize an n-dimensional array of zeros.
public zipWithIndex(array: Array<mixed>): Array<Array<mixed>> source
import {zipWithIndex} from '@jsmlt/jsmlt/src/arrays/index.js'
From an input array, create a new array where each element is comprised of a 2-dimensional array where the first element is the original array entry and the second element is its index
Params:
Name | Type | Attribute | Description |
array | Array<mixed> | Input array |