1

So I have changed my previous code

if advice[i] == real_choice[i]:
        correct[i] = "CORRECT"
      else:
        correct[i] =  "INCORRECT"

to

correct = np.where( advice == real_choice, "CORRECT", "INCORRECT")

but now my code after this, that calculates the number of occurrences of the string "INCORRECT" in correct, no longer works:

num_incorrect = correct.str.count("INCORRECT").sum()

how can I do the above line in a way that is compatible with the new method?

jangles
  • 303
  • 1
  • 6
  • 22

2 Answers2

2

np.where returns a numpy array, not a pandas series. So you want:

(correct=='INCORRECT').sum()
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • That works, and it actually made me realize that it was indeed very simple and I was doing something else wrong. Thank you – jangles Dec 17 '20 at 05:32
0
# numpy array
(correct == "INCORRECT").sum()

dict(zip(*np.unique(correct, return_counts=True)))["INCORRECT"]

# list
list(correct).count("INCORRECT")

or

correct = np.where( advice[i] == real_choice[i], 1, 0)

incorrect_size = correct.size - correct.sum()
conderls
  • 1
  • 2