Introduction to NumPy
NumPy is one of the most important libraries in Python for numerical and scientific computing. It allows you to work with large amounts of data efficiently and makes complicated mathematical operations extremely fast. NumPy is used in data science, machine learning, simulations, financial analysis, and any situation where numbers are processed in bulk.
What Is NumPy?
NumPy stands for Numerical Python. It introduces a powerful data structure called the ndarray, which is faster and more efficient than Python lists. NumPy is optimized in C, meaning operations on arrays run much faster than traditional Python loops.
Installing NumPy
NumPy is not included in Python by default. Install it using pip:
pip install numpy
Importing NumPy
Once installed, import it using the alias np, which is the universal standard
across tutorials and projects.
import numpy as np
Creating NumPy Arrays
NumPy arrays can store multiple values efficiently. They support mathematical operations directly on the array, which makes them extremely useful. Below is the simplest way to create an array from a Python list.
import numpy as np
arr = np.array([10, 20, 30, 40])
print(arr)
Why Arrays Are Better Than Lists
NumPy arrays offer several advantages:
- Faster processing because operations run in optimized C code.
- Less memory usage compared to Python lists.
- Vectorized operations allow math on entire arrays at once.
These benefits make NumPy the foundation for most modern data tools and libraries.
Array Types (ndarray)
All NumPy arrays are of type ndarray, which stands for "N-dimensional array".
This means arrays can be 1D, 2D, 3D, or even higher depending on the problem you are solving.
Multi-dimensional arrays are especially useful in machine learning.
Basic Array Operations
NumPy allows mathematical operations on entire arrays without writing loops. This makes code shorter, cleaner, and much faster.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # addition
print(a * b) # multiplication
print(a ** 2) # power
print(a + 10) # broadcast addition
Indexing and Slicing
NumPy arrays support all the slicing features of Python lists, but with greater power. You can extract ranges, reverse arrays, and select elements effortlessly. This is vital when working with large datasets.
arr = np.array([5, 10, 15, 20, 25])
print(arr[0]) # first element
print(arr[1:4]) # slice
print(arr[-1]) # last element
2D Arrays (Matrices)
NumPy can create and manipulate matrices, which are 2D arrays. These are essential for machine learning, linear algebra, and image processing. A 2D array contains rows and columns similar to a spreadsheet.
matrix = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(matrix)
Array Shape
The shape of an array tells how many rows and columns it has. Knowing the shape is important for machine learning models, which require specific sizes.
print(matrix.shape)
Useful NumPy Functions
NumPy includes many helpful functions for calculations. Here are some commonly used ones:
arr = np.array([10, 20, 30, 40])
print(np.mean(arr)) # average
print(np.max(arr)) # maximum
print(np.min(arr)) # minimum
print(np.sum(arr)) # sum
Real-World Applications of NumPy
NumPy becomes essential when processing large numerical data. It is widely used in:
- Machine learning model development
- Image processing and computer vision
- Financial analysis and forecasting
- Scientific simulations
Because of its speed and efficiency, NumPy is usually the first library learned by data-driven developers.
📝 Practice Exercises
Exercise 1
Create a NumPy array with values from 1 to 10 and print the even numbers.
Exercise 2
Create a 2D array with two rows and three columns. Print its shape.
Exercise 3
Perform addition and multiplication on two arrays of equal length.
Exercise 4
Use NumPy functions to find the mean and maximum of an array.
✅ Practice Answers
Answer 1
import numpy as np
arr = np.arange(1, 11)
print(arr[arr % 2 == 0])
Answer 2
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix.shape)
Answer 3
import numpy as np
a = np.array([2, 4, 6])
b = np.array([1, 3, 5])
print(a + b)
print(a * b)
Answer 4
import numpy as np
arr = np.array([12, 18, 24])
print(np.mean(arr))
print(np.max(arr))