+3 votes
in Programming Languages by (77.2k points)

I am running the following code to generate bin counts. But the line "if int(psx[i]*100) not in prob_bins" returns error: TypeError: only size-1 arrays can be converted to Python scalars.

What is wrong with the code?

In the code, variables y_true and psx are 1D arrays.

    for j in range(len(y_true)):

        if y_true[j] == 0:

            if int(psx*100) not in prob_bins:

                prob_bins[int(psx*100)] = 1

            else:

                prob_bins[int(psx*100)] += 1

1 Answer

+2 votes
by (354k points)
selected by
 
Best answer
In your code, the variable "psx" is a list and you are trying to multiply the list by 100. Therefore, your code is returning the error.
To fix the error, select one item at a time from the list. Make the changes as shown below and it should work.

    for j in range(len(y_true)):
        if y_true[j] == 0:
            if int(psx[j]*100) not in prob_bins:
                prob_bins[int(psx[j]*100)] = 1
            else:
                prob_bins[int(psx[j]*100)] += 1


...