﻿ How to Find a Minimum Value in a 2D Array Without Using Numpy or Flattened in Python - ITCodar

# How to Find a Minimum Value in a 2D Array Without Using Numpy or Flattened in Python

## Minimum value on a 2d array python

However it only evaluates along a single axis and returns the index of the minimum value along a single row/column whereas I wish to evaluate the whole array and return the lowest value not the indices.

`numpy.argmin` does not by default evaluate along a single axis, the default is to evaluate along the flattened matrix and it returns the linear index in the flattened array; from the `numpy` docs that you linked:

By default, the index is into the flattened array, otherwise along the specified axis.

Either way, use `numpy.amin` or `numpy.min` to return the minimum value, or equivalently for an array `arrname` use `arrname.min()`. As you mentioned, `numpy.argmin` returns the index of the minimum value (of course, you can then use this index to return the minimum value by indexing your array with it). You could also flatten into a single dimension array with `arrname.flatten()` and pass that into the built-in `min` function.

The four following methods produce what you want.

``import numpy as npvalues = np.array([    [8,2,3,4,5,6],    [3,6,6,7,2,6],    [3,8,5,1,2,9],    [6,4,2,7,8,3]])values.min()          # = 1np.min(values)        # = 1np.amin(values)       # = 1min(values.flatten()) # = 1``

## Finding the minimum value above 0 in a 2D array. Python

Since you have a numpy array, a much simpler way to filter and get the minimum is to do the following.

``np.min(count1[count1>k])``

And the reason you see a filter object is that `filter` returns a filter object. Also, I am not very sure if `filter` works with 2D arrays. You might have to first flatten it if you want to use `filter`

## how to find max, min and average by using if technique in a 2d array in python

You can use `numpy` module to find min and max values easily:

``import numpy as npx = np.array([[80, 59, 34, 89], [31, 11, 47, 64], [29, 56, 13, 91], [55, 61, 48, 0], [75, 78, 81, 91]])minValue = np.min(x)maxValue = np.max(x)print(minValue)print(maxValue)``

If you need to find them without build-in methods, you can use an approach as follows:

``x = [[80, 59, 34, 89], [31, 11, 47, 64], [29, 56, 13, 91], [55, 61, 48, 0], [75, 78, 81, 91]]minValue = xmaxValue = xsumAll = 0count = 0for inner in x:    for each in inner:        if each > maxValue: maxValue = each        if each < minValue: minValue = each        sumAll += each        count += 1average = sumAll / count``

In this approach, you compare each value to find min and max. At the same time sum, count each element to calculate average.

## How to find the top n minimum values in a two dimensional numpy matrix

You can flatten that matrix, use `np.ravel`, and sort it afterwards:

``>>> varray([[2, 2, 3, 3, 4],       [4, 5, 5, 5, 6],       [6, 7, 7, 8, 8],       [9, 9, 9, 9, 9]])>>> v1 = v.ravel()   #flattened the array>>> v1.sort()        # sorted that.>>> n = 10           # value of n>>> v1[:n]array([2, 2, 3, 3, 4, 4, 5, 5, 5, 6])``

## Get max or min n-elements out of numpy array? (preferably not flattened)

The standard way to get the indices of the largest or smallest values in an array is to use `np.argpartition`. This function uses an introselect algorithm and runs with linear complexity - this performs better than fully sorting for larger arrays (which is typically O(n log n)).

By default this function works along the last axis of the array. To consider an entire array, you need to use `ravel()`. For example, here's a random array `a`:

``>>> a = np.random.randint(0, 100, size=(5, 5))>>> aarray([[60, 68, 86, 66,  9],       [66, 26, 83, 87, 50],       [41, 26,  0, 55,  9],       [57, 80, 71, 50, 22],       [94, 30, 95, 99, 76]])``

Then to get the indices of the five largest values in the (flattened) 2D array, use:

``>>> i = np.argpartition(a.ravel(), -5)[-5:] # argpartition(a.ravel(), 5)[:5] for smallest>>> iarray([ 2,  8, 22, 23, 20])``

To get back the corresponding 2D indices of these positions in `a`, use `unravel_index`:

``>>> i2d = np.unravel_index(i, a.shape)>>> i2d(array([0, 1, 4, 4, 4]), array([2, 3, 2, 3, 0]))``

Then indexing `a` with `i2d` gives back the five largest values:

``>>> a[i2d]array([86, 87, 95, 99, 94])``

## How to find index of minimum non zero element with numpy?

`np.nonzero(theta)` returns the index of the values that are non-zero. In your case, it returns,

`[1,2,3]`

Then, theta[np.nonzero(theta)] returns the values

`[1,2,3]`

When you do `np.argmin(theta[np.nonzero(theta)])` on the previous output, it returns the index of the value `1` which is 0.

Hence, the correct approach would be:

`i,j = np.where( theta==np.min(theta[np.nonzero(theta)]))` where `i,j` are the indices of the minimum non zero element of the original numpy array

`theta[i,j]` or `theta[i]` gives the respective value at that index.

## Filtering a 2D numpy array

With your array being `arr` and your values being `(_min, _max)`, use:

``selection = np.logical_and(_min <= arr, arr <= _max)selection = np.logical_and(selection, selection)filtered_arr = arr[:, selection]``