简体   繁体   中英

Numpy element-wise comparisons

I'm trying to do something like this:

I[ xr1,c1 > xr2,c2 ]

where r i , c i ∈ {0,1,2,...,n} are the row and column indices, respectively, and I[·] is an indicator function whose value is 1 if the condition is true and 0 otherwise.

I'm having trouble getting this set up. I can easily iterate through everything by:

for r1 in range(0,n):
    for c1 in range(0,n):
        for r2 in range(0,n):
            for c2 in range(0,n):
                #do something with (r1,c1,r2,c2)

However I'm trying to vectorize this code to increase efficiency and make the comparisons in one fellow swoop. How can I do this? I'm using numpy .


explanation update:

i am training a simple smile classifier that analyzes a grayscale image x ∈ R 24×24 and outputs a prediction y ∈ {0,1}, indicating whether the image is smiling (1) or not (0). the classifier will make its decision based on very simple features of the input image consisting of binary comparisons between pixel values.

each feature is computed as:

I[ xr1,c1 > xr2,c2 ]

where r i , c i ∈ {0,1,2,...,n} are the row and column indices, respectively, and I[·] is an indicator function whose value is 1 if the condition is true and 0 otherwise. (assume n is defined)

based on what i've mentioned so far, i'm trying to train an ensemble smile classification for 5 features. the output of the ensemble is 1 if it thinks the image is smiling, 0 otherwise. this is determined by the average prediction across all 5 members of the ensemble. if more than half of the 5 ensemble predictors think that the image is smiling, then the ensemble says it's a smile; otherwise, the ensemble says it's not smiling.

this is what i'm trying to do:

at each round j, i want to choose the jth feature (r1,c1,r2,c2) such that – when it is added to the set of j−1 features that have already been selected – the accuracy of the overall classifier on the training set is maximized. more specifically, at every round j ,i'm considering every possible tuple of pixel locations (r1,c1,r2,c2) if thecurrent tuple is the best yet (for round j), then i save it as my “best seen yet” for round j. if not, i ignore it. i then move on to the next possible tuple of pixel locations, and repeat until i've searched all of them. At the end of round j, i will have selected the best feature for that round, and i add it to my set of selected features. once added, it stays in the set forever – it can never be removed. (otherwise, it wouldn't be agreedy algorithm at all.) then i move on to round j+ 1 until you've completed all 5 rounds rounds.

i don't need help with this entire thing. i'm just looking for a way to vectorize code without having to use nested for loops

Take advantage of broadcasting :

first = np.reshape(array, (*array.shape, 1, 1))
second = np.reshape(array, (1, 1, *array.shape))

result = (first > second)

Alternatively, we can use np.greater.outer , which performs a greater-than comparison on every unique permutation of elements:

result = np.greater.outer(array, array)

This will produce an np.ndarray of shape (*array.shape, *array.shape) , where result[r1, c1, r2, c2] is the value you want.

Example:

array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

first = np.reshape(array, (*array.shape, 1, 1))
second = np.reshape(array, (1, 1, *array.shape))

result = (first > second)

print(result[0, 0, 1, 1]) # 1 > 6
print(result[1, 1, 0, 1]) # 6 > 2
print(result[0, 3, 0, 2]) # 3 > 2

Output:

False
True
True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM