-1

Please help on how I could use a.any() or a.all() in such a case, as my attempt np.all(x[i] == 0 and x[j] == 0) == True and True makes no difference.

def count(x):
    """ similar to q table, if 0 and 0 in consecutive ,then 0.3 is appended in the count_set...."""

count_set = []

for i in range(x.shape[0]):
    for j in range(x.shape[1]):
        
        if x[i] == 0 and x[j] == 0:
            count_set.append(0.3)
        elif x[i] == 0 and  x[j] ==1 or x[i] == 1 and  x[j] ==0 :
            count_set = append(0.1)
        else :
            count_set.append(0.5)

   
return np.cumsum(count_set)

input_sample = np.array([[1,0,1,0],[1,1,0,0]])
count(input_sample)

Solution should be calculated as follows 1 and 0 is consecutive twice hence 0.1 is append, as from the 2nd if statement.

1 and 1 is obeys the third if else statement hence 1 and 1 is appended.

Finally, the last two 0s are consecutive , as from the 1st if statement means 0.3 is appended. count_set =[0.1,0.1,1,1,0.3] np.cumsum(count_set ) = 2.5

quadhd
  • 71
  • 1
  • 8
  • Looks like a pure numpy approach would be much more convenient here. Could you share a sample input array and expected output? – yatu Jul 07 '20 at 10:34
  • it is not clear what you are asking. are you looking for consequtive 0 values or are you trying to find the dimensions of `x`? – Finn Jul 07 '20 at 10:36
  • @yatu i have edited the question with a sample input, this could be any array or matrix – quadhd Jul 07 '20 at 10:40
  • @Finn none, my goal is to append values into a new list called count_set depending on the values in the input sample. by using two for loops i transverse through the input array where j is ahead and i behind, if x[i] and x[j] == 0 and 1 , then 0.1 is appended into the count matrix and so on – quadhd Jul 07 '20 at 10:44
  • The first time through the loops x[0] = [1,0,1,0]. Should x have two indices in the loops to point to a scalar not an array? – Tls Chris Jul 07 '20 at 11:01
  • could you please add the solution to `np.array([[1,0,1,0],[1,1,1,1]])`? Ideally the `count_set` before `np.cumsum` – Finn Jul 07 '20 at 11:08
  • @Finn I have edited the question with a sample solution. – quadhd Jul 07 '20 at 13:39
  • @Tls Chris , can u please show me a workable code as to what mean ? – quadhd Jul 07 '20 at 13:39
  • My question was asking for clarification. @Homer's answer and your additions give a bit more clarity. I'll add my effort at an answer below. – Tls Chris Jul 07 '20 at 23:58

2 Answers2

1

With your code, the lines

if x[i] == 0 and x[j] == 0:

and

elif x[i] == 0 and  x[j] ==1:

are potentially not doing what you expect them to.

The indexing of the array x denoted by x[i] will return an array with dimension greater than one (not a single number).

This means, in these lines of code, you are comparing an array x[i] against a single number, which gives this error because it's not really clear what is meant by the value of an array as a single number - it being a collection of numbers itself.

It is unclear exactly what you are trying to do, but I expect replacing the above lines in your code with

if x[i,j] == 0:

and

elif x[i,j] ==1:

respectively should provide you with some clarity.

Running the code with my solution as:

import numpy as np

def count(x):
    """ similar to q table, if 0 and 0 in consecutive ,then 0.3 is appended in the count_set...."""
    count_set = []

    for i in range(x.shape[0]):
        for j in range(x.shape[1]):
            if (x[i,j] == 0):
                count_set.append(0.3)
            elif (x[i,j] == 0):
                count_set.append(0.1)
            else:
                count_set.append(0.5)

    
    return np.cumsum(count_set)

input_sample = np.array([[1,0,1,0],[1,1,1,1]])
print(count(input_sample))

Returns

>>> [0.5 0.8 1.3 1.6 2.1 2.6 3.1 3.6]

Expanding on previous answer

Using the conditonals:

conditon1 = (x[i,j] == 1) & (x[i+1,j+1] == 0) 

and

condition2 = (x[i,j] == 0) & (x[i+1,j+1] == 1)

to change your code into:

import numpy as np

def count(x):
    """ similar to q table, if 0 and 0 in consecutive ,then 0.3 is appended in the count_set...."""
    count_set = []

    for i in range(x.shape[0]):
        for j in range(x.shape[1]):
            if conditon1:
                count_set.append(0.3)
            elif condition2:
                count_set.append(0.1)
            else:
                count_set.append(0.5)

    
    return np.cumsum(count_set)

Should help you find what you are looking for.

Homer
  • 398
  • 2
  • 6
1

First I'd capture the logic in a lookup table, not with if ... else statements. Uncomment the print statements to see what's happening if it helps.

A non numpy solution (except numpy required for cumsum):

import numpy as np

input_sample = np.array([[1,0,1,0],[1,1,0,0]])

lookup = { ( 0, 0 ): 0.3, ( 0, 1 ): 0.1, ( 1, 0 ): 0.1, ( 1, 1 ): 0.5 }

def count( x ):
    res = []
    for row in x:  # Loop through each row
        for key in zip( row[:-1:2], row[1::2] ): # within the row loop through pairs.
            # print( key, lookup[ key ] )
            res.append( lookup[ key ] )  # Lookup the value corresponding to the pair.
    return np.array( res )

count( input_sample )
# array([0.1, 0.1, 0.5, 0.3])

count( input_sample ).cumsum()
# array([0.1, 0.2, 0.7, 1. ])

A numpy solution:

import numpy as np

#                      (0,0) (0,1), (1,0), (1,1) # Map Binary pairs to integers.
#                         0     1      2      3  # Map integers to required floats.
np_lookup = np.array( [ 0.3,  0.1,   0.1,   0.5 ]) 

def np_count( x ):
    # Convert x pairs to integer indices. 
    indices = 2*x[ :, :-1:2 ] + x[ :, 1::2 ] # (0,0) => 0, (0,1) => 1, (1,0) => 2, (1,1) => 3
    # print( indices )
    return np_lookup[ indices ]

np_count( input_sample )
# array([[0.1, 0.1],
#        [0.5, 0.3]])    

np_count( input_sample ).cumsum()
# array([0.1, 0.2, 0.7, 1. ])

I'm not certain this does what's required but it should point in the right direction. I find lookup tables much easier to maintain or understand than a chain of if, elif, else logic.

Tls Chris
  • 3,564
  • 1
  • 9
  • 24