Numpy arrays

Thomas Gamsjäger
2 min readJul 6, 2019

--

Numpy arrays are the fundamental building blocks for data handling in Python as they exhibit much higher efficiency than what Python proper has to offer. The also oftentimes encountered name ‘ndarray’ has its origin in n-dimensional array. One dimension and you have a single row, two dimensions give a matrix, three dimensions a cube and so on.

The creation of arrays

The manual way:

By way of a function:

Or you instead of defining the step size you can also set the number of values with the very valuable and useful function linspace:

Creating prefilled matrices:

Characterization of arrays

Introducing: Methods and attributes can be applied to an object after a point as in: object.method. Everything gets clearer with a real example:

PyNote that arrays are characterized by the actual numbers of rows and columns even though Python and Numpy in pretty much all other contexts always start with the index 0 (instead of 1).

Other attributes to try out:

Indexing and slicing

Specific positions in the array are accessed by using square brackets [ ] and by calling up the desired indices. Slicing means addressing a subset of the elements of the array.

Reductions

Sometimes you don’t need the entire array or even parts of it. Presently, here will be a few operations. But first, consider the fact that there are every so often multiple ways to achieve an outcome.

The promised operations include min, max, argmin (index of the minimum), argmax, mean, median. There are many more out there.

In conclusion

This was a brief first foray into the realm of Numpy arrays. Many more adventures lie ahead…

Featured image: A floating raft made from an array of connected containers.

--

--