Python Forum
Get closest value array for array of arrays.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get closest value array for array of arrays.
#1
Title is quite confusing let me explain:
If I have an 2D array of values 'y': [[10,20,10],[30,20,10],[10,10,10],[45,37,34]]
and a singular array 'x': [25,18,9]
I want to be able to run function findClosestArray(x, y) which will return
Output:
[30,20,10]
because that is the closest array to the one I provided.
If I ran findClosestArray(x, y) with 'x' NOW being [43,37,30] it would return
Output:
[45,37,34]
It's very similar to this:
myList = [6,6,10,50,24,457,24]
myNumber= 9
a = min(myList, key=lambda x:abs(x-myNumber))
print(a)
(which would return 10)
but instead with arrays.

How can I do this?
Reply
#2
(Nov-17-2019, 09:31 PM)DreamingInsanity Wrote: It's very similar to this:
myList = [6,6,10,50,24,457,24]
myNumber= 9
a = min(myList, key=lambda x:abs(x-myNumber))
print(a)
(which would return 10)
but instead with arrays.

How can I do this?

To use min, was already the right way, also the use of abs. The key is used to get for each element one or more values to compare.


def find_closest(x, array2d):
    x_sum = sum(x)
    ###
    diffs = [sum(y) - x_sum for y in array2d]
    print(diffs)
    # just for demonstration
    ###
    result = min(array2d, key=lambda z: abs(sum(z) - x_sum))
    index = array2d.index(result)
    return index, result
If you don't need the index and the debugging information, the function looks like this.
def find_closest(x, array2d):
    x_sum = sum(x)
    return min(array2d, key=lambda z: abs(sum(z) - x_sum))
For fun you can implement other functions for comparison.
If the lists are for example points, you can calculate the longest and shortest distances to other points:

x1, y1, z1 = 4, 5, 9
x2, y2, z2 = 40, 50, 90
dist = math.sqrt( (x2 - x1) ** 2 + (y2 - y1) ** 2 + (z2 - z1) ** 2 )
With Python 3.8 we got some nice changes in the math module.
For example the hypot function accepts now more than two arguments.

hypot(*coordinates) -> value

Multidimensional Euclidean distance from the origin to a point.

Roughly equivalent to:
    sqrt(sum(x**2 for x in coordinates))

For a two dimensional point (x, y), gives the hypotenuse
using the Pythagorean theorem:  sqrt(x*x + y*y).

For example, the hypotenuse of a 3/4/5 right triangle is:

    >>> hypot(3.0, 4.0)
    5.0
Type:      builtin_function_or_method
With this function, it's a bit lesser code:
dist2 = math.hypot(x2-x1, y2-y1, z2-z1)
And to calculate distances between points, we've now a convenience function.
Signature: math.dist(p, q, /)
Docstring:
Return the Euclidean distance between two points p and q.

The points should be specified as sequences (or iterables) of
coordinates.  Both inputs must have the same dimension.

Roughly equivalent to:
    sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))
Type:      builtin_function_or_method
Using the function:
point1 = (x1, y1, z1)
point2 = (x2, y2, z2)
dist3 = math.dist(point1, point2)
Instead of using sum to calculate the sum of the list, you could use key=lambda z: math.dist(x, z).

By the way, Python is also very useful to solve homework with code, which the teacher could understand.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Thanks a lot for this!
It was actually for RGB values but I didn't know about the new python 3.8 function, so that's cool.

I see that you are totalling all the values in the arrays. As I was going to sleep I had a similar thought, but didn't actually know that it was going to work or how I could at least pull it off in a semi-efficient manner.

Thanks again,
Dream
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Elegant way to apply each element of an array to a dataframe? sawtooth500 5 175 1 hour ago
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 191 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,725 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  How Write Part of a Binary Array? Assembler 1 305 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  Loop over an an array of array Chendipeter 1 529 Nov-28-2023, 06:37 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 985 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  IPython errors for numpy array min/max methods muelaner 1 508 Nov-04-2023, 09:22 PM
Last Post: snippsat
  Convert np Array A to networkx G IanAnderson 2 629 Jul-05-2023, 11:42 AM
Last Post: IanAnderson
  Help using a dynamic array excel formula with XLWings FXMonkey 2 1,208 Jun-06-2023, 09:46 PM
Last Post: FXMonkey
  [Newbie] Multiple Array azhuda 3 954 Jun-01-2023, 04:29 AM
Last Post: azhuda

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020