I am looking to count the number of times the values in an array change in polarity (EDIT: Number of times the values in an array cross zero).
Suppose I have an array:<
Here's a numpy
solution. Numpy's methods are generally pretty fast and well-optimized, but if you're not already working with numpy
there's probably some overhead from converting the list to a numpy
array:
import numpy as np
my_list = [80.6, 120.8, -115.6, -76.1, 131.3, 105.1, 138.4, -81.3, -95.3, 89.2, -154.1, 121.4, -85.1, 96.8, 68.2]
(np.diff(np.sign(my_list)) != 0).sum()
Out[8]: 8