Peak detection in a 2D array

Here is an example of a Python function that can be used to detect peaks in a 2D array:

def detect_peaks(arr):
    peaks = []
    for i in range(1, arr.shape[0]-1):
        for j in range(1, arr.shape[1]-1):
            if arr[i,j] > arr[i-1,j] and arr[i,j] > arr[i+1,j] and arr[i,j] > arr[i,j-1] and arr[i,j] > arr[i,j+1]:
                peaks.append((i,j))
    return peaks

This function takes a 2D array as input and returns a list of peak coordinates in the form of tuples (i, j) where i is the row index and j is the column index of the peak element. The function checks if the current element is greater than all its neighboring elements, if it's true, it's considered as a peak.

Watch a course Python - The Practical Guide

You can use this function as follows:

import numpy as np

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(detect_peaks(arr))

This will print [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3)]

Note that this is a very simple example, and in practice you may need to use more advanced peak detection algorithms depending on the nature of your data.