3

I have a multidimensional array(let's call it 'data'), I print it produce like this

[[[255, 255, 255]
  [255, 255, 255]
  [0, 0, 0]
  [255, 255, 255]]
 [[0, 0, 0]
  [255, 255, 255]
  [0, 0, 0]
  [0, 0, 0]]
  ... and so on
  [255, 255, 255]]]

i want to change the content of data like this

[[1,
  1,
  0,
  1]
 [0,
  1,
  0,
  0]
  ... and so on
  1]]

[255, 255, 255] become 1, and [0, 0, 0] become 0

I'm trying with numpy.where, but I'm desperate How to do that in python programming?

4
  • 1
    Are there going to be cases where the numbers are something else? Or are you strictly checking just between 3x255 and 3x0? Commented Apr 7, 2020 at 8:08
  • on your first code block, did you mean [255, 255, 255] or "[255 255 255]" Commented Apr 7, 2020 at 8:12
  • @Alilshaq just between 3x255 and 3x0 Commented Apr 7, 2020 at 8:17
  • @baby idk for sure, but when printed, the result are like that Commented Apr 7, 2020 at 8:17

3 Answers 3

2

One way is checking whether a value is 255, and reducing the boolean result with np.logical_and

np.logical_and.reduce(a==255, axis=2).view('i1') 

For the following example:

a = np.array([[[255, 255 ,255],
               [255, 255, 255],
               [0 ,0, 0],
               [255, 255 ,255]],
              [[0 ,0, 0],
               [255, 255 ,255],
               [0 ,0, 0],
               [0 ,0, 0]]])

np.logical_and.reduce(a==255, axis=2).view('i1') 

array([[1, 1, 0, 1],
       [0, 1, 0, 0]], dtype=int8)
Sign up to request clarification or add additional context in comments.

3 Comments

thanks, it works. i have another question. Why the output when printed is [255 255 255] or [1 1 1], not [255, 255, 255] ?
Sorry I don't really understand what you mean. @IrchamAjiNugroho
sorry for my bad english and explanation :D i've found the answer
1

try this one:

data = [
    [
        [255, 255, 255],
        [255, 255, 255],
        [0, 0, 0],
        [255, 255, 255]
    ],
    [
        [0, 0, 0],
        [255, 255, 255],
        [0, 0, 0],
        [0, 0, 0],
    ],
    [
        [255, 255, 255],
        [255, 255, 255],
        [0, 0, 0],
        [0, 0, 0],
    ]
]

result = [[1 if y == [255, 255, 255] else 0 for y in x] for x in data]
print(result)

Bare in mind that it will work if the only possible values are [255, 255, 255] or [0, 0, 0] (This is what I understand from the question)

5 Comments

I would go with this answer.
Using lists and loopy approaches to work with numpy arrays is never the way to go if there are numpy functions to do the same. If you compare performances on larger arrays, you'll see huge differences
It will be significant on large arrays as you said but the OP didn't mention the scale of his data so this is, IMHO, the simplest solution.
255 and 0s... This is for image data. Usual sizes are of the order of 1024x1024, which already would give you 1048576 different data points for a single image, and that is considering a single channel. For image data, using highly optimized numpy and scipy functions is crucial if you want to have a reasonably performant approach, which is by no means possible with python level loops.
thanks by the way, the size of an array of pixels of an image
0

This is how I would do it:

Using tuple(col) == (255, 255, 255) is important because the truth value of an array with more than one element is ambiguous.

data = np.asarray(im) # (assuming this is how you got your data)

new_data = np.array(np.zeros((len(data),len(data[0]))))

for x,row in enumerate(data):
    for y,col in enumerate(row):
        if tuple(col) == (255, 255, 255):
            new_data[x][y] = 1

print(new_data)

Or in one line:

new_data = [[1 if tuple(col) == (255, 255, 255) else 0 for col in row] for row in data]

The first will result in a numpy array and the second, just a list. However, you can convert it to a numpy array by using:

new_data = np.array([[1 if tuple(col) == (255, 255, 255) else 0 for col in row] for row in data])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.