Home Reference Source

Function

Static Public Summary
public

abs(A: Array<mixed>): Array<mixed>

Get a copy of an array with absolute values of the original array entries.

public

accuracy(yTrue: Array<mixed>, yPred: Array<mixed>, normalize: boolean): number

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

argSort(array: Array<mixed>, compareFunction: function(a: mixed, b: mixed): Number): Array<number>

Sort an array and return the array indices of the sorted elements.

public

auroc(yTrue: Array<number>, yPred: Array<mixed>): number

Calculate the area under the receiver-operator characteristic curve (AUROC) for a set of predictions.

public

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

equal(array1: Array<mixed> | mixed, array2: Array<mixed> | mixed): boolean

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>

Set all entries in an array to a specific value and return the resulting array.

public

flatten(A: Array<mixed>): Array<mixed>

Recursively flatten an array.

public

full(shape: Array<number>, value: mixed): Array<mixed>

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

getShape(A: Array<mixed>): Array<number>

Find the shape of an array, i.e.

public

getTouchCoordinate(e: object, coordinate: string): *

Get touch coordinate (x or y) from touchpad input.

public

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

Load a dataset from a remote CSV file.

public

loadIris(callback: function(X: Array<Array<number>>, y: Array<number>))

Load the iris dataset.

public

Generate a mesh grid, i.e.

public

norm(x: Array<number>): *

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

power(A: Array<mixed>, y: number | Array<number>): Array<mixed>

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

randint(a: number, b: number, shape: number | Array<number>): number | Array<mixed>

Generate a random integer between a lower bound (inclusive) and an upper bound (exclusive).

public

repeat(axis: number, numRepeats: number, A: Array<mixed>): Array<mixed>

Repeat an array multiple times along an axis.

public

reshape(A: Array<mixed>, shape: Array<number>): Array<mixed>

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

scale(A: Array<mixed>, c: number): Array<mixed>

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

shuffle(S: ...Array<mixed>): Array<Array<mixed>>

Randomly shuffle multiple arrays in the primary (first) axis.

public

slice(A: Array<mixed>, start: Array<number>, stop: Array<number>): Array<mixed>

Take a slice out of an input array.

public

subBlock(A: Array<mixed>, offset: Array<number>, shape: Array<number>): Array<mixed>

this function was deprecated. Use slice() instead

Extract a sub-block of a matrix of a particular shape at a particular position.

public

sum(S: ...Array<mixed>): Array<mixed>

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

unique(array: Array<mixed>): Array<mixed>

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

wrapSlice(array: Array<mixed>, begin: number, end: number): Array<mixed>

Take a slice out of an array, but wrap around the beginning an end of the array.

public

zeros(shape: Array<number>): Array<mixed>

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

Get a copy of an array with absolute values of the original array entries.

Params:

NameTypeAttributeDescription
A Array<mixed>

Array to get absolute values array from

Return:

Array<mixed>

Array with absolute values

public accuracy(yTrue: Array<mixed>, yPred: Array<mixed>, normalize: boolean): number source

Evaluate the accuracy of a set of predictions.

Params:

NameTypeAttributeDescription
yTrue Array<mixed>

True labels

yPred Array<mixed>

Predicted labels

normalize boolean
  • optional
  • default: true

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:

NameTypeAttributeDescription
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

Return:

Array<number>

Array of array indices in the original array where the array element matches the filter

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.

Params:

NameTypeAttributeDescription
array Array<number>

Input array

Return:

number

Index of array element with largest value

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:

NameTypeAttributeDescription
array Array<mixed>

Array to be sorted

compareFunction function(a: mixed, b: mixed): Number
  • optional
  • default: null

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.

Return:

Array<number>

Array of array indices such that the elements corresponding with these indices in the original array are sorted

public auroc(yTrue: Array<number>, yPred: Array<mixed>): number source

Calculate the area under the receiver-operator characteristic curve (AUROC) for a set of predictions. Area is calculated using the Trapezoidal rule.

Params:

NameTypeAttributeDescription
yTrue Array<number>

True labels. Must contain only integers 0 and 1

yPred Array<mixed>

Predicted label confidences. Must be between 0 (fully confident in negative prediction) and 1 (fully confident in positive prediction), both inclusive

Return:

number

Calculated AUROC

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.

Params:

NameTypeAttributeDescription
array Array<number>

Sorted input array

value number

Value for which the interval should be found

Return:

number

Index i of array element A[i] such that value >= A[i] && value < A[i + 1]

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:

NameTypeAttributeDescription
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)

Return:

Array

Concatenated array

public dot(x: Array<number>, y: Array<number>): number source

Calculate dot product of two vectors. Vectors should have same size.

Params:

NameTypeAttributeDescription
x Array<number>

First vector

y Array<number>

Second vector

Return:

number

Dot product scalar result

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.

Params:

NameTypeAttributeDescription
array1 Array<mixed> | mixed

First array to check or array element to check

array2 Array<mixed> | mixed

Second array to check ot array element to check. Will be checked against first array

Return:

boolean

Whether the two arrays are the same

public fill(A: Array<mixed>, value: mixed): Array<mixed> source

Set all entries in an array to a specific value and return the resulting array. Original array is not modified.

Params:

NameTypeAttributeDescription
A Array<mixed>

Array of which entries should be changed

value mixed

Value the array entries should be changed to

Return:

Array<mixed>

Array with modified entries

public flatten(A: Array<mixed>): Array<mixed> source

import {flatten} from '@jsmlt/jsmlt/src/arrays/index.js'

Recursively flatten an array.

Params:

NameTypeAttributeDescription
A Array<mixed>

Array to be flattened

Return:

Array<mixed>

Flattened array

public full(shape: Array<number>, value: mixed): Array<mixed> source

Initialize an n-dimensional array of a certain value.

Params:

NameTypeAttributeDescription
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.

value mixed

Value to fill the array with

Return:

Array<mixed>

Array of the specified with zero in all entries

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.

Params:

NameTypeAttributeDescription
A Array<mixed>

Array to get an element from

index Array<number>

Indices to find array element. n-th element corresponds to index in n-th dimension

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:

NameTypeAttributeDescription
A Array<mixed>

Arbitrarily nested array to find shape of.

Return:

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 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.

Params:

NameTypeAttributeDescription
e object

Event

coordinate string

Coordinate ("x" or "y", case insensitive)

Return:

*

public internalSum(A: Array<number>): number source

import {internalSum} from '@jsmlt/jsmlt/src/arrays/index.js'

Sum all elements of an array.

Params:

NameTypeAttributeDescription
A Array<number>

Array

Return:

number

Sum of all vector elements

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).

Params:

NameTypeAttributeDescription
a number

Starting point

b number

Ending point

n number

Number of points

Return:

Array<number>

Array of evenly spaced points on the interval (a,b)

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.

Params:

NameTypeAttributeDescription
input string

Input CSV string

callback function(X: Array<Array<number>>, y: Array<number>)

Callback function with arguments X (features) and y (targets)

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.

Params:

NameTypeAttributeDescription
url string

Input CSV file URL

callback function(X: Array<Array<number>>, y: Array<number>)

Callback function with arguments X (features) and y (targets)

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

Params:

NameTypeAttributeDescription
callback function(X: Array<Array<number>>, y: Array<number>)

Callback function with arguments X (features) and y (targets). Called when the dataset is successfully loaded

Example:

Load the Iris dataset and run a Perceptron classifier on it
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]]

Params:

NameTypeAttributeDescription
x Array<number>

Vector of x-coordinates

y Array<number>

Vector of y-coordinates

Return:

Array<Array<Array<number>>>

Two-dimensional array containing the x-grid as the first element, and the y-grid as the second element

public norm(x: Array<number>): * source

Calculate the Euclidian norm of a vector.

Params:

NameTypeAttributeDescription
x Array<number>

Vector of which to calculate the norm

Return:

*

public pad(A: Array<mixed>, paddingLengths: Array<number>|Array<Array.<number>>, paddingValues: Array<number>|Array<Array.<number>>, axes: Array<number>): Array<mixed> source

Pad an array along one or multiple axes.

Params:

NameTypeAttributeDescription
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 paddingLength specifies the front and back padding of the nth axis in the axes parameter

paddingValues Array<number>|Array<Array.<number>>

The values to pad each axis with. See the specification of the paddingLenghts parameter for the expected structure

axes Array<number>
  • optional

Indices of axes to be padded. Defaults to the first n axes, where n is the number of elements in paddingLengths

Return:

Array<mixed>

Padded array

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.

Params:

NameTypeAttributeDescription
A Array<mixed>

Array of which the axes should be permuted

newAxes Array<number>

For the i-th element of this array, specify the index of the axis in the original array that should be used

Return:

Array<mixed>

Array with permuted axes

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:

NameTypeAttributeDescription
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)

Return:

Array<mixed>

Array containing the input elements, raised to the specified power

public rand(a: number, b: number): number source

Generate a random number between a lower bound (inclusive) and an upper bound (exclusive).

Params:

NameTypeAttributeDescription
a number

Lower bound (inclusive)

b number

Upper bound (exclusive)

Return:

number

Random number in range [a,b)

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:

NameTypeAttributeDescription
a number

Lower bound (inclusive)

b number

Upper bound (exclusive)

shape number | Array<number>
  • optional
  • default: null

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.

Return:

number | Array<mixed>

Random integer in range [a,b), or a possibly nested array of random integers in range [a, b)

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.

Params:

NameTypeAttributeDescription
axis number

Axis to perform repetition on

numRepeats number

Number of times to repeat the array

A Array<mixed>

Array to repeat

Return:

Array<mixed>

Specified array repeated numRepeats times

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.

Params:

NameTypeAttributeDescription
A Array<mixed>

Array to reshape

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.

Return:

Array<mixed>

Reshaped array

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:

NameTypeAttributeDescription
input Array<mixed>

Input array

number number

Number of elements to sample from the input array

withReplacement boolean
  • optional
  • default: true

Whether to sample with (set to true) or without replacement (false)

weights Array<number> | string
  • optional
  • default: 'uniform'

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

Return:

Array<mixed>

Array of length {number} with values sampled from 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.

Params:

NameTypeAttributeDescription
input Array<mixed>

Input array

number number

Number of elements to sample from the input array

Return:

Array<mixed>

Array of length {number} with values sampled from the input array

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).

Params:

NameTypeAttributeDescription
A Array<mixed>

Array to scale

c number

Scalar

Return:

Array<mixed>

Scaled 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.

Params:

NameTypeAttributeDescription
A Array<mixed>

Array to set an element in

index Array<number>

Indices to find array element. n-th element corresponds to index in n-th dimension

value mixed

New element value at index

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:

NameTypeAttributeDescription
S ...Array<mixed>

Arrays to shuffle. They must have the same size in the primary axis

Return:

Array<Array<mixed>>

Shuffled matrices

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:

NameTypeAttributeDescription
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.

Return:

Array<mixed>

Array slice extracted from input array

public subBlock(A: Array<mixed>, offset: Array<number>, shape: Array<number>): Array<mixed> source

import {subBlock} from '@jsmlt/jsmlt/src/arrays/index.js'
this function was deprecated. Use slice() instead

Extract a sub-block of a matrix of a particular shape at a particular position.

Params:

NameTypeAttributeDescription
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.

Return:

Array<mixed>

Sub-block extracted from array

public sum(S: ...Array<mixed>): Array<mixed> source

Calculate element-wise sum of two or more arrays. Arrays should have the same shape.

Params:

NameTypeAttributeDescription
S ...Array<mixed>

Arrays to concatenate. They must have the same shape

Return:

Array<mixed>

Sum of arrays

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:

NameTypeAttributeDescription
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
  • optional
  • default: 0.8

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:

Example with n=5 datapoints and d=2 features per sample
// 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.

Params:

NameTypeAttributeDescription
A Array<Array<number>>

Matrix or vector

Return:

Array<Array<number>>

Transpose of the matrix

public unique(array: Array<mixed>): Array<mixed> source

import {unique} from '@jsmlt/jsmlt/src/arrays/index.js'

Get unique elements in array

Params:

NameTypeAttributeDescription
array Array<mixed>

Input array

Return:

Array<mixed>

Array consisting of all unique elements in the 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:

NameTypeAttributeDescription
array Array<mixed>

Input array

Return:

Array<Array<mixed>>

Array where each element is a 2-dimensional array. In these 2D arrays, the first element corresponds to the unique array value, and the second elements corresponds to the number of times this value occurs in the original 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:

NameTypeAttributeDescription
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.

Params:

NameTypeAttributeDescription
array Array<mixed>

Input array

begin number

Index of first array element

end number

Index of end of slice range (element with this index will itself not be included in output)

Return:

Array<mixed>

Sliced array

public zeros(shape: Array<number>): Array<mixed> source

import {zeros} from '@jsmlt/jsmlt/src/arrays/index.js'

Initialize an n-dimensional array of zeros.

Params:

NameTypeAttributeDescription
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.

Return:

Array<mixed>

Array of the specified with zero in all entries

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:

NameTypeAttributeDescription
array Array<mixed>

Input array

Return:

Array<Array<mixed>>

Output array