Skip to content
Advertisement

How to fix list comprehension ‘bitwise_and’ error and optimize for-loop?

I have the following for loop below, but I want to make this into a more computationally efficient variant. I thought I could do that with list comprehension, but this is giving me the following error: TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

EDIT I: I am trying to compare input1 and input2 and if input1 is larger than than input2, then the difference should be squared and scaled by the scaler. Otherwise, a value of zero should be assigned to the output.

How can I fix this and are there any other way to speed this up even more?

JavaScript

Advertisement

Answer

If you are only trying to optimise the for loop with a list comprehension, the following is equivalent:

JavaScript

EDIT: This is probably faster as numpy can vectorise the operation

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement