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:

Introduction NumPy

In this blog post, we’ll explore following numpy array advanced operations:

  1. Array Inspection
  2. Array Mathematics
  3. Comparison
  4. Statistical Functions
  5. Indexing and Slicing
  6. Array Manipulations

Let’s dive in!

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:

ouptut numpy advanced operations

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:

NumPY Advanced Operations Array Arithmetics

You can also use universal functions (ufuncs):

print("Sine:", np.sin(a))
print("Logarithm:", np.log(a))

Output:

Advanced NumPy Operations That Will Skyrocket Your Python Skills (2025 Guide)

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:

Array mathematical functions numpy advanced operations

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:

exp() and sqrt() functions in numpy module

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

Array Comparison equal and array_equal function

Use logical functions for compound conditions:

print(np.logical_and(arr1 > 10, arr1 < 35))

Output:

relational operators with numpy operations

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 filtered using relational operators in numPy

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

Array Statistical functions of numpy module

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

Numpy Statistical functions Numpy advanced operations

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:

numpy slicing

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

reshape method in NumPy array

Key Manipulation Functions:

  • np.reshape()
  • np.ravel() – Flatten array
  • np.transpose() – Swap axes
  • np.concatenate() – Combine arrays
  • np.split() – Divide arrays

Output

Array manipulations

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:

Advanced NumPy Operations Jupyter Notebook file

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.

Leave a Reply


Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `POST https://api.aspose.cloud/connect/token` resulted in a `429 Too Many Requests` response in /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113 Stack trace: #0 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/guzzle/src/Middleware.php(69): GuzzleHttp\Exception\RequestException::create() #1 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/promises/src/Promise.php(204): GuzzleHttp\Middleware::GuzzleHttp\{closure}() #2 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/promises/src/Promise.php(153): GuzzleHttp\Promise\Promise::callHandler() #3 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/promises/src/TaskQueue.php(48): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}() #4 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/promises/src/Promise.php(248): GuzzleHttp\Promise\TaskQueue->run() #5 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/promises/src/Promise.php(224): GuzzleHttp\Promise\Promise->invokeWaitFn() #6 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/promises/src/Promise.php(269): GuzzleHttp\Promise\Promise->waitIfPending() #7 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/promises/src/Promise.php(226): GuzzleHttp\Promise\Promise->invokeWaitList() #8 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/promises/src/Promise.php(62): GuzzleHttp\Promise\Promise->waitIfPending() #9 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/guzzle/src/Client.php(123): GuzzleHttp\Promise\Promise->wait() #10 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php(50640): GuzzleHttp\Client->send() #11 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php(50648): Aspose\Words\WordsApi->_requestToken() #12 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php(50654): Aspose\Words\WordsApi->_checkAuthToken() #13 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php(50666): Aspose\Words\WordsApi->_getKey() #14 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php(80): Aspose\Words\WordsApi->_checkRsaKey() #15 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/src/AsposeWords/Util.php(24): Aspose\Words\WordsApi->__construct() #16 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/src/AsposeWords/ExportEngine.php(87): AsposeWords\Util::getWordsApi() #17 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/src/AsposeWords/AutoExport.php(26): AsposeWords\ExportEngine->convert() #18 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-includes/class-wp-hook.php(324): AsposeWords\AutoExport->export() #19 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters() #20 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-includes/plugin.php(517): WP_Hook->do_action() #21 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-includes/load.php(1304): do_action() #22 [internal function]: shutdown_action_hook() #23 {main} thrown in /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 113