Introduction to NumPy
Learn what NumPy is, why it is important in Python, and how its multidimensional arrays help you perform numerical calculations efficiently.
1. Learning Objectives
By the end of this lesson, you will be able to:
- Define NumPy and explain its purpose.
- Understand the importance of numerical computing in Python.
- Recognize the difference between Python lists and NumPy arrays.
- Understand the basic structure of a NumPy array.
- Identify common applications of NumPy.
- Write a simple Python program using NumPy.
2. What Is NumPy?
NumPy stands for Numerical Python. It is a widely used Python library designed for numerical computing. It provides powerful multidimensional arrays and functions for working with numerical data.
NumPy makes it easier to perform mathematical calculations, process large collections of numbers, manipulate array shapes, calculate statistical values, and perform linear algebra operations.
A Practical Example
Imagine that a teacher wants to calculate the total marks of five students. The marks are 75, 82, 91, 68, and 84. NumPy can store these values in an array and calculate their total and average using built-in functions.
import numpy as np
marks = np.array([75, 82, 91, 68, 84])
total = np.sum(marks)
average = np.mean(marks)
print("Marks:", marks)
print("Total:", total)
print("Average:", average)
Output:
Marks: [75 82 91 68 84] Total: 400 Average: 80.0
import numpy as np imports NumPy and gives it
the commonly used short name np.
3. Why Do We Need NumPy?
Python lists are useful for storing collections of values. However, numerical tasks often require repeated mathematical operations on many values. NumPy provides specialized arrays and operations that make these tasks convenient and efficient.
| Feature | Python List | NumPy Array |
|---|---|---|
| Primary purpose | General-purpose data storage. | Numerical and array-based computing. |
| Element types | Can contain different data types. | Usually stores elements using one common data type. |
| Arithmetic | Element-wise arithmetic usually requires explicit logic. | Supports vectorized element-wise arithmetic. |
| Dimensions | Nested lists can represent multidimensional structures. | Provides explicit multidimensional array support. |
| Typical use | General collections and application logic. | Numerical analysis, matrices, and scientific computing. |
4. Understanding NumPy Arrays
The central data structure in NumPy is the ndarray, which stands for N-dimensional array. It stores elements in a structured arrangement and supports operations across one or more dimensions.
One-Dimensional Array
import numpy as np
numbers = np.array([10, 20, 30, 40])
print(numbers)
print("Dimensions:", numbers.ndim)
print("Shape:", numbers.shape)
print("Size:", numbers.size)
Output:
[10 20 30 40] Dimensions: 1 Shape: (4,) Size: 4
This array has one dimension, contains four elements,
and has a shape of (4,).
Two-Dimensional Array
import numpy as np
matrix = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(matrix)
print("Dimensions:", matrix.ndim)
print("Shape:", matrix.shape)
print("Size:", matrix.size)
Output:
[[1 2 3] [4 5 6]] Dimensions: 2 Shape: (2, 3) Size: 6
This array has two dimensions, two rows, three columns, and six elements in total.
ndim attribute
reports the number of dimensions, shape describes
the size along each dimension, and size reports
the total number of elements.
5. Important Features of NumPy
1. Multidimensional Arrays
NumPy supports one-dimensional arrays, two-dimensional matrices, and arrays with additional dimensions.
2. Vectorized Operations
Many operations can be applied to an entire array without writing an explicit Python loop.
import numpy as np values = np.array([1, 2, 3, 4]) print(values * 2)
Output:
[2 4 6 8]
3. Mathematical Functions
NumPy provides functions for sums, averages, square roots, trigonometric calculations, and many other numerical tasks.
4. Statistical Operations
You can calculate values such as the mean, median, standard deviation, minimum, and maximum.
5. Linear Algebra
NumPy includes operations for matrix multiplication, solving linear systems, and computing eigenvalues.
6. Where Is NumPy Used?
| Field | Example Application |
|---|---|
| Data Analysis | Calculating averages, totals, and summary statistics. |
| Machine Learning | Representing numerical features and performing array calculations. |
| Scientific Computing | Processing experimental measurements and numerical models. |
| Finance | Analyzing numerical time series and financial calculations. |
| Image Processing | Representing image pixels as multidimensional arrays. |
| Statistics | Calculating distributions and descriptive statistics. |
7. Your First NumPy Program
The following program creates an array of product prices, increases every price by 10%, and calculates the average price.
import numpy as np
prices = np.array([100, 200, 300, 400])
updated_prices = prices * 1.10
average_price = np.mean(prices)
print("Original prices:", prices)
print("Updated prices:", updated_prices)
print("Average price:", average_price)
Output:
Original prices: [100 200 300 400] Updated prices: [110. 220. 330. 440.] Average price: 250.0
How the Program Works
import numpy as npimports the NumPy library.np.array()creates an array from the supplied values.prices * 1.10increases each price by 10%.np.mean(prices)calculates the average original price.print()displays the results.
8. Common Beginner Mistakes
Mistake 1: Using NumPy Without Importing It
Calling np.array() before importing NumPy with
the alias np causes a name-related error.
Mistake 2: Confusing Lists and Arrays
Multiplying a Python list by an integer repeats the list, while multiplying a numerical NumPy array by a number multiplies each element.
python_list = [1, 2, 3] print(python_list * 2) import numpy as np numpy_array = np.array([1, 2, 3]) print(numpy_array * 2)
Output:
[1, 2, 3, 1, 2, 3] [2 4 6]
Mistake 3: Assuming Every Array Has the Same Shape
Array operations involving different shapes must satisfy NumPy's broadcasting rules. Not every pair of shapes can be combined directly.
9. Classroom Practical Activity
Activity: Analyze Student Marks
Create a NumPy array containing the marks of five students. Calculate the total, average, highest mark, and lowest mark.
- Create an array containing 72, 85, 91, 64, and 78.
- Display the complete array.
- Calculate the total marks using
np.sum(). - Calculate the average using
np.mean(). - Find the highest and lowest marks.
- Display each result with a meaningful label.
Starter Code
import numpy as np
marks = np.array([72, 85, 91, 64, 78])
# Calculate the total
total = np.sum(marks)
# Calculate the average
average = np.mean(marks)
# Find the highest and lowest marks
highest = np.max(marks)
lowest = np.min(marks)
print("Marks:", marks)
print("Total:", total)
print("Average:", average)
print("Highest:", highest)
print("Lowest:", lowest)
Practice Questions
- What does NumPy stand for?
- What is the purpose of a NumPy array?
- What does
import numpy as npmean? - How does NumPy array multiplication differ from Python list multiplication?
- What is the difference between an array's shape and size?
- Name three areas where NumPy is useful.
- Which NumPy function calculates the average of numerical values?
Summary
NumPy is a Python library for numerical computing. Its main data structure is the multidimensional ndarray, which supports array-based mathematical operations, statistics, and linear algebra.
You learned how to import NumPy, create arrays, perform element-wise calculations, and calculate basic statistical values. You also explored the differences between Python lists and NumPy arrays.