Home > Blockchain >  numpy.sum with conditional
numpy.sum with conditional

Time:06-13

In the following code:

def compute_class_freqs():
    """
    Compute positive and negative frequences for each class.

    Returns:
        positive_frequencies (np.array): array of positive frequences for each
                                         class, size (num_classes)
        negative_frequencies (np.array): array of negative frequences for each
                                         class, size (num_classes)
    """
    ### START CODE HERE (REPLACE INSTANCES OF 'None' with your code) ###
    labels = [[0,1,0],[1,1,1],[0,1,1]]
    print(labels)
    # total number of patients (rows)
    N = labels[0]
    
    positive_frequencies = None
    negative_frequencies = None

    ### END CODE HERE ###
    return positive_frequencies, negative_frequencies

I want to sum the number of 1's in each row and append each sum to positive_frequencies, and sum the number of 0's in each row and append each sum to negative_frequencies. How can I do this using the numpy functions numpy.sum() and numpy.where()?

Edit: positive_frequencies should be the number of 1s in each column divided by the total number of rows and negative frequencies the number of 0s in each column divided by the total number of rows. Basically, the function should return numpy arrays of floats.

CodePudding user response:

I think this might work, i know there is already an answer in your comments and this doesn't use np.sum or np.where, but i thought i'd share how i'd do it:

labels = [[0,1,0],[1,1,1],[0,1,1]]

positive_frequencies = [len([num for num in listOfNums if num == 1]) for listOfNums in labels]
negative_frequencies = [len([num for num in listOfNums if num == 0]) for listOfNums in labels]

print(positive_frequencies, negative_frequencies)

which got me an output of:

[1, 3, 2] [2, 0, 1]
  • Related