NumPy Basics Guide
NumPy Basics
<h1>Complete NumPy Beginner's Guide</h1>
<h2>Table of Contents</h2>
<ol>
<li><a href="#introduction">Introduction to NumPy</a></li>
<li><a href="#importing">Installing and Importing NumPy</a></li>
<li><a href="#creating-arrays">Creating Arrays</a></li>
<li><a href="#array-properties">Array Properties</a></li>
<li><a href="#data-types">Array Data Types</a></li>
<li><a href="#shapes-dimensions">Array Shapes and Dimensions</a></li>
<li><a href="#array-operations">Array Operations</a></li>
<li><a href="#stacking">Stacking and Combining Arrays</a></li>
<li><a href="#mathematical-operations">Mathematical Operations</a></li>
<li><a href="#special-arrays">Special Arrays</a></li>
<li><a href="#random-numbers">Random Numbers</a></li>
<li><a href="#indexing">Array Indexing and Slicing</a></li>
<li><a href="#broadcasting">Broadcasting</a></li>
<li><a href="#common-functions">Common Functions</a></li>
<li><a href="#practice-examples">Practice Examples</a></li>
</ol>
<hr />
<h2>1. Introduction to NumPy {#introduction}</h2>
<p><strong>NumPy</strong> (Numerical Python) is the fundamental package for scientific
computing in Python. It provides:
- A powerful N-dimensional array object
- Sophisticated broadcasting functions
- Tools for integrating C/C++ and Fortran code
- Useful linear algebra, Fourier transform, and random number capabilities</p>
<h3>Why Use NumPy?</h3>
<ul>
<li><strong>Performance</strong>: NumPy arrays are much faster than Python lists</li>
<li><strong>Memory Efficient</strong>: Uses less memory than Python lists</li>
<li><strong>Mathematical Operations</strong>: Built-in mathematical functions</li>
<li><strong>Broadcasting</strong>: Perform operations on arrays of different shapes</li>
</ul>
<hr />
<h2>2. Installing and Importing NumPy {#importing}</h2>
<h3>Installation</h3>
<div class="codehilite">
NumPy Basics Guide
<pre><span></span>pip<span class="w"> </span>install<span class="w"> </span>numpy
</pre>
</div>
<h3>Importing</h3>
<div class="codehilite">
<pre><span></span><span class="kn">import</span><span class="w"> </span><span
class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w">
</span><span class="nn">np</span>
</pre>
</div>
<p>The np alias is a standard convention used by the NumPy community.</p>
<hr />
<h2>3. Creating Arrays {#creating-arrays}</h2>
<h3>3.1 From Python Lists</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># 1D array from list</span>
<span class="n">py1</span> <span class="o">=</span> <span class="p">[</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span
class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span
class="p">,</span> <span class="mi">6</span><span class="p">]</span>
<span class="n">arr1d</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">(</span><span
class="n">py1</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr1d</span><span
class="p">)</span> <span class="c1"># [1 2 3 4 5 6]</span>
<span class="c1"># 2D array from nested lists</span>
<span class="n">py2</span> <span class="o">=</span> <span class="p">[[</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span
class="mi">4</span><span class="p">],</span> <span class="p">[</span><span
class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span
class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span
class="mi">9</span><span class="p">],</span> <span class="p">[</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span
class="mi">4</span><span class="p">]]</span>
<span class="n">arr2d</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">(</span><span
class="n">py2</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr2d</span><span
class="p">)</span>
<span class="c1"># [[1 2 3 4]</span>
<span class="c1"># [6 7 8 9]</span>
NumPy Basics Guide
<span class="c1"># [1 2 3 4]]</span>
</pre>
</div>
<h3>3.2 Using Built-in Functions</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># Array of zeros</span>
<span class="n">zeros_arr</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">zeros</span><span class="p">((</span><span
class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span
class="p">))</span> <span class="c1"># 3x4 array of zeros</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">zeros_arr</span><span class="p">)</span>
<span class="c1"># Array of ones</span>
<span class="n">ones_arr</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">ones</span><span class="p">((</span><span
class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span
class="p">))</span> <span class="c1"># 2x3 array of ones</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">ones_arr</span><span class="p">)</span>
<span class="c1"># Array with specific value</span>
<span class="n">full_arr</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">full</span><span class="p">((</span><span
class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span
class="p">),</span> <span class="mi">7</span><span class="p">)</span> <span
class="c1"># 2x2 array filled with 7</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">full_arr</span><span class="p">)</span>
<span class="c1"># Identity matrix</span>
<span class="n">identity</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">eye</span><span class="p">(</span><span
class="mi">3</span><span class="p">)</span> <span class="c1"># 3x3 identity
matrix</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">identity</span><span class="p">)</span>
<span class="c1"># Range of numbers</span>
<span class="n">range_arr</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">arange</span><span class="p">(</span><span
class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span
class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="c1">#
[0, 2, 4, 6, 8]</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">range_arr</span><span class="p">)</span>
<span class="c1"># Evenly spaced numbers</span>
<span class="n">linspace_arr</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">linspace</span><span
NumPy Basics Guide
class="p">(</span><span class="mi">0</span><span class="p">,</span> <span
class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span
class="p">)</span> <span class="c1"># 5 numbers from 0 to 1</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">linspace_arr</span><span class="p">)</span> <span class="c1"># [0. 0.25 0.5
0.75 1. ]</span>
</pre>
</div>
<hr />
<h2>4. Array Properties {#array-properties}</h2>
<h3>4.1 Basic Properties</h3>
<div class="codehilite">
<pre><span></span><span class="n">arr</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">array</span><span
class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span
class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span
class="p">,</span> <span class="mi">4</span><span class="p">],</span> <span
class="p">[</span><span class="mi">5</span><span class="p">,</span> <span
class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span
class="p">,</span> <span class="mi">8</span><span class="p">]])</span>
<span class="c1"># Shape - dimensions of the array</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr</span><span
class="o">.</span><span class="n">shape</span><span class="p">)</span> <span
class="c1"># (2, 4) - 2 rows, 4 columns</span>
<span class="c1"># Size - total number of elements</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr</span><span
class="o">.</span><span class="n">size</span><span class="p">)</span> <span
class="c1"># 8</span>
<span class="c1"># Number of dimensions</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr</span><span
class="o">.</span><span class="n">ndim</span><span class="p">)</span> <span
class="c1"># 2</span>
<span class="c1"># Data type</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr</span><span
class="o">.</span><span class="n">dtype</span><span class="p">)</span> <span
class="c1"># int64 (may vary by system)</span>
<span class="c1"># Memory usage</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr</span><span
class="o">.</span><span class="n">nbytes</span><span class="p">)</span> <span
class="c1"># bytes used by the array</span>
</pre>
</div>
NumPy Basics Guide
<h3>4.2 Checking Array Information</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># Get detailed info about array</span>
<span class="k">def</span><span class="w"> </span><span
class="nf">array_info</span><span class="p">(</span><span class="n">arr</span><span
class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Shape: </span><span class="si">{</span><span class="n">arr</span><span
class="o">.</span><span class="n">shape</span><span class="si">}</span><span
class="s2">"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Size: </span><span class="si">{</span><span class="n">arr</span><span
class="o">.</span><span class="n">size</span><span class="si">}</span><span
class="s2">"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Dimensions: </span><span class="si">{</span><span
class="n">arr</span><span class="o">.</span><span class="n">ndim</span><span
class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Data type: </span><span class="si">{</span><span
class="n">arr</span><span class="o">.</span><span class="n">dtype</span><span
class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Memory usage: </span><span class="si">{</span><span
class="n">arr</span><span class="o">.</span><span class="n">nbytes</span><span
class="si">}</span><span class="s2"> bytes"</span><span class="p">)</span>
<span class="n">arr</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span
class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span
class="p">])</span>
<span class="n">array_info</span><span class="p">(</span><span class="n">arr</span><span
class="p">)</span>
</pre>
</div>
<hr />
<h2>5. Array Data Types {#data-types}</h2>
<h3>5.1 Common Data Types</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># Integer types</span>
<span class="n">int_arr</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span
class="n">dtype</span><span class="o">=</span><span class="n">np</span><span
NumPy Basics Guide
class="o">.</span><span class="n">int32</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">int_arr</span><span
class="o">.</span><span class="n">dtype</span><span class="p">)</span> <span
class="c1"># int32</span>
<span class="c1"># Float types</span>
<span class="n">float_arr</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span
class="p">,</span> <span class="mf">3.0</span><span class="p">],</span> <span
class="n">dtype</span><span class="o">=</span><span class="n">np</span><span
class="o">.</span><span class="n">float64</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">float_arr</span><span class="o">.</span><span class="n">dtype</span><span
class="p">)</span> <span class="c1"># float64</span>
<span class="c1"># Boolean type</span>
<span class="n">bool_arr</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="kc">True</span><span class="p">,</span> <span class="kc">False</span><span
class="p">,</span> <span class="kc">True</span><span class="p">],</span> <span
class="n">dtype</span><span class="o">=</span><span class="n">np</span><span
class="o">.</span><span class="n">bool_</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">bool_arr</span><span class="o">.</span><span class="n">dtype</span><span
class="p">)</span> <span class="c1"># bool</span>
<span class="c1"># String type</span>
<span class="n">str_arr</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="s1">'a'</span><span class="p">,</span> <span
class="s1">'b'</span><span class="p">,</span> <span
class="s1">'c'</span><span class="p">],</span> <span class="n">dtype</span><span
class="o">=</span><span class="s1">'U1'</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">str_arr</span><span
class="o">.</span><span class="n">dtype</span><span class="p">)</span> <span
class="c1"># <U1</span>
</pre>
</div>
<h3>5.2 Type Conversion</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># Convert between types</span>
<span class="n">float_arr</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="mf">1.7</span><span class="p">,</span> <span class="mf">2.8</span><span
class="p">,</span> <span class="mf">3.9</span><span class="p">])</span>
<span class="n">int_arr</span> <span class="o">=</span> <span
class="n">float_arr</span><span class="o">.</span><span class="n">astype</span><span
class="p">(</span><span class="n">np</span><span class="o">.</span><span
class="n">int32</span><span class="p">)</span>
NumPy Basics Guide
<span class="nb">print</span><span class="p">(</span><span
class="n">float_arr</span><span class="p">)</span> <span class="c1"># [1.7 2.8
3.9]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">int_arr</span><span
class="p">)</span> <span class="c1"># [1 2 3] - truncated, not rounded</span>
<span class="c1"># Explicit type specification during creation</span>
<span class="n">arr</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span
class="n">dtype</span><span class="o">=</span><span class="n">np</span><span
class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr</span><span
class="o">.</span><span class="n">dtype</span><span class="p">)</span> <span
class="c1"># float32</span>
</pre>
</div>
<hr />
<h2>6. Array Shapes and Dimensions {#shapes-dimensions}</h2>
<h3>6.1 Understanding Shape</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># 1D array</span>
<span class="n">arr1d</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span
class="mi">4</span><span class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"1D Shape: </span><span class="si">{</span><span
class="n">arr1d</span><span class="o">.</span><span class="n">shape</span><span
class="si">}</span><span class="s2">"</span><span class="p">)</span> <span
class="c1"># (4,)</span>
<span class="c1"># 2D array</span>
<span class="n">arr2d</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([[</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span
class="p">[</span><span class="mi">4</span><span class="p">,</span> <span
class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span
class="p">]])</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"2D Shape: </span><span class="si">{</span><span
class="n">arr2d</span><span class="o">.</span><span class="n">shape</span><span
class="si">}</span><span class="s2">"</span><span class="p">)</span> <span
class="c1"># (2, 3)</span>
NumPy Basics Guide
<span class="c1"># 3D array</span>
<span class="n">arr3d</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([[[</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">],</span> <span class="p">[</span><span class="mi">3</span><span
class="p">,</span> <span class="mi">4</span><span class="p">]],</span> <span
class="p">[[</span><span class="mi">5</span><span class="p">,</span> <span
class="mi">6</span><span class="p">],</span> <span class="p">[</span><span
class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span
class="p">]]])</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"3D Shape: </span><span class="si">{</span><span
class="n">arr3d</span><span class="o">.</span><span class="n">shape</span><span
class="si">}</span><span class="s2">"</span><span class="p">)</span> <span
class="c1"># (2, 2, 2)</span>
</pre>
</div>
<h3>6.2 Reshaping Arrays</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># Original array</span>
<span class="n">arr</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span
class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span
class="p">,</span> <span class="mi">6</span><span class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Original: </span><span class="si">{</span><span
class="n">arr</span><span class="o">.</span><span class="n">shape</span><span
class="si">}</span><span class="s2">"</span><span class="p">)</span> <span
class="c1"># (6,)</span>
<span class="c1"># Reshape to 2D</span>
<span class="n">reshaped</span> <span class="o">=</span> <span class="n">arr</span><span
class="o">.</span><span class="n">reshape</span><span class="p">(</span><span
class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span
class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Reshaped: </span><span class="si">{</span><span
class="n">reshaped</span><span class="o">.</span><span class="n">shape</span><span
class="si">}</span><span class="s2">"</span><span class="p">)</span> <span
class="c1"># (2, 3)</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">reshaped</span><span class="p">)</span>
<span class="c1"># [[1 2 3]</span>
<span class="c1"># [4 5 6]]</span>
<span class="c1"># Reshape to 3D</span>
<span class="n">reshaped_3d</span> <span class="o">=</span> <span
class="n">arr</span><span class="o">.</span><span class="n">reshape</span><span
NumPy Basics Guide
class="p">(</span><span class="mi">2</span><span class="p">,</span> <span
class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span
class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"3D Reshaped: </span><span class="si">{</span><span
class="n">reshaped_3d</span><span class="o">.</span><span class="n">shape</span><span
class="si">}</span><span class="s2">"</span><span class="p">)</span> <span
class="c1"># (2, 1, 3)</span>
<span class="c1"># Use -1 for automatic dimension calculation</span>
<span class="n">auto_reshape</span> <span class="o">=</span> <span
class="n">arr</span><span class="o">.</span><span class="n">reshape</span><span
class="p">(</span><span class="mi">3</span><span class="p">,</span> <span
class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="c1">#
3 rows, auto-calculate columns</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Auto reshape: </span><span class="si">{</span><span
class="n">auto_reshape</span><span class="o">.</span><span class="n">shape</span><span
class="si">}</span><span class="s2">"</span><span class="p">)</span> <span
class="c1"># (3, 2)</span>
</pre>
</div>
<h3>6.3 Flattening Arrays</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># 2D array</span>
<span class="n">arr2d</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([[</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span
class="p">[</span><span class="mi">4</span><span class="p">,</span> <span
class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span
class="p">]])</span>
<span class="c1"># Flatten to 1D</span>
<span class="n">flattened</span> <span class="o">=</span> <span
class="n">arr2d</span><span class="o">.</span><span class="n">flatten</span><span
class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">flattened</span><span class="p">)</span> <span class="c1"># [1 2 3 4 5
6]</span>
<span class="c1"># Alternative: ravel (returns view when possible)</span>
<span class="n">raveled</span> <span class="o">=</span> <span
class="n">arr2d</span><span class="o">.</span><span class="n">ravel</span><span
class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="n">raveled</span><span
class="p">)</span> <span class="c1"># [1 2 3 4 5 6]</span>
</pre>
</div>
NumPy Basics Guide
<hr />
<h2>7. Array Operations {#array-operations}</h2>
<h3>7.1 Element-wise Operations</h3>
<div class="codehilite">
<pre><span></span><span class="n">arr1</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">array</span><span
class="p">([</span><span class="mi">1</span><span class="p">,</span> <span
class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span
class="p">,</span> <span class="mi">4</span><span class="p">])</span>
<span class="n">arr2</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span
class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span
class="mi">8</span><span class="p">])</span>
<span class="c1"># Basic arithmetic</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr1</span> <span
class="o">+</span> <span class="n">arr2</span><span class="p">)</span> <span
class="c1"># [ 6 8 10 12]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr1</span> <span
class="o">-</span> <span class="n">arr2</span><span class="p">)</span> <span
class="c1"># [-4 -4 -4 -4]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr1</span> <span
class="o">*</span> <span class="n">arr2</span><span class="p">)</span> <span
class="c1"># [ 5 12 21 32]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr1</span> <span
class="o">/</span> <span class="n">arr2</span><span class="p">)</span> <span
class="c1"># [0.2 0.33 0.43 0.5]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr1</span> <span
class="o">**</span> <span class="n">arr2</span><span class="p">)</span> <span
class="c1"># [1 64 2187 65536]</span>
<span class="c1"># Operations with scalars</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr1</span> <span
class="o">+</span> <span class="mi">10</span><span class="p">)</span> <span
class="c1"># [11 12 13 14]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr1</span> <span
class="o">*</span> <span class="mi">2</span><span class="p">)</span> <span
class="c1"># [2 4 6 8]</span>
</pre>
</div>
<h3>7.2 Comparison Operations</h3>
<div class="codehilite">
<pre><span></span><span class="n">arr1</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">array</span><span
class="p">([</span><span class="mi">1</span><span class="p">,</span> <span
class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span
NumPy Basics Guide
class="p">,</span> <span class="mi">4</span><span class="p">])</span>
<span class="n">arr2</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span
class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span
class="mi">4</span><span class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr1</span> <span
class="o">==</span> <span class="n">arr2</span><span class="p">)</span> <span
class="c1"># [ True False False True]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr1</span> <span
class="o">></span> <span class="n">arr2</span><span class="p">)</span> <span
class="c1"># [False False True False]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr1</span> <span
class="o"><</span> <span class="mi">3</span><span class="p">)</span> <span
class="c1"># [ True True False False]</span>
</pre>
</div>
<hr />
<h2>8. Stacking and Combining Arrays {#stacking}</h2>
<h3>8.1 Horizontal and Vertical Stacking</h3>
<div class="codehilite">
<pre><span></span><span class="n">arr1</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">array</span><span
class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span
class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span
class="p">,</span> <span class="mi">4</span><span class="p">],</span> <span
class="p">[</span><span class="mi">6</span><span class="p">,</span> <span
class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span
class="p">,</span> <span class="mi">9</span><span class="p">],</span> <span
class="p">[</span><span class="mi">1</span><span class="p">,</span> <span
class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span
class="p">,</span> <span class="mi">4</span><span class="p">]])</span>
<span class="n">ones_column</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">ones</span><span
class="p">((</span><span class="mi">3</span><span class="p">,</span> <span
class="mi">1</span><span class="p">))</span>
<span class="c1"># Horizontal stacking (column-wise)</span>
<span class="n">h_stacked</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">hstack</span><span class="p">((</span><span
class="n">arr1</span><span class="p">,</span> <span class="n">ones_column</span><span
class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Horizontal
stacking:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">h_stacked</span><span class="p">)</span>
<span class="c1"># [[1. 2. 3. 4. 1.]</span>
NumPy Basics Guide
<span class="c1"># [6. 7. 8. 9. 1.]</span>
<span class="c1"># [1. 2. 3. 4. 1.]]</span>
<span class="c1"># Vertical stacking (row-wise)</span>
<span class="n">new_row</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([[</span><span
class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span
class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span
class="mi">13</span><span class="p">]])</span>
<span class="n">v_stacked</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">vstack</span><span class="p">((</span><span
class="n">arr1</span><span class="p">,</span> <span class="n">new_row</span><span
class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Vertical
stacking:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">v_stacked</span><span class="p">)</span>
<span class="c1"># Alternative methods</span>
<span class="n">concatenated</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span
class="p">((</span><span class="n">arr1</span><span class="p">,</span> <span
class="n">ones_column</span><span class="p">),</span> <span class="n">axis</span><span
class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="c1">#
axis=1 for horizontal</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Concatenated
(horizontal):"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">concatenated</span><span class="p">)</span>
</pre>
</div>
<h3>8.2 Array Splitting</h3>
<div class="codehilite">
<pre><span></span><span class="n">arr</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">array</span><span
class="p">([</span><span class="mi">1</span><span class="p">,</span> <span
class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span
class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span
class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span
class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span
class="mi">8</span><span class="p">])</span>
<span class="c1"># Split into equal parts</span>
<span class="n">split_arrays</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">split</span><span
class="p">(</span><span class="n">arr</span><span class="p">,</span> <span
class="mi">4</span><span class="p">)</span> <span class="c1"># Split into 4
parts</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Split
arrays:"</span><span class="p">,</span> <span class="n">split_arrays</span><span
NumPy Basics Guide
class="p">)</span>
<span class="c1"># Split 2D array</span>
<span class="n">arr2d</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([[</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span
class="mi">4</span><span class="p">],</span> <span class="p">[</span><span
class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span
class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span
class="mi">8</span><span class="p">],</span> <span class="p">[</span><span
class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span
class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span
class="mi">12</span><span class="p">]])</span>
<span class="n">hsplit_result</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">hsplit</span><span
class="p">(</span><span class="n">arr2d</span><span class="p">,</span> <span
class="mi">2</span><span class="p">)</span> <span class="c1"># Split
horizontally</span>
<span class="n">vsplit_result</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">vsplit</span><span
class="p">(</span><span class="n">arr2d</span><span class="p">,</span> <span
class="mi">3</span><span class="p">)</span> <span class="c1"># Split vertically</span>
</pre>
</div>
<hr />
<h2>9. Mathematical Operations {#mathematical-operations}</h2>
<h3>9.1 Basic Math Functions</h3>
<div class="codehilite">
<pre><span></span><span class="n">arr</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">array</span><span
class="p">([</span><span class="mi">1</span><span class="p">,</span> <span
class="mi">4</span><span class="p">,</span> <span class="mi">9</span><span
class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span
class="mi">25</span><span class="p">])</span>
<span class="c1"># Square root</span>
<span class="nb">print</span><span class="p">(</span><span class="n">np</span><span
class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span
class="n">arr</span><span class="p">))</span> <span class="c1"># [1. 2. 3. 4.
5.]</span>
<span class="c1"># Exponential</span>
<span class="nb">print</span><span class="p">(</span><span class="n">np</span><span
class="o">.</span><span class="n">exp</span><span class="p">(</span><span
class="n">arr</span><span class="p">))</span> <span class="c1"># [2.718...
54.598... etc.]</span>
NumPy Basics Guide
<span class="c1"># Logarithm</span>
<span class="nb">print</span><span class="p">(</span><span class="n">np</span><span
class="o">.</span><span class="n">log</span><span class="p">(</span><span
class="n">arr</span><span class="p">))</span> <span class="c1"># [0. 1.386...
2.197... etc.]</span>
<span class="c1"># Trigonometric functions</span>
<span class="n">angles</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="mi">0</span><span class="p">,</span> <span class="n">np</span><span
class="o">.</span><span class="n">pi</span><span class="o">/</span><span
class="mi">2</span><span class="p">,</span> <span class="n">np</span><span
class="o">.</span><span class="n">pi</span><span class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span class="n">np</span><span
class="o">.</span><span class="n">sin</span><span class="p">(</span><span
class="n">angles</span><span class="p">))</span> <span class="c1"># [0. 1. 0.]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">np</span><span
class="o">.</span><span class="n">cos</span><span class="p">(</span><span
class="n">angles</span><span class="p">))</span> <span class="c1"># [1. 0.
-1.]</span>
</pre>
</div>
<h3>9.2 Statistical Functions</h3>
<div class="codehilite">
<pre><span></span><span class="n">arr</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">array</span><span
class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span
class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span
class="p">],</span> <span class="p">[</span><span class="mi">4</span><span
class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span
class="mi">6</span><span class="p">],</span> <span class="p">[</span><span
class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span
class="p">,</span> <span class="mi">9</span><span class="p">]])</span>
<span class="c1"># Basic statistics</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Sum: </span><span class="si">{</span><span class="n">np</span><span
class="o">.</span><span class="n">sum</span><span class="p">(</span><span
class="n">arr</span><span class="p">)</span><span class="si">}</span><span
class="s2">"</span><span class="p">)</span> <span class="c1"># 45</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Mean: </span><span class="si">{</span><span class="n">np</span><span
class="o">.</span><span class="n">mean</span><span class="p">(</span><span
class="n">arr</span><span class="p">)</span><span class="si">}</span><span
class="s2">"</span><span class="p">)</span> <span class="c1"># 5.0</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Median: </span><span class="si">{</span><span class="n">np</span><span
class="o">.</span><span class="n">median</span><span class="p">(</span><span
class="n">arr</span><span class="p">)</span><span class="si">}</span><span
class="s2">"</span><span class="p">)</span> <span class="c1"># 5.0</span>
NumPy Basics Guide
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Standard deviation: </span><span class="si">{</span><span
class="n">np</span><span class="o">.</span><span class="n">std</span><span
class="p">(</span><span class="n">arr</span><span class="p">)</span><span
class="si">}</span><span class="s2">"</span><span class="p">)</span> <span
class="c1"># 2.58...</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Variance: </span><span class="si">{</span><span
class="n">np</span><span class="o">.</span><span class="n">var</span><span
class="p">(</span><span class="n">arr</span><span class="p">)</span><span
class="si">}</span><span class="s2">"</span><span class="p">)</span> <span
class="c1"># 6.67...</span>
<span class="c1"># Along specific axes</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Sum along rows (axis=0): </span><span class="si">{</span><span
class="n">np</span><span class="o">.</span><span class="n">sum</span><span
class="p">(</span><span class="n">arr</span><span class="p">,</span><span class="w">
</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span
class="p">)</span><span class="si">}</span><span class="s2">"</span><span
class="p">)</span> <span class="c1"># [12 15 18]</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Sum along columns (axis=1): </span><span class="si">{</span><span
class="n">np</span><span class="o">.</span><span class="n">sum</span><span
class="p">(</span><span class="n">arr</span><span class="p">,</span><span class="w">
</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span
class="p">)</span><span class="si">}</span><span class="s2">"</span><span
class="p">)</span> <span class="c1"># [ 6 15 24]</span>
<span class="c1"># Min and max</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Min: </span><span class="si">{</span><span class="n">np</span><span
class="o">.</span><span class="n">min</span><span class="p">(</span><span
class="n">arr</span><span class="p">)</span><span class="si">}</span><span
class="s2">"</span><span class="p">)</span> <span class="c1"># 1</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Max: </span><span class="si">{</span><span class="n">np</span><span
class="o">.</span><span class="n">max</span><span class="p">(</span><span
class="n">arr</span><span class="p">)</span><span class="si">}</span><span
class="s2">"</span><span class="p">)</span> <span class="c1"># 9</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Argmin: </span><span class="si">{</span><span class="n">np</span><span
class="o">.</span><span class="n">argmin</span><span class="p">(</span><span
class="n">arr</span><span class="p">)</span><span class="si">}</span><span
class="s2">"</span><span class="p">)</span> <span class="c1"># 0 (index of min
element)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Argmax: </span><span class="si">{</span><span class="n">np</span><span
class="o">.</span><span class="n">argmax</span><span class="p">(</span><span
class="n">arr</span><span class="p">)</span><span class="si">}</span><span
class="s2">"</span><span class="p">)</span> <span class="c1"># 8 (index of max
element)</span>
NumPy Basics Guide
</pre>
</div>
<hr />
<h2>10. Special Arrays {#special-arrays}</h2>
<h3>10.1 Identity and Diagonal Arrays</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># Identity matrix</span>
<span class="n">identity_3x3</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">eye</span><span
class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"3x3 Identity
matrix:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">identity_3x3</span><span class="p">)</span>
<span class="c1"># Identity with different dimensions</span>
<span class="n">identity_rect</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">eye</span><span
class="p">(</span><span class="mi">3</span><span class="p">,</span> <span
class="mi">4</span><span class="p">)</span> <span class="c1"># 3 rows, 4 columns</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"3x4
Identity-like matrix:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">identity_rect</span><span class="p">)</span>
<span class="c1"># Diagonal matrix</span>
<span class="n">diagonal_values</span> <span class="o">=</span> <span
class="p">[</span><span class="mi">1</span><span class="p">,</span> <span
class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span
class="p">,</span> <span class="mi">4</span><span class="p">]</span>
<span class="n">diag_matrix</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">diag</span><span
class="p">(</span><span class="n">diagonal_values</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Diagonal
matrix:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">diag_matrix</span><span class="p">)</span>
</pre>
</div>
<h3>10.2 Other Special Arrays</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># Array of constants</span>
<span class="n">constant_array</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">full</span><span
class="p">((</span><span class="mi">3</span><span class="p">,</span> <span
class="mi">3</span><span class="p">),</span> <span class="mi">42</span><span
NumPy Basics Guide
class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Array filled
with 42:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">constant_array</span><span class="p">)</span>
<span class="c1"># Empty array (uninitialized)</span>
<span class="n">empty_array</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">empty</span><span
class="p">((</span><span class="mi">2</span><span class="p">,</span> <span
class="mi">2</span><span class="p">))</span> <span class="c1"># Values are
arbitrary</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Empty array
(arbitrary values):"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">empty_array</span><span class="p">)</span>
<span class="c1"># Array like another array</span>
<span class="n">template</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([[</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">],</span> <span class="p">[</span><span class="mi">3</span><span
class="p">,</span> <span class="mi">4</span><span class="p">]])</span>
<span class="n">zeros_like</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">zeros_like</span><span
class="p">(</span><span class="n">template</span><span class="p">)</span>
<span class="n">ones_like</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">ones_like</span><span class="p">(</span><span
class="n">template</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Zeros like
template:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">zeros_like</span><span class="p">)</span>
</pre>
</div>
<hr />
<h2>11. Random Numbers {#random-numbers}</h2>
<h3>11.1 Basic Random Generation</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># Set seed for reproducibility</span>
<span class="n">np</span><span class="o">.</span><span class="n">random</span><span
class="o">.</span><span class="n">seed</span><span class="p">(</span><span
class="mi">42</span><span class="p">)</span>
<span class="c1"># Random floats between 0 and 1</span>
<span class="n">random_floats</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">random</span><span
class="o">.</span><span class="n">rand</span><span class="p">(</span><span
NumPy Basics Guide
class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span
class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Random
floats (0-1):"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">random_floats</span><span class="p">)</span>
<span class="c1"># Random integers</span>
<span class="n">random_ints</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">random</span><span
class="o">.</span><span class="n">randint</span><span class="p">(</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span
class="p">,</span> <span class="n">size</span><span class="o">=</span><span
class="p">(</span><span class="mi">2</span><span class="p">,</span> <span
class="mi">3</span><span class="p">))</span> <span class="c1"># 1-9, 2x3 array</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Random
integers (1-9):"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">random_ints</span><span class="p">)</span>
<span class="c1"># Random from normal distribution</span>
<span class="n">normal_random</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">random</span><span
class="o">.</span><span class="n">randn</span><span class="p">(</span><span
class="mi">1000</span><span class="p">)</span> <span class="c1"># 1000 samples from
standard normal</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Normal random - Mean: </span><span class="si">{</span><span
class="n">np</span><span class="o">.</span><span class="n">mean</span><span
class="p">(</span><span class="n">normal_random</span><span class="p">)</span><span
class="si">:</span><span class="s2">.3f</span><span class="si">}</span><span
class="s2">, Std: </span><span class="si">{</span><span class="n">np</span><span
class="o">.</span><span class="n">std</span><span class="p">(</span><span
class="n">normal_random</span><span class="p">)</span><span class="si">:</span><span
class="s2">.3f</span><span class="si">}</span><span class="s2">"</span><span
class="p">)</span>
<span class="c1"># Random choice from array</span>
<span class="n">choices</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="s1">'apple'</span><span class="p">,</span> <span
class="s1">'banana'</span><span class="p">,</span> <span
class="s1">'cherry'</span><span class="p">,</span> <span
class="s1">'date'</span><span class="p">])</span>
<span class="n">random_choice</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">random</span><span
class="o">.</span><span class="n">choice</span><span class="p">(</span><span
class="n">choices</span><span class="p">,</span> <span class="n">size</span><span
class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Random
choices:"</span><span class="p">,</span> <span class="n">random_choice</span><span
class="p">)</span>
NumPy Basics Guide
</pre>
</div>
<h3>11.2 Advanced Random Operations</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># Shuffle array</span>
<span class="n">arr</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span
class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span
class="p">])</span>
<span class="n">np</span><span class="o">.</span><span class="n">random</span><span
class="o">.</span><span class="n">shuffle</span><span class="p">(</span><span
class="n">arr</span><span class="p">)</span> <span class="c1"># Modifies original
array</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Shuffled
array:"</span><span class="p">,</span> <span class="n">arr</span><span
class="p">)</span>
<span class="c1"># Random sample without replacement</span>
<span class="n">original</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span
class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span
class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span
class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span
class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span
class="mi">10</span><span class="p">])</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">random</span><span class="o">.</span><span
class="n">choice</span><span class="p">(</span><span class="n">original</span><span
class="p">,</span> <span class="n">size</span><span class="o">=</span><span
class="mi">5</span><span class="p">,</span> <span class="n">replace</span><span
class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Random
sample (no replacement):"</span><span class="p">,</span> <span
class="n">sample</span><span class="p">)</span>
<span class="c1"># Random with specific distribution</span>
<span class="n">uniform_random</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">random</span><span
class="o">.</span><span class="n">uniform</span><span class="p">(</span><span
class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span
class="p">,</span> <span class="n">size</span><span class="o">=</span><span
class="mi">5</span><span class="p">)</span> <span class="c1"># Uniform between
10-20</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Uniform
random (10-20):"</span><span class="p">,</span> <span
class="n">uniform_random</span><span class="p">)</span>
NumPy Basics Guide
</pre>
</div>
<hr />
<h2>12. Array Indexing and Slicing {#indexing}</h2>
<h3>12.1 Basic Indexing</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># 1D array indexing</span>
<span class="n">arr1d</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span
class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span
class="mi">40</span><span class="p">,</span> <span class="mi">50</span><span
class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr1d</span><span
class="p">[</span><span class="mi">0</span><span class="p">])</span> <span
class="c1"># 10 (first element)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr1d</span><span
class="p">[</span><span class="o">-</span><span class="mi">1</span><span
class="p">])</span> <span class="c1"># 50 (last element)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr1d</span><span
class="p">[</span><span class="mi">1</span><span class="p">:</span><span
class="mi">4</span><span class="p">])</span> <span class="c1"># [20 30 40]
(slice)</span>
<span class="c1"># 2D array indexing</span>
<span class="n">arr2d</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([[</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span
class="p">[</span><span class="mi">4</span><span class="p">,</span> <span
class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span
class="p">],</span> <span class="p">[</span><span class="mi">7</span><span
class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span
class="mi">9</span><span class="p">]])</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr2d</span><span
class="p">[</span><span class="mi">0</span><span class="p">,</span> <span
class="mi">0</span><span class="p">])</span> <span class="c1"># 1 (first row, first
column)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr2d</span><span
class="p">[</span><span class="mi">1</span><span class="p">,</span> <span
class="mi">2</span><span class="p">])</span> <span class="c1"># 6 (second row, third
column)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr2d</span><span
class="p">[</span><span class="o">-</span><span class="mi">1</span><span
class="p">,</span> <span class="o">-</span><span class="mi">1</span><span
class="p">])</span> <span class="c1"># 9 (last row, last column)</span>
</pre>
</div>
NumPy Basics Guide
<h3>12.2 Advanced Slicing</h3>
<div class="codehilite">
<pre><span></span><span class="n">arr2d</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">array</span><span
class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span
class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span
class="p">,</span> <span class="mi">4</span><span class="p">],</span> <span
class="p">[</span><span class="mi">5</span><span class="p">,</span> <span
class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span
class="p">,</span> <span class="mi">8</span><span class="p">],</span> <span
class="p">[</span><span class="mi">9</span><span class="p">,</span> <span
class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span
class="p">,</span> <span class="mi">12</span><span class="p">]])</span>
<span class="c1"># Row slicing</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"First two
rows:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr2d</span><span
class="p">[:</span><span class="mi">2</span><span class="p">])</span> <span
class="c1"># First 2 rows</span>
<span class="c1"># Column slicing</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"First two
columns:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr2d</span><span
class="p">[:,</span> <span class="p">:</span><span class="mi">2</span><span
class="p">])</span> <span class="c1"># All rows, first 2 columns</span>
<span class="c1"># Combined slicing</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Subarray
(rows 1-2, columns 1-3):"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr2d</span><span
class="p">[</span><span class="mi">1</span><span class="p">:</span><span
class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span
class="p">:</span><span class="mi">4</span><span class="p">])</span> <span class="c1">#
Rows 1-2, columns 1-3</span>
<span class="c1"># Step slicing</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Every other
element:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr2d</span><span
class="p">[::</span><span class="mi">2</span><span class="p">,</span> <span
class="p">::</span><span class="mi">2</span><span class="p">])</span> <span
class="c1"># Every other row and column</span>
</pre>
</div>
<h3>12.3 Boolean Indexing</h3>
<div class="codehilite">
NumPy Basics Guide
<pre><span></span><span class="n">arr</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">array</span><span
class="p">([</span><span class="mi">1</span><span class="p">,</span> <span
class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span
class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span
class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span
class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span
class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span
class="p">,</span> <span class="mi">10</span><span class="p">])</span>
<span class="c1"># Boolean condition</span>
<span class="n">condition</span> <span class="o">=</span> <span class="n">arr</span>
<span class="o">></span> <span class="mi">5</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Elements
> 5:"</span><span class="p">,</span> <span class="n">arr</span><span
class="p">[</span><span class="n">condition</span><span class="p">])</span> <span
class="c1"># [6 7 8 9 10]</span>
<span class="c1"># Complex conditions</span>
<span class="n">complex_condition</span> <span class="o">=</span> <span
class="p">(</span><span class="n">arr</span> <span class="o">></span> <span
class="mi">3</span><span class="p">)</span> <span class="o">&</span> <span
class="p">(</span><span class="n">arr</span> <span class="o"><</span> <span
class="mi">8</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Elements
between 3 and 8:"</span><span class="p">,</span> <span class="n">arr</span><span
class="p">[</span><span class="n">complex_condition</span><span class="p">])</span>
<span class="c1"># [4 5 6 7]</span>
<span class="c1"># Modify elements based on condition</span>
<span class="n">arr</span><span class="p">[</span><span class="n">arr</span> <span
class="o">></span> <span class="mi">7</span><span class="p">]</span> <span
class="o">=</span> <span class="mi">0</span> <span class="c1"># Set elements > 7 to
0</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"After
modification:"</span><span class="p">,</span> <span class="n">arr</span><span
class="p">)</span>
</pre>
</div>
<hr />
<h2>13. Broadcasting {#broadcasting}</h2>
<p>Broadcasting allows NumPy to perform operations on arrays with different shapes.</p>
<h3>13.1 Basic Broadcasting</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># Scalar with array</span>
<span class="n">arr</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
NumPy Basics Guide
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span
class="mi">4</span><span class="p">])</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">arr</span> <span
class="o">+</span> <span class="mi">10</span> <span class="c1"># 10 is broadcast to
each element</span>
<span class="nb">print</span><span class="p">(</span><span class="n">result</span><span
class="p">)</span> <span class="c1"># [11 12 13 14]</span>
<span class="c1"># 1D array with 2D array</span>
<span class="n">arr2d</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([[</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span
class="p">[</span><span class="mi">4</span><span class="p">,</span> <span
class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span
class="p">]])</span>
<span class="n">arr1d</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span
class="p">,</span> <span class="mi">30</span><span class="p">])</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">arr2d</span>
<span class="o">+</span> <span class="n">arr1d</span> <span class="c1"># arr1d is
broadcast to each row</span>
<span class="nb">print</span><span class="p">(</span><span class="n">result</span><span
class="p">)</span>
<span class="c1"># [[11 22 33]</span>
<span class="c1"># [14 25 36]]</span>
</pre>
</div>
<h3>13.2 Broadcasting Rules</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># Compatible shapes for broadcasting</span>
<span class="n">arr1</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([[</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">]])</span> <span
class="c1"># Shape: (1, 3)</span>
<span class="n">arr2</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([[</span><span
class="mi">1</span><span class="p">],</span> <span class="p">[</span><span
class="mi">2</span><span class="p">],</span> <span class="p">[</span><span
class="mi">3</span><span class="p">]])</span> <span class="c1"># Shape: (3, 1)</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">arr1</span> <span
class="o">+</span> <span class="n">arr2</span> <span class="c1"># Result
shape: (3, 3)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Broadcasting
result:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">result</span><span
class="p">)</span>
NumPy Basics Guide
<span class="c1"># Demonstrating broadcasting behavior</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Original
arrays:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"arr1
shape:"</span><span class="p">,</span> <span class="n">arr1</span><span
class="o">.</span><span class="n">shape</span><span class="p">,</span> <span
class="s2">"</span><span class="se">\n</span><span
class="s2">arr1:"</span><span class="p">,</span> <span class="n">arr1</span><span
class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"arr2
shape:"</span><span class="p">,</span> <span class="n">arr2</span><span
class="o">.</span><span class="n">shape</span><span class="p">,</span> <span
class="s2">"</span><span class="se">\n</span><span
class="s2">arr2:"</span><span class="p">,</span> <span class="n">arr2</span><span
class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Result
shape:"</span><span class="p">,</span> <span class="n">result</span><span
class="o">.</span><span class="n">shape</span><span class="p">)</span>
</pre>
</div>
<hr />
<h2>14. Common Functions {#common-functions}</h2>
<h3>14.1 Array Manipulation</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># Transpose</span>
<span class="n">arr</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([[</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span
class="p">[</span><span class="mi">4</span><span class="p">,</span> <span
class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span
class="p">]])</span>
<span class="n">transposed</span> <span class="o">=</span> <span
class="n">arr</span><span class="o">.</span><span class="n">T</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Original
shape:"</span><span class="p">,</span> <span class="n">arr</span><span
class="o">.</span><span class="n">shape</span><span class="p">)</span> <span
class="c1"># (2, 3)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Transposed
shape:"</span><span class="p">,</span> <span class="n">transposed</span><span
class="o">.</span><span class="n">shape</span><span class="p">)</span> <span
class="c1"># (3, 2)</span>
<span class="c1"># Reverse array</span>
<span class="n">arr1d</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
NumPy Basics Guide
class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span
class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span
class="p">])</span>
<span class="n">reversed_arr</span> <span class="o">=</span> <span
class="n">arr1d</span><span class="p">[::</span><span class="o">-</span><span
class="mi">1</span><span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span
class="s2">"Reversed:"</span><span class="p">,</span> <span
class="n">reversed_arr</span><span class="p">)</span> <span class="c1"># [5 4 3 2
1]</span>
<span class="c1"># Sort array</span>
<span class="n">unsorted</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span
class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span
class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span
class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span
class="mi">2</span><span class="p">,</span> <span class="mi">6</span><span
class="p">])</span>
<span class="n">sorted_arr</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">sort</span><span
class="p">(</span><span class="n">unsorted</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span
class="s2">"Sorted:"</span><span class="p">,</span> <span
class="n">sorted_arr</span><span class="p">)</span> <span class="c1"># [1 1 2 3 4 5 6
9]</span>
<span class="c1"># Find unique elements</span>
<span class="n">unique_elements</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">unique</span><span
class="p">(</span><span class="n">unsorted</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Unique
elements:"</span><span class="p">,</span> <span
class="n">unique_elements</span><span class="p">)</span> <span class="c1"># [1 2 3 4 5
6 9]</span>
</pre>
</div>
<h3>14.2 Searching and Counting</h3>
<div class="codehilite">
<pre><span></span><span class="n">arr</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">array</span><span
class="p">([</span><span class="mi">1</span><span class="p">,</span> <span
class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span
class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span
class="mi">4</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">5</span><span class="p">])</span>
<span class="c1"># Find indices where condition is true</span>
<span class="n">indices</span> <span class="o">=</span> <span class="n">np</span><span
NumPy Basics Guide
class="o">.</span><span class="n">where</span><span class="p">(</span><span
class="n">arr</span> <span class="o">==</span> <span class="mi">2</span><span
class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Indices
where arr == 2:"</span><span class="p">,</span> <span class="n">indices</span><span
class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="c1">#
[1 3 5]</span>
<span class="c1"># Count non-zero elements</span>
<span class="n">count_nonzero</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">count_nonzero</span><span
class="p">(</span><span class="n">arr</span> <span class="o">></span> <span
class="mi">2</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Count of
elements > 2:"</span><span class="p">,</span> <span
class="n">count_nonzero</span><span class="p">)</span> <span class="c1"># 3</span>
<span class="c1"># Any and all</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Any element
> 4:"</span><span class="p">,</span> <span class="n">np</span><span
class="o">.</span><span class="n">any</span><span class="p">(</span><span
class="n">arr</span> <span class="o">></span> <span class="mi">4</span><span
class="p">))</span> <span class="c1"># True</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"All elements
> 0:"</span><span class="p">,</span> <span class="n">np</span><span
class="o">.</span><span class="n">all</span><span class="p">(</span><span
class="n">arr</span> <span class="o">></span> <span class="mi">0</span><span
class="p">))</span> <span class="c1"># True</span>
</pre>
</div>
<hr />
<h2>15. Practice Examples {#practice-examples}</h2>
<h3>Example 1: Matrix Operations</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># Create two matrices</span>
<span class="n">A</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([[</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">],</span> <span class="p">[</span><span class="mi">3</span><span
class="p">,</span> <span class="mi">4</span><span class="p">]])</span>
<span class="n">B</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([[</span><span
class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span
class="p">],</span> <span class="p">[</span><span class="mi">7</span><span
class="p">,</span> <span class="mi">8</span><span class="p">]])</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Matrix
A:"</span><span class="p">)</span>
NumPy Basics Guide
<span class="nb">print</span><span class="p">(</span><span class="n">A</span><span
class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Matrix
B:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">B</span><span
class="p">)</span>
<span class="c1"># Element-wise multiplication</span>
<span class="n">elementwise</span> <span class="o">=</span> <span class="n">A</span>
<span class="o">*</span> <span class="n">B</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Element-wise
multiplication:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">elementwise</span><span class="p">)</span>
<span class="c1"># Matrix multiplication (dot product)</span>
<span class="n">matrix_mult</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">dot</span><span
class="p">(</span><span class="n">A</span><span class="p">,</span> <span
class="n">B</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Matrix
multiplication:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">matrix_mult</span><span class="p">)</span>
<span class="c1"># Or using @ operator (Python 3.5+)</span>
<span class="n">matrix_mult_alt</span> <span class="o">=</span> <span class="n">A</span>
<span class="o">@</span> <span class="n">B</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Matrix
multiplication (@ operator):"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span
class="n">matrix_mult_alt</span><span class="p">)</span>
</pre>
</div>
<h3>Example 2: Data Analysis Simulation</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># Simulate student grades</span>
<span class="n">np</span><span class="o">.</span><span class="n">random</span><span
class="o">.</span><span class="n">seed</span><span class="p">(</span><span
class="mi">42</span><span class="p">)</span>
<span class="n">students</span> <span class="o">=</span> <span class="mi">100</span>
<span class="n">subjects</span> <span class="o">=</span> <span class="mi">5</span>
<span class="c1"># Generate random grades (0-100)</span>
<span class="n">grades</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">random</span><span class="o">.</span><span
class="n">randint</span><span class="p">(</span><span class="mi">50</span><span
class="p">,</span> <span class="mi">101</span><span class="p">,</span> <span
class="n">size</span><span class="o">=</span><span class="p">(</span><span
class="n">students</span><span class="p">,</span> <span class="n">subjects</span><span
NumPy Basics Guide
class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Grades array shape: </span><span class="si">{</span><span
class="n">grades</span><span class="o">.</span><span class="n">shape</span><span
class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"First 5
students' grades:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">grades</span><span
class="p">[:</span><span class="mi">5</span><span class="p">])</span>
<span class="c1"># Calculate statistics</span>
<span class="n">average_per_student</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">mean</span><span
class="p">(</span><span class="n">grades</span><span class="p">,</span> <span
class="n">axis</span><span class="o">=</span><span class="mi">1</span><span
class="p">)</span>
<span class="n">average_per_subject</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">mean</span><span
class="p">(</span><span class="n">grades</span><span class="p">,</span> <span
class="n">axis</span><span class="o">=</span><span class="mi">0</span><span
class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"</span><span class="se">\n</span><span class="s2">Overall class
average: </span><span class="si">{</span><span class="n">np</span><span
class="o">.</span><span class="n">mean</span><span class="p">(</span><span
class="n">grades</span><span class="p">)</span><span class="si">:</span><span
class="s2">.2f</span><span class="si">}</span><span class="s2">"</span><span
class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Highest average student: </span><span class="si">{</span><span
class="n">np</span><span class="o">.</span><span class="n">max</span><span
class="p">(</span><span class="n">average_per_student</span><span
class="p">)</span><span class="si">:</span><span class="s2">.2f</span><span
class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Lowest average student: </span><span class="si">{</span><span
class="n">np</span><span class="o">.</span><span class="n">min</span><span
class="p">(</span><span class="n">average_per_student</span><span
class="p">)</span><span class="si">:</span><span class="s2">.2f</span><span
class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Subject averages: </span><span class="si">{</span><span
class="n">average_per_subject</span><span class="si">}</span><span
class="s2">"</span><span class="p">)</span>
<span class="c1"># Find students with average > 85</span>
<span class="n">high_performers</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">where</span><span
class="p">(</span><span class="n">average_per_student</span> <span class="o">></span>
<span class="mi">85</span><span class="p">)[</span><span class="mi">0</span><span
NumPy Basics Guide
class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"</span><span class="se">\n</span><span class="s2">Number of high
performers (>85 average): </span><span class="si">{</span><span
class="nb">len</span><span class="p">(</span><span class="n">high_performers</span><span
class="p">)</span><span class="si">}</span><span class="s2">"</span><span
class="p">)</span>
</pre>
</div>
<h3>Example 3: Image Processing Basics</h3>
<div class="codehilite">
<pre><span></span><span class="c1"># Simulate a small grayscale image (0-255 pixel
values)</span>
<span class="n">image</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">random</span><span class="o">.</span><span
class="n">randint</span><span class="p">(</span><span class="mi">0</span><span
class="p">,</span> <span class="mi">256</span><span class="p">,</span> <span
class="n">size</span><span class="o">=</span><span class="p">(</span><span
class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span
class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span
class="n">np</span><span class="o">.</span><span class="n">uint8</span><span
class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Original
image (10x10):"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">image</span><span
class="p">)</span>
<span class="c1"># Image processing operations</span>
<span class="c1"># Brighten image (add 50 to all pixels, clip to max 255)</span>
<span class="n">brightened</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">clip</span><span
class="p">(</span><span class="n">image</span> <span class="o">+</span> <span
class="mi">50</span><span class="p">,</span> <span class="mi">0</span><span
class="p">,</span> <span class="mi">255</span><span class="p">)</span>
<span class="c1"># Apply threshold (binary image)</span>
<span class="n">threshold</span> <span class="o">=</span> <span class="mi">128</span>
<span class="n">binary</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">where</span><span class="p">(</span><span
class="n">image</span> <span class="o">></span> <span class="n">threshold</span><span
class="p">,</span> <span class="mi">255</span><span class="p">,</span> <span
class="mi">0</span><span class="p">)</span>
<span class="c1"># Calculate image statistics</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"</span><span class="se">\n</span><span class="s2">Image
statistics:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Min pixel value: </span><span class="si">{</span><span
NumPy Basics Guide
class="n">np</span><span class="o">.</span><span class="n">min</span><span
class="p">(</span><span class="n">image</span><span class="p">)</span><span
class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Max pixel value: </span><span class="si">{</span><span
class="n">np</span><span class="o">.</span><span class="n">max</span><span
class="p">(</span><span class="n">image</span><span class="p">)</span><span
class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Mean pixel value: </span><span class="si">{</span><span
class="n">np</span><span class="o">.</span><span class="n">mean</span><span
class="p">(</span><span class="n">image</span><span class="p">)</span><span
class="si">:</span><span class="s2">.2f</span><span class="si">}</span><span
class="s2">"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span
class="s2">"Pixels above threshold (</span><span class="si">{</span><span
class="n">threshold</span><span class="si">}</span><span class="s2">): </span><span
class="si">{</span><span class="n">np</span><span class="o">.</span><span
class="n">sum</span><span class="p">(</span><span class="n">image</span><span class="w">
</span><span class="o">></span><span class="w"> </span><span
class="n">threshold</span><span class="p">)</span><span class="si">}</span><span
class="s2">"</span><span class="p">)</span>
</pre>
</div>
<hr />
<h2>Key Tips for NumPy Beginners</h2>
<h3>1. <strong>Memory Efficiency</strong></h3>
<ul>
<li>NumPy arrays use less memory than Python lists</li>
<li>Operations are vectorized and much faster</li>
<li>Avoid loops when possible, use NumPy functions instead</li>
</ul>
<h3>2. <strong>Shape Awareness</strong></h3>
<ul>
<li>Always check array shapes when debugging: print(arr.shape)</li>
<li>Use reshape() to change dimensions</li>
<li>Remember that operations need compatible shapes</li>
</ul>
<h3>3. <strong>Data Types Matter</strong></h3>
<ul>
<li>Specify data types when creating arrays for memory efficiency</li>
<li>Use astype() to convert between types</li>
<li>Be aware of precision loss when converting float to int</li>
</ul>
NumPy Basics Guide
<h3>4. <strong>Common Pitfalls</strong></h3>
<ul>
<li>Array assignment creates views, not copies (use copy() for independent arrays)</li>
<li>Boolean indexing returns a copy, not a view</li>
<li>Division by zero returns inf or nan, not an error</li>
</ul>
<h3>5. <strong>Performance Tips</strong></h3>
<ul>
<li>Use vectorized operations instead of loops</li>
<li>Pre-allocate arrays when possible</li>
<li>Use appropriate data types (float32 vs float64)</li>
</ul>
<hr />
<h2>Conclusion</h2>
<p>This guide covers the fundamental concepts of NumPy that every beginner should know.
NumPy is the foundation for many other scientific Python libraries like pandas,
scikit-learn, and matplotlib. Master these basics, and you'll be well-prepared for more
advanced data science and scientific computing tasks.</p>
<p>Remember to practice with real data and experiment with different functions to
solidify your understanding. The NumPy documentation (https://numpy.org/doc/) is an
excellent resource for deeper exploration of specific functions and advanced topics.</p>
<hr />
<p><em>Happy coding with NumPy!</em> </p>
<h3>Example: Creating a NumPy Array</h3>
<div class="codehilite">
<pre><span></span><span class="kn">import</span><span class="w"> </span><span
class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w">
</span><span class="nn">np</span>
<span class="n">arr</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span
class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span class="n">arr</span><span
class="p">)</span>
</pre>
</div>
<h3>Example: Array Shape and Reshape</h3>
<div class="codehilite">
NumPy Basics Guide
<pre><span></span><span class="n">arr</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">array</span><span
class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span
class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span
class="p">],</span> <span class="p">[</span><span class="mi">4</span><span
class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span
class="mi">6</span><span class="p">]])</span>
<span class="nb">print</span><span class="p">(</span><span
class="s2">"Shape:"</span><span class="p">,</span> <span
class="n">arr</span><span class="o">.</span><span class="n">shape</span><span
class="p">)</span>
<span class="n">reshaped</span> <span class="o">=</span> <span class="n">arr</span><span
class="o">.</span><span class="n">reshape</span><span class="p">((</span><span
class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span
class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span
class="s2">"Reshaped:</span>
<span class="s2">", reshaped)</span>
</pre>
</div>
<h3>Example: Array Operations</h3>
<div class="codehilite">
<pre><span></span><span class="n">a</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">array</span><span
class="p">([</span><span class="mi">1</span><span class="p">,</span> <span
class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span
class="p">])</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span
class="o">.</span><span class="n">array</span><span class="p">([</span><span
class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span
class="p">,</span> <span class="mi">6</span><span class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span
class="s2">"Sum:"</span><span class="p">,</span> <span class="n">a</span>
<span class="o">+</span> <span class="n">b</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Dot
Product:"</span><span class="p">,</span> <span class="n">np</span><span
class="o">.</span><span class="n">dot</span><span class="p">(</span><span
class="n">a</span><span class="p">,</span> <span class="n">b</span><span
class="p">))</span>
</pre>
</div>
<h3>Example: Indexing and Slicing</h3>
<div class="codehilite">
<pre><span></span><span class="n">arr</span> <span class="o">=</span> <span
class="n">np</span><span class="o">.</span><span class="n">array</span><span
class="p">([</span><span class="mi">10</span><span class="p">,</span> <span
class="mi">20</span><span class="p">,</span> <span class="mi">30</span><span
class="p">,</span> <span class="mi">40</span><span class="p">])</span>
NumPy Basics Guide
<span class="nb">print</span><span class="p">(</span><span class="s2">"First
Element:"</span><span class="p">,</span> <span class="n">arr</span><span
class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span
class="s2">"Slice:"</span><span class="p">,</span> <span
class="n">arr</span><span class="p">[</span><span class="mi">1</span><span
class="p">:</span><span class="mi">3</span><span class="p">])</span>
</pre>
</div>
<h3>Example: Creating Arrays with Zeros, Ones, and Random</h3>
<div class="codehilite">
<pre><span></span><span class="nb">print</span><span class="p">(</span><span
class="s2">"Zeros:</span>
<span class="s2">", np.zeros((2, 2)))</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Ones:</span>
<span class="s2">", np.ones((2, 2)))</span>
<span class="nb">print</span><span class="p">(</span><span
class="s2">"Random:</span>
<span class="s2">", np.random.rand(2, 2))</span>
</pre>
</div>