Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] np.shape()

#1
np.shape()

<div><p>This tutorial explains NumPy’s <code>shape()</code> function. </p>
<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>
<p>Return the shape of an array or <em>array_like</em> object <code>a</code>. </p>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Argument</th>
<th>Data Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>a</code></td>
<td><em>array_like</em></td>
<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’ve created a NumPy array from it.</td>
</tr>
</tbody>
</table>
</figure>
<p><strong>Return Value</strong>: <code>shape</code> — a tuple of integers that are set to the lengths of the corresponding array dimensions. </p>
<h2>Examples</h2>
<p>The straightforward example is when applied to a NumPy array:</p>
<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
>>> a = np.array([[1, 2], [3, 4]])
>>> np.shape(a)
(2, 2)</pre>
<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 – 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×2 quadratic matrix, the result is (2,2). </p>
<p>The following shape is another example of a multi-dimensional array:</p>
<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]])
>>> b
array([[1, 2, 3, 4], [5, 6, 7, 8]])
>>> b.shape
(2, 4)
>>> np.shape(b)
(2, 4)</pre>
<p>The shape is now <code>(2, 4)</code> with two rows and four columns. </p>
<h3>np.shape() vs array.shape</h3>
<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’t a NumPy array but a list, you cannot use <code>b.shape</code> as lists don’t have the shape attribute. Let’s have a look at this example:</p>
<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]]
>>> np.shape(b)
(2, 4)</pre>
<p>The <code>np.shape()</code> function returns the same shape tuple—even if you pass a nested list into the function instead of a NumPy array. </p>
<p>But if you try to access the list.shape attribute, NumPy throws the following error:</p>
<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
Traceback (most recent call last): File "&lt;pyshell#9>", line 1, in &lt;module> b.shape
AttributeError: 'list' object has no attribute 'shape'</pre>
<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>
<hr class="wp-block-separator"/>
<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>
<div class="wp-block-image">
<figure class="aligncenter size-medium"><a href="https://amzn.to/39dEykm" target="_blank" rel="noopener noreferrer"><img 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>
</div>
<h2>References</h2>
<ul>
<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>
</ul>
<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>
</div>


https://www.sickgaming.net/blog/2020/11/18/np-shape/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016