Python code for counting number of zero crossings in an array

前端 未结 6 1471
春和景丽
春和景丽 2021-02-08 01:44

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:<

6条回答
  •  Happy的楠姐
    2021-02-08 02:30

    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
    

提交回复
热议问题