NumPy Array in Python
0 136
NumPy, short for Numerical Python, is a fundamental package for scientific computing in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays efficiently.
Creating NumPy Arrays
To begin using NumPy, you first need to import it:
import numpy as np
NumPy arrays can be created from Python lists or tuples using the np.array()
function:
arr = np.array([1, 2, 3, 4])
For multi-dimensional arrays, you can pass a list of lists:
arr_2d = np.array([[1, 2], [3, 4]])
Key Attributes of NumPy Arrays
NumPy arrays have several important attributes:
- shape: Returns a tuple representing the dimensions of the array.
- dtype: Returns the data type of the array elements.
- ndim: Returns the number of dimensions of the array.
Example:
arr = np.array([[1, 2], [3, 4]])
print(arr.shape) # Output: (2, 2)
print(arr.dtype) # Output: int64
print(arr.ndim) # Output: 2
Operations on NumPy Arrays
NumPy supports a wide range of mathematical operations:
- Element-wise operations: Operations are applied element by element.
- Matrix operations: Operations like dot product are supported.
- Universal functions (ufuncs): Functions that operate element-wise on arrays.
Example:
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print(arr1 + arr2) # Output: [5 7 9]
Dimensions in NumPy Arrays
NumPy arrays can have multiple dimensions:
- 0D: Scalar - A single value.
- 1D: Vector - A list of values.
- 2D: Matrix - A 2D grid of values.
- 3D: Tensor - A 3D grid of values.
Example:
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr_3d.ndim) # Output: 3
NumPy Arrays vs Python Lists
While Python lists are versatile, NumPy arrays offer several advantages for numerical computations:
- Performance: NumPy arrays are faster due to their optimized implementation.
- Memory efficiency: Arrays use less memory by storing data more compactly.
- Functionality: NumPy provides a vast library of mathematical functions.
Conclusion
NumPy arrays are a powerful tool for numerical and scientific computing in Python. They provide efficient storage and manipulation of large datasets, making them essential for data analysis, machine learning, and scientific research.
For more detailed information, visit the GeeksforGeeks NumPy Array in Python tutorial.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments