Jugal kishore

Jun 08, 2025 • 2 min read

🚀 Mastering NumPy for Data Science: Array Attributes, Operations, and Statistical Insights

🚀 Mastering NumPy for Data Science: Array Attributes, Operations, and Statistical Insights

Whether you're diving into data science or building machine learning pipelines, NumPy is your ultimate ally. In this article, we’ll break down three crucial areas that every Python developer must master:

  1. Understanding NumPy Array Attributes

  2. Mastering Indexing, Slicing, and Array Operations

  3. Performing Statistical Analysis on NumPy Arrays

Let’s unlock the power of NumPy step by step 🔍


🔹 Understanding NumPy Array Attributes: ndim, shape, size, dtype, and More

NumPy arrays, known as ndarray, come with powerful attributes that let you inspect and manipulate them efficiently. Let’s look at the most useful ones:

1. ndim: Number of Dimensions

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.ndim)  # Output: 2

💡 This tells you how many axes (dimensions) your array has. In the above example, it's a 2D array.


2. shape: Tuple of Array Dimensions

print(arr.shape)  # Output: (2, 3)

💡 The shape shows 2 rows and 3 columns.


3. size: Total Number of Elements

print(arr.size)  # Output: 6

💡 Total elements = rows × columns.


4. dtype: Data Type of Array Elements

print(arr.dtype)  # Output: int64

💡 NumPy arrays are homogeneous; all elements have the same type.


5. itemsize and nbytes

print(arr.itemsize)  # Size in bytes of each element
print(arr.nbytes)    # Total bytes consumed by the array

🔹 Mastering Indexing, Slicing, and Array Operations in NumPy

1. Indexing

Access elements using standard Python indexing:

print(arr[1, 2])  # Output: 6

2. Slicing

You can slice like lists but with a multi-dimensional twist:

print(arr[0, :2])  # Output: [1 2] (first row, first two columns)

3. Boolean Indexing

print(arr[arr > 3])  # Output: [4 5 6]

4. Array Operations

a. Element-wise Addition

b = np.array([[1, 1, 1], [1, 1, 1]])
print(arr + b)

Output:

[[2 3 4]
 [5 6 7]]

b. Broadcasting

You can add a 1D array to a 2D array:

c = np.array([10, 20, 30])
print(arr + c)

Output:

[[11 22 33]
 [14 25 36]]

🔹 Performing Statistical Analysis on NumPy Arrays

Let’s now explore how to extract key statistical insights from NumPy arrays.

1. max(), min(), mean()

print(np.max(arr))   # Output: 6
print(np.min(arr))   # Output: 1
print(np.mean(arr))  # Output: 3.5

2. std() - Standard Deviation

print(np.std(arr))   # Output: 1.7078

3. Axis-Based Computation

Mean of each column:

print(np.mean(arr, axis=0))  # Output: [2.5 3.5 4.5]

Max of each row:

print(np.max(arr, axis=1))   # Output: [3 6]

🔚 Conclusion

Mastering NumPy means mastering performance, productivity, and precision in your Python projects. In this post, we looked at:

  • Essential array attributes like ndim, shape, size, dtype

  • Powerful slicing and indexing tools

  • Element-wise operations and broadcasting

  • Statistical functions that turn raw data into insights

Now go experiment with your own arrays—play, analyze, and explore! 🚀

Join Jugal on Peerlist!

Join amazing folks like Jugal and thousands of other people in tech.

Create Profile

Join with Jugal’s personal invite link.

0

8

0