Anomaly detection: selecting threshold section

In the additional hints of the selecting threshold section of the assignment, there was part that we could use and operator and in the code used “fp = sum((predictions == 1) & (y_val == 0))”, I guess and and & operators in python have different meaning. And when I changed the code to fp = sum((predictions == 1) and (y_val == 0)) it raises error. So can anyone explain it to me why we are using & why the logical and operator not working. Thank you.

Hello @fishawa! Welcome!

and compares two boolean variables, whereas & compares two arrays of booleans element-wise. I have 2 suggestions for you:

  1. Google “python boolean operator and” and “python numpy element wise and” to start your research on them.
  2. Test them with some simple code example created by yourself, such as
True and False
True & False
np.array([True, False]) and ....


Oh, thank you very much, Reymond. It’s clear now, the “&” operator in Numpy is the shorthand of “np.logical_and”, which is used for elementwise comparing boolean arrays. I didn’t know that. Thanks again.