{"id":887,"date":"2024-02-23T13:14:03","date_gmt":"2024-02-23T13:14:03","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=887"},"modified":"2024-02-23T13:14:04","modified_gmt":"2024-02-23T13:14:04","slug":"numpy-array-indexing","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/02\/23\/numpy-array-indexing\/","title":{"rendered":"NumPy\u00a0Array Indexing"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Access Array Elements<\/h2>\n\n\n\n<p>Array indexing is the same as accessing an array element.<\/p>\n\n\n\n<p>You can access an array element by referring to its index number.<\/p>\n\n\n\n<p>The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ExampleGet your own Python Server<\/h3>\n\n\n\n<p>Get the first element from the following array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0numpy\u00a0as\u00a0np<br><br>arr = np.array(&#91;1,\u00a02,\u00a03,\u00a04])<br><br>print(arr&#91;0])<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Get the second element from the following array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0numpy\u00a0as\u00a0np<br><br>arr = np.array(&#91;1,\u00a02,\u00a03,\u00a04])<br><br>print(arr&#91;1])<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Get third and fourth elements from the following array and add them.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0numpy\u00a0as\u00a0np<br><br>arr = np.array(&#91;1,\u00a02,\u00a03,\u00a04])<br><br>print(arr&#91;2] +\u00a0arr&#91;3])<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Access 2-D Arrays<\/h2>\n\n\n\n<p>To access elements from 2-D arrays we can use comma separated integers representing the dimension and the index of the element.<\/p>\n\n\n\n<p>Think of 2-D arrays like a table with rows and columns, where the dimension represents the row and the index represents the column.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Access the element on the first row, second column:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0numpy\u00a0as\u00a0np\n\narr = np.array(&#91;&#91;1,2,3,4,5], &#91;6,7,8,9,10]])\n\nprint('2nd element on 1st row: ', arr&#91;0,\u00a01])\n\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Access the element on the 2nd row, 5th column:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;&#91;1,2,3,4,5], &#91;6,7,8,9,10]])<br><br>print('5th element on 2nd row: ', arr&#91;1,&nbsp;4])<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_access2d2\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Access 3-D Arrays<\/h2>\n\n\n\n<p>To access elements from 3-D arrays we can use comma separated integers representing the dimensions and the index of the element.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Access the third element of the second array of the first array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;&#91;&#91;1,&nbsp;2,&nbsp;3], &#91;4,&nbsp;5,&nbsp;6]], &#91;&#91;7,&nbsp;8,&nbsp;9], &#91;10,&nbsp;11,&nbsp;12]]])<br><br>print(arr&#91;0,&nbsp;1,&nbsp;2])<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_access3d\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example Explained<\/h3>\n\n\n\n<p><code>arr[0, 1, 2]<\/code>&nbsp;prints the value&nbsp;<code>6<\/code>.<\/p>\n\n\n\n<p>And this is why:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The first number represents the first dimension, which contains two arrays:<br>&#91;&#91;1, 2, 3], &#91;4, 5, 6]]<br>and:<br>&#91;&#91;7, 8, 9], &#91;10, 11, 12]]<br>Since we selected&nbsp;<code>0<\/code>, we are left with the first array:<br>&#91;&#91;1, 2, 3], &#91;4, 5, 6]]<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>The second number represents the second dimension, which also contains two arrays:<br>&#91;1, 2, 3]<br>and:<br>&#91;4, 5, 6]<br>Since we selected&nbsp;<code>1<\/code>, we are left with the second array:<br>&#91;4, 5, 6]<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>The third number represents the third dimension, which contains three values:<br>4<br>5<br>6<br>Since we selected&nbsp;<code>2<\/code>, we end up with the third value:<br>6<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Negative Indexing<\/h2>\n\n\n\n<p>Use negative indexing to access an array from the end.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Print the last element from the 2nd dim:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;&#91;1,2,3,4,5], &#91;6,7,8,9,10]])<br><br>print('Last element from 2nd dim: ', arr&#91;1, -1])<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_index_negative\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Test Yourself With Exercises<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">Exercise:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Insert the correct syntax for printing the first item in the array.arr = np.array(&#91;1, 2, 3, 4, 5]) print(arr)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Access Array Elements Array indexing is the same as accessing an array element. You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc. ExampleGet your own Python Server Get the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-887","post","type-post","status-publish","format-standard","hentry","category-numpy"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/887","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=887"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/887\/revisions"}],"predecessor-version":[{"id":888,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/887\/revisions\/888"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}