AI/ML/DL

Getting Started with NumPy: Fast Numerical Computing with Python

NumPy is a powerful library for numerical 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. It is the foundation for many other scientific libraries like Pandas, SciPy, and scikit-learn.

NumPy (Numerical Python)

Key Features:

  • Arrays: NumPy provides a flexible and efficient array object, ndarray, which allows you to store and manipulate data.
  • Mathematical operations: Supports a wide variety of mathematical operations (e.g., element-wise operations, linear algebra).
  • Broadcasting: Enables operations on arrays of different shapes.
  • Random number generation: Provides functions for generating random numbers.
  • Integration with other libraries: Works well with libraries like Pandas, SciPy, and Matplotlib.

Example:

Creating Array

import numpy as np

# 1D Array (vector)
arr = np.array([1, 2, 3, 4, 5])
print(arr)

# 2D Array (Matrix)
arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr_2d)

Operations

# Element-wise operation
arr2 = np.array([5, 4, 3, 2, 1])
sum_arr = arr + arr2
print(sum_arr)  # [6 6 6 6 6]

# Matrix multiplication
product = np.dot(arr_2d, arr_2d.T)  # Matrix dot product
print(product)

Advance Example

# Broadcasting Example
arr_a = np.array([[1, 2, 3], [4, 5, 6]])
arr_b = np.array([1, 2, 3])

result = arr_a + arr_b  # arr_b is broadcast to each row of arr_a
print(result)

Ideal for numerical computations, array operations, and mathematical functions


About author

author image

Amrit panta

Fullstack developer, content creator



Scroll to Top