Stacking and splitting
1. Stacking and splitting
We've arrived at the last lesson! As we've seen, understanding how to reshape data is critical to getting our data in the proper format for various NumPy operations - or perhaps for preparing it for a machine learning model. A key part of reshaping is adding and removing dimensions while keeping the underlying data intact. Let's get started!2. Slicing dimensions
Recall that we can slice 3D RGB data to get red, green, and blue 2D arrays. Values in the red_array correspond to the red values from each pixel in the original array of all red, green, and blue values.3. Splitting arrays
We can also unpack arrays using np-dot-split, a function which accepts three arguments: the array to split, the number of equally-sized arrays desired after the split, and the axis to split along. Here we split the RGB array into three arrays along the third axis, at index two, in order to isolate the red, green, and blue values. Since np-dot-split allows multiple assignment, it also saves us some code. When we split an array, the resulting arrays have the same number of dimensions as the original array. Notice that our red_array is still three-dimensional.4. Trailing dimensions
If the resulting array has a dimension with a length of one as red_array did, we might remove this dimension using dot-reshape().5. Array division rules
It must be possible for NumPy to create the number of equally-sized arrays specified in the second argument. If it's not possible, NumPy will raise an error.6. Stacking arrays
Remember when we learned np-dot-concatenate, which adds data along an existing axis? We discussed that it is not possible to concatenate data in a new dimension; instead, we use np-dot-stack. As we've found throughout this course, it's easier to work with arrays with fewer dimensions, especially when we need to apply a transformation to only part of an array. Slice or split an array to unpack it, then use np-dot-stack to put it back together once changes have been made.7. Plotting 2D image data
Perhaps we've split the logo_rgb_array into three 2D arrays representing red, green, and blue pixel values. Let's plot the red_array derived from the NumPy logo's RGB data. Matplotlib happily plots 2D data that contains only red values, even though the array shape is different from a 3D RGB array containing red, green, and blue values. This is because some image data is only 2D: think of black and white images, for example. Matplotlib's default colormap is called Viridis. Think of Viridis as like grayscale, but more readable for people with colorblindness. Yellow represents lighter values and purple darker ones.8. Stacking 2D arrays
Let's replace the red_array with an array full of zeros and then reassemble the NumPy logo RGB array using np-dot-stack to see the result. np-dot-stack requires that all arrays have the same shape and number of dimensions, so before stacking we remove the trailing dimension from the green and blue arrays.9. Stacking 2D arrays
Now, stack the arrays to create a 3D RGB array by passing a list of the three arrays to be stacked to np-dot-stack. The axis keyword is set to two, the last axis, because plt-dot-imshow requires 3D RGB arrays to have a shape that corresponds to the pixel width and height of the image in the first two dimensions, with the RGB values in the third dimension. Here's what the NumPy logo looks like if all red RGB values are replaced with zeros!10. Let's practice!
Alright, now it's your turn to split and stack!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.