{"id":120844,"date":"2020-11-18T20:08:35","date_gmt":"2020-11-18T20:08:35","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=17015"},"modified":"2020-11-18T20:08:35","modified_gmt":"2020-11-18T20:08:35","slug":"np-shape","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/11\/18\/np-shape\/","title":{"rendered":"np.shape()"},"content":{"rendered":"<p>This tutorial explains NumPy&#8217;s <code>shape()<\/code> function. <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">numpy.shape(a)<\/pre>\n<p>Return the shape of an array or <em>array_like<\/em> object <code>a<\/code>. <\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th>Argument<\/th>\n<th>Data Type<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>a<\/code><\/td>\n<td><em>array_like<\/em><\/td>\n<td>NumPy array or Python list for which the shape should be returned. If it is a NumPy array, it returns the attribute <code>a.shape<\/code>. If it is a Python list, it returns a tuple of integer values defining the number of elements in each dimension if you would&#8217;ve created a NumPy array from it.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p><strong>Return Value<\/strong>: <code>shape<\/code> &#8212; a tuple of integers that are set to the lengths of the corresponding array dimensions. <\/p>\n<h2>Examples<\/h2>\n<p>The straightforward example is when applied to a NumPy array:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> import numpy as np\n>>> a = np.array([[1, 2], [3, 4]])\n>>> np.shape(a)\n(2, 2)<\/pre>\n<p>You import the NumPy library and create a two-dimensional array from a <a href=\"https:\/\/blog.finxter.com\/python-list-of-lists\/\" title=\"Python List of Lists \u2013 A Helpful Illustrated Guide to Nested Lists in Python\" target=\"_blank\" rel=\"noreferrer noopener\">list of lists<\/a>. If you pass the NumPy array into the shape function, it returns a tuple with two values (=dimensions). Each dimension stores the number of elements in this dimension (=axis). As it is a 2&#215;2 quadratic matrix, the result is (2,2). <\/p>\n<p>The following shape is another example of a multi-dimensional array:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> b = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n>>> b\narray([[1, 2, 3, 4], [5, 6, 7, 8]])\n>>> b.shape\n(2, 4)\n>>> np.shape(b)\n(2, 4)<\/pre>\n<p>The shape is now <code>(2, 4)<\/code> with two rows and four columns. <\/p>\n<h3>np.shape() vs array.shape<\/h3>\n<p>Note that the result of <code>np.shape(b)<\/code> and <code>b.shape<\/code> is the same if <code>b<\/code> is a NumPy array. If <code>b<\/code> isn&#8217;t a NumPy array but a list, you cannot use <code>b.shape<\/code> as lists don&#8217;t have the shape attribute. Let&#8217;s have a look at this example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> b = [[1, 2, 3, 4], [5, 6, 7, 8]]\n>>> np.shape(b)\n(2, 4)<\/pre>\n<p>The <code>np.shape()<\/code> function returns the same shape tuple&#8212;even if you pass a nested list into the function instead of a NumPy array. <\/p>\n<p>But if you try to access the list.shape attribute, NumPy throws the following error:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> b.shape\nTraceback (most recent call last): File \"&lt;pyshell#9>\", line 1, in &lt;module> b.shape\nAttributeError: 'list' object has no attribute 'shape'<\/pre>\n<p>So, the difference between <code>np.shape()<\/code> and <code>array.shape<\/code> is that the former can be used for all kinds of <em>array_like<\/em> objects while the latter can only be used for NumPy arrays with the <code>shape<\/code> attribute.<\/p>\n<hr class=\"wp-block-separator\"\/>\n<p><strong>Do you want to become a NumPy master?<\/strong> Check out our interactive puzzle book <a href=\"https:\/\/amzn.to\/39dEykm\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/amzn.to\/39dEykm\"><strong>Coffee Break NumPy<\/strong><\/a> and boost your data science skills! <em>(Amazon link opens in new tab.)<\/em><\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-medium\"><a href=\"https:\/\/amzn.to\/39dEykm\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" loading=\"lazy\" width=\"200\" height=\"300\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2019\/04\/Cover_Coffee_Break_NumPy-200x300.jpg\" alt=\"Coffee Break NumPy\" class=\"wp-image-2766\"\/><\/a><\/figure>\n<\/div>\n<h2>References<\/h2>\n<ul>\n<li><strong>Implementation<\/strong>: <a href=\"https:\/\/github.com\/numpy\/numpy\/blob\/master\/numpy\/core\/fromnumeric.py#L1926-L1969\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/numpy\/numpy\/blob\/master\/numpy\/core\/fromnumeric.py#L1926-L1969<\/a><\/li>\n<\/ul>\n<p>The post <a href=\"https:\/\/blog.finxter.com\/np-shape\/\" target=\"_blank\" rel=\"noopener noreferrer\">np.shape()<\/a> first appeared on <a href=\"https:\/\/blog.finxter.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Finxter<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial explains NumPy&#8217;s shape() function. numpy.shape(a) Return the shape of an array or array_like object a. Argument Data Type Description a array_like NumPy array or Python list for which the shape should be returned. If it is a NumPy array, it returns the attribute a.shape. If it is a Python list, it returns a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-120844","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/120844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=120844"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/120844\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=120844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=120844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=120844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}