Beth Long
Beth Long

Reputation: 403

np.nan_to_num causes error RuntimeWarning: invalid value encountered in true_divide

I've got some data in np.ndarrays that I want to normalise to be between 0 and 1. I asked here what the best way to do it was, and how to avoid dividing by 0, and someone told me the best way was to use np.nan_to_num(). That seemed to work, I don't think I had any problems with it. Since that previous question, my code has evolved, and I now want to do the same thing with the three different arrays trainingsignals, validationsignals and testsignals:

TrainingSigMaxes = np.max(trainingsignals, axis = 1)
TrainingNormSignals=np.nan_to_num(trainingsignals/TrainingSigMaxes[:,np.newaxis])

ValidationSigMaxes = np.max(validationsignals, axis = 1)
ValidationNormSignals=np.nan_to_num(validationsignals/ValidationSigMaxes[:,np.newaxis])

TestSigMaxes = np.max(testsignals, axis = 1)
TestNormSignals=np.nan_to_num(testsignals/TestSigMaxes[:,np.newaxis])

But when I run the code it gives me the error message: "RuntimeWarning: invalid value encountered in true_divide".

Can anyone help me solve this issue? Thanks a lot in advance.

Upvotes: 1

Views: 632

Answers (1)

Rahul Vishwakarma
Rahul Vishwakarma

Reputation: 1456

It is just a warning. You will still get the 'approximately correct' output.

If you want to hide warnings, you could run this beforehand:

import warnings
warnings.filterwarnings("ignore")

or for more customization, you could refer this.

Note : 'Approximately correct' value is based on the function numpy.nan_to_num. You could refer the documentation.

Upvotes: 2

Related Questions