Because of array depth issues in PHP, receiving this array from Python becomes truncated with an ellipsis (“…”). I’d like to process the array in Python before returning back to php.
Clarification: I need to maintain the inner sets [135, 121, 81]. These are R, G, B values and I’m tying to group sets that occur more than once. Values in sets need to maintain [1, 2, 3] sequence, NOT [1,2,3,4,5,6,7,8] as some answers have suggested below.
How would you simplify this 3D numpy.ndarray to a collection of unique RGB triples?
Here is how the array is printed by Python:
[[[135 121 81]
[135 121 81]
[135 121 81]
...,
[135 121 81]
[135 121 81]
[135 121 81]]
[[135 121 81]
[135 121 81]
[135 121 81]
...,
[135 121 81]
[135 121 81]
[135 121 81]]
[[ 67 68 29]
[135 121 81]
[ 67 68 29]
...,
[135 121 81]
[135 121 81]
[135 121 81]]
...,
[[200 170 19]
[200 170 19]
[200 170 19]
...,
[ 67 68 29]
[ 67 68 29]
[ 67 68 29]]
[[200 170 19]
[200 170 19]
[200 170 19]
...,
[116 146 15]
[116 146 15]
[116 146 15]]
[[200 170 19]
[200 170 19]
[200 170 19]
...,
[116 146 15]
[116 146 15]
[116 146 15]]]
Here is the code that I have attempted:
def uniquify(arr)
keys = []
for c in arr:
if not c in keys:
keys[c] = 1
else:
keys[c] += 1
return keys
result = uniquify(items)
Based on the representation of your “array”, it looks like you’re working with a
numpy.ndarray. This becomes quite a simple problem if that is the case — You can transform to a 1-D iterable simple by using the.flatattribute. To make it unique, you can just use aset:This will give you a set, but you could easily get a list from it:
Here’s how it works:
As a side note, there’s also
np.uniquewhich will give you the unique elements of your array as well.I think I finally got this one figured out:
Seems to work. Phew!
How this works — the pieces that you want to keep as triplets are accessed as:
So, we just need to loop over all of the combinations of
XandY. That is exactly whatitertools.productis good for. We can get the validXandYin an arbitrary number of dimensions:So we pass that to product:
Now we have something that will generate the first to indices — We just need to construct a tuple to pass to
__getitem__that numpy will interpret as(X,Y,:)— That’s easy, we’re already getting(X,Y)from indices_generator — We just need to tack on an emtpy slice:Now we can loop over all_items looking for the unique ones with a set:
Now turn this back into a list, or a numpy array or whatever you want for the purposes of passing it back to PHP.