How to Fix: runtimewarning: invalid value encountered in double_scalars

18 Feb, 2022
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.  
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.
Program to show error code 
Python
# In this program we are demonstrating how wrong# input course invalid value# encountered in double_scalarsimport numpy array1 = [1, 2, 4, 7, 8] # this input array causes errorarray2 = [] Marray1 = numpy.mean(array1)# this line causes the errorMarray2 = 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 of not:
Syntax:
if array1:
   expression;
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 errorarray2 = [] # Here we check errorif 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
Python program showing invalid error encounter in double scaler.
Python
import numpy as npfrom numpy import sinh x = 900y = 711 # This operation  raise errorsol1 = 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 npfrom scipy.special import logsumexp x = 900y = 711  # Solution of the error with the# help of built-in functionsol1 = 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
adnanirshad158
germanshephered48
kk9826225
Picked
Python How-to-fix
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Check if element exists in list in Python
Python | os.path.join() method
How To Convert Python Dictionary To JSON?
Python | Get unique values from a list
Defaultdict in Python