NumPy (short for Numerical Python) is a powerful Python library used for numerical computations. It provides support for large multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on them efficiently.
Why Use NumPy? ๐ค
pip install numpy
import numpy as np
Arrays are the core of NumPy. Hereโs how to create them:
import numpy as np
# 1D array
a = np.array([1, 2, 3])
print(a)
# 2D array
b = np.array([[1, 2], [3, 4]])
print(b)
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Dimensions:", arr.ndim)
print("Shape:", arr.shape)
print("Size:", arr.size)
print("Data type:", arr.dtype)
np.zeros((2, 3))
โ Creates array of all zerosnp.ones((2, 3))
โ Creates array of all onesnp.arange(0, 10, 2)
โ Array with step valuenp.linspace(0, 1, 5)
โ 5 values between 0 and 1np.random.rand(2, 2)
โ Random 2x2 matrixNumPy makes mathematical operations easy:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # [5 7 9]
print(a * b) # [ 4 10 18]
print(np.dot(a, b)) # 32 (dot product)
arr = np.array([1, 2, 3, 4, 5])
print(np.mean(arr)) # 3.0
print(np.median(arr)) # 3.0
print(np.std(arr)) # Standard deviation
print(np.max(arr)) # Max value
Use Jupyter Notebook or Google Colab on mobile for writing and testing NumPy code while learning on the go.
NumPy is a must-know for anyone diving into Python-based data science, machine learning, or analytics. Mastering its basics lays a strong foundation for advanced tools like Pandas, SciPy, and TensorFlow.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!