DEV Community

Cover image for 🔢NumPy Simplified
Shreya Ghorui
Shreya Ghorui

Posted on

🔢NumPy Simplified

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)
Enter fullscreen mode Exit fullscreen mode

🔹 2D Array (Matrix)

b = np.array([[1, 2, 3], [4, 5, 6]])
print(b)
Enter fullscreen mode Exit fullscreen mode

🔹 3D Array (Tensor)

c = np.array([[[1, 2, 3], [4, 7, 9], [12, 6, 3]]])
print(c)
Enter fullscreen mode Exit fullscreen mode

🎯 2. Array Data Types

np.array([1, 2, 3], dtype=float)    # Float array
np.array([1, 2, 3], dtype=complex)  # Complex array
Enter fullscreen mode Exit fullscreen mode

🔁 3. Array Creation Routines

🔸 Using arange()

np.arange(1, 11)
np.arange(1, 11, 2)
Enter fullscreen mode Exit fullscreen mode

🔸 Reshaping Arrays

np.arange(1, 26).reshape(5, 5)
Enter fullscreen mode Exit fullscreen mode

🔸 Ones and Zeros

np.ones((3, 4))
np.zeros((4, 5))
Enter fullscreen mode Exit fullscreen mode

🔸 Random Arrays

np.random.random((3, 4))
Enter fullscreen mode Exit fullscreen mode

🔸 Linearly Spaced Values

np.linspace(-10, 10, 10)
Enter fullscreen mode Exit fullscreen mode

🔸 Identity Matrix

np.identity(3)
Enter fullscreen mode Exit fullscreen mode

🧠 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)
Enter fullscreen mode Exit fullscreen mode

📏 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
Enter fullscreen mode Exit fullscreen mode

🧪 Changing Data Type

a3.astype(np.int32)
Enter fullscreen mode Exit fullscreen mode

➕ 5. Arithmetic Operations

a1 * 2
a1 ** 2
a2 > 5
a1 + a2
a1 * a2
Enter fullscreen mode Exit fullscreen mode

📊 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)
Enter fullscreen mode Exit fullscreen mode

📌 Axis-wise Aggregation

np.max(a2, axis=1)
np.mean(a2, axis=0)
Enter fullscreen mode Exit fullscreen mode

📐 7. Trigonometric & Dot Product

np.sin(a1)              # Sine values
np.dot(a2, a3.reshape(4, 2))  # Matrix multiplication
Enter fullscreen mode Exit fullscreen mode

✂️ 8. Slicing and Indexing

🔸 Basic Indexing

a1[2:5]
a1[6:9:2]
Enter fullscreen mode Exit fullscreen mode

🔸 Multidimensional Indexing

a2[1, 2]
a2[:, 2]
a2[1:, 1:3]
Enter fullscreen mode Exit fullscreen mode

🔸 Tensor Access

a3[1, 1, 0]
Enter fullscreen mode Exit fullscreen mode

🔀 9. Stacking and Splitting

🔸 Horizontal Stacking

a4 = np.ones((3, 4))
a5 = np.zeros((3, 4))
np.hstack((a4, a5))
Enter fullscreen mode Exit fullscreen mode

🔸 Splitting Arrays

np.hsplit(a4, 4)
np.vsplit(a4, 3)
Enter fullscreen mode Exit fullscreen mode

🎾 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)

Collapse
 
anikchand461 profile image
Anik Chand

useful blog. Thanks for it.

Collapse
 
shreya_ghorui profile image
Shreya Ghorui

Thank You