Home > Software engineering >  how to filter and Get min and max from 2D ndarray
how to filter and Get min and max from 2D ndarray

Time:10-25

n2d = np.array(
[
    [10,0],
    [11,0],
    [12,0],
    [0,1],
    [100,1],
    [200,1],
    [20,0],
    [21,0],
    [22,0],
])

This is a ndarray of x-y coordinates.

I want to get the coordinates of the minimum and maximum x value among the coordinates of y=0

The value I want to get is [10, 0], [22, 0]

I want to implement it with the features that numpy has.

CodePudding user response:

import numpy as np

n2d = np.array(
[
    [10,0],
    [11,0],
    [12,0],
    [0,1],
    [100,1],
    [200,1],
    [20,0],
    [21,0],
    [22,0],
])

min_y_coordinate = np.amin(n2d)
filter_list = [True if item[-1] == min_y_coordinate else False for item in n2d ]
filtered_array = n2d[filter_list]
print("min_value" , np.amin(filtered_array, axis=0))
print("max_value" , np.amax(filtered_array, axis=0))
  • Related