How do I remove all zero elements from a NumPy array?

前端 未结 6 465
逝去的感伤
逝去的感伤 2020-12-24 07:14

I have a rank-1 numpy.array of which I want to make a boxplot. However, I want to exclude all values equal to zero in the array. Currently, I solved this by loo

6条回答
  •  心在旅途
    2020-12-24 07:33

    I would like to suggest you to simply utilize NaN for cases like this, where you'll like to ignore some values, but still want to keep the procedure statistical as meaningful as possible. So

    In []: X= randn(1e3, 5)
    In []: X[abs(X)< .1]= NaN
    In []: isnan(X).sum(0)
    Out[: array([82, 84, 71, 81, 73])
    In []: boxplot(X)
    

    enter image description here

提交回复
热议问题