Ignoring -Inf values in arrays using numpy/scipy in Python

前端 未结 5 539
说谎
说谎 2020-12-25 14:11

I have an NxM array in numpy that I would like to take the log of, and ignore entries that were negative prior to taking the log. When I take the log of negative entries, it

5条回答
  •  天涯浪人
    2020-12-25 14:37

    maybe you can index your matrix and use:

    import numpy as np;
    matrix = np.array([[1.,2.,3.,np.Inf],[4.,5.,6.,np.Inf],[7.,8.,9.,np.Inf]]);
    print matrix[:,1];
    print sum(filter(lambda x: x != np.Inf,matrix[:,1]));
    print matrix[1,:];
    print sum(filter(lambda x: x != np.Inf,matrix[1,:]));
    

提交回复
热议问题