What is an array?
Some libraries/packages have their on specific version of a list. Pandas as a pd.series
which we’ve seen before. Numerical computing libraries like numpy or jax have an array: np.array([1, 2])
, jnp.array([1, 2])
.
Arrays have additional functionality relative to lists which make them helpful. Let’s explore this further through a couple of examples.
Example One
With both a list and an array, we can use python’s built-in function max
to compute the maximum value of a list like object.
x1s = [1, 2, 3, 4, 5]
x2s = np.array([1, 2, 3, 4, 5])
print(max(x1s))
print(max(x2s))
However, there is no built-in argmax
function, so we have to rely on numpy’s.
x1s = [1, 2, 3, 4, 5]
x2s = np.array([1, 2, 3, 4, 5])
print(np.argmax(x1s))
print(np.argmax(x2s))
Example Two
We’ll sometimes want to compare each element in a list-like structure to a value. For example, let’s say we have a list of p-values corresponding to the coefficients from a regression. With a pure list, we cannot simply check whether each value is significant using a single comparison operator as below.
[0.00030985831158296417,2.9724745921917515e-14, 0.0,] <= 0.05
If we convert the list to an array though, we can use a single comparison operator!
np.array([0.00030985831158296417,2.9724745921917515e-14, 0.0]) <= 0.05