There is the following calculation result:

1.103567 -0.015367 0.000616 -0.023323 1.138369 -0.018582 -0.003442 0.004548 1.195153 

The device converts these numbers as follows:

 [1.104, -0.01537, 0.000616, -0.02332, 1.138, -0.01858, -0.003442, 0.004548, 1.195] 

For further work, it is necessary to compare the calculations and the numbers inside the device, so there was a need to round up the results of the calculations in the same way as it is done on the device. Round does not give a satisfactory result.

enter image description here

where m is the datarame.

  • What do you mean by "not giving a satisfactory result"? - Alexey Prokopenko
  • @AlexeyProkopenko m.round (3) 1.104 -0.015 0.001 -0.023 1.138 -0.019 -0.003 0.005 1.195 - Shalom Alecheim
  • And what result do you want to get? - Alexey Prokopenko
  • @AlexeyProkopenko such as in the list [1.104, -0.01537, 0.000616, -0.02332, 1.138, -0.01858, -0.003442, 0.004548, 1.195] - Shalom Alecheim
  • 2
    What is m ? (What type: type(m) ?) You probably want numpy.allclose(a,b, 1e-4,0) - jfs

3 answers 3

Comrade in the comments suggested another good way, which is much more preferable for large data arrays: So, we have the result of calculations m and data from the newmatrix device. They need to be compared.

 import numpy as np import pandas as pd m 0 1 2 0 1.099394 -0.015754 0.006890 1 -0.025979 1.119590 -0.002269 2 -0.021262 0.005393 1.191073 type(m) <class 'pandas.core.frame.DataFrame'> newmatrix [1.099, -0.01575, 0.00689, -0.02598, 1.12, -0.002269, -0.02126, 0.005393, 1.191] type(newmatrix) <type 'list'> 

We newmatrix to the DataFrame format

 mat=np.array(newmatrix) mat=mat.reshape((3,3)) mat=pd.DataFrame(mat) mat 0 1 2 0 1.09900 -0.015750 0.006890 1 -0.02598 1.120000 -0.002269 2 -0.02126 0.005393 1.191000 

And then with one function we make a comparison:

 numpy.allclose(m, mat, 1e-3,0) True 

    For your data, I added a working version.

     import math def SpecialRaund(val, n): exponent = 0 while abs(val) < 1: val *= 10 exponent -= 1 val = round(val, n) val *= math.pow(10, exponent) return val print SpecialRaund(1.103567, 3) print SpecialRaund(-0.015367, 3) print SpecialRaund(0.000616, 3) 

    Result:

     1.104 -0.01537 0.000616 

    The SpecialRaund does not consider a case with a positive order number (in terms of mantis-order). In this way

     print SpecialRaund(11035.678934, 3) 

    Result:

     11035.679 

    but I think you understand the idea and add this function easily

      Stumbled upon this method:

       float('%.4g' % 1.195153) 1.195 

      checked on all the numbers, just what was needed