'<' not supported between instances of 'str' and 'int' >>> Not an input problem!

Asked

Viewed 531 times

0

The idea is to create a matrix of confusion. yhat_tr has a hundred values(float), positive and negative. d_tr is a column inside an excel file with the same amount of items as yhat_tr. d_tr values are distributed for each idem, but with values 1 and -1.

#TP - TruePositive, FP: FalsePositive, TN: TrueNegative, FN: FalseNegative
TP, FP, TN, FN = 0,0,0,0

for i in yhat_tr:
  for a in d_tr:
    if i > 0 and a > 0:
      TP += 1

    elif i > 0 and a < 0:
      FP += 1

    elif i < 0 and a < 0:
      TN += 1

    else:
      FN += 1

print (FP)

The attempt is for each item of the column y_hat-tr, if the value of yhat_tr is greater than 0 and also its value in d_tr is greater than 0, it adds 1 in the value of TP. Otherwise sum in the other variables like the above rule.

  • 1

    There’s a string where you say there’s only a float...

  • YES!!! You found the issue. d_tr is bringing the header together with the data. When I replace it with another data source the code works. Thanks! Now I just have to figure out how to import without this column caption.

  • So that’s what the error message is saying, you can’t compare str with int.

1 answer

-1


I’m new in the area too, but I think this loop for which you used is for string list. For number list I would use:

n = len(yhat_tr)
m = len(d_tr)

for i in range(n):
    for a in range(m):
        if yhat_tr[i]>0 and d_tr[a]>0:
            TP += 1
  • It is almost never used with "range" in Python - in general it is not necessary. Code formatting and indentation are also not optional in the language.

  • Thanks, but it didn’t work.... Gave another error (error 0), and always appears when nesting these two loops (n and m).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.