NumPy Tutorial
NumPy (Numerical Python) is one of the most important libraries in Python for scientific computing and numerical operations.
It provides:
- Fast multidimensional arrays
- Mathematical functions
- Statistical operations
- Linear algebra tools
- Random number generation
- Broadcasting capabilities
NumPy serves as the foundation for many popular Python libraries used in data science, machine learning, and artificial intelligence.
What is NumPy?
NumPy is an open-source Python library designed for efficient numerical computations.
Instead of working with traditional Python lists, NumPy uses specialized array objects that provide:
- Better performance
- Lower memory usage
- Faster mathematical operations
Why Use NumPy?
Faster Than Python Lists
NumPy arrays are implemented in optimized C code, making calculations significantly faster.
Less Memory Usage
Arrays consume less memory compared to Python lists.
Powerful Mathematical Functions
Provides built-in functions for:
- Statistics
- Algebra
- Matrix operations
- Scientific computing
Installing NumPy
Install NumPy using pip:
pip install numpyVerify installation:
import numpy as np
print(np.__version__)Importing NumPy
The standard convention is:
import numpy as npHere, np is simply an alias for NumPy.
Creating NumPy Arrays
One-Dimensional Array
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)Output:
[1 2 3 4 5]Two-Dimensional Array
arr = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(arr)Output:
[[1 2 3]
[4 5 6]]Three-Dimensional Array
arr = np.array([
[[1, 2], [3, 4]],
[[5, 6], [7, 8]]
])
print(arr)Array Attributes
Shape
Shows dimensions of an array.
arr = np.array([[1, 2], [3, 4]])
print(arr.shape)Output:
(2, 2)Number of Dimensions
print(arr.ndim)Output:
2Data Type
print(arr.dtype)Output:
int64Creating Special Arrays
Zeros Array
arr = np.zeros((3, 3))Output:
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]Ones Array
arr = np.ones((2, 2))Identity Matrix
arr = np.eye(3)Output:
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]Range of Values
arr = np.arange(0, 10, 2)Output:
[0 2 4 6 8]Array Indexing
arr = np.array([10, 20, 30])
print(arr[0])Output:
10Array Slicing
arr = np.array([1, 2, 3, 4, 5])
print(arr[1:4])Output:
[2 3 4]Mathematical Operations
Addition
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b)Output:
[5 7 9]Multiplication
print(a * b)Output:
[ 4 10 18]Division
print(a / b)Statistical Functions
Sum
arr = np.array([1, 2, 3, 4])
print(np.sum(arr))Output:
10Mean
print(np.mean(arr))Output:
2.5Median
print(np.median(arr))Output:
2.5Standard Deviation
print(np.std(arr))Reshaping Arrays
arr = np.arange(12)
new_arr = arr.reshape(3, 4)
print(new_arr)Output:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]Broadcasting
Broadcasting allows NumPy to perform operations on arrays with different shapes.
arr = np.array([1, 2, 3])
print(arr + 10)Output:
[11 12 13]Random Numbers
Random Float
import numpy as np
print(np.random.rand())Random Array
print(np.random.rand(3, 3))Random Integers
print(np.random.randint(1, 100, 5))Linear Algebra
Matrix Multiplication
a = np.array([[1, 2],
[3, 4]])
b = np.array([[5, 6],
[7, 8]])
print(np.dot(a, b))Output:
[[19 22]
[43 50]]Determinant
print(np.linalg.det(a))Inverse Matrix
print(np.linalg.inv(a))Real-World Example
Calculate average monthly sales:
import numpy as np
sales = np.array([
1200,
1500,
1800,
2000,
2200
])
average = np.mean(sales)
print(average)Output:
1740.0Common NumPy Functions
| Function | Purpose |
|---|---|
| array() | Create array |
| zeros() | Create zeros |
| ones() | Create ones |
| arange() | Create sequence |
| reshape() | Change shape |
| sum() | Calculate sum |
| mean() | Calculate average |
| median() | Find median |
| std() | Standard deviation |
| dot() | Matrix multiplication |
Best Practices
- Use NumPy arrays instead of large Python lists.
- Utilize vectorized operations.
- Avoid unnecessary loops.
- Use broadcasting when possible.
- Leverage built-in mathematical functions.
Summary
NumPy is the foundation of scientific computing in Python.
Key features include:
- Efficient arrays
- Fast computations
- Statistical functions
- Matrix operations
- Broadcasting
- Random number generation
Learning NumPy is essential for data science, machine learning, artificial intelligence, and numerical computing.
Conclusion
NumPy is one of the most powerful and widely used Python libraries. Its ability to process large datasets efficiently makes it indispensable for modern software development, data analysis, and machine learning projects.
Mastering NumPy will significantly improve your Python programming skills and prepare you for advanced topics such as Pandas, Machine Learning, and Deep Learning.


0 Comments