Introduction to NumPy
Python tutorial · PySpark.in
What is NumPy?
NumPy (Numerical Python) is an open-source Python library used for scientific computing and numerical operations.
It provides support for multi-dimensional arrays, matrices, and a large collection of high-level mathematical functions to operate on these arrays efficiently.
NumPy is the core foundation of many scientific and data-related libraries such as:
- Pandas
- Matplotlib
- SciPy
- Scikit-Learn
- TensorFlow
Why NumPy over Python Lists?
NumPy is preferred over Python lists because it provides faster execution, lower memory consumption, and optimized mathematical operations through vectorization and efficient memory management. Python lists are general-purpose containers, whereas NumPy arrays are specialized for numerical computation.
Key Reasons to Use NumPy
1.Speed
- NumPy operations are implemented in C internally.
- They execute faster than Python lists, which use Python loops.
2.Memory Efficiency
- NumPy stores elements of the same data type in contiguous memory.
- Python lists store references to objects results in more memory usage.
3.Vectorized Operations
- NumPy allows element-wise operations without loops.
- This is called vectorization.
4.Mathematical & Scientific Support
- Built-in functions for:
- Linear algebra
- Statistics
- Random number generation
- Fourier transforms
Importing NumPy
NumPy is commonly imported using the alias np.
```
Hide
import numpy as np
```
Example
```
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr)
```
NumPy Array vs Python List
Python List Example
```
list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
result = []
for i in range(len(list1)):
result.append(list1[i] + list2[i])
print(result)
```
NumPy Array Example
```
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([5, 6, 7, 8])
print(a + b)
```
Speed Comparison (Practical)
```
import numpy as np
import time
# Python list
list_data = list(range(1000000))
start = time.time()
sum([x * x for x in list_data])
print("List time:", time.time() - start)
# NumPy array
array_data = np.arange(1000000)
start = time.time()
np.sum(array_data ** 2)
print("NumPy time:", time.time() - start)
```
Result
- NumPy runs much faster due to vectorization and C-level execution.
Memory Comparison
```
import sys
import numpy as np
list_data = [1, 2, 3, 4]
array_data = np.array([1, 2, 3, 4])
print(sys.getsizeof(list_data))
print(array_data.nbytes)
```
Observation
- Python list consumes more memory
- NumPy array uses compact memory
Tabular Comparison
Feature | Python List | NumPy Array |
|---|---|---|
Speed | Slower | Faster |
Memory | High | Low |
Data Type | Mixed | Same type |
Operations | Loop required | Vectorized |
Mathematical Functions | Limited | Extensive |
Usage | General purpose | Scientific computing |
Practical Applications of NumPy
- Data analysis
- Machine learning
- Image processing
- Signal processing
- Scientific simulations
More Python tutorials
- What is Python and Why is it used for Data Science and Data Engineering?
- How Does Python Work in the Backend? Internal Working of Python
- Top 30 Python Interview Questions for Data Science
- 3-Month Python Roadmap to Excel in Data Science and Machine Learning
- Python Data Types Explained – A Beginner’s Guide
- test
All tutorials · Try the free PySpark compiler · Practice challenges