<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Indexing Archives - Be on the Right Side of Change</title>
	<atom:link href="https://blog.finxter.com/tag/indexing/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.finxter.com/tag/indexing/</link>
	<description></description>
	<lastBuildDate>Sat, 13 Feb 2021 19:38:04 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>

<image>
	<url>https://blog.finxter.com/wp-content/uploads/2020/08/cropped-cropped-finxter_nobackground-32x32.png</url>
	<title>Indexing Archives - Be on the Right Side of Change</title>
	<link>https://blog.finxter.com/tag/indexing/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Index Elements in NumPy Arrays?</title>
		<link>https://blog.finxter.com/how-to-index-elements-numpy-arrays/</link>
					<comments>https://blog.finxter.com/how-to-index-elements-numpy-arrays/#respond</comments>
		
		<dc:creator><![CDATA[Chris]]></dc:creator>
		<pubDate>Fri, 12 Feb 2021 08:28:00 +0000</pubDate>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[NumPy]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Indexing]]></category>
		<category><![CDATA[numpy]]></category>
		<category><![CDATA[Python 3]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=469</guid>

					<description><![CDATA[<p>NumPy is a popular Python library for data science for array, vector, and matrix computations. This puzzle introduces basic indexing of elements in NumPy arrays. Problem Formulation: How to index elements in NumPy arrays? Indexing 1D Arrays with Positive Indices The most simple use of indexing is with the square bracket notation and positive integers: ... <a title="How to Index Elements in NumPy Arrays?" class="read-more" href="https://blog.finxter.com/how-to-index-elements-numpy-arrays/" aria-label="Read more about How to Index Elements in NumPy Arrays?">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/how-to-index-elements-numpy-arrays/">How to Index Elements in NumPy Arrays?</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">NumPy is a popular Python library for data science for array, vector, and matrix computations. This puzzle introduces <strong>basic indexing of elements</strong> in <a href="https://blog.finxter.com/numpy-tutorial/" title="NumPy Tutorial – Everything You Need to Know to Get Started" target="_blank" rel="noreferrer noopener">NumPy arrays</a>.</p>



<p class="wp-block-paragraph"><strong>Problem Formulation: </strong>How to index elements in NumPy arrays?</p>



<h2 class="wp-block-heading">Indexing 1D Arrays with Positive Indices</h2>



<p class="wp-block-paragraph">The most simple use of indexing is with the square bracket notation and positive integers:</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])
>>> a
array([1, 2, 3])
>>> a[0]
1
>>> a[1]
2
>>> a[2]
3</pre>



<p class="wp-block-paragraph">If you use a positive index larger or equal than the number of elements in the array, Python will throw an <code><a href="https://blog.finxter.com/resolve-indexerror-list-assignment-index-out-of-range/" target="_blank" rel="noreferrer noopener" title="[Resolve] IndexError: List Assignment Index Out of Range">IndexError</a></code>:</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])
>>> a[3]
Traceback (most recent call last):
  File "&lt;pyshell#19>", line 1, in &lt;module>
    a[3]
IndexError: index 3 is out of bounds for axis 0 with size 3</pre>



<h2 class="wp-block-heading">Indexing 1D Arrays with Negative Indices</h2>



<p class="wp-block-paragraph">You can also use negative indices to access the array elements, starting with the last element and moving to the left:</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([8, 7, 5, 4, 9, 1, 9, 5])
>>> a[-1]
5
>>> a[-2]
9
>>> a[-3]
1
>>> a[-4]
9
>>> a[-5]
4
>>> a[-6]
5
>>> a[-7]
7
>>> a[-8]
8</pre>



<p class="wp-block-paragraph">If you move further into the negative, Python will throw an <code><a href="https://blog.finxter.com/python-indexerror-list-index-out-of-range/" target="_blank" rel="noreferrer noopener" title="Python IndexError: List Index Out of Range (How to Fix This Stupid Bug)">IndexError</a></code>:</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="">>>> a[-9]
Traceback (most recent call last):
  File "&lt;pyshell#17>", line 1, in &lt;module>
    a[-9]
IndexError: index -9 is out of bounds for axis 0 with size 8</pre>



<h2 class="wp-block-heading">Indexing 2D Arrays NumPy</h2>



<p class="wp-block-paragraph">If you use two-dimensional arrays, you can index individual elements with the square bracket notation and comma-separated index values, one per axis. The first index value gives the row index and the second index value gives the column index:</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([[42, 8, 7],
		  [99, 3, 4]])
>>> a[0, 0]
42
>>> a[1, 2]
4
>>> a[1, 1]
3</pre>



<p class="wp-block-paragraph">You can also use negative indexing on one or both axes.</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="">>>> a[-1, -1]
4</pre>



<p class="wp-block-paragraph">If you access elements outside the bound of the maximal possible index, Python raises an <code>IndexError</code>:</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="">>>> a[2, 1]
Traceback (most recent call last):
  File "&lt;pyshell#28>", line 1, in &lt;module>
    a[2, 1]
IndexError: index 2 is out of bounds for axis 0 with size 2</pre>



<h2 class="wp-block-heading">NumPy Array Indexing Multi-dimensional Arrays</h2>



<p class="wp-block-paragraph">If you use multi-dimensional arrays, you can index individual elements with the square bracket notation and comma-separated index values, one per axis.</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 = [[[1, 1], [2, 3]],
	 [[4, 3], [8, 9]]]
>>> a = np.array(a)
>>> a[0, 0, 0]
1
>>> a[0, 0, 1]
1
>>> a[0, 1, 0]
2
>>> a[0, 1, 1]
3
>>> a[1, 0, 0]
4
>>> a[1, 0, 1]
3
>>> a[1, 1, 0]
8
>>> a[1, 1, 1]
9</pre>



<p class="wp-block-paragraph"><strong>As a rule of thumb:</strong> 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>



<h2 class="wp-block-heading">NumPy Array Indexing Puzzle</h2>



<p class="wp-block-paragraph">Train your skills by solving the following NumPy puzzle about indexing and basic array arithmetic:</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

# air quality index AQI data
hong_kong = np.array([42, 40, 41, 43, 44, 43])
new_york = np.array([30, 31, 29, 29, 29, 30])
montreal = np.array([11, 11, 12, 13, 11, 12])

hk_mean = (hong_kong[0] + hong_kong[-1]) / 2.0
ny_mean = (new_york[1] + new_york[-3]) / 2.0
m_mean = (montreal[1] + montreal[-0]) / 2.0

print(hk_mean)
print(ny_mean)
print(m_mean)</pre>



<p class="wp-block-paragraph"><em>What is the output of this puzzle?</em><br>*Beginner Level* (solution below)</p>



<p class="wp-block-paragraph">You can solve this puzzle on our interactive Finxter app and track your skill level here:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><a href="https://app.finxter.com/learn/computer/science/431" target="_blank" rel="noopener"><img fetchpriority="high" decoding="async" width="1024" height="668" src="https://blog.finxter.com/wp-content/uploads/2021/02/image-53-1024x668.png" alt="" class="wp-image-23791" srcset="https://blog.finxter.com/wp-content/uploads/2021/02/image-53-1024x668.png 1024w, https://blog.finxter.com/wp-content/uploads/2021/02/image-53-300x196.png 300w, https://blog.finxter.com/wp-content/uploads/2021/02/image-53-768x501.png 768w, https://blog.finxter.com/wp-content/uploads/2021/02/image-53.png 1250w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure></div>



<p class="wp-block-paragraph">The puzzle analysis data from the real-time air quality index (AQI) for the three cities Hong Kong, New York, and Montreal. The index data aggregates various factors that influence the air quality such as respirable particulate matter, ozone, and nitrogen dioxide. The goal is to compare the air quality data for the three cities. To show how indexing works, we use different indexing schemes to access two data values for each city. Then, we normalize the data by 2.0.</p>



<p class="wp-block-paragraph">You can use positive or negative indices. For positive indices, use 0 to access the first element and increment the index by 1 to index each subsequent element. For negative indices, use -1 to access the last element and decrement the index by 1 to access each previous element. It&#8217;s as simple as that.</p>



<p class="wp-block-paragraph">Are you a master coder?<br><a href="https://app.finxter.com/learn/computer/science/431" target="_blank" rel="noopener">Test your skills now!</a></p>



<h2 class="wp-block-heading">Related Video</h2>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="5- NumPy Array Indexing 1/2" width="937" height="703" src="https://www.youtube.com/embed/yDOy5UewRZM?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph"></p>
<p>The post <a href="https://blog.finxter.com/how-to-index-elements-numpy-arrays/">How to Index Elements in NumPy Arrays?</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.finxter.com/how-to-index-elements-numpy-arrays/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Minified using Disk

Served from: blog.finxter.com @ 2026-08-26 02:20:51 by W3 Total Cache
-->