NumPy (Numerical Python) is one of the most powerful libraries in Python used for numerical computing and data analysis. It provides efficient tools for working with arrays and performing mathematical operations. NumPy is widely used in data science, machine learning, scientific computing, and engineering due to its speed and flexibility.
In this guide, we will explore NumPy arrays, how to create them, and the various mathematical operations you can perform on them.
What is a NumPy Array?
A NumPy array is a grid of values, all of the same data type, indexed by a tuple of non-negative integers. It is the core data structure in NumPy and is more efficient than Python lists in terms of performance and memory usage.
NumPy arrays are also called ndarray (N-dimensional array), meaning they can be one-dimensional, two-dimensional, or multi-dimensional.
Creating NumPy Arrays
To use NumPy, you first need to import the library:
import numpy as np
1. One-Dimensional Array
arr = np.array([1, 2, 3, 4])
print(arr)
2. Two-Dimensional Array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
print(arr_2d)
3. Arrays with Built-in Functions
NumPy also provides functions to create arrays easily:
np.zeros((2, 3)) # Array filled with zeros
np.ones((2, 2)) # Array filled with ones
np.arange(0, 10, 2) # Range of values
np.linspace(0, 1, 5) # Evenly spaced values
Indexing and Slicing in NumPy
You can access elements of an array using indexing and slicing.
arr = np.array([10, 20, 30, 40])print(arr[0]) # First element
print(arr[1:3]) # Slicing
For 2D arrays:
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])print(arr_2d[0, 1]) # Access element
print(arr_2d[:, 1]) # Column selection
Mathematical Operations on NumPy Arrays
One of the biggest advantages of NumPy is its ability to perform fast mathematical operations.
1. Arithmetic Operations
NumPy supports element-wise operations.
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])print(a + b)
print(a - b)
print(a * b)
print(a / b)
These operations are performed on each corresponding element of the arrays.
2. Scalar Operations
You can perform operations between an array and a single number.
arr = np.array([1, 2, 3])print(arr + 5)
print(arr * 2)
3. Mathematical Functions
NumPy provides many built-in mathematical functions:
np.sum()– total sum of elementsnp.mean()– average valuenp.min()– minimum valuenp.max()– maximum valuenp.sqrt()– square root
arr = np.array([4, 9, 16])print(np.sqrt(arr))
4. Aggregation Functions
These functions help summarize data:
arr = np.array([10, 20, 30, 40])print(np.sum(arr))
print(np.mean(arr))
print(np.std(arr)) # Standard deviation
5. Matrix Operations
NumPy supports matrix multiplication using the dot() function or @ operator.
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])print(np.dot(a, b))
Broadcasting in NumPy
Broadcasting allows NumPy to perform operations on arrays of different shapes.
arr = np.array([1, 2, 3])
print(arr + 10)
Here, the scalar value is automatically applied to each element.
Benefits of NumPy Arrays
- Faster execution compared to Python lists
- Efficient memory usage
- Supports vectorized operations
- Easy integration with other libraries like Pandas and Matplotlib
- Suitable for large-scale numerical computations
NumPy arrays are the foundation of numerical computing in Python. They allow efficient storage and manipulation of data while enabling powerful mathematical operations. By understanding array creation, indexing, slicing, and mathematical operations, beginners can build a strong base for advanced topics in data science and machine learning. NumPy is an essential tool for anyone working with data in Python.
For More Information and Updates, Connect With Us
- Name Sumit singh
- Phone Number: +91 9264477176
- Email ID: emancipationedutech@gmail.com
- Our Platforms:
- Digilearn Cloud
- Live Emancipation
- Follow Us on Social Media:
- Instagram – Emancipation
- Facebook – Emancipation
Stay connected and keep learning with Python Training !