I'm trying to perform a precise calculation for linear regression using only one digit as precise number. without numpy it works just fine but numpy performs better for large amount of items that's why I need use numpy. But the issue is that when I build the matrix for the X axis I lose my decimal precision as you can see bellow.
How can I fix it? I mean, to the matrix variable return only one digit as precise number.
import numpy as np
import pandas as pd
dataset = [[17.3,71.7],[19.3,48.3],[19.5,88.3]]
df = pd.DataFrame({
'force': [item[0] for item in dataset],
'push_up':[item[1] for item in dataset]
})
df_x = np.array([item for item in df['force']],dtype=np.float16)
df_y = np.array([item for item in df['push_up']],dtype=np.float16)
print([np.round(item, decimals=1) for item in df['force']])
#check precision
#here is the issue! the return lose my 1 decimal point precision.
# notice !No matter if I use this printed array above.
# also tried using this array construction to reconvert to 1 decimal precision but no success
#print( [np.float16(np.format_float_positional(item, precision=1)) for item in df['force']] )
matrix = np.vstack([df_x, np.ones(len(df_x))]).T
print(matrix[0][0])
#this print "17.296875" that is totally different from 17.3
#print(matrix[2][0]) #uncomment this to see that the half precision is not lost at all
CodePudding user response:
To control dtype
in concatenate
(and all 'stack'), the arguments have to match:
In [274]: np.vstack([np.array([1,2,3], 'float16'), np.ones(3,'float16')])
Out[274]:
array([[1., 2., 3.],
[1., 1., 1.]], dtype=float16)
Default dtype for ones
is float64
:
In [275]: np.vstack([np.array([1,2,3], 'float16'), np.ones(3)])
Out[275]:
array([[1., 2., 3.],
[1., 1., 1.]])
In [276]: _.dtype
Out[276]: dtype('float64')
But as noted in the comments, use of float16
is only superficially a rounding.
In [278]: np.vstack([np.array([1.234235,2.9999,3], 'float16'), np.ones(3,'float16')])
Out[278]:
array([[1.234, 3. , 3. ],
[1. , 1. , 1. ]], dtype=float16)
The transpose does not change values or dtype.