<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>chintak.sh</title><description>Learning notes on deep learning, computer vision, ML systems design and software engineering.</description><link>https://chintak.sh/</link><language>en</language><atom:link href="https://chintak.sh/rss.xml" rel="self" type="application/rss+xml"/><lastBuildDate>Fri, 16 Oct 2026 21:20:49 GMT</lastBuildDate><item><title>NumPy: The tricks of the trade (Part II)</title><link>https://chintak.sh/posts/2013-07-31-numpy-the-tricks-of-the-trade-part-ii/</link><guid isPermaLink="true">https://chintak.sh/posts/2013-07-31-numpy-the-tricks-of-the-trade-part-ii/</guid><description>Vectorization, broadcasting, np.ogrid, as_strided and np.einsum, ending with a loop-free template matching example.</description><pubDate>Wed, 31 Jul 2013 16:00:00 GMT</pubDate><content:encoded>&lt;p&gt;This post will describe about the more advanced and fun stuff about &lt;a href=&quot;http://www.numpy.org/&quot; class=&quot;link-external&quot; rel=&quot;noopener&quot;&gt;NumPy&lt;/a&gt;. For basics, refer to &lt;a href=&quot;https://chintak.sh/posts/2013-07-15-numpy-the-tricks-of-trade-part-i/&quot; class=&quot;link-internal&quot;&gt;Part I&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;vectorization&quot;&gt;Vectorization&lt;/h2&gt;
&lt;p&gt;First let’s revisit how we would do any arithmetic operation on all the elements of list (&lt;a href=&quot;http://www.python.org/&quot; class=&quot;link-external&quot; rel=&quot;noopener&quot;&gt;Python&lt;/a&gt; in-built container). Looping through all the elements is probably the only way to go about it. The neatest syntax is using &lt;a href=&quot;http://en.wikipedia.org/wiki/List_comprehension&quot; class=&quot;link-external&quot; rel=&quot;noopener&quot;&gt;list comprehensions&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# List Comprehension&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; L &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;i &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#9D0006;--1:#FB5D4A&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;--0:#9D0006;--1:#FB5D4A&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; L&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# list comprehension&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;Now imagine doing this for a multi-dimensional list of data and think about the readability. Not so cool, right? This is what vectorization takes care of for NumPy arrays.&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Example of Vectorized operation&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;Simply put, vectorization takes one elemental operation and applies to all the elements in that array for you. Underneath, the implementatiosn are in C, hence providing substantial speed gains. NumPy already has a large number of operations vectorized, for eg: all arithmetic operators, logical operators, etc.
Numpy also provides a way for you to vectorize your function. All you need to do is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Write a function to do the operation you want to do, taking the elements of the array as arguments.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Vectorize the function.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Provide the arrays as inputs to this vectorized function.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Done&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Creating your own Vectorized function&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Note that the following function would already be vectorized since `+` and `**` operations are vectorized&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span style=&quot;--0:#9D0006;--1:#FB5D4A&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt; add_square&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;):&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;     &lt;/span&gt;&lt;span style=&quot;--0:#9D0006;--1:#FB5D4A&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;a &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;...&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a_array &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; b_array &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; vadd_square &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;vectorize&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;add_square&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;vadd_square&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;a_array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; b_array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;25&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;49&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;81&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;This would help skip a lot of loops in your implementation. Towards the end of this post I’ll provide an example which would help connect all the ideas listed here and enable you to perform really powerful operations rather trivially using NumPy. I’ve completely become a fan of NumPy!&lt;/p&gt;
&lt;h2 id=&quot;broadcasting&quot;&gt;Broadcasting&lt;/h2&gt;
&lt;p&gt;If I ask you the answer to “banana * orange = ?”, you’ll most certainly look at me as if I’m crazy. But as it turns out, NumPy is also capable of handling operations between arrays of different sizes. The only criteria being that, NumPy should be able to extend all the arrays involved in an operation to a common shape. This is what we call Broadcasting. Let me give couple of examples to further elaborate on this idea.&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Broadcasting - Basic example&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; b &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[:,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;:]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;Understanding &lt;code&gt;np.newaxis&lt;/code&gt; would really be very helpful here. It basically just adds another dimension (axis). (duh!) But you can choose where you want to place the new axis as in x, y or z direction.&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Broadcasting using `np.newaxis`&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# For illustration purpose, lets suppose that we want to compute the product of all the indices in a 3D grid&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; b &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; c &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[:,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;:,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;:]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;strides&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[:,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;].&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;strides&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Comments&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Here, we are extending &lt;code&gt;a&lt;/code&gt; and making it a 3D array using &lt;code&gt;np.newaxis&lt;/code&gt; however, as you can see the strides, no additional memory is allocated. This makes things a lot easier, instead of creating 3, 3D arrays and then multiplying.
Practically, you can use this for indexing purposes.&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Using Broadcasting for indexing purpose&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; m &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;arange&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;24&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;reshape&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; m&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;13&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;17&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;18&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;19&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;21&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;22&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;23&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; m&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[:,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;:,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;18&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;22&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;h3 id=&quot;npogrid&quot;&gt;&lt;code&gt;np.ogrid&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;You can also this function to create a row array and a column array, and use broadcasting to generate a complete array as follows:&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Using `np.ogrid` - Basics&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; row&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; col &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;ogrid&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# note it&apos;s not a function&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; row&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; col&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]]),&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;arange&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;reshape&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;13&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;row&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;col&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;row&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;col&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;13&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;#  OR&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Comments&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Here, we generate a row and column array using &lt;code&gt;np.ogrid&lt;/code&gt; (note that its not a function). The option with slicing does look like a neater and cleaner approach, however if you want to do this in a loop with a lot of variables flying around, it may not be the best approach.&lt;/p&gt;
&lt;h2 id=&quot;numpylibstride_tricksas_strided&quot;&gt;&lt;code&gt;numpy.lib.stride_tricks.as_strided&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;This is one of the wonders of NumPy which has the power to make loops outdated. Strides, is a method in which NumPy can keep a track or this is how it knows how to get to the nest element in the row or column. How many leaps in the memory to the next element ? This of course also depends on the data type you are using. For example:&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Concept of strides with data types&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;uint8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;strides&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;uint16&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;strides&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Comments&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;So, in the first case, take move 1 byte to get to the next column element and 3 bytes to get to the next row element. Similarly for the second case.&lt;/p&gt;
&lt;p&gt;Now, let’s see how &lt;code&gt;as_strided&lt;/code&gt; works and how can we use this to perform many operations efficiently. Crudely, this function provides a way to access the same underlying array in different shapes. That being said there is also an option to define different strides for this particular view on the array. As we all know that by default, we access the elements in a row: C contiguous (or column: fortran contiguous), one after the other. But using this function it is possible to skip elements in the middle and point to say, all the diagonal elements only, hence enabling you to extract the diagonal entries of even a multi-dimensional tensor using just this function and not allocating any additional memory for the same. Some examples:&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Understanding `as_strided`&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;arange&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;uint8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;reshape&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;uint8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;itemsize  &lt;/span&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Number of bytes taken by each element&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;strides&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#9D0006;--1:#FB5D4A&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; numpy&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;lib&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;stride_tricks &lt;/span&gt;&lt;span style=&quot;--0:#9D0006;--1:#FB5D4A&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; as_strided&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a_view &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;as_strided&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;strides&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;itemsize&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;))&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a_view&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;uint8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a_view&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;strides&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Comments&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Any changes in this strided view will also get reflected in the original array.&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# `as_strided` provides a view; not a copy of the array&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a_view&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;uint8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;p&gt;Caution: This function does not check whether you stay inside the memory block bounds. This could lead to some garbage values popping up.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# as_strided does not check for memory bounds - you have to!&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a_view &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;as_strided&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;strides&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;itemsize&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;))&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a_view&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;uint8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;In fact, it is this command over memory layout which helps NumPy perform wonders like Broadcasting. This is how broadcasting is really implemented underneath, using 0 strides.&lt;/em&gt;&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Broadcasting and as_strided - Same underneath&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;arange&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;uint8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;uint8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;strides&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a_view &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;as_strided&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;strides&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;))&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a_view&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;uint8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; b_view &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;as_strided&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;strides&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;))&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; b_view&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;uint8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; b_view&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; b_view&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;uint8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Comments&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As you can see, all the “1” were really just a single “1”. Hence, when you change any of the element, the change is reflected everywhere since in reality they are all the same.&lt;/p&gt;
&lt;h2 id=&quot;npeinsum&quot;&gt;&lt;code&gt;np.einsum&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;This stands for Einstein’s summation. Using this function you can implement a lot of in-built functions involving summation. The syntax is as follows:&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Basics of `np.einsum`&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;arange&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;reshape&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;13&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;einsum&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;ii&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;30&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;einsum&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;ij&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;13&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;einsum&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;ji&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;13&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;The idea is to represent each dimension (axis) by a label, ‘i’ or ‘j’ here. It is similar to iterating over a loop. In the first case, it picks up all the elements where the indices in both the dimensions are equal and sums it over, summation of the diagonal elements, trace of the array. The order in which the label is alphabetical and important. In the second example, since ‘j’ appears after ‘i’, it first loops through elements along the column. The third example, hence produces the transpose. Now, let’s see an example involving some what more complex applications of &lt;code&gt;np.einsum&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Advanced examples - check below for explanation&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;arange&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;24&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;reshape&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;13&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;17&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;18&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;19&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;21&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;22&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;23&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; b &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;arange&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; b&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;einsum&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;ij, i-&amp;gt;j&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Column sum&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;220&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;235&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;250&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;265&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;einsum&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;ij, i-&amp;gt;i&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Row sum&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;22&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;76&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;162&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;280&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;430&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Comments&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We use &lt;code&gt;-&amp;gt;&lt;/code&gt; to indicate the order of the output array. So think of &lt;code&gt;&apos;ij, i-&amp;gt;j&apos;&lt;/code&gt; as having left hand side (LHS) and right hand side (RHS). Any repetition of labels on the LHS computes the product element wise and then sums over. By changing the label on the RHS (output) side, we can define the axis in which we want to proceed with respect to the input array, i.e. summation along axis 0, 1 and so on. The above two examples can also be computed rather trivially, as follows:&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Alternate to `np.einsum`: Product and reduce&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[:,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;18&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;22&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;36&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;39&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;42&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;45&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;64&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;68&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;72&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;76&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;105&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;110&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;115&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[:,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]).&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;sum&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;22&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;76&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;162&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;280&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;430&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[:,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;newaxis&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]).&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;sum&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;220&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;235&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;250&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;265&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;h2 id=&quot;complete-example&quot;&gt;Complete Example&lt;/h2&gt;
&lt;p&gt;Finally, before concluding this post, I would like to give an example, real code which I am using in my project, to help connect all these ideas and put them in perspective.&lt;/p&gt;
&lt;p&gt;Here is the task: &lt;strong&gt;Template Matching&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Extract a small windowXwindow template/patch about a pixel, i.e. this pixel should also be the centre of patch. (call it template patch)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Loop: Compute a patch about all pixels in the image (call it, sample patch)&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Find the Sum of Squared Differences (SSD) of the template and the sample patch&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Find the patch with the minimum SSD&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The most obvious way to go about this would be using a nested loop. The code for the same is as follows:&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Template Matching using loops&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;total_weight &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; valid_mask&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;sum&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;()&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#9D0006;--1:#FB5D4A&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;--0:#9D0006;--1:#FB5D4A&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;xrange&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;h&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;):&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#9D0006;--1:#FB5D4A&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; j &lt;/span&gt;&lt;span style=&quot;--0:#9D0006;--1:#FB5D4A&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;xrange&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;w&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;):&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;sample &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; image&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;i &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;window &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;):&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;window &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;j &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;window &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;):&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; j &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;window &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;dist &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;template &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; sample&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;ssd&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; j&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; dist&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;sum&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; total_weight  &lt;/span&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# `total_weight` is just for normalization&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;Now this piece of code, doesn’t seem all that scary and probably you’d do this every now and then in C/C++. However, Python loops are pretty darn slow. My complete code with this ran in about 19.37 seconds. The majority of the time being spent here.&lt;/p&gt;
&lt;p&gt;However, there is a better way to write this. For starters, lets get rid of the &lt;em&gt;huge slicing syntax&lt;/em&gt; with… &lt;code&gt;np.ogrid&lt;/code&gt;, of course.&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# np.ogrid for better indexing&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;t_row&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; t_col &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;ogrid&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;window &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;):(&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;window &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;window &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;):(&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;window &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;You would argue about how would using this be any different, well it’s not! It’s just cleaner and more convenient to write this once and henceforth simply use the following to extract a template of size &lt;code&gt;window X window&lt;/code&gt; about a pixel.&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;input_image&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;i &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; t_row&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; j &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; t_col&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;Much like shifting of origin in Geometry!
Let’s rethink, why are we really using the nested loops here? To extract template of the given size pixel by pixel and perform the operations. Is it possible to create such templates beforehand and perform the arithmetic operations directly on all the templates, leveraging the vector property of operators? However, creating separate templates for every pixel would most certainly be expensive on the memory. How can we do this without any additional memory overhead? &lt;code&gt;as_strided&lt;/code&gt;!&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# as_strided to provide a `window` sized window to the `input_image`&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;y &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;as_strided&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;input_image&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;               &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;input_image&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; window_size&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;                      &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;input_image&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; window_size&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,)&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;               &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;window_size&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;               &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;strides&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;input_image&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;strides &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;Suppose here, that input_image has the dimensions &lt;code&gt;(M, N)&lt;/code&gt;, the resulting strided view, y would be a 4D array with &lt;code&gt;(M, N, window, window)&lt;/code&gt; dimensions, as specified by the shape key argument. Note that &lt;code&gt;input_image.strides * 2&lt;/code&gt; represents a multiplication on a tuple which replicates and concatenates the tuple.&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# as_strided work here&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; input_image &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;arange&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;reshape&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; input_image&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;13&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; y &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;as_strided&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;input_image&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;                &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;input_image&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; window_size&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;                       input_image&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; window_size&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,)&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;                window_size&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;                &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;strides&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;input_image&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;strides &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; y&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[[[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;         &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;         &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;         &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;         &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]]],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[[[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;         &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;         &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;13&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;         &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;         &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;13&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]]]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;13&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;shape&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;strides&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;32&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;32&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# 8 is the min step since this data type is &apos;int64&apos; which occupies 8 bytes&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;itemsize&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;dtype&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;int64&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;So, as we can see here, if we iterate through the 1st 2 dimensions of &lt;code&gt;y&lt;/code&gt; we will have a &lt;code&gt;3X3&lt;/code&gt; array with elements surrounding the inner elements of input_image. I find this to be extremely fascinating and really just brilliant!&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Remember: as_strided is just a view, so Without adding any additional memory overhead we are able to accomplish this.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;Warning: In spite of being just a view, directly performing arithmetic operations on this would allocate additional memory, a 4D that too! This would neutralise the unique advantage this stands to offer. Here, is where things get even more exciting: np.einsum.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Using `np.einsum` to calculate the SSD without additional mem allocation&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;ssd &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;einsum&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;ijkl, kl, kl-&amp;gt;ij&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; template&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; valid_mask&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;                &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;ssd &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;*=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;ssd &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;einsum&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;ijkl, ijkl, kl-&amp;gt;ij&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; valid_mask&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;ssd &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;einsum&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;ij, ij, ij&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; template&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; template&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; valid_mask&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;Instead of directly computing the square of the difference (proving to be rather costly), use the basic algebraic expansion: &lt;code&gt;(a - b)**2 = a**2 - 2*a*b + b**2&lt;/code&gt;. Here, observe that, in the first computation, which calculates &lt;code&gt;sample * template&lt;/code&gt; the output is being stored in a 2D array. Following which, the subsequent results are simply accumulated in this very array. So in the end, still the overall memory load is just of allocating this 2D array for storing the output, which is what we sought for.&lt;/p&gt;
&lt;p&gt;The complete function which serves as a replacement for the nested loop is as follows:&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Complete Template Matching function - no loops&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#9D0006;--1:#FB5D4A&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt; _sum_sq_diff&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;input_image&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;valid_mask&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;):&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;This function performs template matching. The metric used is Sum of&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;Squared Difference (SSD). The input taken is the template who&apos;s match is&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;to be found in image.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;Parameters&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;---------&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;input_image : array, np.float&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;Input image of shape (M, N)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;template : array, np.float&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;(window, window) Template who&apos;s match is to be found in input_image.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;valid_mask : array, np.float&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;(window, window), governs differences which are to be considered for&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;SSD computation. Masks out the unknown or unfilled pixels and gives a&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;higher weightage to the center pixel, decreasing as the distance from&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;center pixel increases.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;Returns&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;------&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;ssd : array, np.float&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;(M - window +1, N - window + 1) The desired SSD values for all&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;positions in the input_image&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;total_weight &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; valid_mask&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;sum&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;()&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;window_size &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; template&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;shape&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;y &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;as_strided&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;input_image&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;                   &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;input_image&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; window_size&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;                          &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;input_image&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; window_size&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,)&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;                    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;window_size&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;                    &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;strides&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;input_image&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;strides &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;ssd &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;einsum&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;ijkl, kl, kl-&amp;gt;ij&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; template&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; valid_mask&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;                    &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Refer to the comment below for the explanation&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;ssd &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;*=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;ssd &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;einsum&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;ijkl, ijkl, kl-&amp;gt;ij&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; valid_mask&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;ssd &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;einsum&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#68640C;--1:#B8BB26&quot;&gt;ij, ij, ij&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;&apos;&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; template&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; template&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; valid_mask&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#9D0006;--1:#FB5D4A&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; ssd &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; total_weight&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Comments&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Above, since ‘kl’ are repeated on LHS, we first compute the product of elements in the 3rd and 4th dim of &lt;code&gt;y&lt;/code&gt; with corresponding elements of &lt;code&gt;template&lt;/code&gt; and &lt;code&gt;valid_mask&lt;/code&gt; and sum. Since, the output labels are ‘ij’, we want the summation along the other axes. Hence, each element of &lt;code&gt;ssd&lt;/code&gt; corresponds to element wise product of &lt;code&gt;template&lt;/code&gt; and &lt;code&gt;valid_mask&lt;/code&gt; with the &lt;code&gt;(3, 3)&lt;/code&gt; “sample patch” stored in the 3rd and 4th dimensions of &lt;code&gt;y&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Results&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The fascinating part is that testing this implementation for the same test case as the one used for nested loops completes the computation in about 0.342 seconds! So that’s 19.37 -&amp;gt; 0.34 seconds!&lt;/p&gt;
&lt;p&gt;I am absolutely blown away by NumPy, and if not for anything else, I am definitely thankful to GSoC and scikit-image for introducing me to NumPy!&lt;/p&gt;
&lt;p&gt;Next post will be on my second module for GSoC: Texture Synthesis, whose initial implementation is complete and now in the review phase.&lt;/p&gt;
&lt;p&gt;Cheers!&lt;/p&gt;
&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Much of the content of this post is inspired from the StackOverflow answer to my question by Jaime, of scikit-image community. Thanks a lot! &lt;a href=&quot;http://stackoverflow.com/questions/17881489/faster-way-to-calculate-sum-of-squared-difference-between-an-image-m-n-and-a/17885996?noredirect=1#17885996&quot; class=&quot;link-external&quot; rel=&quot;noopener&quot;&gt;Answer&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;http://scipy-lectures.github.io/&quot; class=&quot;link-external&quot; rel=&quot;noopener&quot;&gt;http://scipy-lectures.github.io/&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Van Der Walt, Stefan, S. Chris Colbert, and Gael Varoquaux. “The NumPy array: a structure for efficient numerical computation.” Computing in Science &amp;amp; Engineering 13.2 (2011): 22-30.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;related-articles&quot;&gt;Related articles&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://chintak.sh/posts/2013-07-15-numpy-the-tricks-of-trade-part-i/&quot; class=&quot;link-internal&quot;&gt;NumPy: The tricks of the trade (Part I)&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=G-Iep_MnSv8&quot; class=&quot;link-external&quot; rel=&quot;noopener&quot;&gt;Diving into NumPy Code, SciPy 2013 Tutorial&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=vWkb7VahaXQ&quot; class=&quot;link-external&quot; rel=&quot;noopener&quot;&gt;Using NumPy to Perform Mathematical Operations in Python&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;updates&quot;&gt;Updates&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;[Feb 2, 2013] Added another example to the section on &lt;code&gt;np.einsum&lt;/code&gt; and comment explaining the role of &lt;code&gt;np.einsum&lt;/code&gt; in the &lt;code&gt;_sum_sq_diff&lt;/code&gt; function. Thanks for pointing out, Juan!&lt;/li&gt;
&lt;/ul&gt;
</content:encoded><dc:creator>Chintak Sheth</dc:creator><category>numpy</category></item><item><title>NumPy: The Tricks of Trade (Part I)</title><link>https://chintak.sh/posts/2013-07-15-numpy-the-tricks-of-trade-part-i/</link><guid isPermaLink="true">https://chintak.sh/posts/2013-07-15-numpy-the-tricks-of-trade-part-i/</guid><description>The basics that give NumPy its edge over Python containers: creating arrays, common attributes and methods, indexing and fancy indexing.</description><pubDate>Mon, 15 Jul 2013 16:00:00 GMT</pubDate><content:encoded>&lt;p&gt;This post aims to highlights some of the basic features of NumPy which gives it an edge over Python in-built containers for computational purposes. The next post will talk about some advanced and more fascinating features of NumPy.
&lt;a href=&quot;http://www.numpy.org/&quot; class=&quot;link-external&quot; rel=&quot;noopener&quot;&gt;NumPy&lt;/a&gt; is an extension package for performing efficient manipulations in multi-dimensional data. The numpy array object is extremely efficient at handling such arithmetics and provides vast support for basic and advanced manipulations.&lt;/p&gt;
&lt;h3 id=&quot;creating-a-numpy-array&quot;&gt;Creating A Numpy Array&lt;/h3&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Creating a NumPy array&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#9D0006;--1:#FB5D4A&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; numpy &lt;/span&gt;&lt;span style=&quot;--0:#9D0006;--1:#FB5D4A&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; an_array &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;uint8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; an_array&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;uint8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;h3 id=&quot;some-commonly-used-attributes-and-methods-of-a-numpy-array-object&quot;&gt;Some Commonly Used Attributes And Methods Of A Numpy Array Object&lt;/h3&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Some Attributes&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; an_array&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;uint8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; an_array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;shape&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; an_array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;ndim&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; an_array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;T &lt;/span&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Also as_array.transpose()&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;uint8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; an_array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;max&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;()&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; an_array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;argmax&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;axis&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Returns index of max element along the given axis&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; an_array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;argmax&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;axis&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; an_array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;mean&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;()&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3.5&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; an_array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;astype&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Convert to another data type&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; an_array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;strides &lt;/span&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Number of steps to move in the memory to reach the next element of row or column&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;There are a whole lot more of such attributes and methods already in-built for the most basic day-to-day operations on a numpy array structure. The last attribute stated here, strides provides an idea of the memory layout underneath and manipulations with these strides can provide with some really powerful techniques by avoiding unnecessary copies of the data. For example, the strides of a numpy array and its transpose are simply swapped. So, the returned array is actually the same data and not a copy of the original data. These kind of techniques make numpy array very efficient and gentle on the memory. More on this in the next post under &lt;code&gt;numpy.lib.stride_tricks.as_strided&lt;/code&gt; function.&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# More about Transpose of an array&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; an_array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;strides&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; array_trans &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; an_array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;T&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; array_trans&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; array_trans&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;uint8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; an_array&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]],&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt;uint8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;h3 id=&quot;indexing&quot;&gt;Indexing&lt;/h3&gt;
&lt;p&gt;NumPy supports the Python-like slicing and indexing. Slices of numpy arrays are views (pointers to data as in C/C++) to the original data and any modification is reflected in the orginal data as well.&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Basics of Indexing&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Indices start from 0, ..., (n-1) if there are &apos;n&apos; elements&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;arange&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;; a &lt;/span&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Function to generate a sequence of numbers&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[::&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[::&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Increments in the index&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Indices start with -1 from the opposite direction: -n, ..., -1&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;h3 id=&quot;fancy-indexing&quot;&gt;Fancy Indexing&lt;/h3&gt;
&lt;p&gt;NumPy also supports Fancy Indexing: indexing using boolean or integer arrays.&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# Fancy Indexing&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# `np.random.random_integers` function to generate random integers&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# First 2 arguments refer to the range in which the random int are to be&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#6A5F54;--0fs:italic;--1:#9D9082;--1fs:italic&quot;&gt;# generated and the 3rd arg is for the size of the output&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; mask &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;random&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;random_integers&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;))&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; mask&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; arr &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; np&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;random&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;random_integers&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;))&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; arr&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;13&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;18&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;],&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;       &lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;17&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; arr&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt;mask &lt;/span&gt;&lt;span style=&quot;--0:#396B4C;--1:#8EC07C&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;--0:#3C3836;--1:#EBDBB2&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#076678;--1:#83A598&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#504945;--1:#D5C4A1&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#8F3F71;--1:#D3869B&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--0:#6B5F56;--1:#A89984&quot;&gt;])&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;h3 id=&quot;references&quot;&gt;References&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;http://scipy-lectures.github.io/&quot; class=&quot;link-external&quot; rel=&quot;noopener&quot;&gt;SciPy GitHub Lectures&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Van Der Walt, Stefan, S. Chris Colbert, and Gael Varoquaux. &lt;em&gt;“The NumPy array: a structure for efficient numerical computation.”&lt;/em&gt; Computing in Science &amp;amp; Engineering 13.2 (2011): 22-30.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
</content:encoded><dc:creator>Chintak Sheth</dc:creator><category>numpy</category></item></channel></rss>