0

I am trying to hold the precision of a calculated field when converting two variables to a array. When calculating the value it is data type of <class 'numpy.float64'> and when converting to an array it remains data type of <class 'numpy.float64'>, however the value moves from 16 numbers -0.2484613592984996 after the decimal to 5 numbers -0.24846 after the decimal respectively.

Here is the code I am using and I tried to use float when creating the array to maintain the data type:

ham_log = np.log(ham / data_len)
spam_log = np.log(spam / data_len)

log_class_priors = np.array([ham_log, spam_log]).astype(float)
N K
  • 147
  • 7
  • Does this answer your question? [Printing numpy.float64 with full precision](https://stackoverflow.com/questions/12956333/printing-numpy-float64-with-full-precision) – yann ziselman Oct 24 '21 at 08:11
  • No, I would not like to change the information to a string, I would like to maintain the 16 decimal digits when converting it to an array and not only 5 – N K Oct 24 '21 at 08:19
  • Check this thread. Numpy.float128 should address your issue. https://stackoverflow.com/questions/6663272/double-precision-floating-values-in-python – DeepakP Oct 24 '21 at 08:28
  • @NK Look at the second answer in the thread I referred you to. The precision of the array does not change. what changes is how the values are printed. if you set the printing options to print 16 decimal digits you'll see that. – yann ziselman Oct 24 '21 at 08:37

1 Answers1

2

As i explained in my comment, the precision of the array does not change. What changes is how the values are printed. Here's an example:

import numpy as np
np.random.seed(0)
a = np.random.rand(2)
print(f'a = \n{a}')

Output:

a = 
[0.5488135  0.71518937]

But when i change numpy's print options here's what i get:

np.set_printoptions(precision=16)
print(f'a = \n{a}')

Output:

a = 
[0.5488135039273248 0.7151893663724195]
yann ziselman
  • 1,952
  • 5
  • 21