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

We’re now going to talk about the different ways that we can combine data and umpire. So let’s first talk about concatenation. A concatenation operation joins a sequence of arrays along an existing axis. And all of the arasments either have the same shape except in the concatenation dimension or B empty. So let’s look at some examples. So here we have two arrays with the same dimensions, 3, 2, 2. This means that we can essentially concatenate these arrays along any axis. If we concatenate them along axis 0, we get a resulting array with shape 6, 2, 2. If we concatenate these arrays along axis 1, we get a resulting array of shape 3, 4, 2. And if we concatenate these arrays along the axis equals 2, we get an array with the shape 3, 2, 4. As you can see, whichever axis we choose to concatenate are to input a array along. We simply add up the dimensions for that particular axis, keeping the rest of the axis dimensions the exact same as that of the input arrays. We’re now going to talk about stacking. So the stacking operation joins a sequence of arrays and creates a new axis. So all the dimensions have to be the same as essentially we are creating a new axis here. So if we have two arrays with the single dimension, we can try stacking them along the axis being 0 and the axis being 1. So in our example over here, we have two arrays with one dimension and stacking them along the axis being 0 gives us a resulting array of shape 2, 3. If we stack them with the axis being equal to 1, we get a resulting array with the shape 3, 2. So the axis parameter here defines the index of the new axis that is being added to the result. If we stack these two arrays along the axis equaling minus 1, that essentially means that we’re adding a new dimension along the last axis. So we get a resulting array with shape 3, 2. Let’s look at another example over here. So here we have two non-py arrays with the shape 3, 4, 5. Now if we added a dimension along the axis 0, we get a new array of the shape 2, 3, 4, 5. And similarly, if we add a dimension at axis 1, we get a new array with the dimension 3, 2, 4, 5, and so on. So we’re now going to talk about the repeat operation. So the repeat operation repeats elements of an array, and the number of repetitions of each element is broadcasted to fit the shape of a given axis. So the axis parameter here essentially specifies the axis along which we want to repeat values. If we don’t specify the axis, the repeat function first flattens the array and then repeats the elements. So for example, here we have an array which has four elements, which are 1, 2, and then 3, 4. So if we use the repeat function and give it the number of repetitions, it first flattens the array and then repeats the elements. Here we have specified the number of repetitions to be 2, so non-py flattens our array and then repeats each element twice. We can see that our resulting array has elements 1, 1, 2, 2, 3, 3, 4, 4, and the shape of the array is a common object. Now if we use the repeat function with the number of repetitions and the axis, it repeats that axis that many times. So here for example, we can see that the axis 0 is being repeated three times. So the dimensions of our output array is 6, 2, as 1, 2, and 3, 4 are being repeated three times each. Similarly, if we set the axis to be 1, this is the output that we get. And we get the dimensions of the output array to be 2, 6. Now if we set the axis to be 0 and set the number of repetitions to be not just a single value but to be an array, we can see that the first inner array has been repeated twice and the second inner array has been repeated three times as we specify the number of repetitions to be 2, 3. The shape of the resulting array is 5, 2.