[Python] numpy.absolute returns negative numbers

Discussão em 'Python' iniciado por Stack, Março 11, 2025.

  1. Stack

    Stack Membro Participativo

    I am using np.absolute to try to find datasets that have meaningful data. The data includes
    negative numbers and I am trying to use the mean to decide wether it is meaningful. However, I don't want the negative values to cancel out the positive ones and give me a mean close to zero. Hence I use np.absolute, and take the mean of that. My code wasn't finding some datasets to be meaningful that I knew were, so I did some troublshooting, and found that np.absolute() was returning negative values! How can this be? Here is a shortened version of the program. I tested it and it has the same problem as the full version.

    import h5py
    import numpy as np


    with h5py.File('<filename>.h5', 'r') as hdf:
    timestream_data = hdf.get('timestream')
    timestream = np.array(timestream_data)
    for x in timestream:
    timestream_list = timestream.tolist()
    dataset_as_list = x.tolist()
    print("Index: %d" % timestream_list.index(dataset_as_list), x, np.absolute(x))


    And the output (I'll only include one of the lines that shows the error): Index: 32 [ 127 -128 -128 ... -128 -128 127] [ 127 -128 -128 ... -128 -128 127]

    Notice the last thing it should be printing on that line is np.absolute(x)... but there are negative values in the array...

    Continue reading...

Compartilhe esta Página