Discover how NumPy arrays elevate Python lists by enabling advanced array transformations like reshaping and transposing. Learn to seamlessly convert one-dimensional data into multidimensional structures for efficient data analysis.
Key Insights
- Explore NumPy arrays' unique capabilities beyond Python lists, such as properties like
shape
andndim
, which describe an array's dimensions, and methods likereshape()
to flexibly transform one-dimensional vectors into two-dimensional matrices. - Understand the distinction between reshaping and transposing: reshaping reorganizes the same sequence of numbers into different row-column configurations without altering the data order, while transposing exchanges rows and columns, changing the order of elements significantly.
- Practice reshaping arrays by creating specialized structures, such as a 3x3 NumPy array representing a tic-tac-toe game board, highlighting NumPy's practical application for visualizing and structuring data clearly.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
This is a lesson preview only. For the full lesson, purchase the course here.
So there's this method called array that you call on NumPy, and you pass it a list and it returns an array which looks and feels at first glance very much like the list, and it is. It's got all the numbers of the list in that order, but it's got all kinds of capabilities that a regular list does not have. So let's try this.
We'll say nums array. We're going to make a nums array from the nums list. That'll be np.array, and we're going to pass it nums, and we'll print the array.
Let's print both of them. We'll print regular nums, and then we'll print the array and see if there's any difference. Well, there's the list and there's the NumPy array.
This is the regular list and the NumPy array. So what's the difference? Have a look. Okay, sure.
The list has commas. Seems like a superficial difference, but yeah, absolutely, there's commas. Now, let's print the array's shape property.
So arrays have properties that regular lists don't have. Among them are ndim, the number of dimensions of the array, and shape, which returns the shape of the array as a tuple. Now, that's the fourth of the four kinds of collections that we talked about, list, set, dictionary, and tuple.
So a tuple goes in parentheses and has items which are immutable, and they're used to show you things like the shape of an array. So let's print nums array.shape, nums array.ndim, and see what we get. And we should get this 12 comma nothing and a one.
Now, the 12 comma nothing indicates when you have, it indicates the number of rows, the number of items, comma means that there's no second dimension so that you're dealing with a 1D vector. And indeed, that is a 1D vector like a regular list, just like a number line. Now, if you have two values, so it's some number comma some other number, then that would not be a vector.
That would be what's called a matrix, which is the underlying structure of a spreadsheet or a data frame, in other words, rows and columns of data. And in fact, in that case, the first value refers to the number of rows, and the second value refers to the number of columns. Once you two-dimensionalize an array from vector to 2D, that 2D structure's sideways values are the rows and the vertical values are the columns.
Now, what if you try to print the shape of a list? Try that. We have a nums list, we're just calling it nums. Let's try printing the ndim and shape.
We've never tried that or talked about that, but does nums have an ndim, number of dimensions and a shape? The answer is no, you get an error. List object has no attribute ndim. So already, aside from not having commas, the arrays have a lot of things that lists don't have.
Now, one thing you can do to an array that you definitely cannot do to a list is reshape it. If you had 12 numbers and you wanted to cluster them into three rows of four columns or four rows of three columns, like we did here, I mean, here we had to hand-do that. I mean, I had it pre-written for you, but that's manually done.
There's no command to take the 12 numbers and cluster them like you see here, but there is when you have arrays. So what we're going to do is we've got our 12-pack array vector, we're going to turn into a matrix. We'll say 3 × 4 equals numsarray.reshape 3 comma 4. You pass into the reshape method essentially two arguments that, because they're in parentheses, basically resemble the tuple that you're going to get back.
And if we print that, here it is. Gives you a little thumbnail representation as well, three rows of four columns. And we can print the shape and the dimensions.
Let me do it in this order. We'll say 3 × 4, shape, and ndim. All right, now let's reshape.
Let's do the, whoops, we'll call this, we'll call that 6c. We'll make this 6b. Let's reshape it as four rows, three columns.
Again, we did that with a list, but we manually sat there and this had to be hard-coded, these little clusters. We have, right now we have this, three rows and four columns, but we've done it with the reshape method. Now we'd like four rows and three columns.
We're going to do reshape, but this time switch the arguments, because it's row comma column. We want four rows and three columns. Maybe change it.
And notice that when you reshape, it has to multiply out to the same number. We've got 12 numbers, no matter how you're clustering them into different rows and columns. There, so now you've got four rows, three columns.
Still two-dimensional, but you've got a new tuple, a new row-column config, and it thumbnails it out differently, of course, as well, which is really nice how it gives you the little thumbnail representation. Now to reshape it as a flat vector to kind of get it back how we started, we could say nums are flat equals, we'll take the 4 × 3 and reshape it. And we're going to reshape it as 12 comma nothing, which do here, yeah, all right, here we go.
There. No, we want the new one, we want the flat thing we just made. There, so 12 comma nothing, and it's flat again like it was originally.
Now try this. Do another one. Now what if you don't, instead of 12 comma nothing, you do 12 comma 1? You say 12 comma 1. It's not the same thing.
This is going to be 2D now. Check it out. I'm going to take our flat thing, or it doesn't matter which one we use as the source material.
We're going to turn it into a 12 × 1. Look at the difference. The vector runs like a list, like a number line. 12 × 1 gives you 12 rows of one column each.
And conversely, convert the array into 12 rows of one column each. Reshape the array into one row of 12 columns each. So that'll be 1 times 12.
There you go. And look, there's one row. Notice it's double wrapped in square brackets.
So it's not quite a vector, right? If you look at a vector, it's only one set of square brackets. In fact, by looking at a printed NumPy array, you can determine how many dimensions it has by counting how many square brackets there are before you start seeing actual numbers. So in the case of a vector, the numbers are only nested in there one square bracket deep.
But in the case of a matrix, two-dimensional, you got two square brackets before you see a number. Because it's one row of 12 columns. OK, challenge.
Make an array called 6 × 2, six rows and two columns. Pause the recording and turn it back on when you're ready for the solution. All right, so to make our 6 × 2, we can actually do it off of any of the arrays.
We can go back to the original nums array if we like. And we're going to say 6 × 2. And we need to have six rows of two columns. And there you go.
And to do 2 × 6, of course, you just switch them. Swap the 6 and the 2. Six rows, two columns—I'm sorry, two rows, six columns. There you go. Look at the difference.
I mean, it's the same 12 numbers in the same order. It's just that they're running in the different little spreadsheet grid, little tabular display of rows and columns. That's what NumPy is all about, providing this 2D structure that's at the foundation of these visual representations of data called data frames that look basically like spreadsheets as Python variables sitting in your code, in your notebook.
OK, now there's another method called transpose, which inverts the shape. It switches it around as if you are inverting the rows and columns, but the rows become column values and vice versa. If you look at the 6 × 2 versus 2 × 6, sure, the shapes are different, right? Six rows of two columns versus six columns, two rows of six columns.
Six rows, two columns, two rows, six columns. But if you just read like a book, the numbers are in the same order. You just scan left to right and then go to the next line.
But when you transpose, the values flip. The rows become columns and the columns become rows, so it's quite different. We're going to transpose the 6 × 2 into a new array.
We're going to tell the rows to become columns. So there's six rows, so then you'll get six columns. So the shape will look like that, but with a difference.
You'll see the numbers are not going to be in the same order anymore, because the rows will become columns and the columns will become numbers. It's not just reshaping, it's transposing, moving the numbers around. Another thing you absolutely cannot do with a list.
We'll say array transposed equals, we'll call this, we'll do it on the 6 × 2 dot transpose. Doesn't need an argument, it just knows what to do. 6 × 2 is transposed.
There you go. So it looks at first glance perhaps like reshaping the 6 × 2 into the 2 × 6. You've got the two rows and the six columns, but take a closer look. The numbers have been changed.
The order of the numbers is not the same. So in the case of the original 6 × 2, you have a row of 95,53. Now when you transpose, the rows become columns.
Now in the transposed version, we'll have a column of 95,53. And indeed you do. So that's transposing, big diff, big diff.
All right, moving on. Make a list of nine X's and O's, OK? We'll call it tic-tac-toe. Oh, for the rest of you, this is for a challenge.
Make a list of X's and O's. OK, got you a little head start. Now make a 3 × 3 out of it that looks like a tic-tac-toe board when you print it so that you can see that X is actually winning.
It's kind of hard when it's flat to tell if anyone won the game, right? You got to see it 3 × 3. So see it 3 × 3 to see that X won. We'll say tic-tac-toe equals itself, if you like, dot reshape.
And we want a 3 × 3. Then you print that. List object has no, right, right, right. No, that's just a list.
We have to say np.array. There you go. OK, right. If you're going to reshape it into 3 × 3, it cannot just be a list.
So you make an array. Make a list of nine X's and O's. Make a 3 × 3 array that looks like a tic-tac-toe board.
And you can actually do it all in one move. You could make your array and reshape it on the fly there, right? So let's look at this in little stages. So that's all one line.
This is all a one-liner. Let's do this in three lines. We'll say first tic-tac-toe list is just going to be the list, step one.
And then tic-tac-toe array is going to be the NumPy array from the list. And then tic-tac-toe 3 × 3 is going to be the tic-tac-toe array reshaped 3 × 3. And then you're going to finally print. So this shows it as steps.
And you could print each step just so we can really see it build out. Go ahead and do that. It's a little bit of extra work, but it's worth it.
I'd like you to pause and type that there. We start with a list, and then we make an array out of it. Then we reshape the array into 3 × 3. Or you just do it all in one line.
Pass the list to the array method and call reshape on the result, chaining it on, chaining on the reshape at the end. All right, now change. How can we do this? Can we make O the winner? I mean, O wouldn't get to move five times, but that's beside the point.
How could O be the winner? We could go to the last item there and make that an O. Or we could go to the middle one and make it an O. There are two ways we could make O the winner. Make O the winner by changing X to O. It says challenge, but the answer is right here. We'll say tic-tac-toe 1,1, item 1, at index 1, at index 1. And then if you print tic-tac-toe, O has won.
But somehow they cheated or they got to go first, right? O went five times, but that's beside the point.