[Numpy-discussion] Rounding float to integer while minizing the difference between the two arrays?
Chao YUE
chaoyuejoy at gmail.com
Wed Jul 16 11:26:05 EDT 2014
Sorry, there is one error in this part of code, it should be:
def convert_integer(x,threshold=0):
"""
This fucntion converts the float number x to integer according to the
threshold.
"""
if abs(x-0) < 1e-5:
return 0
else:
pdec,pint = math.modf(x)
if pdec > threshold:
return int(math.ceil(pint)+1)
else:
return int(math.ceil(pint))
On Wed, Jul 16, 2014 at 3:18 PM, Chao YUE <chaoyuejoy at gmail.com> wrote:
> Dear all,
>
> I have two arrays with both float type, let's say X and Y. I want to round
> the X to integers (intX) according to some decimal threshold, at the same
> time I want to limit the following difference as small:
>
> diff = np.sum(X*Y) - np.sum(intX*Y)
>
> I don't have to necessarily minimize the "diff" variable (If with this
> demand the computation time is too long). But I would like to limit the
> "diff" to, let's say ten percent within np.sum(X*Y).
>
> I have tried to write some functions, but I don't know where to start the
> opitimization.
>
> def convert_integer(x,threshold=0):
> """
> This fucntion converts the float number x to integer according to the
> threshold.
> """
> if abs(x-0) < 1e5:
> return 0
> else:
> pdec,pint = math.modf(x)
> if pdec > threshold:
> return int(math.ceil(pint)+1)
> else:
> return int(math.ceil(pint))
>
> def convert_arr(arr,threshold=0):
> out = arr.copy()
> for i,num in enumerate(arr):
> out[i] = convert_integer(num,threshold=threshold)
> return out
>
> In [147]:
> convert_arr(np.array([0.14,1.14,0.12]),0.13)
>
> Out[147]:
> array([1, 2, 0])
>
> Now my problem is, how can I minimize or limit the following?
> diff = np.sum(X*Y) - np.sum(convert_arr(X,threshold=?)*Y)
>
> Because it's the first time I encounter such kind of question, so please
> give me some clue to start :p Thanks a lot in advance.
>
> Best,
>
> Chao
>
> --
> please visit:
> http://www.globalcarbonatlas.org/
>
> ***********************************************************************************
> Chao YUE
> Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
> UMR 1572 CEA-CNRS-UVSQ
> Batiment 712 - Pe 119
> 91191 GIF Sur YVETTE Cedex
> Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
>
> ************************************************************************************
>
--
please visit:
http://www.globalcarbonatlas.org/
***********************************************************************************
Chao YUE
Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
UMR 1572 CEA-CNRS-UVSQ
Batiment 712 - Pe 119
91191 GIF Sur YVETTE Cedex
Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
************************************************************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140716/2b0a7143/attachment.html>
More information about the NumPy-Discussion
mailing list