'Apply a function to three parallel array of arrays

I have three arrays of arrays like this:

catLabels = [catA, catB, catC]

binaryLabels = [binA, binB, binC]

trueLabels = []
trueLabels.extend(repeat(y_true_categories, len(binaryLabels)))

def binaryConversion(trueLabel, evalLabel, binaryLabel):
    for true,eval, binary in zip(trueLabel, evalLabel, binaryLabel):
        if eval == true:
            binary.append(1)
        else:
            binary.append(0)

for x,y,z in zip(trueLabels,catLabels,binaryLabels):
    binaryConversion(x, y, z)

Each of the values in catLabels and binLabels is an array. binLabels contain an array of empty arrays, each of which I want to fill in 1s and 0s lets say for example catA = [A B C A B D] and binA = []. trueLabels contains multiple arrays each of which are the same (y_true_categories, i.e. my true categorical labels [A C C B B D]. In this case, my binaryConversion function should fill in [1 0 1 0 1 1] for the array binA.

For some reason my current function is not achieving this and leaves each of bin A, binB, binC empty.

What am I doing wrong?



Solution 1:[1]

I have figured out the answer. The inner zip statement will not work because I start with empty binary labels, and zip only works when all the arrays you are zipping are of the same length. So I removed the binaryLabel from the zip function within binaryConversion(trueLabel, evalLabel, binaryLabel) and appended to each binaryLabel empty binary array inside the loop. In addition, I was appending 1s and 0s to the element-wise binary, instead of the actual empty array binaryLabel.

New code:

def binaryConversion(trueLabel, evalLabel, emptyBinaryArray):
    for true,eval in zip(trueLabel, evalLabel):
        if eval == true:
            emptyBinaryArray.append(1)
        else:
            emptyBinaryArray.append(0)

for trueLabels,predictionLabels,emptyBinaryArray in zip(trueLabels,catLabels,binaryLabels):
    binaryConversion(trueLabels, predictionLabels, emptyBinaryArray)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dhruv Ghulati