11-785 Spring 2023 Recitation 0B: Fundamentals of NumPy (Part 4/8)

So we’re not going to talk about transposing arrays within NumPy. The transpose operation reverses the order of an array. It switches the rows to the columns and vice versa. However, this is only true in the case of a two-dimensional array. When it comes to a multi-dimensional array, the transpose operation moves the data from one axis to the other and the order specified in the transpose method. So we’re going to go through some examples. Here we have an array of 50 elements which is further segregated into five rows and 10 columns. If we call the NumPy.transpose function on this array, it simply changes the rows to the columns and the columns to the rows. We see that the first column, which was 0, 10, 20, 30, and 40, now becomes the first row of the transposed array. The second column, which is 1, 11, 21, 31, and 41, now becomes part of the second row and so on. This same effect can be achieved using the dot t notation in NumPy as well. We now move on to three-dimensional arrays. Here we have an array of 60 elements which is comprised of four rows and five columns divided into three batches. Now simply calling the transpose function on this array reverses the order in which the array was originally present. So the rows stay the same and the columns now become the batches. We can also interpret this using axes. We see that the 0th axis represents the number of batches, which in this case is 3. The first axis represents the number of rows, which in this case is 4, and the second axis represents the number of columns, which in this case is 5. When we call the transpose function on a three-dimensional array like this one, what we see is that the batches now become the columns, the rows stay the same, and the columns now become the batches. So let’s first look at the columns. So since 0 is typecasted as 2, we see that 0, 20, and 40, which were part of the first column of our original matrix, now become part of the first row of the first batch of our transpose matrix. We also see that 5, 25, and 45, which were part of the first column, now become the second row of the first batch of our transpose matrix. Once we’ve finished processing this entire column, we see that we have finished processing the entire first batch of our transpose array. We can also interpret this using the shapes of our arrays. We see that the shape of the original array was 3, 4, 5, and after transposing, it becomes 5, 4, 3, as we have simply switched the batches and the columns, but the rows, which the 4 per, which the 4 specifies, is stays the same. We now move on to transposing along specified axes. We take our original array w and transpose it, except this time we’re specifying our axis parameter to be 0, 2, 1. This essentially means that we want the column axes to come in place of the row axis, and the column axis essentially swaps with the row. But the batch stays exactly the same. So essentially the columns represent the rows, and the rows are now seen as columns. So the first row of our original matrix, which was 0 through 4, now becomes the first column of our transpose matrix. 5 through 9, which was our second row of our original matrix, now becomes the second column of our transpose matrix, and so on and so forth for the first batch. Once we’re done processing this batch, we move on to the second batch, and all the way until we’ve completed all the batches. We can also see this using the axes. We originally had a matrix with axes 0, 1, and 2 representing the shapes 3, 4, and 5. Once we have performed this transpose operation, we see that the axes are now 0, 2, and 1, and this corresponds to a shape of 3, 5, 4.

AI video(s) you might be interested in …