Python Forum
'Get closest value array in array of arrays.' follow up help.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'Get closest value array in array of arrays.' follow up help.
#1
I created a post a little while back where I needed a function findClosestArray(x, y) which took two arguments:
'x' - a single array of rgb values [35,76,97]
'y' - a 2d array full of rgb values [[33,76,90], [75, 97, 74], ...]
findClosestArray would take x and find the closest matching array in 'y'. So for the arrays I posted before 'findClosestArray' would return [33,76,90], because that is closest to [35,76,97].
Here is the code for that:
def find_closest(x, array2d):
    x_sum = sum(x)
    return min(array2d, key=lambda z: abs(sum(z) - x_sum))
I have been using this every since, and have thought nothing of it because it worked perfectly. I have pretty much come to the end of my project - I am now in the stage of doing some last extensive testing.
I didn't notice this before (since my range of images was about 50 and it is now 1000 and over), but actually, the image created by my script, in in black and white.

Take these two arrays:
[1]: [50, 67, 98] - mucky blue [2]: [98, 67, 50] - browny-orange. RGB set 2, is just the reverse of set 1. With the code I wanted, calling 'findClosestArray' should return a different value for each array, since they are TWO DIFFERENT COLOURS.
However, the way the code works is that it sums up all values and finds the closest matching one. That's where the problem lies:
although RGB set 2 is a different color to set 1, if they are summed up, they become the SAME VALUE. This means when they are compared to the 2d array of rgb values 'y', it will always return the same value even though it is a different colour.
As well as this, summing up rgb values is pretty much how you find the black and white version of an image.

The problem is all to do with the summing up of the values in the array. That means I need a way to compare 'x' to 'y' without (technically) doing fidClosestArray(sum(x), sum(y))

How can I do that?
Dream
Reply


Messages In This Thread
'Get closest value array in array of arrays.' follow up help. - by DreamingInsanity - Nov-30-2019, 07:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 471 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 307 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,946 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  How Write Part of a Binary Array? Assembler 1 391 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  Loop over an an array of array Chendipeter 1 606 Nov-28-2023, 06:37 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 1,306 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  IPython errors for numpy array min/max methods muelaner 1 602 Nov-04-2023, 09:22 PM
Last Post: snippsat
  Convert np Array A to networkx G IanAnderson 2 712 Jul-05-2023, 11:42 AM
Last Post: IanAnderson
  Help using a dynamic array excel formula with XLWings FXMonkey 2 1,323 Jun-06-2023, 09:46 PM
Last Post: FXMonkey
  [Newbie] Multiple Array azhuda 3 1,050 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