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
python numpy rounding
add a comment |
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
python numpy rounding
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
add a comment |
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
python numpy rounding
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
python numpy rounding
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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