Exploring 2D Selections in Numpy Arrays

Learn to perform two-dimensional slicing and indexing in NumPy arrays.

Unlock the power of NumPy and learn how to perform precise two-dimensional selections and slicing in Python. Master array manipulation by creating and reshaping an eight-by-eight chessboard filled with unique random numbers.

Key Insights

  • Understand how NumPy enables two-dimensional slicing by specifying both row and column indices (row start:end, column start:end), unlike traditional Python lists that only allow one-dimensional indexing.
  • Create an eight-by-eight chessboard by generating a list of 64 unique random integers using Python's random.sample method, converting it into a NumPy array, and reshaping it with NumPy's reshape function.
  • Apply your understanding of NumPy slicing techniques by extracting subsections such as the middle four rows and columns or corner-based three-by-three sections from two-dimensional arrays, preparing you for real-world data manipulation tasks like working with pandas DataFrames.

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.

Now, because we're into two dimensions now, this matrix structure, you can do two-dimensional selections. We spent a lot of time looking at how to go in and slice, right? Just get the last three fruits, the first three, get a few in the middle. Now, but what if you're in a two-dimensional matrix now instead of a one-dimensional vector? How do we go into the 2D structure and grab just little pieces? So, let's do that.

We're going to, NumPy allows 2D selections, which are not possible with a list, wouldn't apply. So, we're going to get the two by two in the upper left corner. So, to do the selections, let me move this up.

So, the way you do list and array selections is in a list, its single item would be the index, array, same deal, one item. In a list, multiple items would be start, colon, stop, and you could optionally do a step if you like. In an array, same deal, if you want a selection, items, right, not one item, start, colon, end.

But in a two-dimensional, you have to start and end along the rows and then start and end along the columns. So, you have two start, colon, ends, a row start, colon, row end, comma, a call start, colon, call end. So, let's get the two by two in the upper left corner of the tic-tac-toe board, that would be this XXOO.

So, to do that, we can say two by two upper left equals tic-tac-toe array start zero and two, comma, start zero and two, right, with two being exclusive. So, you would get row zero and one and column zero and one. And since the zero is a given, if you don't, you know, if you're starting at the beginning, you could just put colon two, colon two, and that should get, whoa, why? One-dimensional but two are indexed.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Oh, it's tic-tac, right, it's tic-tac-toe, it's not the array, it's, there we go, it's the, okay. That is the 3D, the three by three thing, right, that we made here. Maybe we should call it three by three also, just to make that really clear.

So, go into the three by three and get the first two rows and the first two columns. Now, let's get the lower right column. So, that would be, you want lower right, two by two to lower right, you're going to now start at two from the end and go to the end on the rows and the columns, right.

Go from the second of the end row to the end, that would be O-O-O-X, last two rows, last two columns. You know, this, all right, chessboard challenge, challenge, make a list of 64 random unique two-digit integers in the 10 to 99 range. Now, that would be 89 numbers in that range or 90 numbers.

So, if you use that random dot sample and say you want to go from 10 to 100 and you want 64 of them, it'll give you 64. I mean, it's got enough numbers to work with, right. If they've got, if it's got 90 values available, it can get you 64 unique ones.

So, let's start with that. So, start with that, make a list of 64 random unique two-digit ints. From that, from the list, make an eight by eight array that looks like a chessboard, just letting the random numbers fill all 64 squares.

I mean, that's, you don't need to say that. So, pause, see if you can do that. We're looking for an eight by eight printed out, right.

This is a three by three printed out. Going back to numbers, there's a two by six printed out. That's a four by three.

We want an eight by eight. Pause, do the chessboard making challenge of 64 unique random two-digit ints. It's called chessboard, but it's really just like an eight by eight, right, because that's what a chessboard is.

It's an eight by eight matrix. Pause and come back when you're ready to look at the solution with me. Okay, so here we are.

We'll say chessboard equals, let's call this nums, okay. We'll say 64 nums nums equals listification of range. We want random sample range from one to, we actually want to have 10.

We want two digits everywhere, 10 to 100, and we want 64 of them. There, there's your 64 numbers, just pprinted. You can regular print it as well.

There they are, just a list of 64 unique numbers from 10 to 99. Nothing lower than 10, nothing greater than 99, even has 99, and they're all unique, no repeats. That's what random sample does.

Samples imply unique, unique values. So that's step one. Step two, from the list, make an eight by eight array.

We'll say eight by eight array. We'll just call it chessboard. chessboard array equals np.array, pass it your 64 nums, and print the chessboard array, and it looks nothing like a chessboard because we need to reshape the result as an eight by eight.

There you go. We start with this, start with this vector, and then arrayify it, turn it into an array, and then reshape it as eight by eight. And we've already, we're done with our tic-tac-toe board.

Now from the chessboard, get the middle four rows, all columns. We want to start here and get that. Okay, so we're going to say chessboard array, square bracket, we're going to start at index, the middle four rows.

That would be these, that's those middle four. That would be index two through index six. We'll say two comma six, two colon six.

There you go. Get the middle four rows, middle four columns. We still want the same four rows, but now we want to come in and get the middle four columns, like here.

So that's a 2D selection now. We have to specify the columns and the rows. We'll say we want two by six comma two by six.

We want the middle four rows and the middle four columns, and there you go. So here's the syntax, right? For 2D selections on an array, row start, colon, row end, comma, col start, comma, col, colon. Row start, colon, row end, comma, col start, colon, col end.

Numpy array, 2D numpy array item selections. Let's not do these. We've already done selections.

We've done a bunch of that kind of stuff. All right. We're not going to review slicing of lists because we did a ton of that.

We're not going to review slicing strings. We did a ton of that. Okay.

So there's your 2D array matrix selections. I'm going to move that up as well. Okay.

Back to our chessboard. Let's get back to our chessboard. Get the first row of the chessboard.

Okay. So that would be chessboard. Well, straight up just chessboard zero.

That's your first row, right? The first item. It's an array of rows. So each row being an item.

Get the first two rows and last two rows. Okay. First two rows and last two rows.

We'll say chessboard. Well, why don't you try it? Pause. See if you can get the first two rows and last two rows.

Separate selections. All right. We're back.

First two rows, zero to two. Last two rows, negative two to the end. Oh, no.

What am I doing? We're not doing that at all. Okay. We're good.

Get the first column. Okay. Now, how would you come and just get a column out of a chessboard? We want to get that first column, 99, 44, 43, like so.

Your numbers will vary, right? You have random numbers. Get the first column. It's array rows calls.

Now, if you want all columns, you put a colon. We're going to say chessboard array. We want the first column all rows.

So to get all rows, you put a colon, comma. So instead of a negative two or something, you just put a zero or nothing. You want all rows and then you want the first column.

That would be zero. That didn't work because we need a comma. There we go.

That's your first column. If you look at the columns, 99, 44, 43, 32, we've extracted the first column, kind of turned it into this vector basically because it's one dimensional. We want that colon means we want all rows.

Colon means all rows. Zero means first column. Let's get the very first number in the chessboard.

That would be the value at 00, right? In my case, that's a 99. We'll say chessboard array zero and then another zero, kind of a pair of goose eggs there. That's 99.

Now, inside a list, if you want to drill in, you go into a child list and get the item. You do the square brackets and other square brackets. We saw that with fruits, like fruit zero is apple and then fruit 00 is the A for apple as you continue to drill in to the indices, right? The index of the first item of the list and the index zero of the first letter of the string in the list.

We're doing stuff that we've done before with lists, but when you're in an array, which we are, you have a shorthand, which is not available to lists where you don't do the double square brackets. You just put a comma there. So that's row and column.

If I go to row zero and column zero and get the one item at the intersection of row zero and column zero, that's your first number, right? That's this value. Let's get the last column. Okay, so the last column, we want all rows.

So you put a colon comma negative one because we want the last column and 11, 50, 70. Does that? Yes, that's the last column. So again, colon means all rows.

Negative one means last column. Now you got a chessboard. We went into the tic-tac-toe board, got little two by twos.

The chessboard, let's get a tic-tac-toe board out of the upper left corner. So you got your eight by eight, right? And we want to go up into the upper left corner and grab the tic-tac-toe board three by three out of the eight by eight upper corner. So in other words, we want the first three rows and the first three columns.

We'll say challenge. Let's make a challenge. Turn off the recording, pause the video, and have at.

You're looking for a three by three made from the upper left corner of the eight by eight. Okay, we're back for the solution of that. We'll say chessboard array colon zero three comma colon three, right? You can write the zero if you like.

That makes it easier to read. So start at row zero, go to row three exclusive, start at column zero, go to row column three exclusive. That gives you the upper left corner three by three.

And you don't need to put the zeros if that's your start. Challenge. Now go to the other side.

Get the tic-tac-toe board three by three out of the lower right corner of the chessboard. So pause and we'll do the solution. Okay, time for the solution.

We'll say negative three to the end, negative three to the end, last three rows, last three columns. Boom, there it is. Yep, last three rows, last three columns.

And that actually completes the lesson. So Numpy, super important, very powerful. It's the foundation underpinning structural under the hood.

It's this mathy under the hood structure of these two dimensional data frames. So imagine if you're dealing with a spreadsheet, right? It's two dimensional. Well, what are we doing here? We're working in two dimensions.

So the next lesson we get into what are called panda data frames under the hood, those two dimensional data frames, those rows and columns of data, those spreadsheets living in Python, they're built off the foundation of Numpy. This 2D mathy 2D matrix library. All right, we are done with lesson six, Numpy.

And we'll see you in lesson seven when we see how this 2D Numpy matrix structure applies to real data sets. That is spreadsheets called data frames that live in your Python file.

Brian McClain

Brian is an experienced instructor, curriculum developer, and professional web developer, who in recent years has served as Director for a coding bootcamp in New York. Brian joined Noble Desktop in 2022 and is a lead instructor for HTML & CSS, JavaScript, and Python for Data Science. He also developed Noble's cutting-edge Python for AI course. Prior to that, he taught Python Data Science and Machine Learning as an Adjunct Professor of Computer Science at Westchester County College.

More articles by Brian McClain

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram