Multidimensional Arrays
- 01:46
How to create multidimensional matrices in NumPy by passing a list of lists.
Downloads
No associated resources to download.
Glossary
NumPy Numpy arrays PythonTranscript
NumPy arrays are multidimensional, which means you can use them to represent matrices. So in Excel, a spreadsheet is simply a two dimensional matrix. One way to create a multidimensional matrix in NumPy is by passing it a list of lists. To demonstrate that I'm gonna create array four, and here I've given the array function two lists. The first list contains the objects one, two, and three. And the second list contains objects four, five, and six. Now, when I print array four, I'm going to get an error, and this is important. I've put two lists inside of my parentheses, but I didn't enclose them in square bracket. So what I've done is I've simply passed two separate lists to the array function, and the array function cannot interpret that. So what I need to do now is instead of giving the array function two lists, I need to give it one list containing two lists. So now that I've placed these square brackets on the outside, I have a list of lists.
And when I execute that code cell, you can see that NumPy interprets it correctly and gives us this nice two dimensional matrix. Every array has an attribute called Shape and Shape tells you the dimensions of the array. So if I want to know the dimensions of array four, I can just write the name of the array. So array four do shape.
This tells us that array four has two rows and three columns.