<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shastry on Medium]]></title>
        <description><![CDATA[Stories by Shastry on Medium]]></description>
        <link>https://medium.com/@shastry200417?source=rss-36973ea432d9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*w6hlIHUXq2ACqlrp</url>
            <title>Stories by Shastry on Medium</title>
            <link>https://medium.com/@shastry200417?source=rss-36973ea432d9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 11:04:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shastry200417/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[NumPy Library in Python]]></title>
            <link>https://medium.com/@shastry200417/numpy-library-in-python-41ef86687731?source=rss-36973ea432d9------2</link>
            <guid isPermaLink="false">https://medium.com/p/41ef86687731</guid>
            <dc:creator><![CDATA[Shastry]]></dc:creator>
            <pubDate>Wed, 12 Aug 2026 09:24:12 GMT</pubDate>
            <atom:updated>2026-08-12T09:24:12.611Z</atom:updated>
            <content:encoded><![CDATA[<p>Python is one of the most popular programming languages for data science, machine learning, artificial intelligence, scientific computing, and automation. However, when working with large amounts of numerical data, Python’s built-in lists are often slower and less memory-efficient.</p><p>This is where <strong>NumPy</strong> becomes essential.</p><p>NumPy is one of the core libraries in Python for numerical computing. It provides a powerful data structure called the <strong>ndarray (N-dimensional array)</strong>, which allows developers to perform mathematical operations quickly and efficiently. Because of its speed and simplicity, NumPy serves as the foundation for many popular libraries such as Pandas, Matplotlib, Scikit-learn, TensorFlow, and many others.</p><p>Whether you are analyzing data, building machine learning models, processing images, or performing scientific research, learning NumPy is an important step toward becoming a proficient Python programmer.</p><h3>Introduction to NumPy</h3><h3>What is NumPy?</h3><p><strong>NumPy (Numerical Python)</strong> is an open-source Python library designed for performing numerical and mathematical computations efficiently. It introduces the <strong>ndarray</strong>, a powerful multidimensional array object that stores data in a compact and organized manner.</p><p>Unlike Python lists, NumPy arrays are optimized for speed and memory usage, making them ideal for handling large datasets and performing complex calculations.</p><p><strong>Definition</strong></p><blockquote><strong><em>NumPy is a Python library that provides fast, memory-efficient multidimensional arrays along with a large collection of mathematical functions to perform numerical computations.</em></strong></blockquote><h3>Why is NumPy Used?</h3><p>Python lists are flexible because they can store different data types, but they become slow when dealing with large numerical datasets. NumPy solves this problem by storing elements of the same data type in continuous memory locations, allowing operations to execute much faster.</p><p>NumPy is widely used because it:</p><ul><li>Performs calculations significantly faster than Python lists.</li><li>Consumes less memory.</li><li>Simplifies mathematical operations.</li><li>Supports multidimensional arrays.</li><li>Provides hundreds of built-in mathematical functions.</li><li>Integrates seamlessly with other Python libraries.</li></ul><p>Almost every data science and machine learning project uses NumPy in some way.</p><h3>Installing NumPy</h3><p>If NumPy is not already installed on your system, it can be installed using the following command:</p><pre>pip install numpy</pre><h3>Importing NumPy</h3><p>Before using NumPy, it must be imported into the Python program.</p><pre>import numpy as np</pre><h3>Explanation</h3><p>Here, <strong>numpy</strong> is imported using the alias <strong>np</strong>.</p><p>Instead of writing:</p><pre>numpy.array()</pre><p>we simply write:</p><pre>np.array()</pre><p>Using np is the standard convention followed by Python programmers worldwide.</p><h3>Features and Advantages of NumPy</h3><p>NumPy provides several features that make numerical computing easier and more efficient.</p><h3>1. High-Speed Computation</h3><p>NumPy performs operations much faster than Python lists because its internal implementation is written in optimized C code.</p><h3>Example</h3><pre>import numpy as np</pre><pre>numbers = np.array([10, 20, 30, 40])</pre><pre>print(numbers * 2)</pre><h3>Output</h3><pre>[20 40 60 80]</pre><h3>Explanation</h3><p>Each element in the array is automatically multiplied by <strong>2</strong>.</p><p>There is no need to use a loop because NumPy performs element-wise operations internally.</p><h3>2. Supports Multidimensional Arrays</h3><p>NumPy supports arrays with one or more dimensions.</p><h3>Example</h3><pre>import numpy as np</pre><pre>matrix = np.array([<br>    [1, 2, 3],<br>    [4, 5, 6]<br>])</pre><pre>print(matrix)</pre><h3>Output</h3><pre>[[1 2 3]<br> [4 5 6]]</pre><h3>Explanation</h3><p>This is a <strong>two-dimensional array</strong> containing:</p><ul><li>2 rows</li><li>3 columns</li></ul><p>Higher-dimensional arrays can also be created using NumPy.</p><h3>3. Memory Efficient</h3><p>NumPy stores data in continuous memory locations.</p><p>This provides several advantages:</p><ul><li>Faster data access</li><li>Better performance</li><li>Lower memory consumption</li></ul><p>This is one of the main reasons why NumPy is preferred for handling large datasets.</p><h3>4. Rich Collection of Mathematical Functions</h3><p>NumPy provides numerous built-in functions for:</p><ul><li>Arithmetic operations</li><li>Statistical analysis</li><li>Linear algebra</li><li>Matrix operations</li><li>Random number generation</li><li>Sorting</li><li>Searching</li><li>Trigonometric calculations</li></ul><p>These functions reduce the amount of code developers need to write.</p><h3>5. Easy Arithmetic Operations</h3><p>Mathematical operations can be performed directly on entire arrays.</p><h3>Example</h3><pre>import numpy as np</pre><pre>a = np.array([5, 10, 15])</pre><pre>b = np.array([2, 4, 6])</pre><pre>print(a + b)</pre><h3>Output</h3><pre>[ 7 14 21]</pre><h3>Explanation</h3><p>NumPy performs <strong>element-wise addition</strong>.</p><pre>5 + 2 = 7</pre><pre>10 + 4 = 14</pre><pre>15 + 6 = 21</pre><p>No loops are required.</p><h3>6. Broadcasting</h3><p>Broadcasting allows NumPy to perform operations on arrays of different shapes without manually repeating data.</p><h3>Example</h3><pre>import numpy as np</pre><pre>numbers = np.array([10, 20, 30])</pre><pre>print(numbers + 5)</pre><h3>Output</h3><pre>[15 25 35]</pre><h3>Explanation</h3><p>The value <strong>5</strong> is automatically added to every element of the array.</p><p>Instead of creating another array like [5, 5, 5], NumPy broadcasts the scalar value across all elements.</p><p>This feature reduces both memory usage and coding effort.</p><h3>Advantages of Using NumPy</h3><p>Some of the major advantages of NumPy include:</p><ul><li>Faster execution than Python lists</li><li>Efficient memory utilization</li><li>Simple and clean syntax</li><li>Support for multidimensional arrays</li><li>Hundreds of built-in mathematical functions</li><li>Excellent performance with large datasets</li><li>Easy integration with data science and machine learning libraries</li><li>Open-source and free to use</li><li>Platform independent</li><li>Widely used in industry and research</li></ul><h3>NumPy Arrays</h3><p>The most important feature of NumPy is the <strong>ndarray (N-dimensional Array)</strong>. An ndarray stores multiple values of the same data type in a single object. Unlike Python lists, NumPy arrays are stored in contiguous memory locations, making computations faster and more memory-efficient.</p><p>Arrays can have one dimension, two dimensions, three dimensions, or even higher dimensions depending on the type of data being represented.</p><h3>Creating NumPy Arrays</h3><p>NumPy provides several ways to create arrays depending on the requirement.</p><h3>Creating a One-Dimensional Array</h3><p>A one-dimensional array contains elements arranged in a single row.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30, 40, 50])</pre><pre>print(arr)</pre><h3>Output</h3><pre>[10 20 30 40 50]</pre><h3>Explanation</h3><p>The array() function converts the Python list into a NumPy array containing five elements.</p><h3>Creating a Two-Dimensional Array</h3><p>A two-dimensional array consists of rows and columns.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([<br>    [1, 2, 3],<br>    [4, 5, 6]<br>])</pre><pre>print(arr)</pre><h3>Output</h3><pre>[[1 2 3]<br> [4 5 6]]</pre><h3>Explanation</h3><p>This array contains:</p><ul><li>2 rows</li><li>3 columns</li></ul><p>Two-dimensional arrays are commonly used to represent tables, matrices, spreadsheets, and images.</p><h3>Creating a Three-Dimensional Array</h3><p>A three-dimensional array contains multiple two-dimensional arrays.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([<br>    [[1, 2], [3, 4]],<br>    [[5, 6], [7, 8]]<br>])</pre><pre>print(arr)</pre><h3>Output</h3><pre>[[[1 2]<br>  [3 4]]</pre><pre> [[5 6]<br>  [7 8]]]</pre><h3>Explanation</h3><p>This array contains:</p><ul><li>2 matrices</li><li>Each matrix has 2 rows</li><li>Each row has 2 columns</li></ul><p>Three-dimensional arrays are widely used in image processing, deep learning, and scientific simulations.</p><h3>Creating Arrays Using Built-in Functions</h3><p>NumPy provides many predefined functions to create arrays quickly.</p><h3>Using zeros()</h3><p>The zeros() function creates an array filled entirely with zeros.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.zeros((2, 3))</pre><pre>print(arr)</pre><h3>Output</h3><pre>[[0. 0. 0.]<br> [0. 0. 0.]]</pre><h3>Explanation</h3><p>A 2 × 3 array is created, and every element is initialized to zero.</p><h3>Using ones()</h3><p>The ones() function creates an array where every element is one.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.ones((2, 4))</pre><pre>print(arr)</pre><h3>Output</h3><pre>[[1. 1. 1. 1.]<br> [1. 1. 1. 1.]]</pre><h3>Explanation</h3><p>The array contains 2 rows and 4 columns, with every value equal to one.</p><h3>Using full()</h3><p>The full() function creates an array filled with a specified value.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.full((2, 3), 7)</pre><pre>print(arr)</pre><h3>Output</h3><pre>[[7 7 7]<br> [7 7 7]]</pre><h3>Explanation</h3><p>Every element in the array is initialized with the value <strong>7</strong>.</p><h3>Using arange()</h3><p>The arange() function creates evenly spaced values within a specified range.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.arange(1, 11)</pre><pre>print(arr)</pre><h3>Output</h3><pre>[ 1  2  3  4  5  6  7  8  9 10]</pre><h3>Explanation</h3><p>The numbers start from <strong>1</strong> and continue up to <strong>10</strong>.</p><p>The ending value is <strong>not included</strong> in arange().</p><h3>Using linspace()</h3><p>The linspace() function generates evenly spaced numbers between two values.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.linspace(0, 10, 6)</pre><pre>print(arr)</pre><h3>Output</h3><pre>[ 0.  2.  4.  6.  8. 10.]</pre><h3>Explanation</h3><p>Instead of specifying the step size, we specify the number of values required.</p><p>Here, six equally spaced numbers are generated between 0 and 10.</p><h3>Understanding Array Dimensions</h3><p>The dimension of an array indicates how many axes it has.</p><h3>One-Dimensional Array</h3><pre>import numpy as np</pre><pre>arr = np.array([1, 2, 3])</pre><pre>print(arr.ndim)</pre><h3>Output</h3><pre>1</pre><h3>Explanation</h3><p>The array has only one axis.</p><h3>Two-Dimensional Array</h3><pre>import numpy as np</pre><pre>arr = np.array([[1, 2], [3, 4]])</pre><pre>print(arr.ndim)</pre><h3>Output</h3><pre>2</pre><h3>Explanation</h3><p>The array has two axes:</p><ul><li>Rows</li><li>Columns</li></ul><h3>Three-Dimensional Array</h3><pre>import numpy as np</pre><pre>arr = np.array([<br>    [[1], [2]],<br>    [[3], [4]]<br>])</pre><pre>print(arr.ndim)</pre><h3>Output</h3><pre>3</pre><h3>Explanation</h3><p>The array contains three dimensions, making it suitable for representing complex datasets such as videos or RGB images.</p><h3>Understanding the Shape of an Array</h3><p>The shape attribute returns the size of each dimension.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([<br>    [10, 20, 30],<br>    [40, 50, 60]<br>])</pre><pre>print(arr.shape)</pre><h3>Output</h3><pre>(2, 3)</pre><h3>Explanation</h3><p>The output (2, 3) means:</p><ul><li>2 rows</li><li>3 columns</li></ul><p>The shape always describes the structure of the array.</p><h3>Difference Between Dimension and Shape</h3><p>Many beginners confuse <strong>dimension</strong> and <strong>shape</strong>, but they represent different concepts.</p><p><strong>Dimension (</strong><strong>ndim)</strong> tells us <strong>how many axes</strong> an array has.</p><p><strong>Shape (</strong><strong>shape)</strong> tells us <strong>how many elements are present along each axis</strong>.</p><p>For example, consider the following array:</p><pre>import numpy as np</pre><pre>arr = np.array([<br>    [1, 2, 3],<br>    [4, 5, 6]<br>])</pre><pre>print(arr.ndim)<br>print(arr.shape)</pre><h3>Output</h3><pre>2<br>(2, 3)</pre><h3>Explanation</h3><ul><li>ndim returns <strong>2</strong> because the array has two dimensions.</li><li>shape returns <strong>(2, 3)</strong> because there are two rows and three columns.</li></ul><p>Although these attributes are related, they provide different information about an array’s structure.</p><h3>Data Types (dtype)</h3><p>Every NumPy array has a <strong>data type</strong>, commonly referred to as <strong>dtype</strong>. It specifies the type of values stored in the array, such as integers, floating-point numbers, strings, or Boolean values.</p><p>Unlike Python lists, a NumPy array stores elements of the <strong>same data type</strong>, which improves both performance and memory efficiency.</p><h3>Finding the Data Type of an Array</h3><p>The dtype attribute returns the data type of the elements stored in an array.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30, 40])</pre><pre>print(arr.dtype)</pre><h3>Output</h3><pre>int64</pre><h3>Explanation</h3><p>The array contains only integer values, so NumPy assigns the data type <strong>int64</strong> (or <strong>int32</strong> depending on the operating system).</p><h3>Array with Floating-Point Numbers</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([1.5, 2.8, 3.9])</pre><pre>print(arr.dtype)</pre><h3>Output</h3><pre>float64</pre><h3>Explanation</h3><p>Since every value contains a decimal point, NumPy stores the elements as floating-point numbers.</p><h3>Array with String Values</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([&quot;Apple&quot;, &quot;Banana&quot;, &quot;Orange&quot;])</pre><pre>print(arr.dtype)</pre><h3>Output</h3><pre>&lt;U6</pre><h3>Explanation</h3><p>&lt;U6 indicates a Unicode string with a maximum length of six characters.</p><p>The exact output may vary depending on the length of the strings.</p><h3>Array with Boolean Values</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([True, False, True])</pre><pre>print(arr.dtype)</pre><h3>Output</h3><pre>bool</pre><h3>Explanation</h3><p>Since the array contains only Boolean values, its data type is bool.</p><h3>Specifying the Data Type Explicitly</h3><p>NumPy allows us to specify the desired data type while creating an array.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([1, 2, 3], dtype=float)</pre><pre>print(arr)<br>print(arr.dtype)</pre><h3>Output</h3><pre>[1. 2. 3.]<br>float64</pre><h3>Explanation</h3><p>Although the original values are integers, NumPy converts them into floating-point numbers because dtype=float was specified.</p><h3>Converting One Data Type to Another</h3><p>The astype() function converts an array from one data type to another.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30])</pre><pre>new_arr = arr.astype(float)</pre><pre>print(new_arr)<br>print(new_arr.dtype)</pre><h3>Output</h3><pre>[10. 20. 30.]<br>float64</pre><h3>Explanation</h3><p>The original integer array is converted into a floating-point array without changing the values.</p><h3>Array Attributes</h3><p>NumPy provides several useful attributes that describe an array’s structure and memory usage.</p><p>Understanding these attributes helps in analyzing arrays and writing efficient programs.</p><h3>shape</h3><p>The shape attribute returns the number of rows and columns in an array.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([<br>    [1, 2, 3],<br>    [4, 5, 6]<br>])</pre><pre>print(arr.shape)</pre><h3>Output</h3><pre>(2, 3)</pre><h3>Explanation</h3><p>The array contains:</p><ul><li>2 rows</li><li>3 columns</li></ul><h3>size</h3><p>The size attribute returns the total number of elements in an array.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([<br>    [1, 2, 3],<br>    [4, 5, 6]<br>])</pre><pre>print(arr.size)</pre><h3>Output</h3><pre>6</pre><h3>Explanation</h3><p>There are six elements in total.</p><pre>1 2 3<br>4 5 6</pre><p>Total elements = <strong>6</strong></p><h3>ndim</h3><p>The ndim attribute returns the number of dimensions (axes) in an array.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([<br>    [1, 2],<br>    [3, 4]<br>])</pre><pre>print(arr.ndim)</pre><h3>Output</h3><pre>2</pre><h3>Explanation</h3><p>Since the array has rows and columns, it is two-dimensional.</p><h3>itemsize</h3><p>The itemsize attribute returns the memory occupied by one element in bytes.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30], dtype=np.int32)</pre><pre>print(arr.itemsize)</pre><h3>Output</h3><pre>4</pre><h3>Explanation</h3><p>Each integer occupies <strong>4 bytes</strong> of memory because the array uses the int32 data type.</p><h3>nbytes</h3><p>The nbytes attribute returns the total memory occupied by the entire array.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30], dtype=np.int32)</pre><pre>print(arr.nbytes)</pre><h3>Output</h3><pre>12</pre><h3>Explanation</h3><p>Each element occupies <strong>4 bytes</strong>, and there are <strong>3 elements</strong>.</p><pre>3 × 4 = 12 bytes</pre><h3>T</h3><p>The T attribute returns the transpose of an array by interchanging rows and columns.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([<br>    [1, 2, 3],<br>    [4, 5, 6]<br>])</pre><pre>print(arr.T)</pre><h3>Output</h3><pre>[[1 4]<br> [2 5]<br> [3 6]]</pre><h3>Explanation</h3><p>Rows become columns, and columns become rows.</p><h3>Summary of Common Array Attributes</h3><p>The following attributes are frequently used while working with NumPy arrays:</p><ul><li><strong>shape</strong> – Returns the dimensions of the array.</li><li><strong>size</strong> – Returns the total number of elements.</li><li><strong>ndim</strong> – Returns the number of dimensions.</li><li><strong>dtype</strong> – Returns the data type of the elements.</li><li><strong>itemsize</strong> – Returns the memory occupied by one element (in bytes).</li><li><strong>nbytes</strong> – Returns the total memory occupied by the array.</li><li><strong>T</strong> – Returns the transpose of the array.</li></ul><p>These attributes provide valuable information about an array’s structure, storage, and memory usage, making them essential tools when working with NumPy.</p><h3>Array Operations</h3><p>One of the biggest advantages of NumPy is its ability to perform mathematical operations on entire arrays without using loops. These operations are called <strong>vectorized operations</strong>, making the code shorter, easier to read, and significantly faster.</p><p>NumPy supports arithmetic operations, comparison operations, broadcasting, and Boolean indexing, all of which are essential for numerical computing.</p><h3>Arithmetic Operations</h3><p>Arithmetic operations are performed <strong>element by element</strong> when two arrays have the same shape.</p><h3>Addition</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr1 = np.array([10, 20, 30])</pre><pre>arr2 = np.array([1, 2, 3])</pre><pre>print(arr1 + arr2)</pre><h3>Output</h3><pre>[11 22 33]</pre><h3>Explanation</h3><p>Each element in the first array is added to the corresponding element in the second array.</p><pre>10 + 1 = 11</pre><pre>20 + 2 = 22</pre><pre>30 + 3 = 33</pre><h3>Subtraction</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr1 = np.array([20, 40, 60])</pre><pre>arr2 = np.array([5, 10, 15])</pre><pre>print(arr1 - arr2)</pre><h3>Output</h3><pre>[15 30 45]</pre><h3>Explanation</h3><p>Each element in the second array is subtracted from the corresponding element in the first array.</p><h3>Multiplication</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr1 = np.array([2, 4, 6])</pre><pre>arr2 = np.array([3, 5, 7])</pre><pre>print(arr1 * arr2)</pre><h3>Output</h3><pre>[ 6 20 42]</pre><h3>Explanation</h3><p>NumPy multiplies the corresponding elements of both arrays.</p><pre>2 × 3 = 6</pre><pre>4 × 5 = 20</pre><pre>6 × 7 = 42</pre><h3>Division</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr1 = np.array([20, 40, 60])</pre><pre>arr2 = np.array([2, 4, 6])</pre><pre>print(arr1 / arr2)</pre><h3>Output</h3><pre>[10. 10. 10.]</pre><h3>Explanation</h3><p>Each element in the first array is divided by the corresponding element in the second array.</p><h3>Exponentiation</h3><p>The ** operator raises each element to the specified power.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([2, 3, 4])</pre><pre>print(arr ** 2)</pre><h3>Output</h3><pre>[ 4  9 16]</pre><h3>Explanation</h3><p>Every element is squared.</p><pre>2² = 4</pre><pre>3² = 9</pre><pre>4² = 16</pre><h3>Modulus</h3><p>The modulus operator returns the remainder after division.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 15, 22])</pre><pre>print(arr % 2)</pre><h3>Output</h3><pre>[0 1 0]</pre><h3>Explanation</h3><ul><li>10 is divisible by 2.</li><li>15 leaves a remainder of 1.</li><li>22 is divisible by 2.</li></ul><h3>Broadcasting</h3><p>Broadcasting is one of NumPy’s most powerful features. It allows arrays of different shapes to participate in arithmetic operations without manually expanding the smaller array.</p><p>Instead of creating duplicate values, NumPy automatically stretches the smaller array whenever possible.</p><h3>Broadcasting with a Scalar</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([5, 10, 15])</pre><pre>print(arr + 10)</pre><h3>Output</h3><pre>[15 20 25]</pre><h3>Explanation</h3><p>NumPy automatically adds <strong>10</strong> to every element.</p><p>Internally, it behaves as if the operation were:</p><pre>[5, 10, 15]</pre><pre>+</pre><pre>[10, 10, 10]</pre><p>without actually creating the second array.</p><h3>Broadcasting Between Arrays</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr1 = np.array([<br>    [1],<br>    [2],<br>    [3]<br>])</pre><pre>arr2 = np.array([10, 20, 30])</pre><pre>print(arr1 + arr2)</pre><h3>Output</h3><pre>[[11 21 31]<br> [12 22 32]<br> [13 23 33]]</pre><h3>Explanation</h3><p>The first array has a shape of (3,1).</p><p>The second array has a shape of (3,).</p><p>NumPy automatically broadcasts both arrays into compatible shapes before performing the addition.</p><h3>Comparison Operations</h3><p>Comparison operators compare elements and return Boolean values.</p><h3>Greater Than (&gt;)</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 25, 30, 15])</pre><pre>print(arr &gt; 20)</pre><h3>Output</h3><pre>[False  True  True False]</pre><h3>Explanation</h3><p>Each element is checked individually.</p><p>Only the values greater than 20 return True.</p><h3>Equal To (==)</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([5, 10, 5, 20])</pre><pre>print(arr == 5)</pre><h3>Output</h3><pre>[ True False  True False]</pre><h3>Explanation</h3><p>NumPy checks whether each element is equal to 5.</p><h3>Boolean Indexing</h3><p>Boolean indexing allows us to select elements based on a condition instead of their positions.</p><p>This is one of the most useful techniques in data analysis because it helps filter data quickly.</p><h3>Selecting Elements Greater Than a Value</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([12, 35, 18, 50, 22])</pre><pre>result = arr[arr &gt; 20]</pre><pre>print(result)</pre><h3>Output</h3><pre>[35 50 22]</pre><h3>Explanation</h3><p>The condition arr &gt; 20 creates a Boolean array.</p><pre>[False True False True True]</pre><p>Only the elements corresponding to True are returned.</p><h3>Selecting Even Numbers</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([11, 14, 17, 20, 25, 28])</pre><pre>print(arr[arr % 2 == 0])</pre><h3>Output</h3><pre>[14 20 28]</pre><h3>Explanation</h3><p>The expression arr % 2 == 0 checks whether each number is divisible by 2.</p><p>Only the even numbers are selected.</p><h3>Selecting Negative Numbers</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([-5, 10, -8, 20, -2])</pre><pre>print(arr[arr &lt; 0])</pre><h3>Output</h3><pre>[-5 -8 -2]</pre><h3>Explanation</h3><p>Only elements less than zero satisfy the condition, so only negative numbers are returned.</p><h3>Practical Use Case</h3><p>Suppose a teacher wants to find students who scored more than 75 marks.</p><h3>Example</h3><pre>import numpy as np</pre><pre>marks = np.array([55, 82, 67, 91, 74, 88])</pre><pre>top_students = marks[marks &gt; 75]</pre><pre>print(top_students)</pre><h3>Output</h3><pre>[82 91 88]</pre><h3>Explanation</h3><p>Boolean indexing filters the marks and returns only those students who scored above 75.</p><p>This technique is widely used in data analysis, machine learning, finance, healthcare, and business analytics for filtering large datasets efficiently.</p><h3>Indexing and Slicing</h3><p>Indexing and slicing are fundamental operations in NumPy that allow us to access or extract specific elements from an array. Whether working with one-dimensional, two-dimensional, or higher-dimensional arrays, these techniques make it easy to retrieve the required data efficiently.</p><p>NumPy supports four main types of indexing:</p><ul><li>Basic Indexing</li><li>Slicing</li><li>Fancy Indexing</li><li>Boolean Indexing</li></ul><p>Understanding these concepts is essential because they are frequently used in data analysis, machine learning, image processing, and scientific computing.</p><h3>Basic Indexing</h3><p>Basic indexing is used to access a single element from an array by specifying its position (index).</p><p>In NumPy, indexing starts from <strong>0</strong>, just like Python lists.</p><h3>Accessing Elements in a One-Dimensional Array</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30, 40, 50])</pre><pre>print(arr[0])<br>print(arr[2])<br>print(arr[4])</pre><h3>Output</h3><pre>10<br>30<br>50</pre><h3>Explanation</h3><p>The elements are stored at the following positions:</p><pre>Index :   0   1   2   3   4</pre><pre>Value :  10  20  30  40  50</pre><ul><li>arr[0] returns the first element.</li><li>arr[2] returns the third element.</li><li>arr[4] returns the fifth element.</li></ul><h3>Negative Indexing</h3><p>Negative indexing starts from the end of the array.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30, 40, 50])</pre><pre>print(arr[-1])<br>print(arr[-2])<br>print(arr[-5])</pre><h3>Output</h3><pre>50<br>40<br>10</pre><h3>Explanation</h3><p>The positions are counted from right to left.</p><pre>Index :  -5  -4  -3  -2  -1</pre><pre>Value :  10  20  30  40  50</pre><p>Negative indexing is useful when the position from the beginning is unknown but the position from the end is known.</p><h3>Accessing Elements in a Two-Dimensional Array</h3><p>To access elements in a two-dimensional array, specify the <strong>row index</strong> followed by the <strong>column index</strong>.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([<br>    [10, 20, 30],<br>    [40, 50, 60],<br>    [70, 80, 90]<br>])</pre><pre>print(arr[0, 1])<br>print(arr[2, 2])</pre><h3>Output</h3><pre>20<br>90</pre><h3>Explanation</h3><ul><li>arr[0,1] means first row, second column.</li><li>arr[2,2] means third row, third column.</li></ul><h3>Accessing Elements in a Three-Dimensional Array</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([<br>    [[1, 2], [3, 4]],<br>    [[5, 6], [7, 8]]<br>])</pre><pre>print(arr[1, 0, 1])</pre><h3>Output</h3><pre>6</pre><h3>Explanation</h3><p>The indices represent:</p><ul><li>First index → Matrix</li><li>Second index → Row</li><li>Third index → Column</li></ul><p>So arr[1,0,1] accesses the value <strong>6</strong>.</p><h3>Slicing</h3><p>Slicing extracts multiple elements from an array.</p><p>The general syntax is:</p><pre>array[start : stop : step]</pre><p>Where:</p><ul><li>start → Starting index (included)</li><li>stop → Ending index (excluded)</li><li>step → Interval between elements</li></ul><h3>Basic Slicing</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30, 40, 50, 60])</pre><pre>print(arr[1:5])</pre><h3>Output</h3><pre>[20 30 40 50]</pre><h3>Explanation</h3><p>The slice starts at index <strong>1</strong> and ends before index <strong>5</strong>.</p><h3>Omitting the Starting Index</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30, 40, 50])</pre><pre>print(arr[:3])</pre><h3>Output</h3><pre>[10 20 30]</pre><h3>Explanation</h3><p>When the starting index is omitted, slicing begins from index <strong>0</strong>.</p><h3>Omitting the Ending Index</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30, 40, 50])</pre><pre>print(arr[2:])</pre><h3>Output</h3><pre>[30 40 50]</pre><h3>Explanation</h3><p>The slice starts at index <strong>2</strong> and continues until the end of the array.</p><h3>Using Step Size</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30, 40, 50, 60])</pre><pre>print(arr[::2])</pre><h3>Output</h3><pre>[10 30 50]</pre><h3>Explanation</h3><p>The step size of <strong>2</strong> means every second element is selected.</p><h3>Slicing a Two-Dimensional Array</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([<br>    [10, 20, 30],<br>    [40, 50, 60],<br>    [70, 80, 90]<br>])</pre><pre>print(arr[0:2, 1:3])</pre><h3>Output</h3><pre>[[20 30]<br> [50 60]]</pre><h3>Explanation</h3><ul><li>Rows selected: 0 and 1</li><li>Columns selected: 1 and 2</li></ul><p>The resulting array contains only the specified rows and columns.</p><h3>Fancy Indexing</h3><p>Fancy indexing allows us to access multiple elements simultaneously by providing a list or array of indices.</p><p>Unlike normal indexing, the indices do not have to be consecutive.</p><h3>Fancy Indexing in a One-Dimensional Array</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30, 40, 50])</pre><pre>print(arr[[0, 2, 4]])</pre><h3>Output</h3><pre>[10 30 50]</pre><h3>Explanation</h3><p>NumPy returns the elements at indices <strong>0</strong>, <strong>2</strong>, and <strong>4</strong>.</p><h3>Fancy Indexing in a Two-Dimensional Array</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([<br>    [10, 20],<br>    [30, 40],<br>    [50, 60]<br>])</pre><pre>print(arr[[0, 2]])</pre><h3>Output</h3><pre>[[10 20]<br> [50 60]]</pre><h3>Explanation</h3><p>The first and third rows are selected.</p><p>Fancy indexing is useful when specific rows or columns need to be retrieved.</p><h3>Boolean Indexing</h3><p>Boolean indexing filters elements based on a condition.</p><p>Instead of specifying index positions, we specify a logical condition, and NumPy returns only the elements that satisfy that condition.</p><h3>Selecting Values Greater Than 50</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([25, 40, 55, 70, 90])</pre><pre>print(arr[arr &gt; 50])</pre><h3>Output</h3><pre>[55 70 90]</pre><h3>Explanation</h3><p>The condition arr &gt; 50 creates a Boolean array.</p><pre>[False False True True True]</pre><p>Only the elements corresponding to True are returned.</p><h3>Selecting Even Numbers</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([11, 14, 17, 20, 25, 28])</pre><pre>print(arr[arr % 2 == 0])</pre><h3>Output</h3><pre>[14 20 28]</pre><h3>Explanation</h3><p>The condition checks whether each number is divisible by <strong>2</strong>.</p><p>Only the even numbers satisfy the condition and are returned.</p><h3>Basic Indexing vs Fancy Indexing vs Boolean Indexing</h3><p>Understanding the difference between these indexing methods helps in choosing the right approach for different situations.</p><p><strong>Basic Indexing</strong></p><ul><li>Accesses one element at a time using its position.</li><li>Best suited for retrieving individual values.</li></ul><p><strong>Fancy Indexing</strong></p><ul><li>Accesses multiple elements at once using a list or array of indices.</li><li>Best suited for selecting specific rows, columns, or elements that are not consecutive.</li></ul><p><strong>Boolean Indexing</strong></p><ul><li>Selects elements based on a logical condition.</li><li>Best suited for filtering data, such as finding values greater than a threshold or selecting even numbers.</li></ul><p>Each indexing method has its own purpose, and together they provide powerful tools for accessing and manipulating data efficiently in NumPy.</p><h3>Array Manipulation</h3><p>In real-world applications, data rarely comes in the exact format required for analysis. Before performing calculations or building machine learning models, we often need to change the shape, size, or arrangement of arrays.</p><p>NumPy provides several built-in functions for manipulating arrays efficiently. These functions help us reorganize data without manually creating new arrays.</p><p>Some of the most commonly used array manipulation functions are:</p><ul><li>reshape()</li><li>resize()</li><li>flatten()</li><li>ravel()</li><li>transpose()</li><li>concatenate()</li><li>stack()</li><li>split()</li></ul><p>Let’s understand each function in detail.</p><h3>reshape()</h3><p>The reshape() function changes the shape of an array <strong>without changing its data</strong>.</p><p>The total number of elements must remain the same before and after reshaping.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([1, 2, 3, 4, 5, 6])</pre><pre>new_arr = arr.reshape(2, 3)</pre><pre>print(new_arr)</pre><h3>Output</h3><pre>[[1 2 3]<br> [4 5 6]]</pre><h3>Explanation</h3><p>The original array has <strong>6 elements</strong>.</p><p>The new shape (2,3) also requires <strong>6 elements</strong>, so the reshape operation is successful.</p><h3>Another Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30, 40, 50, 60])</pre><pre>print(arr.reshape(3, 2))</pre><h3>Output</h3><pre>[[10 20]<br> [30 40]<br> [50 60]]</pre><h3>Explanation</h3><p>The data remains unchanged.</p><p>Only the arrangement of rows and columns changes.</p><h3>resize()</h3><p>The resize() function changes both the <strong>shape</strong> and <strong>size</strong> of an array.</p><p>If the new size is larger than the original array, NumPy repeats existing elements.</p><p>If the new size is smaller, extra elements are removed.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([1, 2, 3, 4])</pre><pre>arr.resize((2, 3))</pre><pre>print(arr)</pre><h3>Output</h3><pre>[[1 2 3]<br> [4 0 0]]</pre><h3>Explanation</h3><p>The original array had only four elements.</p><p>The new shape requires six elements.</p><p>NumPy fills the remaining positions with zeros.</p><h3>Increasing the Size Using np.resize()</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([1, 2, 3])</pre><pre>new_arr = np.resize(arr, 8)</pre><pre>print(new_arr)</pre><h3>Output</h3><pre>[1 2 3 1 2 3 1 2]</pre><h3>Explanation</h3><p>Since eight elements are required, NumPy repeats the original values until the requested size is reached.</p><h3>reshape() vs resize()</h3><p>Although both functions change the arrangement of an array, they behave differently.</p><h3>reshape()</h3><ul><li>Changes only the shape.</li><li>Total number of elements must remain the same.</li><li>Does not repeat or remove elements.</li><li>Returns a reshaped array.</li></ul><h3>resize()</h3><ul><li>Changes both shape and size.</li><li>Can increase or decrease the number of elements.</li><li>May repeat values or fill with zeros depending on how it is used.</li><li>Can modify the original array.</li><li><strong>Use </strong><strong>reshape() when only the layout needs to change.</strong></li></ul><p><strong>Use </strong><strong>resize() when the actual size of the array must change.</strong></p><h3>flatten()</h3><p>The flatten() function converts a multidimensional array into a one-dimensional array.</p><p>It always returns a <strong>copy</strong> of the original array.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([<br>    [1, 2],<br>    [3, 4]<br>])</pre><pre>print(arr.flatten())</pre><h3>Output</h3><pre>[1 2 3 4]</pre><h3>Explanation</h3><p>The two-dimensional array becomes a one-dimensional array.</p><p>The original array remains unchanged because flatten() creates a copy.</p><h3>ravel()</h3><p>The ravel() function also converts an array into one dimension.</p><p>However, it returns a <strong>view</strong> whenever possible instead of creating a new copy.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([<br>    [1, 2],<br>    [3, 4]<br>])</pre><pre>print(arr.ravel())</pre><h3>Output</h3><pre>[1 2 3 4]</pre><h3>Explanation</h3><p>The output is the same as flatten().</p><p>However, ravel() usually shares memory with the original array, making it more memory efficient.</p><h3>flatten() vs ravel()</h3><p>Both functions convert multidimensional arrays into one-dimensional arrays.</p><h3>flatten()</h3><ul><li>Returns a copy.</li><li>Original array is unaffected.</li><li>Uses more memory.</li><li>ravel()</li><li>Returns a view whenever possible.</li><li>Shares memory with the original array.</li><li>Faster and more memory efficient.</li><li>If you do not want changes to affect the original array, use flatten().</li></ul><p>If performance is important and modifying the original array is acceptable, use ravel().</p><h3>transpose()</h3><p>The transpose() function exchanges rows and columns.</p><p>It is commonly used in matrix operations and machine learning.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([<br>    [1, 2, 3],<br>    [4, 5, 6]<br>])</pre><pre>print(arr.transpose())</pre><h3>Output</h3><pre>[[1 4]<br> [2 5]<br> [3 6]]</pre><h3>Explanation</h3><p>Rows become columns, and columns become rows.</p><p>The shape changes from (2,3) to (3,2).</p><h3>concatenate()</h3><p>The concatenate() function joins two or more arrays along an existing axis.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr1 = np.array([1, 2, 3])</pre><pre>arr2 = np.array([4, 5, 6])</pre><pre>result = np.concatenate((arr1, arr2))</pre><pre>print(result)</pre><h3>Output</h3><pre>[1 2 3 4 5 6]</pre><h3>Explanation</h3><p>The two arrays are combined into a single larger array.</p><h3>stack()</h3><p>The stack() function joins arrays by creating a <strong>new axis</strong>.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr1 = np.array([1, 2, 3])</pre><pre>arr2 = np.array([4, 5, 6])</pre><pre>print(np.stack((arr1, arr2)))</pre><h3>Output</h3><pre>[[1 2 3]<br> [4 5 6]]</pre><h3>Explanation</h3><p>Unlike concatenate(), stack() creates an additional dimension.</p><p>The resulting array has two rows.</p><h3>concatenate() vs stack()</h3><p>Although both functions combine arrays, they work differently.</p><h3>concatenate()</h3><ul><li>Joins arrays along an existing axis.</li><li>Does not create a new dimension.</li><li>stack()</li><li>Joins arrays by creating a new axis.</li><li>Increases the number of dimensions.</li></ul><p>Use concatenate() when extending an existing array.</p><p>Use stack() when grouping arrays together into a higher-dimensional structure.</p><h3>split()</h3><p>The split() function divides an array into multiple smaller arrays.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30, 40, 50, 60])</pre><pre>result = np.split(arr, 3)</pre><pre>print(result)</pre><h3>Output</h3><pre>[array([10, 20]), array([30, 40]), array([50, 60])]</pre><h3>Explanation</h3><p>The original array is divided into three equal parts.</p><p>Each part contains two elements.</p><h3>Practical Use Case</h3><p>Suppose a company stores the monthly sales of six products.</p><pre>import numpy as np</pre><pre>sales = np.array([120, 150, 180, 200, 220, 250])</pre><pre>monthly_sales = sales.reshape(2, 3)</pre><pre>print(monthly_sales)</pre><h3>Output</h3><pre>[[120 150 180]<br> [200 220 250]]</pre><h3>Explanation</h3><p>Instead of storing all values in a single row, the sales data is reorganized into two rows and three columns, making it easier to analyze, visualize, and process.</p><p>Array manipulation functions like reshape(), transpose(), concatenate(), and split() are extensively used in data preprocessing, machine learning pipelines, image processing, and scientific computing because they allow data to be rearranged efficiently without manual effort.</p><h3>Statistical and Mathematical Functions</h3><p>NumPy provides a rich collection of statistical and mathematical functions that help analyze data quickly and efficiently. Instead of writing lengthy loops, a single NumPy function can calculate averages, sums, standard deviations, variances, minimum values, maximum values, and much more.</p><p>These functions are widely used in data science, machine learning, finance, engineering, healthcare, and scientific research.</p><p>Some of the most commonly used statistical and mathematical functions are:</p><h3>sum()</h3><p>The sum() function calculates the total sum of all elements in an array.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30, 40])</pre><pre>print(np.sum(arr))</pre><h3>Output</h3><pre>100</pre><h3>Explanation</h3><p>The function adds all the elements in the array.</p><pre>10 + 20 + 30 + 40 = 100</pre><h3>mean()</h3><p>The mean() function calculates the average of all elements.</p><h3>Formula</h3><pre>Mean = Sum of Elements / Number of Elements</pre><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30, 40])</pre><pre>print(np.mean(arr))</pre><h3>Output</h3><pre>25.0</pre><h3>Explanation</h3><pre>(10 + 20 + 30 + 40) / 4 = 25</pre><p>The average value of the array is <strong>25.0</strong>.</p><h3>median()</h3><p>The median() function returns the middle value after sorting the data.</p><p>If the number of elements is even, it returns the average of the two middle values.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([5, 2, 8, 1, 9])</pre><pre>print(np.median(arr))</pre><h3>Output</h3><pre>5.0</pre><h3>Explanation</h3><p>After sorting:</p><pre>[1, 2, 5, 8, 9]</pre><p>The middle value is <strong>5</strong>.</p><h3>Example with Even Number of Elements</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30, 40])</pre><pre>print(np.median(arr))</pre><h3>Output</h3><pre>25.0</pre><h3>Explanation</h3><p>The sorted array is:</p><pre>[10, 20, 30, 40]</pre><p>The two middle values are <strong>20</strong> and <strong>30</strong>.</p><pre>(20 + 30) / 2 = 25</pre><h3>std()</h3><p>The std() function calculates the <strong>standard deviation</strong>, which measures how much the values are spread around the mean.</p><p>A smaller standard deviation indicates that the values are close to the average, while a larger standard deviation indicates greater variation.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30, 40])</pre><pre>print(np.std(arr))</pre><h3>Output</h3><pre>11.180339887498949</pre><h3>Explanation</h3><p>The standard deviation measures the spread of the dataset.</p><p>Since the values are fairly spread apart, the standard deviation is approximately <strong>11.18</strong>.</p><h3>var()</h3><p>The var() function calculates the <strong>variance</strong> of the dataset.</p><p>Variance is the square of the standard deviation.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30, 40])</pre><pre>print(np.var(arr))</pre><h3>Output</h3><pre>125.0</pre><h3>Explanation</h3><p>Variance indicates how much the values differ from the average.</p><p>Higher variance means the data is more spread out.</p><h3>min()</h3><p>The min() function returns the smallest element in the array.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([25, 10, 50, 15])</pre><pre>print(np.min(arr))</pre><h3>Output</h3><pre>10</pre><h3>Explanation</h3><p>Among all the values, <strong>10</strong> is the smallest.</p><h3>max()</h3><p>The max() function returns the largest element in the array.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([25, 10, 50, 15])</pre><pre>print(np.max(arr))</pre><h3>Output</h3><pre>50</pre><h3>Explanation</h3><p>The largest value present in the array is <strong>50</strong>.</p><h3>argmin()</h3><p>The argmin() function returns the <strong>index</strong> of the smallest element.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([25, 10, 50, 15])</pre><pre>print(np.argmin(arr))</pre><h3>Output</h3><pre>1</pre><h3>Explanation</h3><p>The smallest value is <strong>10</strong>, which is located at index <strong>1</strong>.</p><h3>argmax()</h3><p>The argmax() function returns the <strong>index</strong> of the largest element.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([25, 10, 50, 15])</pre><pre>print(np.argmax(arr))</pre><h3>Output</h3><pre>2</pre><h3>Explanation</h3><p>The largest value is <strong>50</strong>, which is located at index <strong>2</strong>.</p><h3>Sorting Functions</h3><p>Sorting arranges elements in ascending order by default.</p><p>The sort() function returns a sorted array.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([40, 10, 30, 20])</pre><pre>print(np.sort(arr))</pre><h3>Output</h3><pre>[10 20 30 40]</pre><h3>Explanation</h3><p>The elements are rearranged from the smallest value to the largest value.</p><h3>Sorting a Two-Dimensional Array</h3><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([<br>    [3, 1, 2],<br>    [6, 5, 4]<br>])</pre><pre>print(np.sort(arr))</pre><h3>Output</h3><pre>[[1 2 3]<br> [4 5 6]]</pre><h3>Explanation</h3><p>Each row is sorted independently.</p><h3>Searching Functions</h3><p>NumPy provides several searching functions to locate elements efficiently.</p><h3>where()</h3><p>The where() function returns the indices of elements that satisfy a condition.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 25, 30, 25, 50])</pre><pre>print(np.where(arr == 25))</pre><h3>Output</h3><pre>(array([1, 3]),)</pre><h3>Explanation</h3><p>The value <strong>25</strong> appears at indices <strong>1</strong> and <strong>3</strong>.</p><h3>searchsorted()</h3><p>The searchsorted() function returns the position where a value should be inserted to maintain the sorted order.</p><h3>Example</h3><pre>import numpy as np</pre><pre>arr = np.array([10, 20, 30, 40, 50])</pre><pre>print(np.searchsorted(arr, 35))</pre><h3>Output</h3><pre>3</pre><h3>Explanation</h3><p>The value <strong>35</strong> should be inserted at index <strong>3</strong> to keep the array sorted.</p><h3>Practical Use Case</h3><p>Suppose a teacher wants to analyze the marks scored by students in a class.</p><h3>Example</h3><pre>import numpy as np</pre><pre>marks = np.array([75, 82, 91, 68, 88])</pre><pre>print(&quot;Average Marks:&quot;, np.mean(marks))<br>print(&quot;Highest Marks:&quot;, np.max(marks))<br>print(&quot;Lowest Marks:&quot;, np.min(marks))</pre><h3>Output</h3><pre>Average Marks: 80.8<br>Highest Marks: 91<br>Lowest Marks: 68</pre><h3>Explanation</h3><p>Using just a few NumPy functions, the teacher can quickly determine:</p><ul><li>The average performance of the class.</li><li>The highest score.</li><li>The lowest score.</li><li>In real-world scenarios, these functions are extensively used to analyze business sales, financial records, healthcare reports, student performance, scientific experiments, and machine learning datasets.</li></ul><h3>Real-World Applications of NumPy</h3><p>NumPy is much more than just an array library. It forms the backbone of the Python ecosystem for scientific computing and is used in almost every field that involves numerical data.</p><p>From analyzing millions of customer records to training artificial intelligence models, NumPy provides the speed, efficiency, and mathematical capabilities required to process data effectively.</p><p>The following are some of the most common real-world applications of NumPy.</p><h3>1. Data Science and Data Analysis</h3><p>Data scientists use NumPy to clean, organize, and analyze large datasets before drawing meaningful insights.</p><p>NumPy makes it easy to perform calculations such as averages, sums, filtering, sorting, and statistical analysis on millions of records.</p><h3>Example</h3><pre>import numpy as np</pre><pre>sales = np.array([1200, 1500, 1800, 2100])</pre><pre>print(&quot;Average Sales:&quot;, np.mean(sales))<br>print(&quot;Total Sales:&quot;, np.sum(sales))</pre><h3>Output</h3><pre>Average Sales: 1650.0<br>Total Sales: 6600</pre><h3>Explanation</h3><p>A business can quickly calculate the average and total sales without writing lengthy loops.</p><h3>2. Machine Learning</h3><p>Machine learning algorithms require numerical data for training models.</p><p>Libraries such as <strong>Scikit-learn</strong>, <strong>TensorFlow</strong>, and <strong>PyTorch</strong> use NumPy arrays for storing datasets and performing mathematical computations efficiently.</p><h3>Example</h3><pre>import numpy as np</pre><pre>features = np.array([<br>    [170, 65],<br>    [180, 72],<br>    [160, 55]<br>])</pre><pre>print(features.shape)</pre><h3>Output</h3><pre>(3, 2)</pre><h3>Explanation</h3><p>The dataset contains:</p><ul><li>3 observations</li><li>2 features</li><li>This structured data can be directly used for training machine learning models.</li></ul><h3>3. Artificial Intelligence</h3><p>Artificial intelligence systems perform millions of mathematical operations every second.</p><p>NumPy provides optimized array operations that help AI applications process numerical information efficiently.</p><p>Examples include:</p><ul><li>Face recognition</li><li>Speech recognition</li><li>Recommendation systems</li><li>Chatbots</li><li>Self-driving vehicles</li></ul><h3>4. Image Processing</h3><p>Digital images are represented as multidimensional arrays.</p><p>Each pixel stores numerical values representing colors or brightness.</p><p>NumPy enables developers to manipulate these arrays for various image processing tasks.</p><h3>Example</h3><pre>import numpy as np</pre><pre>image = np.array([<br>    [120, 140],<br>    [180, 200]<br>])</pre><pre>print(image)</pre><h3>Output</h3><pre>[[120 140]<br> [180 200]]</pre><h3>Explanation</h3><p>Each number represents the intensity of a pixel.</p><p>Image processing libraries such as OpenCV use NumPy arrays extensively.</p><h3>5. Scientific Computing</h3><p>Scientists and engineers use NumPy for solving complex mathematical problems involving:</p><ul><li>Linear algebra</li><li>Differential equations</li><li>Matrix calculations</li><li>Numerical simulations</li><li>These calculations would be much slower using normal Python lists.</li></ul><h3>6. Financial Analysis</h3><p>Banks and financial institutions analyze enormous amounts of numerical data every day.</p><p>NumPy helps perform calculations such as:</p><ul><li>Profit analysis</li><li>Investment returns</li><li>Risk analysis</li><li>Stock market predictions</li><li>Portfolio optimization</li></ul><h3>Example</h3><pre>import numpy as np</pre><pre>profits = np.array([25000, 28000, 30000, 27000])</pre><pre>print(&quot;Maximum Profit:&quot;, np.max(profits))</pre><h3>Output</h3><pre>Maximum Profit: 30000</pre><h3>Explanation</h3><p>Businesses can quickly identify their highest profit using NumPy.</p><h3>7. Healthcare and Medical Research</h3><p>Hospitals and research organizations generate massive amounts of patient data.</p><p>NumPy is used to analyze:</p><ul><li>Blood test reports</li><li>Medical images</li><li>Patient statistics</li><li>Disease prediction datasets</li><li>Clinical research data</li></ul><p>Its speed makes large-scale medical analysis much more efficient.</p><h3>8. Weather Forecasting</h3><p>Weather forecasting systems process millions of numerical measurements collected from satellites and weather stations.</p><p>NumPy helps analyze:</p><ul><li>Temperature</li><li>Humidity</li><li>Rainfall</li><li>Wind speed</li><li>Atmospheric pressure</li></ul><p>These calculations support accurate weather prediction models.</p><h3>9. Robotics</h3><p>Robots rely on mathematical calculations for navigation and movement.</p><p>NumPy is commonly used for:</p><ul><li>Sensor data processing</li><li>Motion planning</li><li>Position calculations</li><li>Robot vision</li><li>Path optimization</li></ul><h3>10. Game Development</h3><p>Modern games perform numerous mathematical operations involving:</p><ul><li>Character movement</li><li>Collision detection</li><li>Physics simulations</li><li>Animation</li><li>Object transformations</li></ul><p>NumPy simplifies these calculations and improves performance.</p><h3>Why NumPy is Preferred Over Python Lists</h3><p>Although Python lists are useful for storing general data, they are not designed for intensive numerical computation.</p><p>NumPy provides several advantages:</p><ul><li>Faster execution due to optimized internal implementation.</li><li>Lower memory consumption.</li><li>Support for multidimensional arrays.</li><li>Efficient vectorized operations.</li><li>Hundreds of built-in mathematical functions.</li><li>Easy integration with libraries such as Pandas, Matplotlib, TensorFlow, and Scikit-learn.</li></ul><p>Because of these benefits, NumPy has become the standard library for numerical computing in Python.</p><h3>Conclusion</h3><p>NumPy is one of the most fundamental and widely used libraries in Python. It provides powerful multidimensional arrays and a comprehensive collection of mathematical, statistical, and array manipulation functions that make numerical computing simple, efficient, and highly optimized.</p><p>Throughout this article, we explored the core concepts of NumPy, including array creation, dimensions, data types, array attributes, indexing, slicing, arithmetic operations, broadcasting, Boolean indexing, array manipulation techniques, statistical functions, mathematical operations, sorting, searching, and real-world applications. Each of these concepts plays an essential role in solving practical problems involving data.</p><p>Whether you are a beginner learning Python or an aspiring data scientist, mastering NumPy is an important step toward understanding advanced libraries such as Pandas, Matplotlib, Scikit-learn, TensorFlow, and PyTorch. A strong foundation in NumPy not only improves programming efficiency but also prepares you for real-world projects in data science, machine learning, artificial intelligence, finance, healthcare, engineering, and scientific research.</p><p>By learning NumPy, you gain the ability to work with large datasets efficiently, write cleaner code, and perform complex numerical computations with ease. This is why NumPy continues to be one of the most essential libraries in the Python ecosystem and a must-learn tool for every Python programmer.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41ef86687731" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>