1

RuntimeWarning: invalid value encountered in multiply

I have a code:

a = Y_list * np.log(Y_list/E_Y) print(a) 

My Y_list contains 0 values, I'm wondering how to do when Y_list = 0 , np.log(0) = 0?

4
  • The logarithm of 0 in any base is undefined. Not zero. Commented Aug 30, 2021 at 12:56
  • 2
    hi thanks for the reply! im actually doing a school assignment and they require such a condition..'If Y = 0, the expression log[Y/(E(Y))] will be taken as zero' Commented Aug 30, 2021 at 12:58
  • are Y_list and E_Y numpy arrays? you can use np.where or an inline if depending on the answer. Commented Aug 30, 2021 at 13:02
  • yup both are numpy arrays! Commented Aug 30, 2021 at 13:03

2 Answers 2

3

You can use np.where It lets you define a condition for true and false and assign different values.

np.where((Y_list/E_Y)!= 0, np.log(Y_list/E_Y),0) 
Sign up to request clarification or add additional context in comments.

Comments

1

Alternatively, we can run np.log with a where parameter:

import numpy as np a = np.arange(0, 5000, 1000) np.log(a, where=a != 0) # array([0. , 6.90775528, 7.60090246, 8.00636757, 8.29404964]) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.