How to always round up a XX.5 in numpy











up vote
1
down vote

favorite












I read that numpy is unbiased in rounding and that it works the way its designed. That "if you always round 0.5 up to the next largest number, then the average of a bunch rounded numbers is likely to be slightly larger than the average of the unrounded numbers: this bias or drift can have very bad effects on some numerical algorithms and make them inaccurate."



Disregarding this information and assuming that I always want to round up, how can I do it in numpy? Assuming my array can be quite large.



For simplicity, lets assume i have the array:



import numpy as np

A = [ [10, 15, 30], [25, 134, 41], [134, 413, 51]]
A = np.array(A, dtype=np.int16)

decimal = A * .1
whole = np.round(decimal)


decimal looks like:



[[  1.    1.5   3. ]
[ 2.5 13.4 4.1]
[ 13.4 41.3 5.1]]


whole looks like:



[[  1.   2.   3.]
[ 2. 13. 4.]
[ 13. 41. 5.]]


As you can see, 1.5 rounded to 2 and 2.5 also rounded to 2. How can I force to always get a round up answer for a XX.5? I know I can loop through the array and use python round() but that would definitely be much slower. Was wondering if there is a way to do it using numpy functions










share|improve this question
























  • What behaviour do you want for negative numbers? Should -2.5 round to -3.0 or to -2.0?
    – Mark Dickinson
    Nov 9 at 8:36










  • Thats a good question, although in my scenario there are no negative numbers so I didnt really think of it
    – user1179317
    Nov 9 at 15:39















up vote
1
down vote

favorite












I read that numpy is unbiased in rounding and that it works the way its designed. That "if you always round 0.5 up to the next largest number, then the average of a bunch rounded numbers is likely to be slightly larger than the average of the unrounded numbers: this bias or drift can have very bad effects on some numerical algorithms and make them inaccurate."



Disregarding this information and assuming that I always want to round up, how can I do it in numpy? Assuming my array can be quite large.



For simplicity, lets assume i have the array:



import numpy as np

A = [ [10, 15, 30], [25, 134, 41], [134, 413, 51]]
A = np.array(A, dtype=np.int16)

decimal = A * .1
whole = np.round(decimal)


decimal looks like:



[[  1.    1.5   3. ]
[ 2.5 13.4 4.1]
[ 13.4 41.3 5.1]]


whole looks like:



[[  1.   2.   3.]
[ 2. 13. 4.]
[ 13. 41. 5.]]


As you can see, 1.5 rounded to 2 and 2.5 also rounded to 2. How can I force to always get a round up answer for a XX.5? I know I can loop through the array and use python round() but that would definitely be much slower. Was wondering if there is a way to do it using numpy functions










share|improve this question
























  • What behaviour do you want for negative numbers? Should -2.5 round to -3.0 or to -2.0?
    – Mark Dickinson
    Nov 9 at 8:36










  • Thats a good question, although in my scenario there are no negative numbers so I didnt really think of it
    – user1179317
    Nov 9 at 15:39













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I read that numpy is unbiased in rounding and that it works the way its designed. That "if you always round 0.5 up to the next largest number, then the average of a bunch rounded numbers is likely to be slightly larger than the average of the unrounded numbers: this bias or drift can have very bad effects on some numerical algorithms and make them inaccurate."



Disregarding this information and assuming that I always want to round up, how can I do it in numpy? Assuming my array can be quite large.



For simplicity, lets assume i have the array:



import numpy as np

A = [ [10, 15, 30], [25, 134, 41], [134, 413, 51]]
A = np.array(A, dtype=np.int16)

decimal = A * .1
whole = np.round(decimal)


decimal looks like:



[[  1.    1.5   3. ]
[ 2.5 13.4 4.1]
[ 13.4 41.3 5.1]]


whole looks like:



[[  1.   2.   3.]
[ 2. 13. 4.]
[ 13. 41. 5.]]


As you can see, 1.5 rounded to 2 and 2.5 also rounded to 2. How can I force to always get a round up answer for a XX.5? I know I can loop through the array and use python round() but that would definitely be much slower. Was wondering if there is a way to do it using numpy functions










share|improve this question















I read that numpy is unbiased in rounding and that it works the way its designed. That "if you always round 0.5 up to the next largest number, then the average of a bunch rounded numbers is likely to be slightly larger than the average of the unrounded numbers: this bias or drift can have very bad effects on some numerical algorithms and make them inaccurate."



Disregarding this information and assuming that I always want to round up, how can I do it in numpy? Assuming my array can be quite large.



For simplicity, lets assume i have the array:



import numpy as np

A = [ [10, 15, 30], [25, 134, 41], [134, 413, 51]]
A = np.array(A, dtype=np.int16)

decimal = A * .1
whole = np.round(decimal)


decimal looks like:



[[  1.    1.5   3. ]
[ 2.5 13.4 4.1]
[ 13.4 41.3 5.1]]


whole looks like:



[[  1.   2.   3.]
[ 2. 13. 4.]
[ 13. 41. 5.]]


As you can see, 1.5 rounded to 2 and 2.5 also rounded to 2. How can I force to always get a round up answer for a XX.5? I know I can loop through the array and use python round() but that would definitely be much slower. Was wondering if there is a way to do it using numpy functions







python numpy rounding






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 4:23

























asked Nov 8 at 4:13









user1179317

590718




590718












  • What behaviour do you want for negative numbers? Should -2.5 round to -3.0 or to -2.0?
    – Mark Dickinson
    Nov 9 at 8:36










  • Thats a good question, although in my scenario there are no negative numbers so I didnt really think of it
    – user1179317
    Nov 9 at 15:39


















  • What behaviour do you want for negative numbers? Should -2.5 round to -3.0 or to -2.0?
    – Mark Dickinson
    Nov 9 at 8:36










  • Thats a good question, although in my scenario there are no negative numbers so I didnt really think of it
    – user1179317
    Nov 9 at 15:39
















What behaviour do you want for negative numbers? Should -2.5 round to -3.0 or to -2.0?
– Mark Dickinson
Nov 9 at 8:36




What behaviour do you want for negative numbers? Should -2.5 round to -3.0 or to -2.0?
– Mark Dickinson
Nov 9 at 8:36












Thats a good question, although in my scenario there are no negative numbers so I didnt really think of it
– user1179317
Nov 9 at 15:39




Thats a good question, although in my scenario there are no negative numbers so I didnt really think of it
– user1179317
Nov 9 at 15:39












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










import numpy as np
A = [ [1.0, 1.5, 3.0], [2.5, 13.4, 4.1], [13.4, 41.3, 5.1]]
A = np.array(A)

print(A)

def rounder(x):
if (x-int(x) >= 0.5):
return np.ceil(x)
else:
return np.floor(x)

rounder_vec = np.vectorize(rounder)
whole = rounder_vec(A)
print(whole)


Alternatively, you can also look at numpy.ceil, numpy.floor, numpy.trunc for other rounding styles






share|improve this answer























  • np.ceil will always round it up. I still want 1.3 to round to 1 and 1.5 to round to 2. Maybe that wasnt clear in my question, sorry
    – user1179317
    Nov 8 at 4:22










  • If that is the case, then there is no function for that in numpy. What you could do is to loop through every element and check for round up or round down
    – Andreas
    Nov 8 at 4:29










  • edited based on your comment
    – Andreas
    Nov 8 at 4:41


















up vote
1
down vote













In [25]: def yourroundfunction(number):
...: if(int(number)+0.5<=number):
...: number=int(number)+1
...: else:
...: number=int(number)
...: return number
...:
...:

In [26]:
In [26]: customround=np.vectorize(yourroundfunction)

In [27]: customround(A)
Out[27]:
array([[ 1, 2, 3],
[ 3, 13, 4],
[13, 41, 5]])


Well you could define a customround function and then pass it to np.vectorize and then get your expected output . I wrote this custom function because calling round in ipython doesnt work exactly the same as round in python






share|improve this answer























  • for some reason i am getting an error when i pass the decimal into customround. Andreas answer seem to work ok, even though you mentioned using np.vectorize first :P. Thanks though, i didnt know about np.vectorize
    – user1179317
    Nov 8 at 4:45











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201470%2fhow-to-always-round-up-a-xx-5-in-numpy%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










import numpy as np
A = [ [1.0, 1.5, 3.0], [2.5, 13.4, 4.1], [13.4, 41.3, 5.1]]
A = np.array(A)

print(A)

def rounder(x):
if (x-int(x) >= 0.5):
return np.ceil(x)
else:
return np.floor(x)

rounder_vec = np.vectorize(rounder)
whole = rounder_vec(A)
print(whole)


Alternatively, you can also look at numpy.ceil, numpy.floor, numpy.trunc for other rounding styles






share|improve this answer























  • np.ceil will always round it up. I still want 1.3 to round to 1 and 1.5 to round to 2. Maybe that wasnt clear in my question, sorry
    – user1179317
    Nov 8 at 4:22










  • If that is the case, then there is no function for that in numpy. What you could do is to loop through every element and check for round up or round down
    – Andreas
    Nov 8 at 4:29










  • edited based on your comment
    – Andreas
    Nov 8 at 4:41















up vote
2
down vote



accepted










import numpy as np
A = [ [1.0, 1.5, 3.0], [2.5, 13.4, 4.1], [13.4, 41.3, 5.1]]
A = np.array(A)

print(A)

def rounder(x):
if (x-int(x) >= 0.5):
return np.ceil(x)
else:
return np.floor(x)

rounder_vec = np.vectorize(rounder)
whole = rounder_vec(A)
print(whole)


Alternatively, you can also look at numpy.ceil, numpy.floor, numpy.trunc for other rounding styles






share|improve this answer























  • np.ceil will always round it up. I still want 1.3 to round to 1 and 1.5 to round to 2. Maybe that wasnt clear in my question, sorry
    – user1179317
    Nov 8 at 4:22










  • If that is the case, then there is no function for that in numpy. What you could do is to loop through every element and check for round up or round down
    – Andreas
    Nov 8 at 4:29










  • edited based on your comment
    – Andreas
    Nov 8 at 4:41













up vote
2
down vote



accepted







up vote
2
down vote



accepted






import numpy as np
A = [ [1.0, 1.5, 3.0], [2.5, 13.4, 4.1], [13.4, 41.3, 5.1]]
A = np.array(A)

print(A)

def rounder(x):
if (x-int(x) >= 0.5):
return np.ceil(x)
else:
return np.floor(x)

rounder_vec = np.vectorize(rounder)
whole = rounder_vec(A)
print(whole)


Alternatively, you can also look at numpy.ceil, numpy.floor, numpy.trunc for other rounding styles






share|improve this answer














import numpy as np
A = [ [1.0, 1.5, 3.0], [2.5, 13.4, 4.1], [13.4, 41.3, 5.1]]
A = np.array(A)

print(A)

def rounder(x):
if (x-int(x) >= 0.5):
return np.ceil(x)
else:
return np.floor(x)

rounder_vec = np.vectorize(rounder)
whole = rounder_vec(A)
print(whole)


Alternatively, you can also look at numpy.ceil, numpy.floor, numpy.trunc for other rounding styles







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 4:41

























answered Nov 8 at 4:18









Andreas

1,510516




1,510516












  • np.ceil will always round it up. I still want 1.3 to round to 1 and 1.5 to round to 2. Maybe that wasnt clear in my question, sorry
    – user1179317
    Nov 8 at 4:22










  • If that is the case, then there is no function for that in numpy. What you could do is to loop through every element and check for round up or round down
    – Andreas
    Nov 8 at 4:29










  • edited based on your comment
    – Andreas
    Nov 8 at 4:41


















  • np.ceil will always round it up. I still want 1.3 to round to 1 and 1.5 to round to 2. Maybe that wasnt clear in my question, sorry
    – user1179317
    Nov 8 at 4:22










  • If that is the case, then there is no function for that in numpy. What you could do is to loop through every element and check for round up or round down
    – Andreas
    Nov 8 at 4:29










  • edited based on your comment
    – Andreas
    Nov 8 at 4:41
















np.ceil will always round it up. I still want 1.3 to round to 1 and 1.5 to round to 2. Maybe that wasnt clear in my question, sorry
– user1179317
Nov 8 at 4:22




np.ceil will always round it up. I still want 1.3 to round to 1 and 1.5 to round to 2. Maybe that wasnt clear in my question, sorry
– user1179317
Nov 8 at 4:22












If that is the case, then there is no function for that in numpy. What you could do is to loop through every element and check for round up or round down
– Andreas
Nov 8 at 4:29




If that is the case, then there is no function for that in numpy. What you could do is to loop through every element and check for round up or round down
– Andreas
Nov 8 at 4:29












edited based on your comment
– Andreas
Nov 8 at 4:41




edited based on your comment
– Andreas
Nov 8 at 4:41












up vote
1
down vote













In [25]: def yourroundfunction(number):
...: if(int(number)+0.5<=number):
...: number=int(number)+1
...: else:
...: number=int(number)
...: return number
...:
...:

In [26]:
In [26]: customround=np.vectorize(yourroundfunction)

In [27]: customround(A)
Out[27]:
array([[ 1, 2, 3],
[ 3, 13, 4],
[13, 41, 5]])


Well you could define a customround function and then pass it to np.vectorize and then get your expected output . I wrote this custom function because calling round in ipython doesnt work exactly the same as round in python






share|improve this answer























  • for some reason i am getting an error when i pass the decimal into customround. Andreas answer seem to work ok, even though you mentioned using np.vectorize first :P. Thanks though, i didnt know about np.vectorize
    – user1179317
    Nov 8 at 4:45















up vote
1
down vote













In [25]: def yourroundfunction(number):
...: if(int(number)+0.5<=number):
...: number=int(number)+1
...: else:
...: number=int(number)
...: return number
...:
...:

In [26]:
In [26]: customround=np.vectorize(yourroundfunction)

In [27]: customround(A)
Out[27]:
array([[ 1, 2, 3],
[ 3, 13, 4],
[13, 41, 5]])


Well you could define a customround function and then pass it to np.vectorize and then get your expected output . I wrote this custom function because calling round in ipython doesnt work exactly the same as round in python






share|improve this answer























  • for some reason i am getting an error when i pass the decimal into customround. Andreas answer seem to work ok, even though you mentioned using np.vectorize first :P. Thanks though, i didnt know about np.vectorize
    – user1179317
    Nov 8 at 4:45













up vote
1
down vote










up vote
1
down vote









In [25]: def yourroundfunction(number):
...: if(int(number)+0.5<=number):
...: number=int(number)+1
...: else:
...: number=int(number)
...: return number
...:
...:

In [26]:
In [26]: customround=np.vectorize(yourroundfunction)

In [27]: customround(A)
Out[27]:
array([[ 1, 2, 3],
[ 3, 13, 4],
[13, 41, 5]])


Well you could define a customround function and then pass it to np.vectorize and then get your expected output . I wrote this custom function because calling round in ipython doesnt work exactly the same as round in python






share|improve this answer














In [25]: def yourroundfunction(number):
...: if(int(number)+0.5<=number):
...: number=int(number)+1
...: else:
...: number=int(number)
...: return number
...:
...:

In [26]:
In [26]: customround=np.vectorize(yourroundfunction)

In [27]: customround(A)
Out[27]:
array([[ 1, 2, 3],
[ 3, 13, 4],
[13, 41, 5]])


Well you could define a customround function and then pass it to np.vectorize and then get your expected output . I wrote this custom function because calling round in ipython doesnt work exactly the same as round in python







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 4:43

























answered Nov 8 at 4:33









Albin Paul

1,335617




1,335617












  • for some reason i am getting an error when i pass the decimal into customround. Andreas answer seem to work ok, even though you mentioned using np.vectorize first :P. Thanks though, i didnt know about np.vectorize
    – user1179317
    Nov 8 at 4:45


















  • for some reason i am getting an error when i pass the decimal into customround. Andreas answer seem to work ok, even though you mentioned using np.vectorize first :P. Thanks though, i didnt know about np.vectorize
    – user1179317
    Nov 8 at 4:45
















for some reason i am getting an error when i pass the decimal into customround. Andreas answer seem to work ok, even though you mentioned using np.vectorize first :P. Thanks though, i didnt know about np.vectorize
– user1179317
Nov 8 at 4:45




for some reason i am getting an error when i pass the decimal into customround. Andreas answer seem to work ok, even though you mentioned using np.vectorize first :P. Thanks though, i didnt know about np.vectorize
– user1179317
Nov 8 at 4:45


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201470%2fhow-to-always-round-up-a-xx-5-in-numpy%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini