1

I have a numpy array,

> [[  0   0   0   0   0]
 [  0   0   0   0   0]
 [  0   0   0   0   0]
 ...
 [  0 255   0 255   0]
 [  0 255   0   0   0]
 [  0 255   0   0   0]]

I would like to check values in each column of this 2d numpy array - whether or not there are more than one column with alternating 0 and 255 values

Example column

>   [[  0 0   0 255   0]
     [  0 255 0   0   0]
     [  0 255 0   0   0]
     [  0 255 0   0   0]
     [  0 0   0   0   0]
     [  0 0   0   0   0]
     [  0 0   0   0   0]
     [  0 0   0   0   0]
     [  0 255 0   0   0]
     [  0 255 0   0   0]
     [  0 255 0   0   0]
     [  0 255 0   0   0]
     [  0 255 0   0   0]]

In the above example, column two has alternating 0 and 255 values, hence, the output should be that this numpy array has one column with these alternating values

2
  • The fourth column also has alternating 0 and 255 values, no? Commented Aug 7, 2018 at 15:00
  • It has, but I will add a threshold of the occurences of 255 and 0s Commented Aug 9, 2018 at 7:32

4 Answers 4

1
np.sum(np.count_nonzero(np.diff(a, axis=0), axis=0) > 1)
Sign up to request clarification or add additional context in comments.

3 Comments

Wow, beautiful.
Does it check all the columns of the array?
I need the number of columns that have a majority of alternating 0s and 255s.
0

I dont understand how the second column has alternate values of 0 and 255 but here is an idea...

>>> unique, counts = numpy.unique(numpy.transpose(nd_a)[0], return_counts=True)
>>> counts
array([13], dtype=int64)
>>> unique, counts = numpy.unique(numpy.transpose(nd_a)[1], return_counts=True)
>>> counts
array([5, 8], dtype=int64)

if len(nd_a) is same as counts then its not alternating but if values are somewhat similar(e.g. 5 and 8 take 5/13 and 8/13) then its alternating.

Comments

0

Assuming "alternating" means that the same value appears more than once without being adjacent to its other occurrences:

If you take the diff of each row's value with its successor (per column), there should be three possible values: 0, 255, -255. Only columns with alternating values will have both 255 and -255. Testing for the presence of these two diff values (using a few Pandas methods) will give you the answer.

import pandas as pd

def check_alt(data):
    return (pd.DataFrame(data)
            .diff()
            .apply(lambda x: x.value_counts())
            .loc[[-255,255]]
            .notnull()
            .all()
            .sum())

check_alt(a) # 2
check_alt(b) # 4

Data:

a = [[0,0,0,255,0],
[0,255,0,0,255],
[0,255,0,0,0],
[0,255,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,255,0,0,0],
[0,255,0,0,0],
[0,255,0,0,0],
[0,255,0,0,0],
[0,255,0,0,0]]

b = [[0,0,0,255,0],
[255,255,0,0,255],
[0,0,0,255,0]]

The nested list a is your example data. I also included an extra array, b, to demonstrate the edge case where there are only constantly alternating values (i.e. every next value is not the one before it). The expected count of alternating columns for b is 4.

Comments

0

Here is another solution;

import numpy as np

#construct numpy array
c = np.zeros((15,5)) # Pre-allocate matrix


#populate some columns with number patterns

c[1:4,1] = 255
c[8:13,1] = 255

c[1,3] = 255

c[1:4,4] = 255
c[7:12,4] = 255

OUT = np.empty(shape=[15,0])

for ni in range(0,c.shape[1]):
    #work on column ni from c
    a = c[:,ni] #work on array a
    b = np.diff(a) #differene between neighbouring elements
    d = np.where(b != 0) #work out where the transitions from 0->255 or 255->0 are

    #work out what constitutes an alternating pattern (you may want to change the logic)
    if np.size(d) > 2:
        #this column from C has been deemed to have an alternating pattern
        OUT = np.append(OUT, c[:,ni:ni+1], axis=1) # add the column to the OUT array

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.