偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

如何使用Python進(jìn)行正確的四舍五入?這個(gè)坑有點(diǎn)大

開發(fā) 后端
本文主要分享基于python的數(shù)據(jù)分析三方庫(kù)pandas,numpy的一次爬坑經(jīng)歷,發(fā)現(xiàn)并分析了python語(yǔ)言對(duì)于浮點(diǎn)數(shù)精度處理不準(zhǔn)確的問(wèn)題,并在最后給出合理的解決方案。如果你也在用python處理數(shù)據(jù),建議看一下。

本文主要分享基于python的數(shù)據(jù)分析三方庫(kù)pandas,numpy的一次爬坑經(jīng)歷,發(fā)現(xiàn)并分析了python語(yǔ)言對(duì)于浮點(diǎn)數(shù)精度處理不準(zhǔn)確的問(wèn)題,并在最后給出合理的解決方案。如果你也在用python處理數(shù)據(jù),建議看一下,畢竟0.1的誤差都可能造成比較大的影響。

問(wèn)題出現(xiàn)

早上到了公司,領(lǐng)導(dǎo)發(fā)了幾個(gè)文件過(guò)來(lái),說(shuō)這兩天測(cè)試環(huán)境跑出來(lái)的數(shù)據(jù),與實(shí)際情況有所出入,看看哪出的問(wèn)題,盡快解決···

開始排查

  • 先對(duì)比數(shù)據(jù),發(fā)現(xiàn)并不是所有的數(shù)據(jù)都出現(xiàn)問(wèn)題,只有10%左右的數(shù)據(jù)有這個(gè)問(wèn)題,說(shuō)明應(yīng)該不是邏輯上的問(wèn)題,初步判斷可能為個(gè)別情況需要特殊處理,考慮不周導(dǎo)致
  • 檢查梳理各個(gè)運(yùn)算模塊,用debug斷點(diǎn)調(diào)試一波,確定了數(shù)據(jù)出現(xiàn)偏差的模塊
  • 通過(guò)單獨(dú)測(cè)試這個(gè)單元模塊最終確定,涉及到兩數(shù)相除結(jié)果為0.5(浮點(diǎn)數(shù))的地方有問(wèn)題
  • 預(yù)期結(jié)果:np.round(0.5)=1,實(shí)際運(yùn)算結(jié)果:np.round(0.5)=0,于是我做了如下的試驗(yàn)
  1. # 基于python3.7版本 
  2. >>> import numpy as np 
  3.  
  4. # 先看看 0 < x < 1 這個(gè)范圍的結(jié)果,發(fā)現(xiàn)有問(wèn)題 
  5. >>> np.round(0.50) 
  6. 0.0 
  7. >>> np.round(0.51) 
  8. 1.0 
  9. >>> np.round(0.49) 
  10. 0.0 
  11.  
  12. # 我擔(dān)心是不是只有小數(shù)點(diǎn)為.5的都會(huì)呈現(xiàn)這種問(wèn)題,所以測(cè)試了 x > 1的結(jié)果,發(fā)現(xiàn)還是有問(wèn)題 
  13. >>> np.round(1.5) 
  14. 2.0 
  15. >>> np.round(2.5) 
  16. 2.0 
  17. >>> np.round(3.5) 
  18. 4.0 
  19. >>> np.round(4.5) 
  20. 4.0 

通過(guò)對(duì)比,發(fā)現(xiàn)確實(shí)涉及到.5的值會(huì)有些和預(yù)想的不同,看看啥原因

分析問(wèn)題

確實(shí)發(fā)現(xiàn)了關(guān)于浮點(diǎn)數(shù)(.5出現(xiàn)了理解上的偏差),看看官方文檔怎么解釋這個(gè)現(xiàn)象

  1. numpy.around(a, decimals=0, out=None)[source] 
  2. Evenly round to the given number of decimals. 
  3.  
  4. # 對(duì)于恰好介于四舍五入的十進(jìn)制值之間的中間值(.5),NumPy會(huì)四舍五入為最接近的偶數(shù)值。  
  5. # 因此1.5和2.5四舍五入為2.0,-0.5和0.5四舍五入為0.0,依此類推。 
  6. For values exactly halfway between rounded decimal values,  
  7. NumPy rounds to the nearest even value.  
  8. Thus 1.5 and 2.5 round to 2.0, -0.5 and 0.5 round to 0.0, etc. 
  9.  
  10. # np.around使用快速但有時(shí)不精確的算法來(lái)舍入浮點(diǎn)數(shù)據(jù)類型。 
  11. # 對(duì)于正小數(shù),它等效于np.true_divide(np.rint(a * 10 **小數(shù)),10 **小數(shù)), 
  12. # 由于IEEE浮點(diǎn)標(biāo)準(zhǔn)[1]和 十次方縮放時(shí)引入的錯(cuò)誤 
  13. np.around uses a fast but sometimes inexact algorithm to round floating-point datatypes.  
  14. For positive decimals it is equivalent to np.true_divide(np.rint(a * 10**decimals), 10**decimals),  
  15. which has error due to the inexact representation of decimal fractions in the IEEE floating point standard [1]  
  16. and errors introduced when scaling by powers of ten 
  • 其實(shí)也就是說(shuō):對(duì)于帶有.5這種剛好介于中間的值,返回的是相鄰的偶數(shù)值
  • 白話解釋:如果一個(gè)數(shù)字帶有浮點(diǎn)數(shù)(.5),整數(shù)部分為偶數(shù),則返回這個(gè)偶數(shù);整數(shù)部分奇數(shù),則返回這個(gè)奇數(shù)+1的偶數(shù)
  • 規(guī)律解釋:如果整數(shù)部分能夠整除2,則返回整數(shù)部分;如果整數(shù)部分不能整除2,則返回整數(shù)部分 +1

解決問(wèn)題

先不做任何改動(dòng),看下數(shù)據(jù)誤差的情形

  1. # 我們?yōu)榱讼瓤聪卢F(xiàn)象,構(gòu)造如下案例 
  2. import pandas as pd 
  3. import numpy as np 
  4.  
  5. df = pd.DataFrame({"num1": [1, 1, 1.5, 5, 7.5], "num2": [2, 3, 1, 6, 3]}) 
  6.  
  7. df["真實(shí)值"] = df["num1"] / df["num2"
  8. # 看下round函數(shù)過(guò)后的結(jié)果 
  9. df["偏差值"] = np.round(df["num1"] / df["num2"]) 

原始結(jié)果圖片如下

 

如何使用python進(jìn)行正確的四舍五入?這個(gè)坑有點(diǎn)大
不做處理,期望值和偏差值不等的情況出現(xiàn)

我的解決方案

  • 我根據(jù)我的精度要求,構(gòu)建精度范圍所需要保留的小數(shù)點(diǎn)的最后一位,通過(guò)這個(gè)數(shù)字是否為5,判斷是否需要向上取整
  • 舉例來(lái)說(shuō),本案例中我只需要保留整數(shù)部分的數(shù)據(jù),那么我只需要確定小數(shù)點(diǎn)后第一位是否是數(shù)字5就可以了

上代碼

  1. import pandas as pd 
  2. import numpy as np 
  3. import math 
  4.  
  5. df = pd.DataFrame({"除數(shù)": [1, 1, 1.5, 5, 7.5], "被除數(shù)": [2, 3, 1, 6, 3]}) 
  6.  
  7. # 記錄真實(shí)值 
  8. df["真實(shí)值"] = df["除數(shù)"] / df["被除數(shù)"
  9.  
  10. # 記錄整數(shù)部分 
  11. df["輔助整數(shù)列"] = df["真實(shí)值"].apply(lambda x: math.modf(x)[1]) 
  12.  
  13. # 記錄小數(shù)部分,因?yàn)槲业淖詈蠼Y(jié)果精度為只保留整數(shù)部分,所以我只需要保留一個(gè)小數(shù)點(diǎn)位進(jìn)行判斷是否需要進(jìn)位操作 
  14. df["輔助小數(shù)列"] = df["真實(shí)值"].apply(lambda x: str(math.modf(x)[0]).split(".")[1][0]) 
  15.  
  16. # 小數(shù)點(diǎn)后的第一位是為5,則向上取整,不是5則調(diào)用原np.round就行了 
  17. df["期望值修正"] = df.apply(lambda x: x.輔助整數(shù)列 + 1 if (x.輔助小數(shù)列 == "5"else np.round(x.真實(shí)值), axis=1) 

結(jié)果如下所示

 

如何使用python進(jìn)行正確的四舍五入?這個(gè)坑有點(diǎn)大
期望修正值已經(jīng)達(dá)成

 

 

責(zé)任編輯:未麗燕 來(lái)源: 今日頭條
相關(guān)推薦

2010-06-09 11:09:06

MySQL四舍五入

2010-06-02 14:42:57

MySQL四舍五入

2021-09-29 20:56:50

MySQLMariaDB字段

2022-09-16 07:24:12

Python 的四舍

2009-11-10 17:22:23

VB.NET Form

2022-12-19 15:28:31

JavaScrip字符串

2011-04-01 16:30:26

T-SQLDateTime

2010-02-03 14:15:18

Python 開發(fā)

2010-02-22 10:06:17

Python調(diào)用

2010-02-22 14:13:38

安裝Python

2010-02-02 14:11:14

Python 進(jìn)行編程

2010-02-02 18:20:43

Python編寫

2010-02-03 15:40:37

Python函數(shù)

2010-01-08 14:41:24

JSON 緩存數(shù)據(jù)

2010-01-18 17:14:50

C++語(yǔ)言

2024-04-10 08:39:56

BigDecimal浮點(diǎn)數(shù)二進(jìn)制

2020-07-17 09:58:31

Python開發(fā)工具

2023-10-18 18:38:44

數(shù)據(jù)校驗(yàn)業(yè)務(wù)

2022-09-16 13:26:49

云計(jì)算云遷移數(shù)據(jù)

2022-06-06 06:10:00

密碼驗(yàn)證安全
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)