From Grocery Lists to Rocket Launches: Why Numbers Rule the World?
From the price of your favorite snack to the trajectory of a Mars rover โ numbers silently shape our world. ๐๐ฐ๏ธ Whether it's your fitness tracker counting steps or the traffic lights syncing to rush hour chaos, there's a mathematical brain behind it all.๐
Curious how it all works under the hood? Letโs peel back the curtain and explore the toolkit that helps decode the universe of data.
In this guide, weโll unravel the magic of NumPy. From creating simple vectors to handling high-dimensional tensors, you'll learn how to manipulate data like a pro. Whether you're a beginner or brushing up your skills, this blog is your entry point into the world of efficient, high-performance computing! ๐ป๐
๐งฑ 1. Creating NumPy Arrays
๐น 1D Array (Vector)
import numpy as np
a = np.array([1, 2, 3])
print(a)
๐น 2D Array (Matrix)
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b)
๐น 3D Array (Tensor)
c = np.array([[[1, 2, 3], [4, 7, 9], [12, 6, 3]]])
print(c)
๐ฏ 2. Array Data Types
np.array([1, 2, 3], dtype=float) # Float array
np.array([1, 2, 3], dtype=complex) # Complex array
๐ 3. Array Creation Routines
๐ธ Using arange()
np.arange(1, 11)
np.arange(1, 11, 2)
๐ธ Reshaping Arrays
np.arange(1, 26).reshape(5, 5)
๐ธ Ones and Zeros
np.ones((3, 4))
np.zeros((4, 5))
๐ธ Random Arrays
np.random.random((3, 4))
๐ธ Linearly Spaced Values
np.linspace(-10, 10, 10)
๐ธ Identity Matrix
np.identity(3)
๐ง 4. Array Properties
a1 = np.arange(10, dtype=np.int32)
a2 = np.arange(12, dtype=float).reshape(3, 4)
a3 = np.arange(8).reshape(2, 2, 2)
๐ Shape, Type, Size, and More
a2.ndim # Number of dimensions
a1.shape # Shape of the array
a2.size # Total elements
a1.itemsize # Bytes per item
a1.dtype # Data type
๐งช Changing Data Type
a3.astype(np.int32)
โ 5. Arithmetic Operations
a1 * 2
a1 ** 2
a2 > 5
a1 + a2
a1 * a2
๐ 6. Aggregation Functions
np.max(a1)
np.min(a1)
np.sum(a1)
np.prod(a1)
np.mean(a1)
np.median(a1)
np.std(a1)
np.var(a1)
๐ Axis-wise Aggregation
np.max(a2, axis=1)
np.mean(a2, axis=0)
๐ 7. Trigonometric & Dot Product
np.sin(a1) # Sine values
np.dot(a2, a3.reshape(4, 2)) # Matrix multiplication
โ๏ธ 8. Slicing and Indexing
๐ธ Basic Indexing
a1[2:5]
a1[6:9:2]
๐ธ Multidimensional Indexing
a2[1, 2]
a2[:, 2]
a2[1:, 1:3]
๐ธ Tensor Access
a3[1, 1, 0]
๐ 9. Stacking and Splitting
๐ธ Horizontal Stacking
a4 = np.ones((3, 4))
a5 = np.zeros((3, 4))
np.hstack((a4, a5))
๐ธ Splitting Arrays
np.hsplit(a4, 4)
np.vsplit(a4, 3)
๐พ Wrap-Up
With NumPy, working with arrays becomes intuitive and efficient. Mastering these basics will set a strong foundation for data science, AI, and scientific computing! ๐๐
Top comments (2)
useful blog. Thanks for it.
Thank You