{"id":895,"date":"2024-02-23T13:18:23","date_gmt":"2024-02-23T13:18:23","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=895"},"modified":"2024-02-23T13:18:24","modified_gmt":"2024-02-23T13:18:24","slug":"numpy-array-shape","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/02\/23\/numpy-array-shape\/","title":{"rendered":"NumPy\u00a0Array Shape"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Shape of an Array<\/h2>\n\n\n\n<p>The shape of an array is the number of elements in each dimension.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Get the Shape of an Array<\/h2>\n\n\n\n<p>NumPy arrays have an attribute called&nbsp;<code>shape<\/code>&nbsp;that returns a tuple with each index having the number of corresponding elements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ExampleGet your own Python Server<\/h3>\n\n\n\n<p>Print the shape of a 2-D array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0numpy\u00a0as\u00a0np\n\narr = np.array(&#91;&#91;1,\u00a02,\u00a03,\u00a04],\u00a0&#91;5,\u00a06,\u00a07,\u00a08]])\n\nprint(arr.shape)\n\n<\/code><\/pre>\n\n\n\n<p>The example above returns&nbsp;<code>(2, 4)<\/code>, which means that the array has 2 dimensions, where the first dimension has 2 elements and the second has 4.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Create an array with 5 dimensions using&nbsp;<code>ndmin<\/code>&nbsp;using a vector with values 1,2,3,4 and verify that last dimension has value 4:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0numpy\u00a0as\u00a0np\n\narr = np.array(&#91;1,\u00a02,\u00a03,\u00a04], ndmin=5)\n\nprint(arr)\nprint('shape of array :', arr.shape)\n\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">What does the shape tuple represent?<\/h2>\n\n\n\n<p>Integers at every index tells about the number of elements the corresponding dimension has.<\/p>\n\n\n\n<p>In the example above at index-4 we have value 4, so we can say that 5th ( 4 + 1 th) dimension has 4 elements.<\/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<p>Use the correct NumPy syntax to check the shape of an array.arr = np.array([1, 2, 3, 4, 5]) print(arr.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Shape of an Array The shape of an array is the number of elements in each dimension. Get the Shape of an Array NumPy arrays have an attribute called&nbsp;shape&nbsp;that returns a tuple with each index having the number of corresponding elements. ExampleGet your own Python Server Print the shape of a 2-D array: The example [&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-895","post","type-post","status-publish","format-standard","hentry","category-numpy"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/895","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=895"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/895\/revisions"}],"predecessor-version":[{"id":896,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/895\/revisions\/896"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}