Skip to content

Vectors

1D-array operations

Create

Action Code Details
Undefined, length n
np.empty(n)
Warning: don't use the initial values
Float zeros, length n
np.zeros(n)
Int zeros, length n
np.zeros(n, dtype=np.int64)
Ones, length n
np.ones(n)
True values, length n
np.full(n, fill_value=True)
False values, length n
np.full(n, fill_value=False)
Fill with value v
np.full(n, fill_value=v)
Increasing numbers [0, b-1]
np.arrange(b)
Increasing numbers [a, b]
np.arrange(a, b)
Numbers from a to b with step size s
np.arrange(a, b, s)
Linear range from a to b of length n
np.linspace(a, b, num=n)
From tuple
np.array(tuple)
From list
np.array(list)
From iter
np.fromiter(iter)
From iter (max length n)
np.fromiter(iter, count=n)
From two vectors
np.append(v1, v2)
From two vectors
np.concatenate((v1, v2))

Test

Action Code Details
Numpy array is vector
v.ndim == 1
Vectors are equal
np.array_equal(v, v2)
All numerical elements are equal
np.ptp(v) == 0
Logical type
v.dtype == np.bool_
Float type
v.dtype == np.float_
Integer type
v.dtype == np.int_
Contains nan
np.isnan(v).any()
Contains inf
np.isinf(v).any()
Contains value
value in v
Does not contain value
value not in v
All finite
np.isfinite(v).all()
All elements are equal to value
np.all(v == value)
All elements are equal to value
(v == value).all()
All numerical elements are close to value
np.all(np.isclose(v, value))

Extract

Action Code Details
Number of elements
len(v)
Hash
hash(v.data.tobytes())
Unique values
numpy.unique(v)
Unique values
set(v)
Number of unique values
len(numpy.unique(v))
Count per unique value
np.unique(v, return_counts=True)
Count non-zero values
np.count_nonzero(v)
Count per positive integer from [0, max(v)]
np.bincount(v)
Elements must be nonnegative ints
Index of first max element
np.argmax(v)
Returns index of NaN if present!
Index of first min element
np.argmin(v)
Returns index of NaN if present!

Aggregate

See the API docs for a complete list

Update

All operations are in-place

Transform

Action Code Details
Set first element
v[0] = value
Set last element
v[-1] = value
Set value of the _i_th element
v[i] = value
Fill with constant value
v[:] = value
Fill first n values
v[:n] = value

Order

Action Code Details
Sort elements ascending
v.sort()
NaNs are put last
Sort elements descending
v[::-1].sort()
Note that this puts NaNs first!

Derive

Map

Action Code Details
Clip (truncate) between [a, b ]
np.clip(v, a_min=a, a_max=b)
Bin index
np.digitize(v, bins)
Piecewise-linear interpolation of coordinate mapping xp -> yp
np.interp(v, xp, yp)
No option for extrapolation!
Piecewise-linear interpolation with extrapolation
f = scipy.interpolate.interp1d(xp, yp, fill_value = 'extrapolate')
f(v)
Deprecated without replacement, lol
Ascending order (indices) of elements
np.argsort(v)
Descending order (indices) of elements
np.argsort(-v)

Reorder elements

Action Code Details
Reverse elements
v[::-1]
Reverse elements
np.flip(v)
Shift elements forwards, with roll-over
np.roll(v, 1)
Shift elements backwards, with roll-over
np.roll(v, -1)
Sort ascending
np.sort(v)
NaNs are last
Sort descending
v[np.argsort(-v)]
NaNs are last

Shrink

Action Code Details
Pairwise difference to next element
np.diff(v)
n-1 elements
Pairwise difference between elements with given lag
np.diff(v, n=lag)

Grow

Action Code Details
Pad with value
np.pad(v, pad_width=1, constant_values=v)
Pad with outer elements
np.pad(v, pad_width=1, mode='edge')
Replicate n times
np.repeat(v, n)
Append vector
np.append(v1, v2)
Append vectors
np.concatenate((v1, v2, v3))

Convert

Action Code Details
Bytes
v.tobytes()
Tuple
tuple(v)
List
v.tolist()
Set
set(v)