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