<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[NumPy]]></title><description><![CDATA[NumPy]]></description><link>https://numpyy.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 21 Oct 2026 14:32:36 GMT</lastBuildDate><atom:link href="https://numpyy.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Mastering NumPy]]></title><description><![CDATA[NumPy (Numerical Python).It’s the foundation of almost every analytical and ML operation — efficient, powerful, and surprisingly elegant. Let’s go…]]></description><link>https://numpyy.hashnode.dev/mastering-numpy</link><guid isPermaLink="true">https://numpyy.hashnode.dev/mastering-numpy</guid><category><![CDATA[numpy]]></category><category><![CDATA[Python]]></category><category><![CDATA[Data Science]]></category><category><![CDATA[Machine Learning]]></category><dc:creator><![CDATA[M Krunal]]></dc:creator><pubDate>Sun, 02 Nov 2025 07:17:12 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1762062461349/27a0bd19-af84-4c89-a345-b26678a5746b.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-mastering-numpy-from-basics-to-interview-ready">🌟 Mastering NumPy — From Basics to Interview-Ready!</h2>
<hr />
<h2 id="heading-introduction">🧠 Introduction</h2>
<p><strong>NumPy (Numerical Python)</strong>.<br />It’s the foundation of almost every analytical and ML operation — efficient, powerful, and surprisingly elegant.</p>
<p>Let’s go step-by-step with examples.</p>
<hr />
<h2 id="heading-quick-setup-amp-why-numpy">⚙️ Quick Setup &amp; Why NumPy</h2>
<p>Install it easily:</p>
<pre><code>pip install numpy
</code></pre><h3 id="heading-why-numpy">Why NumPy?</h3>
<ul>
<li><p><strong>Efficient, typed arrays (</strong><code>ndarray</code>) — compact and lightning-fast.</p>
</li>
<li><p><strong>Vectorized operations</strong> — no slow Python loops.</p>
</li>
<li><p><strong>Broadcasting rules</strong> — work with arrays of different shapes easily.</p>
</li>
</ul>
<blockquote>
<p>💬 “Why not just use Python lists?”<br />NumPy arrays store <strong>homogeneous types</strong> and support <strong>vectorized math</strong>, making them <strong>faster and memory-efficient</strong> compared to Python lists.</p>
</blockquote>
<hr />
<h2 id="heading-arrays-vs-python-lists">🔸 Arrays vs Python Lists</h2>
<pre><code><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

lst = list(range(<span class="hljs-number">10</span>))
print(lst)

arr = np.arange(<span class="hljs-number">10</span>)
print(arr)
</code></pre><p><strong>Output:</strong></p>
<pre><code>[<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>]
array([<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>])
</code></pre><h3 id="heading-key-differences">Key Differences</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature</td><td>Python List</td><td>NumPy Array</td></tr>
</thead>
<tbody>
<tr>
<td>Data type</td><td>Mixed allowed</td><td>Single dtype</td></tr>
<tr>
<td>Arithmetic</td><td>Requires loops</td><td>Element-wise and fast</td></tr>
<tr>
<td>Memory</td><td>Higher</td><td>Optimized (C-based)</td></tr>
</tbody>
</table>
</div><p><strong>In short:</strong><br />👉 <em>NumPy arrays = speed + simplicity.</em></p>
<hr />
<h2 id="heading-creating-arrays-amp-matrices">🧩 Creating Arrays &amp; Matrices</h2>
<p>Let’s create a <strong>5×5 matrix with values from 1 to 25:</strong></p>
<pre><code>mat1 = np.arange(<span class="hljs-number">1</span>,<span class="hljs-number">26</span>).reshape(<span class="hljs-number">5</span>,<span class="hljs-number">5</span>)
print(mat1)
</code></pre><p><strong>Output:</strong></p>
<pre><code>[[ <span class="hljs-number">1</span>  <span class="hljs-number">2</span>  <span class="hljs-number">3</span>  <span class="hljs-number">4</span>  <span class="hljs-number">5</span>]
 [ <span class="hljs-number">6</span>  <span class="hljs-number">7</span>  <span class="hljs-number">8</span>  <span class="hljs-number">9</span> <span class="hljs-number">10</span>]
 [<span class="hljs-number">11</span> <span class="hljs-number">12</span> <span class="hljs-number">13</span> <span class="hljs-number">14</span> <span class="hljs-number">15</span>]
 [<span class="hljs-number">16</span> <span class="hljs-number">17</span> <span class="hljs-number">18</span> <span class="hljs-number">19</span> <span class="hljs-number">20</span>]
 [<span class="hljs-number">21</span> <span class="hljs-number">22</span> <span class="hljs-number">23</span> <span class="hljs-number">24</span> <span class="hljs-number">25</span>]]
</code></pre><h3 id="heading-quick-concept-arange-vs-linspace">Quick Concept: <code>arange</code> vs <code>linspace</code></h3>
<ul>
<li><p><code>np.arange(start, stop)</code> → steps by 1 (or given step), stop <strong>excluded</strong>.</p>
</li>
<li><p><code>np.linspace(start, stop, num)</code> → divides range into <code>num</code> equal intervals (stop <strong>included</strong>).</p>
</li>
</ul>
<p><strong>Tip:</strong> Mention this difference if asked in an interview — it shows clarity and precision. 😉</p>
<hr />
<h2 id="heading-indexing-amp-slicing">🎯 Indexing &amp; Slicing</h2>
<p>Extract the <strong>middle 3×3</strong> matrix:</p>
<pre><code>mat2 = mat1[<span class="hljs-number">1</span>:<span class="hljs-number">4</span>, <span class="hljs-number">1</span>:<span class="hljs-number">4</span>]
print(mat2)
</code></pre><p><strong>Result:</strong></p>
<pre><code>[[ <span class="hljs-number">7</span>  <span class="hljs-number">8</span>  <span class="hljs-number">9</span>]
 [<span class="hljs-number">12</span> <span class="hljs-number">13</span> <span class="hljs-number">14</span>]
 [<span class="hljs-number">17</span> <span class="hljs-number">18</span> <span class="hljs-number">19</span>]]
</code></pre><h3 id="heading-concept">Concept:</h3>
<ul>
<li><p>Python uses <strong>zero-based indexing</strong>.</p>
</li>
<li><p><code>a:b</code> means <strong>start at a, end before b</strong>.</p>
</li>
</ul>
<p>So, <code>mat1[1:4, 1:4]</code> selects rows <strong>1–3</strong> and columns <strong>1–3</strong>.</p>
<p>💡 <em>Bonus:</em><br />To pick the <strong>center element</strong>, use:</p>
<pre><code>mat1[<span class="hljs-number">2</span>, <span class="hljs-number">2</span>]  # returns <span class="hljs-number">13</span>
</code></pre><hr />
<h2 id="heading-aggregations-sum-amp-mean">📊 Aggregations — Sum &amp; Mean</h2>
<h3 id="heading-sum-of-each-row">Sum of Each Row</h3>
<pre><code>row_sum = np.sum(mat1, axis=<span class="hljs-number">1</span>)
print(<span class="hljs-string">"Row wise sum:"</span>, row_sum)
</code></pre><p>Output:</p>
<pre><code>Row wise sum: [ <span class="hljs-number">15</span>  <span class="hljs-number">40</span>  <span class="hljs-number">65</span>  <span class="hljs-number">90</span> <span class="hljs-number">115</span>]
</code></pre><h3 id="heading-mean-of-each-column">Mean of Each Column</h3>
<pre><code>col_mean = np.mean(mat1, axis=<span class="hljs-number">0</span>)
print(<span class="hljs-string">"Column wise mean:"</span>, col_mean)
</code></pre><p>Output:</p>
<pre><code>Column wise mean: [<span class="hljs-number">11.</span> <span class="hljs-number">12.</span> <span class="hljs-number">13.</span> <span class="hljs-number">14.</span> <span class="hljs-number">15.</span>]
</code></pre><h3 id="heading-understanding-axis">Understanding <code>axis</code></h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Axis</td><td>Operation Direction</td><td>Description</td></tr>
</thead>
<tbody>
<tr>
<td><code>axis=0</code></td><td>↓ (column-wise)</td><td>Collapses rows</td></tr>
<tr>
<td><code>axis=1</code></td><td>→ (row-wise)</td><td>Collapses columns</td></tr>
</tbody>
</table>
</div><p><strong>Note:</strong><br />If you just call <code>mat1.sum()</code> (without axis), it sums <strong>all elements</strong> and returns a scalar.</p>
<hr />
<h2 id="heading-boolean-indexing-conditional-selection">🔍 Boolean Indexing — Conditional Selection</h2>
<p>Let’s find values <strong>greater than 20</strong>:</p>
<pre><code>high_values = mat1[mat1 &gt; <span class="hljs-number">20</span>]
print(<span class="hljs-string">"High values above 20:"</span>, high_values)
</code></pre><p><strong>Output:</strong></p>
<pre><code>High values above <span class="hljs-number">20</span>: [<span class="hljs-number">21</span> <span class="hljs-number">22</span> <span class="hljs-number">23</span> <span class="hljs-number">24</span> <span class="hljs-number">25</span>]
</code></pre><h3 id="heading-how-it-works">How it works</h3>
<ul>
<li><p><code>mat1 &gt; 20</code> → creates a <strong>boolean mask</strong> (True/False values).</p>
</li>
<li><p><code>mat1[mask]</code> → returns only elements where mask is <code>True</code>.</p>
</li>
</ul>
<p>Example mask:</p>
<pre><code>[[False False ...]
 [False False ...]
 [False False ...]
 [False False ...]
 [ True  True  True  True  True]]
</code></pre><p>💬 In simple words:<br />This mask works like a <strong>filter</strong> — wherever the condition is True, those values get selected.<br />Thoda sa logic samajh lo, aur tum boolean indexing ke master ban jaoge 😄</p>
<hr />
<h2 id="heading-common-questions">💬 Common Questions</h2>
<p><strong>Q1:</strong> What is <em>broadcasting</em>?<br /><strong>A:</strong> It’s NumPy’s way of doing arithmetic on arrays of different shapes by automatically expanding them along missing dimensions.</p>
<p><strong>Q2:</strong> How to reshape without copying data?<br /><strong>A:</strong> <code>arr.reshape()</code> returns a <strong>view</strong> if possible; use <code>arr.copy()</code> for a deep copy.</p>
<p><strong>Q3:</strong> How to combine arrays?<br /><strong>A:</strong> Use <code>np.concatenate</code>, <code>np.vstack</code>, or <code>np.hstack</code>.</p>
<p><strong>Q4:</strong> How to get unique elements?<br /><strong>A:</strong> <code>np.unique(arr)</code></p>
<p><strong>Q5:</strong> How to handle <code>NaN</code> values?<br /><strong>A:</strong> Use <code>np.nanmean</code>, <code>np.nansum</code>, or filter them before aggregation.</p>
<hr />
<h2 id="heading-best-practices">🧭 Best Practices</h2>
<p>✅ Use <code>.shape</code> to verify dimensions<br />✅ Check <code>dtype</code> regularly<br />✅ Avoid Python loops — prefer vectorized ops<br />✅ Be careful with <strong>views vs copies</strong><br />✅ Print small samples while debugging</p>
<hr />
<h2 id="heading-final-words">🏁 Final Words</h2>
<p>NumPy is not just a library — it’s the <strong>language of data</strong> in Python.<br />Har din thoda practice, aur jaldi hi aap bolenge — <em>“NumPy? Easy yaar!”</em> 😎</p>
]]></content:encoded></item></channel></rss>