Runtime warning in power of np.array

Asked 2 years ago, Updated 2 years ago, 46 views

Please let me know as I was at a loss because the power of the 2D array of numpy (power by element) could not be successful.

Environment:
Python: 3.7.5
Number: 1.17.4

If you try to multiply the float32 type 2D np.array (which contains positive or negative real values, some NaNs) by 1.514, both `** and np.power()` will get the following error and return np.array containing values that are not 1.514 multiplied by the element.

RuntimeWarning: invalid value encountered in power

The foo is a float32 type, obtained in NetCDF format from a weather data distribution server.

If you take out one element and multiply it by 1.514, you will also get Runtime warning in the following description.

 foo[0][0]** 1.514
(foo: 2D np.array)

However, creating an array var in which elements are extracted and re-stored one by one in a loop using the following method, and var**1.514 returns a normal value.

 var=np.zeros(len(lat),len(lon))
for i in range (len(lat)) :
    for jin range (len(lon)) :
        var[i][j]=foo[i][j]
(foo is the original np.array)

This method will solve the problem for now, but it's creepy, so could you tell me the cause?
Thank you for your cooperation.

python numpy

2022-09-30 11:41

1 Answers

The numPy power() function does not seem to support negative or complex power.
I got caught up in the same thing and got to this question, but as I went through a lot of things, I found a similar question on the English page and made a similar guess.

This problem probably doesn't occur when squared, but if it's the 1.7th power, it's an error even if the target is not negative.

As a countermeasure for me, abs() takes the absolute value, which makes it a little less efficient, but I was able to deal with it.

z=np.power(abs(x),y)
z=abs(x)**y

That's what it looks like.


2022-09-30 11:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.