Basics of NumPy Arrays
×


Basics of NumPy Arrays

642

NumPy, short for Numerical Python, is a cornerstone library in the Python ecosystem, pivotal for scientific computing and data analysis. It offers a powerful N-dimensional array object and a plethora of mathematical functions to operate on these arrays efficiently.

What is a NumPy Array?

At its core, a NumPy array is a grid of values, all of the same type, indexed by a tuple of nonnegative integers. It has a shape (the number of elements along each axis) and a dtype (the data type of the elements). NumPy arrays are more efficient for numerical computations than Python's built-in lists due to their fixed size and homogeneous data type.

Creating NumPy Arrays

NumPy arrays can be created from Python lists or tuples using the np.array() function:

import numpy as np
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]])

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]

Indexing and Slicing

Accessing elements in a NumPy array is straightforward:

print(arr[0])  # Output: 1

Slicing allows you to access a range of elements:

print(arr[1:3])  # Output: [2 3]

Reshaping Arrays

NumPy provides functions to reshape arrays without changing their data:

reshaped = np.reshape(arr, (2, 2))
print(reshaped)

This will output:

[[1 2]
 [3 4]]

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 Basics of NumPy Arrays 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