Home > front end >  NumPy array with values of 16 digits after the decimal precision
NumPy array with values of 16 digits after the decimal precision

Time:10-24

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)

CodePudding user response:

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]
  • Related