Unlock the full potential of NumPy with these advanced NumPy operations. Boost your Python skills and write faster, cleaner, and more efficient code today!
NumPy, or Numerical Python, is the cornerstone of scientific computing in Python. While beginners often use NumPy for basic array creation and manipulation, advanced NumPy operations unlock its full potential for efficient data analysis, machine learning, and scientific simulations.
For a complete beginner’s guide to NumPy, check out our post on introduction to NumPy and NumPy array creation, follow this link:
In this blog post, we’ll explore following numpy array advanced operations:
- Array Inspection
- Array Mathematics
- Comparison
- Statistical Functions
- Indexing and Slicing
- Array Manipulations
Let’s dive in!
Topics Covered
Array inspection – Advanced NumPy Operations
Before performing any operation on an array, inspecting its structure is crucial. NumPy offers several attributes and methods to understand an array’s shape, data type, and memory layout.
Key Functions for Array Inspections
- .shape: Returns the dimensions of the array.
- .ndim: Shows how many dimensions the array has.
- .dtype: Indicates the data type of elements.
- .size: Total number of elements.
Example code for array inspection
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Shape:", arr.shape)
print("Dimensions:", arr.ndim)
print("Data type:", arr.dtype)
print("Size:", arr.size)
This code shows output as follows:

Tip: Use these inspections before and after operations to ensure consistency in your data pipeline.
Array Mathematics
NumPy allows element-wise mathematical operations, which are highly optimized through vectorization.
Operators to perform mathematical operations
The arithmatic operators +,-,*,/,//,**,% works with NumPy arrya as usual. Observe this code:
a = np.array([10, 20, 30])
b = np.array([1, 2, 3])
print("Addition:", a + b)
print("Multiplication:", a * b)
print("Exponentiation:", a ** 2)
Output:

You can also use universal functions (ufuncs):
print("Sine:", np.sin(a))
print("Logarithm:", np.log(a))
Output:

Common Mathematical Functions:
- sum() – return sum of array
- subtract() – return difference of arrays
- divide() – return the quotient after division
- exp() – returns the exponential value
- sqrt() – returns the square root of number
- sin() – returns singn value of anglec
- cos() – returns cos value of angle
- log() – returns log value
Example:
sum() function
import numpy as np
#arr=np.array([[11,55.55,67,89,32],[44,6,77,88,22],[1,2,3,4,5]])
a1=np.array([[11,55,67,89,32],[1,2,3,4,5]])
a2=np.array([[30,20,50,55,43],[4,5,6,7,8]])
print("Array 1:",a1)
print("Array 1:",a2)
#OverAll Sum
print("OverAll Sum:",np.sum((a1,a2)))
# Row wise sum
print("Rowwise Sum:",np.sum((a1,a2),axis=0))
# Column wise sum
print("Columnwise Sum:",np.sum((a1,a2),axis=1))
Output:

exp() and sqrt() Function
import numpy as np
a1=np.array([[11.544578,55,67,89,32],[1,2,3,4,5]])
a2=np.array([[30,20,50,55,43],[4,5,6,7,8]])
print("Array 1:",a1)
print("Array 2:",a2)
Exponent values
print("Exponent:",np.exp(a1))
Square Root
print("Square Root:",np.sqrt(a1))
Output:

Array Comparison
Comparing arrays is useful for filtering data and making decisions. They are compared by element as well as by array too. Have a look at following:
Element wise comparison
The equal() function compare the arrays element by element
Array wise comparison
The array_equal() compare the array by array
arr1=np.array([[11,55,67,89,32],[1,2,3,4,5]])
arr2=np.array([[30,20,67,55,43],[4,5,6,7,8]])
print("Checking Equality Element wise:",np.equal(arr1,arr2))
print("Checking Equality whole array:",np.array_equal(arr1,arr2))
Output

Use logical functions for compound conditions:
print(np.logical_and(arr1 > 10, arr1 < 35))
Output:

Useful Comparison Functions:
- np.equal()
- np.not_equal()
- np.greater()
- np.less()
- np.logical_and()
- np.logical_or()
You can use these results to filter arrays:
filtered = arr1[arr1 > 25]
print("Filtered array:", filtered)
Output:

Array Statistical Functions
Statistical analysis is where NumPy truly shines. The functions provided by NumPy for statistical functions are as follows:
- min()
- max()
- mean()
- median()
- corrcoef()
- std()
a1=np.array([[11,55,67,89,32],[1,2,3,4,5]])
print("Minumum:",np.min(a1,axis=0))
print("")
print("Mean:", np.mean(a1))
print("Median:", np.median(a1))
print(np.corrcoef(a1))
print("Standard Deviation:", np.std(a1))
Output

Advanced Statistical Methods:
- np.var(): Variance
- np.percentile(): Percentile calculations
import numpy as np
data = [10, 20, 30, 40, 50]
variance = np.var(data)
print("Variance:", variance)
p25 = np.percentile(data, 25) # 25th percentile
p50 = np.percentile(data, 50) # 50th percentile (median)
p75 = np.percentile(data, 75) # 75th percentile
print("25th Percentile:", p25)
print("50th Percentile:", p50)
print("75th Percentile:", p75)
Output

These are essential in data science and analytics for understanding the distribution and relationships in your datasets.
Indexing and Slicing
Indexing and slicing in NumPy are more powerful than standard Python lists.
arr = np.array([[10, 20, 30], [40, 50, 60]])
print("Element at (0,1):", arr[0,1])
print("Second column:", arr[:,1])
Output:

Tips:
- Use colons : to slice rows/columns.
- Negative indexing works too: arr[-1]
- You can even assign slices: arr[0, :] = [1, 2, 3]
Advanced slicing is essential in machine learning workflows where multidimensional data is common.
Array Manipulations
Changing the structure of arrays is often required during preprocessing and reshaping datasets.
arr = np.array([[1, 2, 3], [4, 5, 6]])
reshaped = arr.reshape(3, 2)
print("Reshaped array:\n", reshaped)
Output

Key Manipulation Functions:
- np.reshape()
- np.ravel() – Flatten array
- np.transpose() – Swap axes
- np.concatenate() – Combine arrays
- np.split() – Divide arrays
Output

Reshaping is particularly useful in neural networks where data input shapes must be exact.
Watch this video for more understandig:
Download the notebook file from here:
Conclusion
Understanding and using advanced NumPy operations like array inspection, mathematics, comparison, and slicing allows you to work efficiently with large datasets. Whether you’re analyzing financial data, building machine learning models, or performing scientific research, mastering these operations is a must for any data enthusiast.