Recap of NumPy Library (CBSE Class 12 Artificial Intelligence)
Class 12 · Artificial Intelligence
Recap of NumPy Library
Before building Artificial Intelligence (AI) and Machine Learning (ML) models, it is important to understand how numerical data is stored and processed efficiently. In Class XI, you learned the basics of the NumPy library. In this section, we will revise those concepts and understand why NumPy is one of the most important Python libraries used in AI.
NumPy provides a powerful multidimensional array object and a collection of mathematical functions that allow developers to perform numerical computations quickly and efficiently. Almost every Machine Learning and Data Science library is built on top of NumPy.
Learning Objectives
- Understand the purpose of the NumPy library.
- Learn about NumPy arrays and their dimensions.
- Create one-dimensional and multidimensional arrays.
- Use common NumPy functions.
- Understand the role of NumPy in Artificial Intelligence.
What is NumPy?
NumPy stands for Numerical Python. It is an open-source Python library specially designed for numerical computing and scientific calculations.
NumPy is a Python library that provides fast multidimensional arrays and a large collection of mathematical functions for numerical computation.
Why Do We Need NumPy?
Python lists are useful for storing data, but they become slow when performing calculations on large datasets. Artificial Intelligence applications often deal with thousands or millions of data records. NumPy makes these calculations much faster by storing data efficiently and providing optimized mathematical operations.
Advantages of NumPy
- Faster than Python lists.
- Consumes less memory.
- Supports multidimensional arrays.
- Provides hundreds of mathematical functions.
- Performs statistical calculations easily.
- Supports matrix operations.
- Forms the foundation of Machine Learning libraries.
Applications of NumPy in Artificial Intelligence
| Application | Use of NumPy |
|---|---|
| Machine Learning | Stores and processes training data. |
| Computer Vision | Represents images as matrices. |
| Natural Language Processing | Processes numerical text representations. |
| Robotics | Performs mathematical computations. |
| Data Analytics | Calculates statistical measures. |
Importing the NumPy Library
Before using NumPy, it must be imported into the Python program.
import numpy as np
Here, np is an alias (short name) for the NumPy library.
Understanding NumPy Arrays
The primary data structure provided by NumPy is the ndarray (N-dimensional array). Unlike Python lists, NumPy arrays store elements of the same data type and provide faster mathematical operations.
Types of Arrays
| Type | Description | Example |
|---|---|---|
| One-Dimensional Array | Contains a single row of elements. | [10, 20, 30] |
| Two-Dimensional Array | Contains rows and columns. | [[10,20],[30,40]] |
| Three-Dimensional Array | Contains multiple matrices. | 3D Dataset |
Creating a One-Dimensional Array
import numpy as np
marks = np.array([80,85,90,95])
print(marks)
Output
[80 85 90 95]
Creating a Two-Dimensional Array
import numpy as np
marks = np.array([[80,85],
[90,95]])
print(marks)
Output
[[80 85]
[90 95]]
Rank (Dimension) of an Array
According to the CBSE AI handbook, the number of dimensions of an array is called its Rank.
| Array | Rank |
|---|---|
| [1,2,3] | 1 |
| [[1,2],[3,4]] | 2 |
| Three-dimensional array | 3 |
Finding the Dimension of an Array
import numpy as np
arr = np.array([[10,20],[30,40]])
print(arr.ndim)
Output
2
Common NumPy Functions
| Function | Purpose |
|---|---|
| sum() | Returns the sum of elements. |
| mean() | Calculates the average. |
| median() | Returns the median value. |
| std() | Calculates standard deviation. |
| min() | Returns the smallest value. |
| max() | Returns the largest value. |
| shape | Returns rows and columns. |
| ndim | Returns the dimension (rank). |
Example Program
import numpy as np
marks = np.array([75,82,91,88,95])
print("Marks =", marks)
print("Sum =", np.sum(marks))
print("Average =", np.mean(marks))
print("Highest =", np.max(marks))
print("Lowest =", np.min(marks))
Output
Marks = [75 82 91 88 95]
Sum = 431
Average = 86.2
Highest = 95
Lowest = 75
Dry Run
| Step | Statement | Result |
|---|---|---|
| 1 | Create array | [75,82,91,88,95] |
| 2 | np.sum() | 431 |
| 3 | np.mean() | 86.2 |
| 4 | np.max() | 95 |
| 5 | np.min() | 75 |
Real-Life Example
A school wants to calculate the average marks of 5,000 students. Using NumPy arrays, all marks can be stored efficiently, and functions like mean(), max(), and min() can calculate statistics within seconds.
Think Like an AI Engineer
An autonomous vehicle continuously receives millions of sensor readings every minute. Which Python library should be used to perform high-speed numerical calculations on this data?
Click to View Answer
NumPy, because it is optimized for fast numerical computation and efficient array processing.
Competency-Based Question
A weather department collects temperature readings from thousands of sensors every hour. Explain why NumPy is more suitable than Python lists for storing and analyzing this data.
Common Beginner Mistakes
- Forgetting to import NumPy before using its functions.
- Using Python lists instead of NumPy arrays for mathematical computations.
- Confusing the
shapeattribute withndim. - Using different data types within the same NumPy array.
Quick Revision
- NumPy = Numerical Python.
- Main data structure = ndarray.
- Supports multidimensional arrays.
- Used for numerical and statistical computations.
- Foundation of Machine Learning and Artificial Intelligence.
Memory Trick
Exam Tips
- Remember that the number of dimensions of an array is called its Rank.
- Practice creating one-dimensional and two-dimensional arrays.
- Know the purpose of commonly used NumPy functions.
- Understand why NumPy is preferred over Python lists for numerical computation.
Summary
- NumPy is a powerful Python library for numerical computing.
- It provides fast multidimensional arrays and optimized mathematical functions.
- NumPy arrays consume less memory and execute faster than Python lists.
- It is widely used in Artificial Intelligence, Machine Learning, Data Science, and Scientific Computing.
- NumPy serves as the foundation for many advanced Python libraries used in AI.