NumPy Basics
NumPy is one of the most important Python libraries used in Machine Learning (ML), Data Science, Artificial Intelligence, Scientific Computing, and Deep Learning.
The word NumPy stands for:
Numerical Python
NumPy provides powerful tools for:
- Numerical computation
- Array operations
- Matrix manipulation
- Statistical analysis
- Mathematical functions
Why NumPy is Important in ML
Machine Learning algorithms work with large amounts of numerical data.
NumPy makes data processing:
- Faster
- More efficient
- Memory optimized
- Easy to manage
Most ML libraries such as:
- Scikit-learn
- TensorFlow
- PyTorch
- Pandas
are built using NumPy.
Installing NumPy
NumPy can be installed using pip.
pip install numpy
Importing NumPy
NumPy is commonly imported using the alias np.
import numpy as np
What is an Array?
An array is a collection of elements stored in a structured format.
NumPy arrays are:
- Fast
- Efficient
- Memory optimized
Creating a NumPy Array
NumPy provides the array() function.
Example
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr)
Output
[1 2 3 4]
Advantages of NumPy Arrays
- Faster than Python lists
- Efficient mathematical operations
- Supports multidimensional data
- Less memory usage
Checking Array Type
print(type(arr))
Output
<class 'numpy.ndarray'>
Dimensions in NumPy Arrays
NumPy supports:
- 1D Arrays
- 2D Arrays
- 3D Arrays
1D Array
A one-dimensional array contains a single row of elements.
arr = np.array([1, 2, 3])
2D Array
A two-dimensional array contains rows and columns.
arr = np.array([
[1, 2],
[3, 4]
])
print(arr)
Output
[[1 2]
[3 4]]
Matrix Representation
:contentReference[oaicite:0]{index=0}3D Array
Three-dimensional arrays are useful in Deep Learning and image processing.
arr = np.array([
[
[1, 2],
[3, 4]
]
])
Checking Array Dimensions
NumPy provides the ndim property.
arr = np.array([[1, 2], [3, 4]])
print(arr.ndim)
Output
2
Checking Array Shape
The shape property returns rows and columns.
arr = np.array([[1, 2], [3, 4]])
print(arr.shape)
Output
(2, 2)
Checking Array Size
print(arr.size)
Output
4
Checking Data Type of Array
print(arr.dtype)
Output
int64
Creating Arrays with Special Functions
Array of Zeros
arr = np.zeros((2, 3))
print(arr)
Output
[[0. 0. 0.]
[0. 0. 0.]]
Array of Ones
arr = np.ones((2, 2))
print(arr)
Identity Matrix
Identity matrices are important in linear algebra and ML.
arr = np.eye(3)
print(arr)
Identity Matrix Formula
:contentReference[oaicite:1]{index=1}Using arange()
The arange() function creates arrays with sequences.
arr = np.arange(1, 10)
print(arr)
Output
[1 2 3 4 5 6 7 8 9]
Using linspace()
The linspace() function generates evenly spaced numbers.
arr = np.linspace(0, 1, 5)
print(arr)
Output
[0. 0.25 0.5 0.75 1. ]
Array Indexing
Indexing accesses specific elements.
arr = np.array([10, 20, 30])
print(arr[1])
Output
20
2D Array Indexing
arr = np.array([
[1, 2],
[3, 4]
])
print(arr[1, 0])
Output
3
Array Slicing
Slicing extracts parts of arrays.
arr = np.array([1, 2, 3, 4, 5])
print(arr[1:4])
Output
[2 3 4]
NumPy Mathematical Operations
NumPy supports fast mathematical calculations.
Addition Example
arr = np.array([1, 2, 3])
print(arr + 5)
Output
[6 7 8]
Multiplication Example
arr = np.array([1, 2, 3])
print(arr * 2)
Output
[2 4 6]
Vector Representation
:contentReference[oaicite:2]{index=2}Statistical Functions
NumPy provides statistical functions useful in Machine Learning.
| Function | Description |
|---|---|
| mean() | Average value |
| median() | Middle value |
| std() | Standard deviation |
| sum() | Total sum |
Mean Example
arr = np.array([10, 20, 30])
print(np.mean(arr))
Output
20.0
Mean Formula
:contentReference[oaicite:3]{index=3}Standard Deviation
arr = np.array([10, 20, 30])
print(np.std(arr))
Standard Deviation Formula
:contentReference[oaicite:4]{index=4}Array Reshaping
Reshaping changes array dimensions.
arr = np.array([1, 2, 3, 4])
new_arr = arr.reshape(2, 2)
print(new_arr)
Output
[[1 2]
[3 4]]
Random Numbers in NumPy
Random data is important in Machine Learning.
arr = np.random.rand(3)
print(arr)
NumPy in Machine Learning
NumPy is heavily used in:
- Data preprocessing
- Feature engineering
- Matrix operations
- Deep Learning
- Scientific computing
NumPy and Linear Algebra
Linear Algebra is the foundation of Machine Learning.
Dot Product Example
a = np.array([1, 2])
b = np.array([3, 4])
print(np.dot(a, b))
Output
11
Dot Product Formula
:contentReference[oaicite:5]{index=5}Advantages of NumPy
- Fast computation
- Efficient memory usage
- Supports multidimensional arrays
- Powerful mathematical functions
- Foundation for ML libraries
Limitations of NumPy
- Limited support for labeled data
- Complex for beginners initially
- Less suitable for extremely large distributed datasets
Best Practices
- Use vectorized operations
- Avoid unnecessary loops
- Use appropriate array dimensions
- Understand broadcasting properly
Real-World Example
In image processing systems, images are stored as NumPy arrays.
Machine Learning models use NumPy arrays to process:
- Pixels
- Features
- Audio signals
- Sensor data
Future of NumPy
NumPy remains one of the most important libraries in AI and Data Science.
Future improvements include:
- Better performance
- GPU acceleration support
- Advanced scientific computation
- Cloud integration
Conclusion
NumPy is the foundation of numerical computing in Python.
It provides:
- Efficient arrays
- Fast mathematical operations
- Linear algebra support
- Statistical analysis tools
Mastering NumPy is essential for becoming successful in:
- Machine Learning
- Data Science
- Artificial Intelligence
- Deep Learning