Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python for proper rounding

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use Python for correct rounding, the content is concise and easy to understand, can definitely make your eyes bright, through the detailed introduction of this article, I hope you can learn something.

This paper mainly shares a crawl experience of pandas,numpy, a data analysis tripartite library based on python, finds and analyzes the problem that python language is not accurate in dealing with floating point precision, and finally gives a reasonable solution. If you are also using python to process data, it is recommended to take a look, after all, 0.1 error may cause a greater impact.

Problems arise

When I arrived at the company in the morning, the leader sent several documents, saying that the data from the test environment in the past two days were different from the actual situation, to see which problem went wrong and to solve it as soon as possible.

Start troubleshooting.

Compared with the data first, it is found that not all the data have problems, only about 10% of the data have this problem, indicating that it should not be a logical problem. The preliminary judgment may require special treatment for individual cases, resulting in poor consideration.

Check and sort out each operation module, debug a wave with debug breakpoint, and determine the module in which the data has deviation.

By testing this unit module separately, it is finally determined that there is a problem where the division result of two numbers is 0.5 (floating point number).

Expected result: np.round (0.5) = 1, actual result: np.round (0.5) = 0, so I did the following experiment

# based on python3.7 version > import numpy as np # look at 0 first

< x < 1 这个范围的结果,发现有问题 >

> > np.round (0.500.0) > np.round (0.511.0 > np.round (0.49) 0.0 # I am worried that only the decimal point is .5, so I tested the result of x > 1, and found that there are still problems > np.round (1.52.0 > np.round (2.5i) > > np.round (3.54.0 > np.round (4.54.0)).

By comparison, it is found that the value of .5 is a little different from what was expected. Let's see why.

Analyze the problem

It is true that there is a misunderstanding about floating point numbers (.5). See how the official documentation explains this phenomenon.

Numpy.around (a, decimals=0, out=None) [source] Evenly round to the given number of decimals. # for intermediate values (.5) that are exactly between the decimal values that are rounded, NumPy rounds to the nearest even value. # so 1.5 and 2.5 are rounded to 2.0 and 0.5 are rounded to 0.0, and so on. For values exactly halfway between rounded decimal values, NumPy rounds to the nearest even value. Thus 1.5 and 2.5 round to 2.0,-0.5 and 0.5 round to 0.0, etc. # np.around uses fast but sometimes imprecise algorithms to round off floating-point data types. For positive decimals, it is equivalent to np.true_divide (np.rint (a * 10 * * decimal), 10 * * decimal), # due to the IEEE floating point standard [1] and the error np.around uses a fast but sometimes inexact algorithm to round floating-point datatypes introduced when scaling to the tenth power. For positive decimals it is equivalent to np.true_divide (np.rint (a * 10**decimals), 10**decimals), which has error due to the inexact representation of decimal fractions in the IEEE floating point standard [1] and errors introduced when scaling by powers of ten

In fact, that is to say: for values with .5, which are right in the middle, adjacent even values are returned.

Vernacular explanation: if a number has a floating point number (.5) and the integer part is even, the even number is returned; if the integer part is odd, the even number of this odd number + 1 is returned.

Explanation of the law: if the integer part is divisible by 2, the integer part is returned; if the integer part is not divisible by 2, the integer part + 1 is returned.

Solve the problem

Let's take a look at the data errors without making any changes.

# in order to look at the phenomenon first, we construct the following case import pandas as pd import numpy as np df = pd.DataFrame ({"num1": [1,1,1.5,5,7.5], "num2": [2,3,1,6) 3]}) df ["real value"] = df ["num1"] / df ["num2"] # look at the result df ["deviation"] = np.round (df ["num1"] / df ["num2"]) after the round function

The picture of the original result is as follows

If there is no treatment, the expected value and deviation value will be different.

My solution

According to my precision requirements, I construct the last decimal place of the precision range, and judge whether it needs to be rounded up by whether the number is 5.

For example, in this case, I only need to keep the integer part of the data, so I just need to determine whether the first place after the decimal point is the number 5.

Upper code

Import pandas as pd import numpy as np import math df = pd.DataFrame ({"Divisor": [1,1,1.5,5,7.5], "Divisor": [2,3,1,6) 3]}) # record the real value df ["real value"] = df ["divisor"] / df ["divisor"] # record the integer part df ["auxiliary integer column"] = df ["real value"] .apply (lambda x: math.modf (x) [1]) # record the decimal part, because my final result precision is to keep only the integer part So I just need to keep one decimal place to determine whether the carry operation df ["auxiliary decimal series"] = df ["real value"] .apply (lambda x: str (math.modf (x) [0]). Split (".") [1] [0]) # the first place after the decimal point is 5 If it is not 5, then call the original np.round and df ["expected value correction"] = df.apply (lambda x: X. Auxiliary integer column + 1 if (x. Auxiliary decimal series = "5") else np.round (x. True value), axis=1)

The results are as follows

The expected correction has achieved the above, that is, how to use Python for correct rounding, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report