Numpy - ndarray
×


Numpy - ndarray

133

NumPy, short for Numerical Python, is a cornerstone library for scientific computing in Python. At its heart lies the ndarray (N-dimensional array), a powerful data structure that enables efficient storage and manipulation of large datasets.

What is an ndarray?

The ndarray is a multi-dimensional, homogeneous array of fixed-size items. Unlike Python's built-in lists, all elements in an ndarray must be of the same data type, which allows for optimized performance and memory efficiency. This structure is ideal for numerical computations, making it a fundamental component in fields like data science, machine learning, and scientific research.

Creating ndarrays

NumPy provides several ways to create ndarrays:

  • np.array(): Converts a list or tuple into an ndarray.
  • np.zeros(): Creates an array filled with zeros.
  • np.ones(): Creates an array filled with ones.
  • np.arange(): Creates an array with a range of values.
  • np.linspace(): Creates an array with evenly spaced values over a specified range.

Example:

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)

Key Attributes of ndarray

Understanding the attributes of an ndarray is crucial:

  • ndim: Number of dimensions (axes) of the array.
  • shape: Tuple representing the dimensions of the array.
  • size: Total number of elements in the array.
  • dtype: Data type of the array elements.
  • itemsize: Size in bytes of each element of the array.

Example:

arr = np.array([[1, 2], [3, 4]])
print("Shape:", arr.shape)
print("Dimensions:", arr.ndim)
print("Size:", arr.size)
print("Data type:", arr.dtype)
print("Item size:", arr.itemsize)

Indexing and Slicing

NumPy allows for advanced indexing and slicing:

  • Basic Indexing: Access elements using indices.
  • Slicing: Extract sub-arrays using slice notation.
  • Integer Array Indexing: Use integer arrays to index.
  • Boolean Indexing: Use boolean arrays to index.

Example:

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr[0, 2])  # Output: 3
print(arr[1, :])  # Output: [4 5 6]

Array Operations

NumPy supports a wide range of operations:

  • Element-wise Operations: Perform arithmetic operations on each element.
  • Universal Functions (ufuncs): Functions that operate element-wise on arrays.
  • Linear Algebra Operations: Functions like dot product, matrix multiplication, etc.
  • Statistical Operations: Functions like mean, median, standard deviation, etc.

Example:

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print(arr1 + arr2)  # Output: [5 7 9]

Broadcasting

Broadcasting allows NumPy to perform arithmetic operations on arrays of different shapes. It extends the smaller array across the larger array so that they have compatible shapes.

Example:

arr = np.array([[1, 2], [3, 4]])
print(arr + 5)  # Output: [[6 7]
               #          [8 9]]

Conclusion

The ndarray is a powerful data structure that forms the backbone of numerical computing in Python. Its efficiency and versatility make it indispensable for tasks ranging from simple data manipulation to complex scientific computations.

For more detailed information, visit the GeeksforGeeks NumPy ndarray 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!


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat