Skip to main content

unique

Find the sorted unique elements of an array. Optionally return indices, inverse mapping, and counts.
function unique(
  ar: ArrayLike,
  returnIndex?: boolean,
  returnInverse?: boolean,
  returnCounts?: boolean
): NDArray | { values: NDArray; indices?: NDArray; inverse?: NDArray; counts?: NDArray; }
NameTypeDefaultDescription
arArrayLikeInput array. It is flattened before computing unique values.
returnIndexbooleanfalseIf true, also return the indices of the first occurrences.
returnInversebooleanfalseIf true, also return the indices to reconstruct the original from the unique array.
returnCountsbooleanfalseIf true, also return the number of times each unique value appears.
Returns: NDArray when no optional flags are true. Object { values, indices?, inverse?, counts? } when one or more flags are true.
import * as np from 'numpy-ts';

const a = np.array([3, 1, 2, 1, 3, 2, 3]);

np.unique(a);
// array([1, 2, 3])

const result = np.unique(a, true, true, true);
console.log(result.values.toString());   // array([1, 2, 3])
console.log(result.indices!.toString()); // first occurrence positions
console.log(result.inverse!.toString()); // reconstruct original
console.log(result.counts!.toString());  // occurrence counts

unique_all

Return sorted unique values along with their first-occurrence indices, inverse mapping, and counts. This is a structured-return alternative to calling unique with all flags set to true.
function unique_all(x: ArrayLike): {
  values: NDArray;
  indices: NDArray;
  inverse_indices: NDArray;
  counts: NDArray;
}
NameTypeDefaultDescription
xArrayLikeInput array.
Returns: An object with four properties: values (sorted unique elements), indices (first-occurrence indices), inverse_indices (mapping to reconstruct input), and counts (element counts).
import * as np from 'numpy-ts';

const result = np.unique_all([3, 1, 2, 1, 3]);
// result.values          = array([1, 2, 3])
// result.indices         = array([1, 2, 0])
// result.inverse_indices = array([2, 0, 1, 0, 2])
// result.counts          = array([2, 1, 2])

unique_counts

Return sorted unique values and their counts.
function unique_counts(x: ArrayLike): {
  values: NDArray;
  counts: NDArray;
}
NameTypeDefaultDescription
xArrayLikeInput array.
Returns: An object with values (sorted unique elements) and counts (number of occurrences of each).
import * as np from 'numpy-ts';

const result = np.unique_counts([1, 1, 2, 3, 3, 3]);
// result.values = array([1, 2, 3])
// result.counts = array([2, 1, 3])

unique_inverse

Return sorted unique values and the inverse mapping to reconstruct the original array.
function unique_inverse(x: ArrayLike): {
  values: NDArray;
  inverse_indices: NDArray;
}
NameTypeDefaultDescription
xArrayLikeInput array.
Returns: An object with values (sorted unique elements) and inverse_indices (indices into values that reconstruct the original).
import * as np from 'numpy-ts';

const result = np.unique_inverse([3, 1, 2, 1]);
// result.values          = array([1, 2, 3])
// result.inverse_indices = array([2, 0, 1, 0])
// Reconstruct: values[inverse_indices] -> [3, 1, 2, 1]

unique_values

Return only the sorted unique values (no indices, inverse, or counts).
function unique_values(x: ArrayLike): NDArray
NameTypeDefaultDescription
xArrayLikeInput array.
Returns: NDArray — Sorted unique values.
import * as np from 'numpy-ts';

np.unique_values([3, 1, 2, 1, 3]);
// array([1, 2, 3])

in1d

Test whether each element of a 1D array is also present in a second array. Returns a flattened boolean array.
function in1d(ar1: ArrayLike, ar2: ArrayLike): NDArray
NameTypeDefaultDescription
ar1ArrayLikeInput array to test.
ar2ArrayLikeValues to test against.
invertbooleanfalseIf true, invert the result: return true where elements of ar1 are not in ar2.
Returns: NDArray — Boolean array of the same length as ar1 (flattened).
import * as np from 'numpy-ts';

np.in1d([1, 2, 3, 4], [2, 4]);
// array([false, true, false, true])

np.in1d([1, 2, 3, 4], [2, 4], true);
// array([true, false, true, false])  -- inverted

isin

Test whether each element of an array is present in a set of test elements. Unlike in1d, this preserves the shape of the input.
function isin(element: ArrayLike, testElements: ArrayLike): NDArray
NameTypeDefaultDescription
elementArrayLikeInput array to test.
testElementsArrayLikeValues to test against. Flattened internally.
invertbooleanfalseIf true, return true where elements are not in testElements.
Returns: NDArray — Boolean array with the same shape as element.
import * as np from 'numpy-ts';

const a = np.array([[1, 2], [3, 4]]);
np.isin(a, [2, 3]);
// array([[false, true],
//        [true, false]])

intersect1d

Find the sorted intersection of two arrays. Optionally return the indices of the intersecting elements in the original arrays.
function intersect1d(
  ar1: ArrayLike,
  ar2: ArrayLike
): NDArray
NameTypeDefaultDescription
ar1ArrayLikeFirst input array.
ar2ArrayLikeSecond input array.
Returns: NDArray of sorted common values.
import * as np from 'numpy-ts';

np.intersect1d([1, 2, 3, 4], [3, 4, 5, 6]);
// array([3, 4])

union1d

Find the sorted union of two arrays.
function union1d(ar1: ArrayLike, ar2: ArrayLike): NDArray
NameTypeDefaultDescription
ar1ArrayLikeFirst input array.
ar2ArrayLikeSecond input array.
Returns: NDArray — Sorted 1D array of unique values present in either input.
import * as np from 'numpy-ts';

np.union1d([1, 2, 3], [2, 3, 4, 5]);
// array([1, 2, 3, 4, 5])

setdiff1d

Find the set difference: elements in ar1 that are not in ar2.
function setdiff1d(ar1: ArrayLike, ar2: ArrayLike): NDArray
NameTypeDefaultDescription
ar1ArrayLikeInput array.
ar2ArrayLikeElements to exclude.
assume_uniquebooleanfalseIf true, assume both arrays contain unique elements.
Returns: NDArray — Sorted 1D array of values in ar1 that are not in ar2.
import * as np from 'numpy-ts';

np.setdiff1d([1, 2, 3, 4, 5], [2, 4]);
// array([1, 3, 5])

setxor1d

Find the symmetric difference: elements that are in either of two arrays, but not in both.
function setxor1d(ar1: ArrayLike, ar2: ArrayLike): NDArray
NameTypeDefaultDescription
ar1ArrayLikeFirst input array.
ar2ArrayLikeSecond input array.
assume_uniquebooleanfalseIf true, assume both arrays contain unique elements.
Returns: NDArray — Sorted 1D array of values in exactly one of the two inputs.
import * as np from 'numpy-ts';

np.setxor1d([1, 2, 3], [2, 3, 4]);
// array([1, 4])

np.setxor1d([1, 1, 2], [2, 3, 3]);
// array([1, 3])

trim_zeros

Trim leading and/or trailing zeros from a 1D array.
function trim_zeros(filt: ArrayLike, trim?: 'f' | 'b' | 'fb'): NDArray
NameTypeDefaultDescription
filtArrayLike1D input array.
trimstring'fb'A string of 'f' (front), 'b' (back), or 'fb' (both). Controls which end(s) to trim.
Returns: NDArray — The trimmed array.
import * as np from 'numpy-ts';

np.trim_zeros([0, 0, 1, 2, 3, 0, 0]);
// array([1, 2, 3])

np.trim_zeros([0, 0, 1, 2, 0], 'f');
// array([1, 2, 0])  -- trim front only

np.trim_zeros([0, 0, 1, 2, 0], 'b');
// array([0, 0, 1, 2])  -- trim back only