[Tut] Python – Return NumPy Array From Function - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] Python – Return NumPy Array From Function (/thread-100092.html) |
[Tut] Python – Return NumPy Array From Function - xSicKxBot - 10-16-2022 Python – Return NumPy Array From Function <div> <div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{"align":"left","id":"791161","slug":"default","valign":"top","ignore":"","reference":"auto","class":"","count":"1","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}"> <div class="kksr-stars"> <div class="kksr-stars-inactive"> <div class="kksr-star" data-star="1" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="2" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="3" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="4" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="5" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> <div class="kksr-stars-active" style="width: 142.5px;"> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> </div> <div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div> </div> <p>Do you need to <strong>create a function that returns a NumPy array</strong> but you don’t know how? No worries, in sixty seconds, you’ll know! Go! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f680.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p> <p class="has-pale-cyan-blue-background-color has-background">A Python function can return any object such as a NumPy Array. To return an array, first create the array object within the function body, assign it to a variable <code>arr</code>, and return it to the caller of the function using the <a title="Return Keyword in Python – A Simple Illustrated Guide" rel="noreferrer noopener" href="https://blog.finxter.com/python-return/" target="_blank">keyword </a>operation “<code>return arr</code>“. </p> <p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended Tutorial</strong>: <a href="https://blog.finxter.com/how-to-initialize-a-numpy-array-6-easy-ways/" data-type="URL" data-id="https://blog.finxter.com/how-to-initialize-a-numpy-array-6-easy-ways/" target="_blank" rel="noreferrer noopener">How to Initialize a NumPy Array? 6 Easy Ways</a></p> <h2>Create and Return 1D Array</h2> <p>For example, the following code creates a function <code>create_array()</code> of numbers 0, 1, 2, …, 9 using the <code><a href="https://blog.finxter.com/numpy-arange/" data-type="post" data-id="548" target="_blank" rel="noreferrer noopener">np.arange()</a></code> function and returns the array to the caller of the function:</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="6" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np def create_array(): ''' Function to return array ''' return np.arange(10) numbers = create_array() print(numbers) # [0 1 2 3 4 5 6 7 8 9] </pre> <p>The <code>np.arange([start,] stop[, step])</code> function creates a new NumPy array with evenly-spaced integers between <code>start</code> (inclusive) and <code>stop</code> (exclusive). </p> <p>The <code>step</code> size defines the difference between subsequent values. For example, <code>np.arange(1, 6, 2)</code> creates the NumPy array <code>[1, 3, 5]</code>.</p> <p>To better understand the function, have a look at this video:</p> <figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/python-return-numpy-array-from-function/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVERAXaqUsjc%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure> <p>I also created this figure to demonstrate how NumPy’s <code>arange()</code> function works on three examples:</p> <figure class="wp-block-image size-full"><img loading="lazy" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-134.png" alt="" class="wp-image-791189" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-134.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/10/image-134-300x169.png 300w, https://blog.finxter.com/wp-content/uploads/2022/10/image-134-768x432.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure> <p>In the code example, we used <code>np.arange(10)</code> with default <code>start=0</code> and <code>step=1</code> only specifying the <code>stop=10</code> argument.</p> <p>If you need an even deeper understanding, I’d recommend you check out our full guide on the Finxter blog.</p> <p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended Tutorial</strong>: <a href="https://blog.finxter.com/numpy-arange/" data-type="URL" data-id="https://blog.finxter.com/numpy-arange/" target="_blank" rel="noreferrer noopener">NumPy Arange Function — A Helpful Illustrated Guide</a></p> <h2>Create and Return 2D NumPy Array</h2> <p class="has-base-background-color has-background">You can also create a 2D (or multi-dimensional) array in a Python function by first creating a 2D or (xD) <a href="https://blog.finxter.com/python-list-of-lists/" data-type="post" data-id="7890" target="_blank" rel="noreferrer noopener">nested list</a> and converting the nested list to a NumPy array by passing it into the <code><a href="https://blog.finxter.com/numpy-tutorial/" data-type="post" data-id="1356" target="_blank" rel="noreferrer noopener">np.array()</a></code> function.</p> <p>The following code snippet uses <a href="https://blog.finxter.com/how-does-nested-list-comprehension-work-in-python/" data-type="post" data-id="7596" target="_blank" rel="noreferrer noopener">nested list comprehension</a> to create a 2D NumPy array following a more complicated creation pattern:</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np def create_array(a,b): ''' Function to return array ''' lst = [[(i+j)**2 for i in range(a)] for j in range(b)] return np.array(lst) arr = create_array(4,3) print(arr) </pre> <p>Output:</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="">[[ 0 1 4 9] [ 1 4 9 16] [ 4 9 16 25]]</pre> <p>I definitely recommend reading the following tutorial to understand nested list comprehension in Python:</p> <p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended Tutorial</strong>: <a href="https://blog.finxter.com/how-does-nested-list-comprehension-work-in-python/" data-type="post" data-id="7596" target="_blank" rel="noreferrer noopener">Nested List Comprehension in Python</a></p> </p> <h2>More Ways</h2> <p>There are many other ways to return an array in Python. </p> <p>For example, you can use either of those methods inside the function body to create and initialize a NumPy array:</p> <ul> <li><strong>Method 1</strong>: Use <a rel="noreferrer noopener" href="https://numpy.org/doc/stable/reference/generated/numpy.array.html?highlight=numpy%20array#numpy.array" target="_blank"><code>np.array()</code></a></li> <li><strong>Method 2</strong>: Use <a rel="noreferrer noopener" href="https://numpy.org/doc/stable/reference/generated/numpy.zeros.html" target="_blank"><code>np.zeros()</code></a></li> <li><strong>Method 3</strong>: Use <a rel="noreferrer noopener" href="https://numpy.org/doc/stable/reference/generated/numpy.ones.html?highlight=ones#numpy.ones" target="_blank"><code>np.ones()</code></a></li> <li><strong>Method 4</strong>: Use <a rel="noreferrer noopener" href="https://numpy.org/doc/stable/reference/generated/numpy.full.html?highlight=full" target="_blank"><code>np.full()</code></a></li> <li><strong>Method 5</strong>: Use <a rel="noreferrer noopener" href="https://numpy.org/doc/stable/reference/generated/numpy.empty.html?highlight=empty#numpy.empty" target="_blank"><code>np.empty()</code></a></li> <li><strong>Method 6</strong>: Use <a rel="noreferrer noopener" href="https://numpy.org/doc/stable/reference/generated/numpy.arange.html?highlight=arange#numpy.arange" target="_blank"><code>np.arange()</code></a></li> <li><strong>Bonus</strong>: Initialize a <a rel="noreferrer noopener" href="https://blog.finxter.com/numpy-tutorial/" target="_blank">NumPy</a> array with CSV data</li> </ul> <p>To get a quick overview what to put into the function and how these methods work, I’d recommend you check out our full tutorial.</p> <p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended Tutorial</strong>: <a href="https://blog.finxter.com/how-to-initialize-a-numpy-array-6-easy-ways/" data-type="post" data-id="453551" target="_blank" rel="noreferrer noopener">How to Initialize a NumPy Array? 6 Easy Ways</a></p> <h2>Related Tutorials</h2> <ul> <li><a rel="noreferrer noopener" href="https://blog.finxter.com/python-return-string-from-function/" data-type="post" data-id="784920" target="_blank">Python Return String From Function</a></li> <li><a href="https://blog.finxter.com/python-return-dictionary-from-function/" data-type="post" data-id="34362" target="_blank" rel="noreferrer noopener">Python Return Dict From Function</a></li> <li><a href="https://blog.finxter.com/python-return-set-from-function/" data-type="post" data-id="34346" target="_blank" rel="noreferrer noopener">Python Return Set From Function</a></li> </ul> <h2>Programmer Humor</h2> <pre class="wp-block-preformatted has-global-color-8-background-color has-background"><code><strong>Q</strong>: How do you tell an introverted computer scientist from an extroverted computer scientist? <strong>A</strong>: An extroverted computer scientist looks at <strong><em>your</em></strong> shoes when he talks to you.</code></pre> </div> https://www.sickgaming.net/blog/2022/10/15/python-return-numpy-array-from-function/ |