πŸš€ NumPy for Everyone: From Basics to Advanced

Nitin KumarNitin Kumar
3 min read

🧠 What is NumPy?

NumPy (short for Numerical Python) is a powerful Python library used for mathematical and scientific computing. It provides a fast way to work with arrays (lists of numbers), matrices, and numerical functions.

βœ… In simple terms: NumPy helps Python do fast maths with lots of numbers at once.


🎯 Why Use NumPy?

  • Python lists are slow for math operations.

  • NumPy arrays are faster and memory efficient.

  • NumPy lets you:

    • Add, subtract, multiply numbers easily

    • Create matrices, reshape them

    • Perform statistics and linear algebra

    • Work with images, signals, data science models, and much more


πŸ”§ Installation and Import

To use NumPy, first install it:

pip install numpy

Then import it in your Python file:

import numpy as np

πŸ“¦ NumPy Array (ndarray) – Core Object

The most important thing in NumPy is the ndarray object β€” this is what stores numbers in a fast, structured way.

Let’s create one:

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

Now let’s understand the attributes of this array.


πŸ” Important Attributes of NumPy Array (with Examples)

AttributeDescriptionExample Output
ndimNumber of dimensions2 for a 2D array
shapeTuple showing rows & columns(2, 3)
sizeTotal number of elements6
dtypeData type of elementsint64
itemsizeSize (in bytes) of each element8 (for int64)
nbytesTotal memory used by array48 bytes
TTranspose of the arrayConverts rows to columns

Full Example:

arr = np.array([[1, 2, 3], [4, 5, 6]])

print("Array:\n", arr)
print("Dimensions (ndim):", arr.ndim)
print("Shape:", arr.shape)
print("Total Elements (size):", arr.size)
print("Data Type (dtype):", arr.dtype)
print("Item Size (bytes):", arr.itemsize)
print("Total Bytes (nbytes):", arr.nbytes)
print("Transpose:\n", arr.T)

πŸ”¨ Creating Arrays – Methods You’ll Love

np.zeros((2, 3))     # Array of all 0s
np.ones((2, 3))      # Array of all 1s
np.full((2, 2), 7)   # Filled with 7
np.eye(3)            # Identity matrix
np.arange(0, 10, 2)  # From 0 to 10 with step 2
np.linspace(1, 5, 4) # 4 equally spaced values from 1 to 5

πŸ” Array Operations

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.sin(a))  # Apply sin function to each element

NumPy automatically performs operations element-wise.


🧬 Reshaping & Indexing

arr = np.array([[1, 2, 3], [4, 5, 6]])

print(arr[1])        # [4 5 6]
print(arr[:, 1])     # [2 5] (2nd column)
print(arr[0, 2])     # 3

reshaped = arr.reshape(3, 2)

🧠 Broadcasting

Even if two arrays have different shapes, NumPy can sometimes "stretch" them to work together:

a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([1, 2, 3])
print(a + b)  # Adds b to each row of a

🎲 Random Arrays

np.random.rand(2, 3)       # Random floats
np.random.randint(0, 10, 5) # Random integers between 0–9

πŸ“‚ Saving and Loading

np.save("myarray.npy", arr)
loaded = np.load("myarray.npy")

πŸŽ“ Conclusion: Why Learn NumPy?

βœ… Whether you’re into machine learning, data analysis, robotics, or just want to speed up your Python math, NumPy is the first library to learn.

πŸ§ͺ Practice small examples. Try reshaping, slicing, and operating on arrays. You'll see why NumPy is powerful and fun!

1
Subscribe to my newsletter

Read articles from Nitin Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nitin Kumar
Nitin Kumar