Skip to content

Commit

Permalink
advanced indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
negin513 committed Jun 6, 2024
1 parent 19c97f4 commit f25af4c
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions intermediate/indexing/advanced-indexing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"source": [
"## Overview\n",
"\n",
"In the previous notebooks, we learned basic forms of indexing with Xarray (positional and name based dimensions, integer and label based indexing), datetime Indexing, and nearest neighbor lookups. Xarray positional indexing deviates from the NumPy when indexing with multiple arrays like `arr[[0, 1], [0, 1]]`. In this tutorial we learn about this difference and how to do vectorized/pointwise indexing using Xarray.\n",
"In the previous notebooks, we learned basic forms of indexing with Xarray (positional and name based dimensions, integer and label based indexing), datetime Indexing, and nearest neighbor lookups. Xarray positional indexing deviates from the NumPy when indexing with multiple arrays like `arr[[0, 1], [0, 1]]`.\n",
"\n",
"In this tutorial we learn about this difference and how to do vectorized/pointwise indexing using Xarray.\n",
"\n",
"For this notebook, first, we should learn about orthogonal (i.e. outer) and vectorized (i.e. pointwise) indexing concepts. \n",
"\n",
Expand All @@ -31,7 +33,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 47,
"metadata": {},
"outputs": [
{
Expand All @@ -44,7 +46,7 @@
" [21, 22, ..., 24, 25]])"
]
},
"execution_count": 42,
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -66,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 48,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -441,11 +443,11 @@
" ...,\n",
" [16, 17, ..., 19, 20],\n",
" [21, 22, ..., 24, 25]])\n",
"Dimensions without coordinates: dim_0, dim_1</pre><div class='xr-wrap' style='display:none'><div class='xr-header'><div class='xr-obj-type'>xarray.DataArray</div><div class='xr-array-name'></div><ul class='xr-dim-list'><li><span>dim_0</span>: 5</li><li><span>dim_1</span>: 5</li></ul></div><ul class='xr-sections'><li class='xr-section-item'><div class='xr-array-wrap'><input id='section-99ce812d-8ac9-4e4f-a34c-72175a80a4d1' class='xr-array-in' type='checkbox' checked><label for='section-99ce812d-8ac9-4e4f-a34c-72175a80a4d1' title='Show/hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-array-preview xr-preview'><span>1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25</span></div><div class='xr-array-data'><pre>array([[ 1, 2, ..., 4, 5],\n",
"Dimensions without coordinates: dim_0, dim_1</pre><div class='xr-wrap' style='display:none'><div class='xr-header'><div class='xr-obj-type'>xarray.DataArray</div><div class='xr-array-name'></div><ul class='xr-dim-list'><li><span>dim_0</span>: 5</li><li><span>dim_1</span>: 5</li></ul></div><ul class='xr-sections'><li class='xr-section-item'><div class='xr-array-wrap'><input id='section-d70dbedf-cbdc-49d1-bf71-061abecd150a' class='xr-array-in' type='checkbox' checked><label for='section-d70dbedf-cbdc-49d1-bf71-061abecd150a' title='Show/hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-array-preview xr-preview'><span>1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25</span></div><div class='xr-array-data'><pre>array([[ 1, 2, ..., 4, 5],\n",
" [ 6, 7, ..., 9, 10],\n",
" ...,\n",
" [16, 17, ..., 19, 20],\n",
" [21, 22, ..., 24, 25]])</pre></div></div></li><li class='xr-section-item'><input id='section-7aabddcb-df05-4363-be31-836d8138e41e' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-7aabddcb-df05-4363-be31-836d8138e41e' class='xr-section-summary' title='Expand/collapse section'>Coordinates: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'></ul></div></li><li class='xr-section-item'><input id='section-29a47bfe-a5f5-4897-b22d-649160fef139' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-29a47bfe-a5f5-4897-b22d-649160fef139' class='xr-section-summary' title='Expand/collapse section'>Indexes: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'></ul></div></li><li class='xr-section-item'><input id='section-e8d8f9fe-bc35-46c5-96dd-8ffbeadb8084' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-e8d8f9fe-bc35-46c5-96dd-8ffbeadb8084' class='xr-section-summary' title='Expand/collapse section'>Attributes: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><dl class='xr-attrs'></dl></div></li></ul></div></div>"
" [21, 22, ..., 24, 25]])</pre></div></div></li><li class='xr-section-item'><input id='section-0f5204e1-b4e9-4a04-bde3-a957f811243b' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-0f5204e1-b4e9-4a04-bde3-a957f811243b' class='xr-section-summary' title='Expand/collapse section'>Coordinates: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'></ul></div></li><li class='xr-section-item'><input id='section-6b04e499-d4ae-4cfc-b8b6-2afe2ba7f64e' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-6b04e499-d4ae-4cfc-b8b6-2afe2ba7f64e' class='xr-section-summary' title='Expand/collapse section'>Indexes: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'></ul></div></li><li class='xr-section-item'><input id='section-b69aa3d6-758a-431c-8a51-2a7faf8398fc' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-b69aa3d6-758a-431c-8a51-2a7faf8398fc' class='xr-section-summary' title='Expand/collapse section'>Attributes: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><dl class='xr-attrs'></dl></div></li></ul></div></div>"
],
"text/plain": [
"<xarray.DataArray (dim_0: 5, dim_1: 5)>\n",
Expand All @@ -457,7 +459,7 @@
"Dimensions without coordinates: dim_0, dim_1"
]
},
"execution_count": 43,
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -470,7 +472,7 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": 49,
"metadata": {},
"outputs": [
{
Expand All @@ -479,7 +481,7 @@
"array([ 1, 13, 25])"
]
},
"execution_count": 44,
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -490,7 +492,7 @@
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": 50,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -863,9 +865,9 @@
"array([[ 1, 3, 5],\n",
" [11, 13, 15],\n",
" [21, 23, 25]])\n",
"Dimensions without coordinates: dim_0, dim_1</pre><div class='xr-wrap' style='display:none'><div class='xr-header'><div class='xr-obj-type'>xarray.DataArray</div><div class='xr-array-name'></div><ul class='xr-dim-list'><li><span>dim_0</span>: 3</li><li><span>dim_1</span>: 3</li></ul></div><ul class='xr-sections'><li class='xr-section-item'><div class='xr-array-wrap'><input id='section-026ff380-84a8-43c1-93e2-46c7509a8ffb' class='xr-array-in' type='checkbox' checked><label for='section-026ff380-84a8-43c1-93e2-46c7509a8ffb' title='Show/hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-array-preview xr-preview'><span>1 3 5 11 13 15 21 23 25</span></div><div class='xr-array-data'><pre>array([[ 1, 3, 5],\n",
"Dimensions without coordinates: dim_0, dim_1</pre><div class='xr-wrap' style='display:none'><div class='xr-header'><div class='xr-obj-type'>xarray.DataArray</div><div class='xr-array-name'></div><ul class='xr-dim-list'><li><span>dim_0</span>: 3</li><li><span>dim_1</span>: 3</li></ul></div><ul class='xr-sections'><li class='xr-section-item'><div class='xr-array-wrap'><input id='section-8fd865dc-8567-451f-b112-8c1b2498ed3c' class='xr-array-in' type='checkbox' checked><label for='section-8fd865dc-8567-451f-b112-8c1b2498ed3c' title='Show/hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-array-preview xr-preview'><span>1 3 5 11 13 15 21 23 25</span></div><div class='xr-array-data'><pre>array([[ 1, 3, 5],\n",
" [11, 13, 15],\n",
" [21, 23, 25]])</pre></div></div></li><li class='xr-section-item'><input id='section-3c56aef8-49cc-4092-bdb0-8276ec37e94c' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-3c56aef8-49cc-4092-bdb0-8276ec37e94c' class='xr-section-summary' title='Expand/collapse section'>Coordinates: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'></ul></div></li><li class='xr-section-item'><input id='section-6949b10b-e6a2-4442-891a-4b4facda1db8' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-6949b10b-e6a2-4442-891a-4b4facda1db8' class='xr-section-summary' title='Expand/collapse section'>Indexes: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'></ul></div></li><li class='xr-section-item'><input id='section-30c001f5-bed8-411f-8c59-5ccf0c56c38c' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-30c001f5-bed8-411f-8c59-5ccf0c56c38c' class='xr-section-summary' title='Expand/collapse section'>Attributes: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><dl class='xr-attrs'></dl></div></li></ul></div></div>"
" [21, 23, 25]])</pre></div></div></li><li class='xr-section-item'><input id='section-e9e5a0e0-378a-40b8-adf8-e56945587b8e' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-e9e5a0e0-378a-40b8-adf8-e56945587b8e' class='xr-section-summary' title='Expand/collapse section'>Coordinates: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'></ul></div></li><li class='xr-section-item'><input id='section-82534dc8-bb98-4d11-8390-5523ba80f6a0' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-82534dc8-bb98-4d11-8390-5523ba80f6a0' class='xr-section-summary' title='Expand/collapse section'>Indexes: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'></ul></div></li><li class='xr-section-item'><input id='section-895bd060-ffcf-4216-a98a-5eb4c446cc99' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-895bd060-ffcf-4216-a98a-5eb4c446cc99' class='xr-section-summary' title='Expand/collapse section'>Attributes: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><dl class='xr-attrs'></dl></div></li></ul></div></div>"
],
"text/plain": [
"<xarray.DataArray (dim_0: 3, dim_1: 3)>\n",
Expand All @@ -875,7 +877,7 @@
"Dimensions without coordinates: dim_0, dim_1"
]
},
"execution_count": 45,
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -888,7 +890,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The image below shows the difference between orthogonal and vectorized indexing for a 2D 5x5 array. \n",
"The image below summarizes the difference between orthogonal and vectorized indexing for a 2D 5x5 array. \n",
"\n",
"![Orthogonal vs. Vectorized Indexing](../../images/orthogonal_vs_vectorized.png)"
]
Expand Down

0 comments on commit f25af4c

Please sign in to comment.