Lecture 1 / 30
```html
Topic 01 - Unit I - Introduction to NumPy

What is NumPy?

Syllabus Topic Template
Lecture Template

NumPy (Numerical Python) is a powerful, open-source Python library used extensively in data science, engineering, artificial intelligence, machine learning, scientific computing, statistics, and mathematics. It provides support for large, multi-dimensional arrays and matrices, along with a vast collection of high-level mathematical functions that allow you to perform complex calculations efficiently.

Before NumPy was created, Python programmers mainly relied on lists for storing collections of data. Although Python lists are flexible and easy to use, they are not optimized for numerical calculations. NumPy was specifically designed to solve this problem by providing a much faster and more efficient way to work with numbers.

Today, NumPy is considered one of the most important libraries in the Python ecosystem. Many other popular libraries such as Pandas, SciPy, Matplotlib, Scikit-learn, and even some deep learning frameworks rely on NumPy internally for performing mathematical operations.

Why Learn NumPy?

If you plan to work in fields such as data science, machine learning, artificial intelligence, finance, engineering, or scientific research, learning NumPy is essential. It serves as the foundation for numerical computing in Python.

  • Perform mathematical calculations much faster than regular Python code.
  • Store large amounts of numerical data efficiently.
  • Work with one-dimensional, two-dimensional, and multi-dimensional datasets.
  • Carry out statistical calculations with minimal code.
  • Create data that can easily be visualized using plotting libraries.
  • Prepare datasets for machine learning algorithms.
💡 Real-World Example

Imagine a school stores the marks of 100,000 students. Performing calculations such as finding the average score, highest mark, lowest mark, or standard deviation using Python lists can take considerably longer. NumPy performs these operations much faster because its arrays are optimized for numerical processing.

Why Use NumPy Over Python Lists?

In standard Python, we use lists to store collections of values. While lists are flexible and can contain different types of data, they become inefficient when working with very large datasets or performing mathematical operations repeatedly.

NumPy arrays are specifically designed for numerical computation. Since every element in a NumPy array has the same data type and is stored in continuous memory locations, calculations become much faster and require less memory.

  • Speed: NumPy arrays can be up to 50 times faster than ordinary Python lists for numerical operations.
  • Memory Efficiency: NumPy stores data in contiguous memory blocks, reducing memory usage and improving processing speed.
  • Built-in Mathematical Functions: Hundreds of mathematical, statistical, and scientific functions are already available.
  • Easy Calculations: You can perform operations on an entire array without writing loops.
  • Scalability: NumPy easily handles thousands or even millions of values efficiently.
Python List vs NumPy Array
Python List NumPy Array
Can store different data types Stores one data type for maximum efficiency
Slower for mathematical operations Highly optimized for calculations
Consumes more memory Uses memory efficiently
Requires loops for many operations Supports vectorized operations without loops
💡 Did You Know?

The core of NumPy is written mainly in C and C++. These low-level languages execute much faster than pure Python, allowing NumPy to perform millions of calculations in a very short time.

Although you write NumPy code using Python syntax, many of the heavy mathematical computations are actually performed by highly optimized C/C++ code behind the scenes.

Installing NumPy

Before using NumPy, you must install it on your computer. If you are using Anaconda or Jupyter Notebook, NumPy is usually pre-installed. Otherwise, you can install it using Python's package manager.

Terminal
pip install numpy
Note

After installation, you only need to import NumPy once at the beginning of your Python program using the import statement.

Importing NumPy

NumPy is almost always imported using the alias np. The alias makes your code shorter and follows the standard convention used by Python programmers around the world.

main.py
import numpy as np
Why Use "np"?

Writing np.array() is much shorter and cleaner than writing numpy.array() every time. Nearly every NumPy tutorial, book, and professional project uses this naming convention.

The NumPy Array Object

The most important object in NumPy is the ndarray, which stands for N-dimensional array. An ndarray is a collection of elements arranged in one or more dimensions.

Every element inside an ndarray has the same data type, making memory usage predictable and allowing the processor to perform calculations much more efficiently than with Python lists.

Arrays can contain one dimension (like a list), two dimensions (like a table), or multiple dimensions (used in machine learning, image processing, and scientific simulations).

💡 Remember

Think of an ndarray as a smarter and faster version of a Python list that is specially built for mathematical operations.

Example: Your First NumPy Array

main.py
# Import the library (commonly aliased as 'np')
import numpy as np

# Create an ndarray from a Python list
arr = np.array([1, 2, 3, 4, 5])

print("Array:", arr)
print("Type:", type(arr))
Explanation
  • import numpy as np imports the NumPy library.
  • np.array() converts a Python list into a NumPy array.
  • The variable arr stores the newly created ndarray.
  • print(arr) displays the values inside the array.
  • type(arr) confirms that the object is a NumPy ndarray.
Output
Array: [1 2 3 4 5]
Type: <class 'numpy.ndarray'>
        
💡 Observation

Notice that the printed array does not contain commas between the numbers. This is the standard display format used by NumPy arrays and is perfectly normal.

Summary

  • NumPy stands for Numerical Python.
  • It is the most widely used numerical computing library in Python.
  • NumPy arrays are faster and more memory-efficient than Python lists.
  • The main object in NumPy is the ndarray.
  • NumPy is used extensively in data science, machine learning, artificial intelligence, engineering, and scientific computing.
  • Most Python data science libraries are built on top of NumPy.
Practice Task

Open your Python environment or a Jupyter Notebook and complete the following exercises:

  • Import the numpy library as np.
  • Create a NumPy array containing the numbers 10 through 50.
  • Print the array.
  • Verify its type using Python's type() function.
  • Create another NumPy array containing five decimal numbers.
  • Create an array containing your five favorite numbers.
  • Observe how NumPy displays arrays compared to Python lists.
```
Topic 02 - Unit I - Introduction to NumPy

Installation & Setup

Syllabus Topic
Installing and Configuring NumPy

Before using NumPy for numerical computing and data analysis, it must be installed and properly configured in your Python environment. NumPy is distributed as a Python package and can be installed using Python's package manager, pip.

NumPy is one of the most important libraries in the Python ecosystem. Many popular libraries such as Pandas, Scikit-Learn, TensorFlow, Matplotlib, and SciPy are built on top of NumPy. Therefore, learning how to install and configure NumPy correctly is the first step toward becoming proficient in Data Science, Machine Learning, and Scientific Computing.


Prerequisites for NumPy Installation

Before installing NumPy, ensure that Python is already installed on your system.

You can verify your Python installation by opening the terminal or command prompt and executing the following command:

python --version

If Python is installed successfully, the system will display the installed Python version.

Example Output:

Python 3.12.2

It is recommended to use Python 3.x because modern versions of NumPy are optimized for current Python releases.


Checking pip Installation

pip is Python's package manager and is used to download, install, update, and remove Python libraries.

To check whether pip is available, run:

pip --version

Example Output:

pip 24.0 from ...

If pip is not installed, it can usually be installed by updating Python or using Python's installation tools.


Installing NumPy Using pip

The simplest way to install NumPy is through pip.

Execute the following command:

pip install numpy

The package manager will automatically download and install the latest stable version of NumPy along with its dependencies.

After installation, a message similar to the following may appear:

Successfully installed numpy
Important Note

Always install packages from trusted sources such as the official Python Package Index (PyPI) to ensure security and compatibility.


Installing a Specific Version of NumPy

In professional projects, developers sometimes need a specific version of NumPy to maintain compatibility with other libraries.

To install a particular version:

pip install numpy==1.26.4

This command installs version 1.26.4 instead of the latest release.


Verifying the Installation

After installation, it is important to verify that NumPy is working correctly.

Open the Python interpreter:

python

Then execute:

import numpy as np

print(np.__version__)
        

If NumPy is installed correctly, Python will display the installed version number.

Example:

2.0.1

This confirms that NumPy has been installed successfully.


Installing NumPy in Jupyter Notebook

Many Data Scientists use Jupyter Notebook for interactive coding and experimentation.

If NumPy is not available in Jupyter Notebook, install it using:

!pip install numpy
        

The exclamation mark allows terminal commands to be executed directly inside notebook cells.


Installing NumPy Using Anaconda

Anaconda is a popular Python distribution used for Data Science and Machine Learning.

NumPy is often included by default with Anaconda. However, it can also be installed manually using:

conda install numpy

Conda automatically manages dependencies and package compatibility.


Importing NumPy

After installation, NumPy must be imported into Python programs before its functions can be used.

The standard convention is:

import numpy as np
        

Here, np acts as an alias for NumPy and is widely used by developers around the world.

Instead of writing:

numpy.array()
        

We can simply write:

np.array()
        

This makes code shorter and easier to read.


Your First NumPy Program

After importing NumPy, let's create a simple array.

import numpy as np

numbers = np.array([10, 20, 30, 40, 50])

print(numbers)
        

Output:

[10 20 30 40 50]
        

This is your first NumPy array. Arrays are the core data structure of NumPy and form the foundation for all future operations.


Common Installation Errors

1. ModuleNotFoundError

ModuleNotFoundError: No module named 'numpy'
        

This error occurs when NumPy has not been installed in the current Python environment.

Solution:

pip install numpy

2. pip Not Recognized

'pip' is not recognized as an internal or external command
        

This usually indicates that Python or pip is not added to the system PATH.

Reinstall Python and ensure the "Add Python to PATH" option is selected during installation.

3. Version Compatibility Issues

Some older projects require older NumPy versions.

In such cases, install the required version using:

pip install numpy==version_number
        

Best Practices for Environment Setup

  • Always use the latest stable Python version.
  • Keep NumPy updated for performance improvements and bug fixes.
  • Use virtual environments to isolate project dependencies.
  • Verify installation immediately after setup.
  • Follow the standard import convention import numpy as np.

Why NumPy is Essential

NumPy provides high-performance multidimensional arrays and mathematical functions that are significantly faster than traditional Python lists.

It serves as the foundation for many advanced fields including:

  • Machine Learning
  • Artificial Intelligence
  • Data Science
  • Scientific Computing
  • Deep Learning
  • Computer Vision
  • Data Visualization

Mastering NumPy installation and setup ensures that you are ready to explore powerful numerical computing techniques in the upcoming lectures.


Key Takeaways

  • NumPy is installed using the pip package manager.
  • The command pip install numpy installs the latest version.
  • The library is typically imported using import numpy as np.
  • Installation should always be verified after setup.
  • NumPy arrays form the foundation of numerical computing in Python.
  • Many popular Data Science and Machine Learning libraries depend on NumPy.
Topic 03 - Unit I - Introduction to NumPy

Creating Ndarrays

Syllabus Topic
Introduction to NumPy Arrays

The core data structure of NumPy is the ndarray, which stands for N-Dimensional Array. An ndarray is a collection of elements stored in a fixed-size grid. Unlike Python lists, NumPy arrays are optimized for numerical operations and provide significantly better performance.

Every data science, machine learning, and scientific computing project that uses NumPy relies heavily on ndarrays. Therefore, understanding how to create arrays is one of the most important skills when learning NumPy.

Key Concept

An ndarray can store data in one dimension, two dimensions, or multiple dimensions while allowing fast mathematical operations on entire datasets.

Creating an Array from a Python List

The simplest way to create an ndarray is by converting an existing Python list into a NumPy array using the array() function.

import numpy as np

numbers = np.array([10, 20, 30, 40, 50])

print(numbers)
        

Output:

[10 20 30 40 50]
        

NumPy automatically converts the list into an ndarray object that supports efficient mathematical computations.

Creating a Two-Dimensional Array

A two-dimensional array can be created by passing a list of lists to the array() function.

import numpy as np

matrix = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])

print(matrix)
        

Output:

[[1 2 3]
 [4 5 6]
 [7 8 9]]
        

This structure resembles a table with rows and columns and is commonly used for datasets.

Understanding Array Dimensions

NumPy arrays can have different dimensions.

  • 1D Array: Single row of values.
  • 2D Array: Rows and columns.
  • 3D Array: Multiple matrices stacked together.
  • N-Dimensional Array: Higher-dimensional structures.

You can check the number of dimensions using the ndim attribute.

arr = np.array([[1,2],[3,4]])

print(arr.ndim)
        

Output:

2
        
Creating Arrays with Zeros

Sometimes we need an array initialized with zeros. NumPy provides the zeros() function for this purpose.

import numpy as np

arr = np.zeros(5)

print(arr)
        

Output:

[0. 0. 0. 0. 0.]
        

To create a two-dimensional zero matrix:

arr = np.zeros((3,4))
        

This creates 3 rows and 4 columns filled with zeros.

Creating Arrays with Ones

The ones() function creates arrays where every element is initialized to 1.

import numpy as np

arr = np.ones(5)

print(arr)
        

Output:

[1. 1. 1. 1. 1.]
        

This function is useful when creating default values for calculations and machine learning models.

Creating Arrays with a Range of Values

NumPy provides the arange() function for generating sequences of numbers.

import numpy as np

arr = np.arange(1, 11)

print(arr)
        

Output:

[1 2 3 4 5 6 7 8 9 10]
        

The syntax follows:

np.arange(start, stop, step)
        

Example:

np.arange(0, 20, 2)
        

Output:

[0 2 4 6 8 10 12 14 16 18]
        
Creating Arrays Using linspace()

The linspace() function generates evenly spaced values between two numbers.

import numpy as np

arr = np.linspace(0, 100, 5)

print(arr)
        

Output:

[  0.  25.  50.  75. 100.]
        

This function is frequently used in scientific computing and graph plotting.

Creating Identity Matrices

An identity matrix is a square matrix in which all diagonal elements are 1 and all other elements are 0.

import numpy as np

arr = np.eye(4)

print(arr)
        

Output:

[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
        

Identity matrices are widely used in linear algebra and machine learning algorithms.

Creating Random Arrays

NumPy includes functions for generating random numbers.

Random Decimal Values

import numpy as np

arr = np.random.rand(5)

print(arr)
        

Random Integer Values

arr = np.random.randint(1, 100, 5)

print(arr)
        

Random arrays are commonly used for simulations, testing, and machine learning experiments.

Specifying Data Types

NumPy allows developers to explicitly define the data type of array elements.

import numpy as np

arr = np.array([1, 2, 3], dtype=float)

print(arr)
        

Output:

[1. 2. 3.]
        

Common data types include:

  • int
  • float
  • bool
  • complex
  • str
Comparing NumPy Arrays and Python Lists
Feature Python List NumPy Array
Speed Slower Faster
Memory Usage Higher Lower
Mathematical Operations Limited Optimized
Multi-Dimensional Support Basic Excellent
Key Takeaways
  • ndarray is the primary data structure in NumPy.
  • Arrays can be created from Python lists using np.array().
  • zeros() creates arrays filled with zeros.
  • ones() creates arrays filled with ones.
  • arange() generates sequences of values.
  • linspace() generates evenly spaced numbers.
  • eye() creates identity matrices.
  • Random arrays can be generated using NumPy's random module.
  • NumPy arrays are faster and more efficient than Python lists.
Topic 04 - Unit I - Introduction to NumPy

Array Dimensions & Shapes

Syllabus Topic
Placeholder

Content for Array Dimensions & Shapes goes here.

Topic 05 - Unit I - Introduction to NumPy

Array Indexing & Slicing

Syllabus Topic
Placeholder

Content for Array Indexing & Slicing goes here.

Topic 06 - Unit I - Introduction to NumPy

NumPy Data Types

Syllabus Topic
Placeholder

Content for NumPy Data Types goes here.

Topic 07 - Unit II - Advanced NumPy Operations

Copy vs View

Syllabus Topic
Placeholder

Content for Copy vs View goes here.

Topic 08 - Unit II - Advanced NumPy Operations

Array Reshaping

Syllabus Topic
Placeholder

Content for Array Reshaping goes here.

Topic 09 - Unit II - Advanced NumPy Operations

Iterating Arrays

Syllabus Topic
Placeholder

Content for Iterating Arrays goes here.

Topic 10 - Unit II - Advanced NumPy Operations

Joining & Splitting Arrays

Syllabus Topic
Placeholder

Content for Joining & Splitting Arrays goes here.

Topic 11 - Unit II - Advanced NumPy Operations

Searching & Sorting

Syllabus Topic
Placeholder

Content for Searching & Sorting goes here.

Topic 12 - Unit II - Advanced NumPy Operations

Random Numbers in NumPy

Syllabus Topic
Placeholder

Content for Random Numbers in NumPy goes here.

Topic 13 - Unit II - Advanced NumPy Operations

Universal Functions (ufuncs)

Syllabus Topic
Placeholder

Content for Universal Functions (ufuncs) goes here.

Topic 14 - Unit III - Introduction to Pandas

Pandas Overview

Syllabus Topic
Placeholder

Content for Pandas Overview goes here.

Topic 15 - Unit III - Introduction to Pandas

Pandas Series

Syllabus Topic
Placeholder

Content for Pandas Series goes here.

Topic 16 - Unit III - Introduction to Pandas

Pandas DataFrames

Syllabus Topic
Placeholder

Content for Pandas DataFrames goes here.

Topic 17 - Unit III - Introduction to Pandas

Reading CSV Files

Syllabus Topic
Placeholder

Content for Reading CSV Files goes here.

Topic 18 - Unit III - Introduction to Pandas

Reading JSON Files

Syllabus Topic
Placeholder

Content for Reading JSON Files goes here.

Topic 19 - Unit III - Introduction to Pandas

Analyzing Data (head, tail, info)

Syllabus Topic
Placeholder

Content for Analyzing Data (head, tail, info) goes here.

Topic 20 - Unit IV - Data Cleaning

Data Cleaning Overview

Syllabus Topic
Placeholder

Content for Data Cleaning Overview goes here.

Topic 21 - Unit IV - Data Cleaning

Handling Missing Data (Empty Cells)

Syllabus Topic
Placeholder

Content for Handling Missing Data (Empty Cells) goes here.

Topic 22 - Unit IV - Data Cleaning

Cleaning Wrong Formats

Syllabus Topic
Placeholder

Content for Cleaning Wrong Formats goes here.

Topic 23 - Unit IV - Data Cleaning

Fixing Wrong Data

Syllabus Topic
Placeholder

Content for Fixing Wrong Data goes here.

Topic 24 - Unit IV - Data Cleaning

Removing Duplicates

Syllabus Topic
Placeholder

Content for Removing Duplicates goes here.

Topic 25 - Unit V - Advanced Pandas & Analysis

Data Correlations

Syllabus Topic
Placeholder

Content for Data Correlations goes here.

Topic 26 - Unit V - Advanced Pandas & Analysis

Plotting with Pandas

Syllabus Topic
Placeholder

Content for Plotting with Pandas goes here.

Topic 27 - Unit V - Advanced Pandas & Analysis

GroupBy Operations

Syllabus Topic
Placeholder

Content for GroupBy Operations goes here.

Topic 28 - Unit V - Advanced Pandas & Analysis

Merging & Concatenating

Syllabus Topic
Placeholder

Content for Merging & Concatenating goes here.

Topic 29 - Unit V - Advanced Pandas & Analysis

Pivot Tables

Syllabus Topic
Placeholder

Content for Pivot Tables goes here.

Topic 30 - Unit V - Advanced Pandas & Analysis

Time Series Analysis

Syllabus Topic
Placeholder

Content for Time Series Analysis goes here.