In this article, we will discuss how to fix runtimewarning: invalid value encountered in double_scalars using Python.
The error which we basically encounter when we use the Numpy library is Runtimewarning: invalid value encountered in doubled_scalars. We face this error basically when we do mathematical operations on a list of very large numbers or vary the small number and when we supply any invalid input to NumPy operation like NaN or null as input.
This error simply occurs when we performing a math operation and we encounter which is not valid as input. When we perform some complex mathematical operation that requires a very large number or vary small number some libraries cannot handle such a large number so it throws an error. It turns these numbers to null or NaN which causes an error in operation.
Steps need to prevent this error:
- The easiest way to prevent this error is to use the function which is able to handle that big number so the operation cannot throw an error.
- Or in place or complex mathematical function use built-in function so we avoid human error.
- We can do some mathematical change in operation so the value should not rise above value so it can’t raise an error.
Here we will see some examples which raise the error and see some solutions.
Method 1: Using if
Program to show error code
Python
# In this program we are demonstrating how wrong # input course invalid value # encountered in double_scalars import numpy array1 = [ 1 , 2 , 4 , 7 , 8 ] # this input array causes error array2 = [] Marray1 = numpy.mean(array1) # this line causes the error Marray2 = numpy.mean(array2) print (Marray1) print (Marray2) |
Output:
RuntimeWarning: Mean of empty slice.
RuntimeWarning: invalid value encountered in double_scalars
We can fix this error if we check the array before calculating the mean of the array that it providing the valid input or not:
Syntax:
if array1:
expression;
Fixed code
In this program, we are demonstrating how wrong input course invalid value encountered in double_scalars
Python
import numpy array1 = [ 1 , 2 , 4 , 7 , 8 ] # this input array causes error array2 = [] # Here we check error if array1 and array2: print ( "Mean of the array 1 is : " , numpy.mean(array1)) print ( "Mean of the array is :" , numpy.mean(array2)) else : print ( "please Enter valid array" ) |
Output:
please Enter valid array
Method 2: Using numpy.special.logsumexp
Python program showing invalid error encounter in double scaler.
Python
import numpy as np from numpy import sinh x = 900 y = 711 # This operation raise error sol1 = np.log(np. sum (np.exp(x))) / np.log(np. sum (np.exp(y))) print (sol1) |
Output:
main.py:14: RuntimeWarning: overflow encountered in exp
sol1 = np.log(np.sum(np.exp(x)))/np.log(np.sum(np.exp(y)));
main.py:14: RuntimeWarning: invalid value encountered in double_scalars
sol1 = np.log(np.sum(np.exp(x)))/np.log(np.sum(np.exp(y)));
nan
Here we have seen that error cause this because NumPy library cannot handle this large number on so complex a structure so we have to use some built-in function that can handle small numbers. For this purpose, we use numpy.special.logsumexp function which is use for calculating the value for expression “np.log(np.sum(np.exp(x)))” :
Syntax: numpy.special.logsumexp(x);
Example: Fixed code
Python
# Python program showing # invalid error encounter in double scaler import numpy as np from scipy.special import logsumexp x = 900 y = 711 # Solution of the error with the # help of built-in function sol1 = logsumexp(x) - logsumexp(y) # sol1 = np.log(np.sum(np.exp(x)))/np.log(np.sum(np.exp(y))) print ( "Now we can print our Answer :" ) print (sol1) |
Output:
Now we can print our Answer : 189.0