Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 49 ยท Shapes and Combining

Small Array Utilities You Will Meet Sooner or Later

Learn the handy NumPy helpers resize, trim_zeros, flip, fliplr, flipud, nditer, ndenumerate, extract, fromiter and fromfunction, with a 2-D Gaussian grid example.

NumPy has a long tail of small functions that each do one job well. You will not use them every day, but knowing they exist saves you from writing loops by hand. This lesson collects the most useful ones: changing size, trimming and flipping, iterating, extracting, and building arrays from iterators and functions.

Most of them build on ideas from reshaping, boolean masks and views and copies, so skim those first if any term is new.

resize: reshape that changes the size

reshape keeps the number of elements the same and fails otherwise. np.resize(a, new_shape) accepts any new size: if the new array is larger it is filled by repeating the data, and if it is smaller the data is cut. Be careful, because there are two versions with different behaviour. The function np.resize repeats; the method a.resize(...) pads with zeros and works in place.

Python resize versus reshape

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

trim_zeros and flip

np.trim_zeros(a, trim="fb") removes zeros from the front (f), the back (b) or both of a 1-D array. Zeros in the middle stay. It is handy for cleaning signals or padded results.

np.flip(a, axis) reverses the order along the given axes, and it returns a view, not a copy. np.fliplr flips the columns left to right, np.flipud flips the rows upside down, and np.rot90 rotates in steps of 90 degrees.

Python Trimming and flipping

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Iterating over arrays

A plain for loop over a 2-D array gives you its rows. When you need every element, or the index of every element, two helpers do it:

  • np.ndenumerate(a) yields (index_tuple, value) pairs.
  • np.nditer(a) is a flexible iterator that visits each element in memory order. It can also write back with op_flags=["readwrite"], iterate several arrays together and broadcast them.

For speed you should still prefer vectorized code; these tools are for the moments when a loop is the clearest thing.

Python ndenumerate and nditer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

np.extract(condition, a) returns the elements where the condition is true, as a 1-D array. It is the same as a[condition] for boolean masks, but the function form reads nicely when you build the condition on the fly.

Python extract, select and compress

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

fromiter, fromfunction and fromrecords

np.fromiter(iterable, dtype, count=-1) builds a 1-D array straight from any iterator or generator without first building a Python list, which saves memory for big sequences. Give count when you know the length to avoid reallocations.

np.fromfunction(f, shape) calls f once with whole index arrays (not once per element) and returns the result, which is why f must be written in vectorized style.

For tables there is np.rec.fromrecords(list_of_tuples, names=...), which creates a record array whose columns are reachable as attributes.

Python Building arrays from iterators and functions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A 2-D Gaussian array

A classic use of fromfunction-style thinking is a Gaussian kernel: a small grid whose values form a bell-shaped bump, used to blur images. The recipe is to make coordinate grids centred on zero, apply the Gaussian formula exp(-(x^2 + y^2) / (2 * sigma^2)), then divide by the sum so the weights add to 1.

Python A Gaussian kernel and a blur

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The nested loop covers only 9 kernel cells, not every pixel, so it is fast. Doing a real convolution with large kernels is the job of scipy.ndimage or FFTs, which you can meet in Signals and FFT; the image tutorial Image Filters uses the same idea.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does np.resize(np.array([1, 2, 3]), 5) return?

    The function form repeats the data to fill the larger shape. The method a.resize pads with zeros instead.
  2. 2. What does np.trim_zeros(np.array([0, 2, 0, 3, 0])) return?

    By default zeros are removed from both ends only. The zero between 2 and 3 stays.
  3. 3. np.flip returns what kind of object?

    Flipping only changes how the strides are read, so no data is copied. Writing into the result changes the original.
  4. 4. Why must the function passed to np.fromfunction be written in vectorized style?

    fromfunction builds index grids and calls your function one time with them, so the body should use array operations.

Summary

  • np.resize repeats data to fill a new size; a.resize pads with zeros in place; reshape needs equal sizes.
  • np.trim_zeros strips zeros from the ends; np.flip, fliplr, flipud and rot90 return views.
  • np.ndenumerate yields indices and values; np.nditer walks elements and can write back.
  • np.extract, np.compress and np.select pick elements by condition.
  • np.fromiter and np.fromfunction build arrays from iterators and index grids; a Gaussian kernel is exp(-(x^2 + y^2) / (2 sigma^2)) normalised to sum 1.

Next: Vectors, Dot Products and the @ Operator.