Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Call an Element from a Numpy Array?

#1
How to Call an Element from a Numpy Array?

<div><div class="kk-star-ratings kksr-valign-top kksr-align-left " data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;430233&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;count&quot;:&quot;0&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;}">
<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: 0px;">
<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"> <span class="kksr-muted">Rate this post</span> </div>
</div>
<div id="ez-toc-container" class="ez-toc-v2_0_24_1 counter-hierarchy counter-decimal ez-toc-light-blue">
<div class="ez-toc-title-container">
<p class="ez-toc-title">Table of Contents</p>
<p><span class="ez-toc-title-toggle"><a class="ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle" style="display: none;"><i class="ez-toc-glyphicon ez-toc-icon-toggle"></i></a></span></div>
<nav>
<ul class="ez-toc-list ez-toc-list-level-1">
<li class="ez-toc-page-1 ez-toc-heading-level-2"><a class="ez-toc-link ez-toc-heading-1" href="https://blog.finxter.com/how-to-call-an-element-from-a-numpy-array/#Retrieving_Elements_from_a_1D_Array" title="Retrieving Elements from a 1D Array">Retrieving Elements from a 1D Array</a></li>
<li class="ez-toc-page-1 ez-toc-heading-level-2"><a class="ez-toc-link ez-toc-heading-2" href="https://blog.finxter.com/how-to-call-an-element-from-a-numpy-array/#Retrieving_Elements_from_a_2D_Array" title="Retrieving Elements from a 2D Array">Retrieving Elements from a 2D Array</a></li>
<li class="ez-toc-page-1 ez-toc-heading-level-2"><a class="ez-toc-link ez-toc-heading-3" href="https://blog.finxter.com/how-to-call-an-element-from-a-numpy-array/#Retrieving_Elements_from_a_Multi-Dimensional_Array" title="Retrieving Elements from a Multi-Dimensional Array">Retrieving Elements from a Multi-Dimensional Array</a></li>
<li class="ez-toc-page-1 ez-toc-heading-level-2"><a class="ez-toc-link ez-toc-heading-4" href="https://blog.finxter.com/how-to-call-an-element-from-a-numpy-array/#Accessing_Elements_Using_Negative_Indexing" title="Accessing Elements Using Negative Indexing">Accessing Elements Using Negative Indexing</a></li>
<li class="ez-toc-page-1 ez-toc-heading-level-2"><a class="ez-toc-link ez-toc-heading-5" href="https://blog.finxter.com/how-to-call-an-element-from-a-numpy-array/#Conclusion" title="Conclusion">Conclusion</a></li>
</ul>
</nav>
</div>
<p><strong>Problem: </strong>Given a <strong><a href="https://blog.finxter.com/numpy-tutorial/">Numpy array</a></strong>; how will you call an element from the given array?</p>
<p><strong>Example: </strong>When you call an element from a Numpy array, the element being referenced is retrieved from a specified index. Let’s have a look at the following scenario, which demonstrates the concept:</p>
<pre class="wp-block-preformatted"><strong>Given</strong>:
my_array = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]] <strong>Question</strong>: Retrieve the elements 3 and 8 from the given 2D array. <strong>Expected Output: </strong>[3 8] - The element 3 has been retrieved from row 0 and column 2.
- The element 8 has been retrieved from row 1 and column 2. </pre>
<p>To master the art of retrieving elements from a Numpy array, you must have a clear picture of two essential concepts – <br />(1)<strong> Indexing Numpy arrays</strong> <br />(2) <strong>Slicing Numpy Arrays</strong></p>
<p>In this tutorial, we will dive into numerous examples to conquer the above concepts and thereby learn how to call Numpy array elements in a practical way. </p>
<p><strong>#NOTE:</strong> Before we begin, it is extremely important to note that indexing in Python always begins from 0, meaning the first element will have the index 0, the second element will have the index 1 and so on.</p>
<h2><strong>Retrieving Elements from a 1D Array</strong></h2>
<p>To access an element from a 1D array, you simply have to refer it using its index within square brackets, i.e., <code>arr[i]</code> where <code data-enlighter-language="generic" class="EnlighterJSRAW">arr</code> is the given array and <code data-enlighter-language="generic" class="EnlighterJSRAW">i</code> denotes the index of the element to be accessed. </p>
<p><strong>Example:</strong></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 arr = np.array([10, 20, 30, 40, 50])
# accessing the first array element at index 0
print(arr[0])
# accessing the middle array element at index 2
print(arr[2])
# accessing the last array element at index 0
print(arr[4])
# accessing and adding first and last element
print(arr[0]+arr[4])</pre>
<p><strong>Output:</strong></p>
<pre class="wp-block-code"><code>10
30
50
60</code></pre>
<p>The above examples were a classic case of indexing 1D array elements. But what if we need to access a contiguous group of elements from the given array. This is where slicing comes into the picture. </p>
<ul>
<li>Slicing allows you to access elements starting from a given index until a specified end index. </li>
<li><strong>Syntax: </strong><code>arr[start:end:step]</code>
<ul>
<li>If start is not specified, then it is automatically considered as 0.</li>
<li>If end is not specified, then it is automatically considered as the length of the array in that dimension.</li>
<li>If step is not specified, then it is automatically considered as 1.</li>
</ul>
</li>
</ul>
<p><strong>Example</strong> <strong>1:</strong> Accessing the first three elements of a given 1D 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 arr = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
print(arr[0:3])
# or
print(arr[:3]) # OUTPUT: [10 20 30]</pre>
<p><strong>Example 2: </strong>Accessing the last three elements of a given 1D 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 arr = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
print(arr[7:])
# or
print(arr[7:]) # OUTPUT: [ 80 90 100]</pre>
<p><strong>Example 3:</strong> Accessing every other element of a given 1D 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 arr = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
print(arr[0:10:2])
# or
print(arr[::2]) # OUTPUT: [10 30 50 70 90]</pre>
<h2><strong>Retrieving Elements from a 2D Array</strong></h2>
<p>To retrieve elements from a given 2D Numpy array, you must access their row and column indices using the syntax <code>arr[i,j]</code>, where <code data-enlighter-language="generic" class="EnlighterJSRAW">arr</code> represents the given array, <code>i</code> represents the row index and <code data-enlighter-language="generic" class="EnlighterJSRAW">j</code> represents the column index.</p>
<p><strong>Examples:</strong></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 arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
# accessing the 3rd element of 1st row
print(arr[0, 2])
# accessing the 1st element of the 2nd row
print(arr[1, 0])
# accessing and adding 1st element of 1st row (1) and last element of second row (10)
print(arr[0, 0] + arr[1, 4])</pre>
<p><strong>Output:</strong></p>
<pre class="wp-block-code"><code>3
6
11</code></pre>
<p>Now let us look at how we can slice 2D arrays to access contiguous elements lying within an index range.</p>
<p><strong>Example 1: </strong>Accessing the first three elements from the first inner array.</p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="269" height="153" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-143.png" alt="" class="wp-image-430502" /></figure>
<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 arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0, 0:3])
# or
print(arr[0, :3]) # OUTPUT: [1 2 3]</pre>
<p><strong>Example 2: </strong>Accessing the last three elements of the second inner array.</p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="394" height="142" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-140.png" alt="" class="wp-image-430490" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-140.png 394w, https://blog.finxter.com/wp-content/uplo...00x108.png 300w" sizes="(max-width: 394px) 100vw, 394px" /></figure>
<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 arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[1, 2:])
# or
print(arr[1, 2:]) # OUTPUT: [ 8 9 10]</pre>
<p><strong>Example 3: </strong>Access the third element from both the inner arrays.</p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="300" height="168" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-141.png" alt="" class="wp-image-430491" /></figure>
<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 arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, 2])
# or
print(arr[:, 2])
# or
print(arr[0:, 2])
# or
print(arr[:2, 2]) # OUTPUT: [3 8]</pre>
<p><strong>Example 4: </strong>Accessing middle elements from both the arrays.</p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="296" height="159" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-142.png" alt="" class="wp-image-430495" /></figure>
<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 arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, 1:4])
# or
print(arr[:, 1:4])
# or
print(arr[0:, 1:4])
# or
print(arr[:2, 1:4]) # OUTPUT: [[2 3 4]
[7 8 9]]</pre>
<p>There’s one more way to select multiple array elements from a given 2D array. Considering that you want to retrieve elements from the i-th row and j-th column, you can pack them in a tuple to specify the indexes of each element you want to retrieve. </p>
<p><strong>Example:</strong></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 arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[(0, 1), (2, 2)]) # OUTPUT: [3 8]</pre>
<p><strong>Explanation: </strong>The first tuple contains the indices of the rows and the second tuple contains the indices of the columns.</p>
<h2><strong>Retrieving Elements from a Multi-Dimensional Array</strong></h2>
<p>To retrieve elements of multi-dimensional arrays, you can access the index of individual elements with the help of square bracket notation and comma-separated index values, one per axis.</p>
<p><strong>As a rule of thumb:</strong>&nbsp;the first element in the comma-separated square bracket notation identifies the outermost axis, the second element the second-outermost axis, and so on.</p>
<p><strong>Example: </strong>In the following code we will access the third element from the second array of the second dimension.</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 arr = np.array([[[100, 200, 300], [400, 500, 600]], [[700, 800, 900], [1, 2, 3]]])
print(arr[1, 1, 2]) # OUTPUT: 3</pre>
<p><strong>Graphical Visualization</strong>:</p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="585" height="159" src="https://blog.finxter.com/wp-content/uploads/2022/06/array-access-elements.gif" alt="" class="wp-image-430420" /></figure>
<p><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f58a.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Note: </strong>You must remember that each axis can be sliced separately. In case the slice notation is not specified for a particular axis, then the interpreter will automatically apply the default slicing (i.e., the colon&nbsp;<code>:</code>).</p>
<h2><strong>Accessing Elements Using Negative Indexing</strong></h2>
<p>You can also access elements of arrays using negative indices, starting from the end element and then moving towards the left.</p>
<p><strong>Negative Indexing with 1D Arrays</strong></p>
<p><strong>Example 1: </strong>Accessing last element of a given 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 arr = np.array([10, 20, 30, 40, 50])
print(arr[-1]) # OUTPUT: 50</pre>
<p><strong>Example 2: </strong>Accessing the last three elements of a given 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 arr = np.array([10, 20, 30, 40, 50])
print(arr[-3:]) # OUTPUT: [30 40 50]</pre>
<p><strong>Negative Indexing with 2D Arrays </strong></p>
<p><strong>Example 1: </strong>Accessing last elements of both inner arrays.</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 arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, -1]) # OUTPUT: [ 5 10]</pre>
<p><strong>Example 2: </strong>Accessing last three elements of both arrays.</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 arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, -3:])</pre>
<p> <strong>Output:</strong></p>
<pre class="wp-block-code"><code>[[ 3 4 5] [ 8 9 10]]</code></pre>
<p><strong>Example 3: </strong>Access all columns except the last one.</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 arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[:, :-1])</pre>
<p><strong>Output:</strong></p>
<pre class="wp-block-code"><code>[[1 2 3 4] [6 7 8 9]]</code></pre>
<p class="has-background" style="background-color:#f9fcb3"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f48e.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>ADVANCED READ: </strong>Learn how to conditionally select elements in a NumPy Array here:<br /><strong><a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-conditionally-select-elements-in-a-numpy-array/" target="_blank">Conditional Indexing: How to Conditionally Select Elements in a NumPy Array?</a></strong></p>
<h2>Conclusion</h2>
<p>Congratulations! You have successfully mastered the art of retrieving elements from arrays. We have seen numerous examples and demonstrations of selecting elements from 1D, 2D and other multi-dimensional arrays. I hope this tutorial helped you. Here’s a list of highly recommended tutorials that will further enhance your Numpy skills:</p>
<ul class="has-base-2-background-color has-background">
<li><strong><a rel="noreferrer noopener" href="https://blog.finxter.com/numpy-tutorial/" target="_blank">NumPy Tutorial – Everything You Need to Know to Get Started</a></strong></li>
<li><strong><a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-index-elements-numpy-arrays/" target="_blank">How to Index Elements in NumPy Arrays?</a></strong></li>
<li><strong><a href="https://blog.finxter.com/how-to-remove-specific-elements-in-a-numpy-array/" target="_blank" rel="noreferrer noopener">How to Remove Specific Elements in a Numpy Array?</a></strong></li>
</ul>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<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>
</div>


https://www.sickgaming.net/blog/2022/06/...mpy-array/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016