Python function that identifies if the numbers in a list or array are closer to 0 or 1
up vote
8
down vote
favorite
I have a numpy
array of numbers. Below is an example:
[[-2.10044520e-04 1.72314372e-04 1.77235336e-04 -1.06613465e-04
6.76617611e-07 2.71623057e-03 -3.32789944e-05 1.44899758e-05
5.79249863e-05 4.06502549e-04 -1.35823707e-05 -4.13955189e-04
5.29862793e-05 -1.98286005e-04 -2.22829175e-04 -8.88758230e-04
5.62228710e-05 1.36249752e-05 -2.00474996e-05 -2.10090068e-05
1.00007518e+00 1.00007569e+00 -4.44597417e-05 -2.93724453e-04
1.00007513e+00 1.00007496e+00 1.00007532e+00 -1.22357142e-03
3.27903892e-06 1.00007592e+00 1.00007468e+00 1.00007558e+00
2.09869172e-05 -1.97610235e-05 1.00007529e+00 1.00007530e+00
1.00007503e+00 -2.68725642e-05 -3.00372853e-03 1.00007386e+00
1.00007443e+00 1.00007388e+00 5.86993822e-05 -8.69989983e-06
1.00007590e+00 1.00007488e+00 1.00007515e+00 8.81850779e-04
2.03875532e-05 1.00007480e+00 1.00007425e+00 1.00007517e+00
-2.44678912e-05 -4.36556267e-08 1.00007436e+00 1.00007558e+00
1.00007571e+00 -5.42990711e-04 1.45517859e-04 1.00007522e+00
1.00007469e+00 1.00007575e+00 -2.52271817e-05 -7.46339417e-05
1.00007427e+00]]
I want to know if each of the numbers is closer to 0 or 1. Is there a function in Python that could do it or do I have to do it manually?
python arrays list function numpy
add a comment |
up vote
8
down vote
favorite
I have a numpy
array of numbers. Below is an example:
[[-2.10044520e-04 1.72314372e-04 1.77235336e-04 -1.06613465e-04
6.76617611e-07 2.71623057e-03 -3.32789944e-05 1.44899758e-05
5.79249863e-05 4.06502549e-04 -1.35823707e-05 -4.13955189e-04
5.29862793e-05 -1.98286005e-04 -2.22829175e-04 -8.88758230e-04
5.62228710e-05 1.36249752e-05 -2.00474996e-05 -2.10090068e-05
1.00007518e+00 1.00007569e+00 -4.44597417e-05 -2.93724453e-04
1.00007513e+00 1.00007496e+00 1.00007532e+00 -1.22357142e-03
3.27903892e-06 1.00007592e+00 1.00007468e+00 1.00007558e+00
2.09869172e-05 -1.97610235e-05 1.00007529e+00 1.00007530e+00
1.00007503e+00 -2.68725642e-05 -3.00372853e-03 1.00007386e+00
1.00007443e+00 1.00007388e+00 5.86993822e-05 -8.69989983e-06
1.00007590e+00 1.00007488e+00 1.00007515e+00 8.81850779e-04
2.03875532e-05 1.00007480e+00 1.00007425e+00 1.00007517e+00
-2.44678912e-05 -4.36556267e-08 1.00007436e+00 1.00007558e+00
1.00007571e+00 -5.42990711e-04 1.45517859e-04 1.00007522e+00
1.00007469e+00 1.00007575e+00 -2.52271817e-05 -7.46339417e-05
1.00007427e+00]]
I want to know if each of the numbers is closer to 0 or 1. Is there a function in Python that could do it or do I have to do it manually?
python arrays list function numpy
1
You may want to take a look atnumpy.rint
. Although it returns floats and not ints, for whatever reason, but you can cast the result to int after applying this function.
– ForceBru
2 days ago
@ForceBru: It returns floats because float to int conversion can overflow.
– Kevin
2 days ago
5
What behaviour do you want for0.5
?
– Eric Towers
2 days ago
3
@ForceBru or even just (x >= 0.5).
– The Great Duck
2 days ago
add a comment |
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I have a numpy
array of numbers. Below is an example:
[[-2.10044520e-04 1.72314372e-04 1.77235336e-04 -1.06613465e-04
6.76617611e-07 2.71623057e-03 -3.32789944e-05 1.44899758e-05
5.79249863e-05 4.06502549e-04 -1.35823707e-05 -4.13955189e-04
5.29862793e-05 -1.98286005e-04 -2.22829175e-04 -8.88758230e-04
5.62228710e-05 1.36249752e-05 -2.00474996e-05 -2.10090068e-05
1.00007518e+00 1.00007569e+00 -4.44597417e-05 -2.93724453e-04
1.00007513e+00 1.00007496e+00 1.00007532e+00 -1.22357142e-03
3.27903892e-06 1.00007592e+00 1.00007468e+00 1.00007558e+00
2.09869172e-05 -1.97610235e-05 1.00007529e+00 1.00007530e+00
1.00007503e+00 -2.68725642e-05 -3.00372853e-03 1.00007386e+00
1.00007443e+00 1.00007388e+00 5.86993822e-05 -8.69989983e-06
1.00007590e+00 1.00007488e+00 1.00007515e+00 8.81850779e-04
2.03875532e-05 1.00007480e+00 1.00007425e+00 1.00007517e+00
-2.44678912e-05 -4.36556267e-08 1.00007436e+00 1.00007558e+00
1.00007571e+00 -5.42990711e-04 1.45517859e-04 1.00007522e+00
1.00007469e+00 1.00007575e+00 -2.52271817e-05 -7.46339417e-05
1.00007427e+00]]
I want to know if each of the numbers is closer to 0 or 1. Is there a function in Python that could do it or do I have to do it manually?
python arrays list function numpy
I have a numpy
array of numbers. Below is an example:
[[-2.10044520e-04 1.72314372e-04 1.77235336e-04 -1.06613465e-04
6.76617611e-07 2.71623057e-03 -3.32789944e-05 1.44899758e-05
5.79249863e-05 4.06502549e-04 -1.35823707e-05 -4.13955189e-04
5.29862793e-05 -1.98286005e-04 -2.22829175e-04 -8.88758230e-04
5.62228710e-05 1.36249752e-05 -2.00474996e-05 -2.10090068e-05
1.00007518e+00 1.00007569e+00 -4.44597417e-05 -2.93724453e-04
1.00007513e+00 1.00007496e+00 1.00007532e+00 -1.22357142e-03
3.27903892e-06 1.00007592e+00 1.00007468e+00 1.00007558e+00
2.09869172e-05 -1.97610235e-05 1.00007529e+00 1.00007530e+00
1.00007503e+00 -2.68725642e-05 -3.00372853e-03 1.00007386e+00
1.00007443e+00 1.00007388e+00 5.86993822e-05 -8.69989983e-06
1.00007590e+00 1.00007488e+00 1.00007515e+00 8.81850779e-04
2.03875532e-05 1.00007480e+00 1.00007425e+00 1.00007517e+00
-2.44678912e-05 -4.36556267e-08 1.00007436e+00 1.00007558e+00
1.00007571e+00 -5.42990711e-04 1.45517859e-04 1.00007522e+00
1.00007469e+00 1.00007575e+00 -2.52271817e-05 -7.46339417e-05
1.00007427e+00]]
I want to know if each of the numbers is closer to 0 or 1. Is there a function in Python that could do it or do I have to do it manually?
python arrays list function numpy
python arrays list function numpy
edited yesterday
timgeb
45.8k106286
45.8k106286
asked 2 days ago
Eliyah
2841524
2841524
1
You may want to take a look atnumpy.rint
. Although it returns floats and not ints, for whatever reason, but you can cast the result to int after applying this function.
– ForceBru
2 days ago
@ForceBru: It returns floats because float to int conversion can overflow.
– Kevin
2 days ago
5
What behaviour do you want for0.5
?
– Eric Towers
2 days ago
3
@ForceBru or even just (x >= 0.5).
– The Great Duck
2 days ago
add a comment |
1
You may want to take a look atnumpy.rint
. Although it returns floats and not ints, for whatever reason, but you can cast the result to int after applying this function.
– ForceBru
2 days ago
@ForceBru: It returns floats because float to int conversion can overflow.
– Kevin
2 days ago
5
What behaviour do you want for0.5
?
– Eric Towers
2 days ago
3
@ForceBru or even just (x >= 0.5).
– The Great Duck
2 days ago
1
1
You may want to take a look at
numpy.rint
. Although it returns floats and not ints, for whatever reason, but you can cast the result to int after applying this function.– ForceBru
2 days ago
You may want to take a look at
numpy.rint
. Although it returns floats and not ints, for whatever reason, but you can cast the result to int after applying this function.– ForceBru
2 days ago
@ForceBru: It returns floats because float to int conversion can overflow.
– Kevin
2 days ago
@ForceBru: It returns floats because float to int conversion can overflow.
– Kevin
2 days ago
5
5
What behaviour do you want for
0.5
?– Eric Towers
2 days ago
What behaviour do you want for
0.5
?– Eric Towers
2 days ago
3
3
@ForceBru or even just (x >= 0.5).
– The Great Duck
2 days ago
@ForceBru or even just (x >= 0.5).
– The Great Duck
2 days ago
add a comment |
10 Answers
10
active
oldest
votes
up vote
9
down vote
accepted
numpy.rint
is a ufunc that will round the elements of an array to the nearest integer.
>>> a = np.arange(0, 1.1, 0.1)
>>> a
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
>>> np.rint(a)
array([0., 0., 0., 0., 0., 0., 1., 1., 1., 1., 1.])
What if the numbers don't have to be between 0 and 1?
In that case, I'd use numpy.where
.
>>> a = np.arange(-2, 2.1, 0.1)
>>> a
array([-2.00000000e+00, -1.90000000e+00, -1.80000000e+00, -1.70000000e+00,
-1.60000000e+00, -1.50000000e+00, -1.40000000e+00, -1.30000000e+00,
-1.20000000e+00, -1.10000000e+00, -1.00000000e+00, -9.00000000e-01,
-8.00000000e-01, -7.00000000e-01, -6.00000000e-01, -5.00000000e-01,
-4.00000000e-01, -3.00000000e-01, -2.00000000e-01, -1.00000000e-01,
1.77635684e-15, 1.00000000e-01, 2.00000000e-01, 3.00000000e-01,
4.00000000e-01, 5.00000000e-01, 6.00000000e-01, 7.00000000e-01,
8.00000000e-01, 9.00000000e-01, 1.00000000e+00, 1.10000000e+00,
1.20000000e+00, 1.30000000e+00, 1.40000000e+00, 1.50000000e+00,
1.60000000e+00, 1.70000000e+00, 1.80000000e+00, 1.90000000e+00,
2.00000000e+00])
>>> np.where(a <= 0.5, 0, 1)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
1
What if my numbers is above 2?
– Eliyah
2 days ago
2
@Eliyah thennp.where
!
– timgeb
2 days ago
np.minimum(np.rint(a),1) returns 0 for a<0.5 and 1 for a>=0.5.
– Tls Chris
2 days ago
@TlsChris On my machine:np.minimum(np.rint(0.5),1)
->0
becausenp.rint(0.5)
->0
.
– timgeb
2 days ago
1
@timgeb. np.rint(0.5) rounds down on my machine too. I shouldn't make assumptions :-).
– Tls Chris
2 days ago
|
show 2 more comments
up vote
19
down vote
A straightforward way:
lst=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
closerTo1 = [x >= 0.5 for x in lst]
Or you can use np:
import numpy as np
lst=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
arr = np.array(lst)
closerTo1 = arr >= 0.5
Note that >= 0.5
can be changed to > 0.5
, however you choose to treat it.
3
Best to use NumPy for NumPy arrays, +1. Also worth mentioning other options if performance is an issue.
– jpp
2 days ago
1
Did not mention rounding solutions, as no range was defined.
– Or Dinari
2 days ago
add a comment |
up vote
4
down vote
You could use numpy.where:
import numpy as np
arr = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 2.0])
result = np.where(arr >= 0.5, 1, 0)
print(result)
Output
[0 0 0 0 1 1 1 1 1 1]
Note that this will return 1 for numbers above 1 (for instance 2).
add a comment |
up vote
3
down vote
You could use abs()
to measure distances between your number and 0
and 1
and check which on is shorter.
x = [[-2.10044520e-04, 1.72314372e-04, 1.77235336e-04, -1.06613465e-04,
6.76617611e-07, 2.71623057e-03, -3.32789944e-05, 1.44899758e-05,
5.79249863e-05, 4.06502549e-04, -1.35823707e-05, -4.13955189e-04,
5.29862793e-05, -1.98286005e-04, -2.22829175e-04, -8.88758230e-04,
5.62228710e-05, 1.36249752e-05, -2.00474996e-05, -2.10090068e-05,
1.00007518e+00, 1.00007569e+00, -4.44597417e-05, -2.93724453e-04,
1.00007513e+00, 1.00007496e+00, 1.00007532e+00, -1.22357142e-03,
3.27903892e-06, 1.00007592e+00, 1.00007468e+00, 1.00007558e+00,
2.09869172e-05, -1.97610235e-05, 1.00007529e+00, 1.00007530e+00,
1.00007503e+00, -2.68725642e-05, -3.00372853e-03, 1.00007386e+00,
1.00007443e+00, 1.00007388e+00, 5.86993822e-05, -8.69989983e-06,
1.00007590e+00, 1.00007488e+00, 1.00007515e+00, 8.81850779e-04,
2.03875532e-05, 1.00007480e+00, 1.00007425e+00, 1.00007517e+00,
-2.44678912e-05, -4.36556267e-08, 1.00007436e+00, 1.00007558e+00,
1.00007571e+00, -5.42990711e-04, 1.45517859e-04, 1.00007522e+00,
1.00007469e+00, 1.00007575e+00, -2.52271817e-05, -7.46339417e-05,
1.00007427e+00]]
rounded_x = [0 if abs(i) < abs(1-i) else 1 for i in x[0]]
print(rounded_x)
Output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1]
add a comment |
up vote
3
down vote
Here's a simple generalization for any arbitrary numbers a
and b
, instead of just 0
and 1
:
def closerab(l, a=0, b=1):
l = np.asarray(l)
boolarr = (np.abs(l - b) > np.abs(l - a))
# returns two lists of indices, one for numbers closer to a and one for numbers closer to b
return boolarr.nonzero()[0], (boolarr==0).nonzero()[0]
This'll return two lists, one with the indices of the numbers closer to a
, and one with the indices of the numbers closer to b
.
Testing it out:
l = [
-2.10044520e-04, 1.72314372e-04, 1.77235336e-04, 1.06613465e-04,
6.76617611e-07, 2.71623057e-03, 3.32789944e-05, 1.44899758e-05,
5.79249863e-05, 4.06502549e-04, 1.35823707e-05, 4.13955189e-04,
5.29862793e-05, 1.98286005e-04, 2.22829175e-04, 8.88758230e-04,
5.62228710e-05, 1.36249752e-05, 2.00474996e-05, 2.10090068e-05,
1.00007518e+00, 1.00007569e+00, 4.44597417e-05, 2.93724453e-04,
1.00007513e+00, 1.00007496e+00, 1.00007532e+00, 1.22357142e-03,
3.27903892e-06, 1.00007592e+00, 1.00007468e+00, 1.00007558e+00,
2.09869172e-05, 1.97610235e-05, 1.00007529e+00, 1.00007530e+00,
1.00007503e+00, 2.68725642e-05, 3.00372853e-03, 1.00007386e+00,
1.00007443e+00, 1.00007388e+00, 5.86993822e-05, 8.69989983e-06,
1.00007590e+00, 1.00007488e+00, 1.00007515e+00, 8.81850779e-04,
2.03875532e-05, 1.00007480e+00, 1.00007425e+00, 1.00007517e+00,
-2.44678912e-05, 4.36556267e-08, 1.00007436e+00, 1.00007558e+00,
1.00007571e+00, 5.42990711e-04, 1.45517859e-04, 1.00007522e+00,
1.00007469e+00, 1.00007575e+00, 2.52271817e-05, 7.46339417e-05,
1.00007427e+00
]
print(closerab(l, 0, 1))
This outputs:
(array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 22, 23, 27, 28, 32, 33, 37, 38, 42, 43, 47, 48, 52, 53,
57, 58, 62, 63]),
array([20, 21, 24, 25, 26, 29, 30, 31, 34, 35, 36, 39, 40, 41, 44, 45, 46,
49, 50, 51, 54, 55, 56, 59, 60, 61, 64]))
add a comment |
up vote
2
down vote
Here is one simple way to do this:
>>> a = np.arange(-2, 2.1, 0.1)
>>> (a >= .5).astype(np.float)
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1.])
(Change np.float
to np.int
if you want integers.)
Cleanest and probably fastest solution. Pity it did not attract many votes so far.
– Luca Citi
2 days ago
You don't have to usenp.float
ornp.int
in.astype
, a regularfloat
orint
will do just fine. Numpy will interpret it as the equivalent numpy variant.
– Rob
yesterday
add a comment |
up vote
1
down vote
From the Python built-in function docs round(number[, ndigits])
:
Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, it defaults to zero. The result is a floating point number. Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done away from 0 (so, for example,
round(0.5)
is1.0
andround(-0.5)
is-1.0
).
For numpy arrays in particular, you can use the numpy.round_
function.
New contributor
What about numbers that are not in range of 0 and 1?
– Filip Młynarski
2 days ago
1
Round works outside of the[0, 1]
range. Soround(2.2)
would be2.0
,round(-1.2)
would be-1.0
, andround(3.141, 2)
would be3.14
.
– Andrew Fiorillo
2 days ago
@AndrewFiorillo But I just wanted an output 0 and 1 :)
– Eliyah
2 days ago
Then @FilipMłynarski 's answer above is probably the most robust.
– Andrew Fiorillo
2 days ago
add a comment |
up vote
1
down vote
your_list=[[-2.10044520e-04, 1.72314372e-04, 1.77235336e-04, 1.06613465e-04,
6.76617611e-07, 2.71623057e-03, 3.32789944e-05, 1.44899758e-05,
5.79249863e-05, 4.06502549e-04, 1.35823707e-05, 4.13955189e-04,
5.29862793e-05, 1.98286005e-04, 2.22829175e-04, 8.88758230e-04,
5.62228710e-05, 1.36249752e-05, 2.00474996e-05, 2.10090068e-05,
1.00007518e+00, 1.00007569e+00, 4.44597417e-05, 2.93724453e-04,
1.00007513e+00, 1.00007496e+00, 1.00007532e+00, 1.22357142e-03,
3.27903892e-06, 1.00007592e+00, 1.00007468e+00, 1.00007558e+00,
2.09869172e-05, 1.97610235e-05, 1.00007529e+00, 1.00007530e+00,
1.00007503e+00, 2.68725642e-05, 3.00372853e-03, 1.00007386e+00,
1.00007443e+00, 1.00007388e+00, 5.86993822e-05, 8.69989983e-06,
1.00007590e+00, 1.00007488e+00, 1.00007515e+00, 8.81850779e-04,
2.03875532e-05, 1.00007480e+00, 1.00007425e+00, 1.00007517e+00,
-2.44678912e-05, 4.36556267e-08, 1.00007436e+00, 1.00007558e+00,
1.00007571e+00, 5.42990711e-04, 1.45517859e-04, 1.00007522e+00,
1.00007469e+00, 1.00007575e+00, 2.52271817e-05, 7.46339417e-05,
1.00007427e+00]]
close_to_one_or_zero=[1 if x > 0.5 else 0 for x in your_list[0]]
close_to_one_or_zero
[0, 0, 0, 0, 0,....... 1, 1, 1, 0, 0, 1]
add a comment |
up vote
1
down vote
You can use round
:
[round(i) for i in [0.1,0.2,0.3,0.8,0.9]]
add a comment |
up vote
1
down vote
Alternatively, you can use a ternary operator.
x = [-0.2, 0.1, 1.1, 0.75, 0.4, 0.2, 1.5, 0.9]
a = 0
b = 1
[a if i <= (a+b)/2 else b for i in x]
add a comment |
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
numpy.rint
is a ufunc that will round the elements of an array to the nearest integer.
>>> a = np.arange(0, 1.1, 0.1)
>>> a
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
>>> np.rint(a)
array([0., 0., 0., 0., 0., 0., 1., 1., 1., 1., 1.])
What if the numbers don't have to be between 0 and 1?
In that case, I'd use numpy.where
.
>>> a = np.arange(-2, 2.1, 0.1)
>>> a
array([-2.00000000e+00, -1.90000000e+00, -1.80000000e+00, -1.70000000e+00,
-1.60000000e+00, -1.50000000e+00, -1.40000000e+00, -1.30000000e+00,
-1.20000000e+00, -1.10000000e+00, -1.00000000e+00, -9.00000000e-01,
-8.00000000e-01, -7.00000000e-01, -6.00000000e-01, -5.00000000e-01,
-4.00000000e-01, -3.00000000e-01, -2.00000000e-01, -1.00000000e-01,
1.77635684e-15, 1.00000000e-01, 2.00000000e-01, 3.00000000e-01,
4.00000000e-01, 5.00000000e-01, 6.00000000e-01, 7.00000000e-01,
8.00000000e-01, 9.00000000e-01, 1.00000000e+00, 1.10000000e+00,
1.20000000e+00, 1.30000000e+00, 1.40000000e+00, 1.50000000e+00,
1.60000000e+00, 1.70000000e+00, 1.80000000e+00, 1.90000000e+00,
2.00000000e+00])
>>> np.where(a <= 0.5, 0, 1)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
1
What if my numbers is above 2?
– Eliyah
2 days ago
2
@Eliyah thennp.where
!
– timgeb
2 days ago
np.minimum(np.rint(a),1) returns 0 for a<0.5 and 1 for a>=0.5.
– Tls Chris
2 days ago
@TlsChris On my machine:np.minimum(np.rint(0.5),1)
->0
becausenp.rint(0.5)
->0
.
– timgeb
2 days ago
1
@timgeb. np.rint(0.5) rounds down on my machine too. I shouldn't make assumptions :-).
– Tls Chris
2 days ago
|
show 2 more comments
up vote
9
down vote
accepted
numpy.rint
is a ufunc that will round the elements of an array to the nearest integer.
>>> a = np.arange(0, 1.1, 0.1)
>>> a
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
>>> np.rint(a)
array([0., 0., 0., 0., 0., 0., 1., 1., 1., 1., 1.])
What if the numbers don't have to be between 0 and 1?
In that case, I'd use numpy.where
.
>>> a = np.arange(-2, 2.1, 0.1)
>>> a
array([-2.00000000e+00, -1.90000000e+00, -1.80000000e+00, -1.70000000e+00,
-1.60000000e+00, -1.50000000e+00, -1.40000000e+00, -1.30000000e+00,
-1.20000000e+00, -1.10000000e+00, -1.00000000e+00, -9.00000000e-01,
-8.00000000e-01, -7.00000000e-01, -6.00000000e-01, -5.00000000e-01,
-4.00000000e-01, -3.00000000e-01, -2.00000000e-01, -1.00000000e-01,
1.77635684e-15, 1.00000000e-01, 2.00000000e-01, 3.00000000e-01,
4.00000000e-01, 5.00000000e-01, 6.00000000e-01, 7.00000000e-01,
8.00000000e-01, 9.00000000e-01, 1.00000000e+00, 1.10000000e+00,
1.20000000e+00, 1.30000000e+00, 1.40000000e+00, 1.50000000e+00,
1.60000000e+00, 1.70000000e+00, 1.80000000e+00, 1.90000000e+00,
2.00000000e+00])
>>> np.where(a <= 0.5, 0, 1)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
1
What if my numbers is above 2?
– Eliyah
2 days ago
2
@Eliyah thennp.where
!
– timgeb
2 days ago
np.minimum(np.rint(a),1) returns 0 for a<0.5 and 1 for a>=0.5.
– Tls Chris
2 days ago
@TlsChris On my machine:np.minimum(np.rint(0.5),1)
->0
becausenp.rint(0.5)
->0
.
– timgeb
2 days ago
1
@timgeb. np.rint(0.5) rounds down on my machine too. I shouldn't make assumptions :-).
– Tls Chris
2 days ago
|
show 2 more comments
up vote
9
down vote
accepted
up vote
9
down vote
accepted
numpy.rint
is a ufunc that will round the elements of an array to the nearest integer.
>>> a = np.arange(0, 1.1, 0.1)
>>> a
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
>>> np.rint(a)
array([0., 0., 0., 0., 0., 0., 1., 1., 1., 1., 1.])
What if the numbers don't have to be between 0 and 1?
In that case, I'd use numpy.where
.
>>> a = np.arange(-2, 2.1, 0.1)
>>> a
array([-2.00000000e+00, -1.90000000e+00, -1.80000000e+00, -1.70000000e+00,
-1.60000000e+00, -1.50000000e+00, -1.40000000e+00, -1.30000000e+00,
-1.20000000e+00, -1.10000000e+00, -1.00000000e+00, -9.00000000e-01,
-8.00000000e-01, -7.00000000e-01, -6.00000000e-01, -5.00000000e-01,
-4.00000000e-01, -3.00000000e-01, -2.00000000e-01, -1.00000000e-01,
1.77635684e-15, 1.00000000e-01, 2.00000000e-01, 3.00000000e-01,
4.00000000e-01, 5.00000000e-01, 6.00000000e-01, 7.00000000e-01,
8.00000000e-01, 9.00000000e-01, 1.00000000e+00, 1.10000000e+00,
1.20000000e+00, 1.30000000e+00, 1.40000000e+00, 1.50000000e+00,
1.60000000e+00, 1.70000000e+00, 1.80000000e+00, 1.90000000e+00,
2.00000000e+00])
>>> np.where(a <= 0.5, 0, 1)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
numpy.rint
is a ufunc that will round the elements of an array to the nearest integer.
>>> a = np.arange(0, 1.1, 0.1)
>>> a
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
>>> np.rint(a)
array([0., 0., 0., 0., 0., 0., 1., 1., 1., 1., 1.])
What if the numbers don't have to be between 0 and 1?
In that case, I'd use numpy.where
.
>>> a = np.arange(-2, 2.1, 0.1)
>>> a
array([-2.00000000e+00, -1.90000000e+00, -1.80000000e+00, -1.70000000e+00,
-1.60000000e+00, -1.50000000e+00, -1.40000000e+00, -1.30000000e+00,
-1.20000000e+00, -1.10000000e+00, -1.00000000e+00, -9.00000000e-01,
-8.00000000e-01, -7.00000000e-01, -6.00000000e-01, -5.00000000e-01,
-4.00000000e-01, -3.00000000e-01, -2.00000000e-01, -1.00000000e-01,
1.77635684e-15, 1.00000000e-01, 2.00000000e-01, 3.00000000e-01,
4.00000000e-01, 5.00000000e-01, 6.00000000e-01, 7.00000000e-01,
8.00000000e-01, 9.00000000e-01, 1.00000000e+00, 1.10000000e+00,
1.20000000e+00, 1.30000000e+00, 1.40000000e+00, 1.50000000e+00,
1.60000000e+00, 1.70000000e+00, 1.80000000e+00, 1.90000000e+00,
2.00000000e+00])
>>> np.where(a <= 0.5, 0, 1)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
edited 2 days ago
answered 2 days ago
timgeb
45.8k106286
45.8k106286
1
What if my numbers is above 2?
– Eliyah
2 days ago
2
@Eliyah thennp.where
!
– timgeb
2 days ago
np.minimum(np.rint(a),1) returns 0 for a<0.5 and 1 for a>=0.5.
– Tls Chris
2 days ago
@TlsChris On my machine:np.minimum(np.rint(0.5),1)
->0
becausenp.rint(0.5)
->0
.
– timgeb
2 days ago
1
@timgeb. np.rint(0.5) rounds down on my machine too. I shouldn't make assumptions :-).
– Tls Chris
2 days ago
|
show 2 more comments
1
What if my numbers is above 2?
– Eliyah
2 days ago
2
@Eliyah thennp.where
!
– timgeb
2 days ago
np.minimum(np.rint(a),1) returns 0 for a<0.5 and 1 for a>=0.5.
– Tls Chris
2 days ago
@TlsChris On my machine:np.minimum(np.rint(0.5),1)
->0
becausenp.rint(0.5)
->0
.
– timgeb
2 days ago
1
@timgeb. np.rint(0.5) rounds down on my machine too. I shouldn't make assumptions :-).
– Tls Chris
2 days ago
1
1
What if my numbers is above 2?
– Eliyah
2 days ago
What if my numbers is above 2?
– Eliyah
2 days ago
2
2
@Eliyah then
np.where
!– timgeb
2 days ago
@Eliyah then
np.where
!– timgeb
2 days ago
np.minimum(np.rint(a),1) returns 0 for a<0.5 and 1 for a>=0.5.
– Tls Chris
2 days ago
np.minimum(np.rint(a),1) returns 0 for a<0.5 and 1 for a>=0.5.
– Tls Chris
2 days ago
@TlsChris On my machine:
np.minimum(np.rint(0.5),1)
-> 0
because np.rint(0.5)
-> 0
.– timgeb
2 days ago
@TlsChris On my machine:
np.minimum(np.rint(0.5),1)
-> 0
because np.rint(0.5)
-> 0
.– timgeb
2 days ago
1
1
@timgeb. np.rint(0.5) rounds down on my machine too. I shouldn't make assumptions :-).
– Tls Chris
2 days ago
@timgeb. np.rint(0.5) rounds down on my machine too. I shouldn't make assumptions :-).
– Tls Chris
2 days ago
|
show 2 more comments
up vote
19
down vote
A straightforward way:
lst=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
closerTo1 = [x >= 0.5 for x in lst]
Or you can use np:
import numpy as np
lst=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
arr = np.array(lst)
closerTo1 = arr >= 0.5
Note that >= 0.5
can be changed to > 0.5
, however you choose to treat it.
3
Best to use NumPy for NumPy arrays, +1. Also worth mentioning other options if performance is an issue.
– jpp
2 days ago
1
Did not mention rounding solutions, as no range was defined.
– Or Dinari
2 days ago
add a comment |
up vote
19
down vote
A straightforward way:
lst=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
closerTo1 = [x >= 0.5 for x in lst]
Or you can use np:
import numpy as np
lst=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
arr = np.array(lst)
closerTo1 = arr >= 0.5
Note that >= 0.5
can be changed to > 0.5
, however you choose to treat it.
3
Best to use NumPy for NumPy arrays, +1. Also worth mentioning other options if performance is an issue.
– jpp
2 days ago
1
Did not mention rounding solutions, as no range was defined.
– Or Dinari
2 days ago
add a comment |
up vote
19
down vote
up vote
19
down vote
A straightforward way:
lst=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
closerTo1 = [x >= 0.5 for x in lst]
Or you can use np:
import numpy as np
lst=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
arr = np.array(lst)
closerTo1 = arr >= 0.5
Note that >= 0.5
can be changed to > 0.5
, however you choose to treat it.
A straightforward way:
lst=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
closerTo1 = [x >= 0.5 for x in lst]
Or you can use np:
import numpy as np
lst=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
arr = np.array(lst)
closerTo1 = arr >= 0.5
Note that >= 0.5
can be changed to > 0.5
, however you choose to treat it.
answered 2 days ago
Or Dinari
1,069321
1,069321
3
Best to use NumPy for NumPy arrays, +1. Also worth mentioning other options if performance is an issue.
– jpp
2 days ago
1
Did not mention rounding solutions, as no range was defined.
– Or Dinari
2 days ago
add a comment |
3
Best to use NumPy for NumPy arrays, +1. Also worth mentioning other options if performance is an issue.
– jpp
2 days ago
1
Did not mention rounding solutions, as no range was defined.
– Or Dinari
2 days ago
3
3
Best to use NumPy for NumPy arrays, +1. Also worth mentioning other options if performance is an issue.
– jpp
2 days ago
Best to use NumPy for NumPy arrays, +1. Also worth mentioning other options if performance is an issue.
– jpp
2 days ago
1
1
Did not mention rounding solutions, as no range was defined.
– Or Dinari
2 days ago
Did not mention rounding solutions, as no range was defined.
– Or Dinari
2 days ago
add a comment |
up vote
4
down vote
You could use numpy.where:
import numpy as np
arr = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 2.0])
result = np.where(arr >= 0.5, 1, 0)
print(result)
Output
[0 0 0 0 1 1 1 1 1 1]
Note that this will return 1 for numbers above 1 (for instance 2).
add a comment |
up vote
4
down vote
You could use numpy.where:
import numpy as np
arr = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 2.0])
result = np.where(arr >= 0.5, 1, 0)
print(result)
Output
[0 0 0 0 1 1 1 1 1 1]
Note that this will return 1 for numbers above 1 (for instance 2).
add a comment |
up vote
4
down vote
up vote
4
down vote
You could use numpy.where:
import numpy as np
arr = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 2.0])
result = np.where(arr >= 0.5, 1, 0)
print(result)
Output
[0 0 0 0 1 1 1 1 1 1]
Note that this will return 1 for numbers above 1 (for instance 2).
You could use numpy.where:
import numpy as np
arr = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 2.0])
result = np.where(arr >= 0.5, 1, 0)
print(result)
Output
[0 0 0 0 1 1 1 1 1 1]
Note that this will return 1 for numbers above 1 (for instance 2).
answered 2 days ago
Daniel Mesejo
8,4821923
8,4821923
add a comment |
add a comment |
up vote
3
down vote
You could use abs()
to measure distances between your number and 0
and 1
and check which on is shorter.
x = [[-2.10044520e-04, 1.72314372e-04, 1.77235336e-04, -1.06613465e-04,
6.76617611e-07, 2.71623057e-03, -3.32789944e-05, 1.44899758e-05,
5.79249863e-05, 4.06502549e-04, -1.35823707e-05, -4.13955189e-04,
5.29862793e-05, -1.98286005e-04, -2.22829175e-04, -8.88758230e-04,
5.62228710e-05, 1.36249752e-05, -2.00474996e-05, -2.10090068e-05,
1.00007518e+00, 1.00007569e+00, -4.44597417e-05, -2.93724453e-04,
1.00007513e+00, 1.00007496e+00, 1.00007532e+00, -1.22357142e-03,
3.27903892e-06, 1.00007592e+00, 1.00007468e+00, 1.00007558e+00,
2.09869172e-05, -1.97610235e-05, 1.00007529e+00, 1.00007530e+00,
1.00007503e+00, -2.68725642e-05, -3.00372853e-03, 1.00007386e+00,
1.00007443e+00, 1.00007388e+00, 5.86993822e-05, -8.69989983e-06,
1.00007590e+00, 1.00007488e+00, 1.00007515e+00, 8.81850779e-04,
2.03875532e-05, 1.00007480e+00, 1.00007425e+00, 1.00007517e+00,
-2.44678912e-05, -4.36556267e-08, 1.00007436e+00, 1.00007558e+00,
1.00007571e+00, -5.42990711e-04, 1.45517859e-04, 1.00007522e+00,
1.00007469e+00, 1.00007575e+00, -2.52271817e-05, -7.46339417e-05,
1.00007427e+00]]
rounded_x = [0 if abs(i) < abs(1-i) else 1 for i in x[0]]
print(rounded_x)
Output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1]
add a comment |
up vote
3
down vote
You could use abs()
to measure distances between your number and 0
and 1
and check which on is shorter.
x = [[-2.10044520e-04, 1.72314372e-04, 1.77235336e-04, -1.06613465e-04,
6.76617611e-07, 2.71623057e-03, -3.32789944e-05, 1.44899758e-05,
5.79249863e-05, 4.06502549e-04, -1.35823707e-05, -4.13955189e-04,
5.29862793e-05, -1.98286005e-04, -2.22829175e-04, -8.88758230e-04,
5.62228710e-05, 1.36249752e-05, -2.00474996e-05, -2.10090068e-05,
1.00007518e+00, 1.00007569e+00, -4.44597417e-05, -2.93724453e-04,
1.00007513e+00, 1.00007496e+00, 1.00007532e+00, -1.22357142e-03,
3.27903892e-06, 1.00007592e+00, 1.00007468e+00, 1.00007558e+00,
2.09869172e-05, -1.97610235e-05, 1.00007529e+00, 1.00007530e+00,
1.00007503e+00, -2.68725642e-05, -3.00372853e-03, 1.00007386e+00,
1.00007443e+00, 1.00007388e+00, 5.86993822e-05, -8.69989983e-06,
1.00007590e+00, 1.00007488e+00, 1.00007515e+00, 8.81850779e-04,
2.03875532e-05, 1.00007480e+00, 1.00007425e+00, 1.00007517e+00,
-2.44678912e-05, -4.36556267e-08, 1.00007436e+00, 1.00007558e+00,
1.00007571e+00, -5.42990711e-04, 1.45517859e-04, 1.00007522e+00,
1.00007469e+00, 1.00007575e+00, -2.52271817e-05, -7.46339417e-05,
1.00007427e+00]]
rounded_x = [0 if abs(i) < abs(1-i) else 1 for i in x[0]]
print(rounded_x)
Output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1]
add a comment |
up vote
3
down vote
up vote
3
down vote
You could use abs()
to measure distances between your number and 0
and 1
and check which on is shorter.
x = [[-2.10044520e-04, 1.72314372e-04, 1.77235336e-04, -1.06613465e-04,
6.76617611e-07, 2.71623057e-03, -3.32789944e-05, 1.44899758e-05,
5.79249863e-05, 4.06502549e-04, -1.35823707e-05, -4.13955189e-04,
5.29862793e-05, -1.98286005e-04, -2.22829175e-04, -8.88758230e-04,
5.62228710e-05, 1.36249752e-05, -2.00474996e-05, -2.10090068e-05,
1.00007518e+00, 1.00007569e+00, -4.44597417e-05, -2.93724453e-04,
1.00007513e+00, 1.00007496e+00, 1.00007532e+00, -1.22357142e-03,
3.27903892e-06, 1.00007592e+00, 1.00007468e+00, 1.00007558e+00,
2.09869172e-05, -1.97610235e-05, 1.00007529e+00, 1.00007530e+00,
1.00007503e+00, -2.68725642e-05, -3.00372853e-03, 1.00007386e+00,
1.00007443e+00, 1.00007388e+00, 5.86993822e-05, -8.69989983e-06,
1.00007590e+00, 1.00007488e+00, 1.00007515e+00, 8.81850779e-04,
2.03875532e-05, 1.00007480e+00, 1.00007425e+00, 1.00007517e+00,
-2.44678912e-05, -4.36556267e-08, 1.00007436e+00, 1.00007558e+00,
1.00007571e+00, -5.42990711e-04, 1.45517859e-04, 1.00007522e+00,
1.00007469e+00, 1.00007575e+00, -2.52271817e-05, -7.46339417e-05,
1.00007427e+00]]
rounded_x = [0 if abs(i) < abs(1-i) else 1 for i in x[0]]
print(rounded_x)
Output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1]
You could use abs()
to measure distances between your number and 0
and 1
and check which on is shorter.
x = [[-2.10044520e-04, 1.72314372e-04, 1.77235336e-04, -1.06613465e-04,
6.76617611e-07, 2.71623057e-03, -3.32789944e-05, 1.44899758e-05,
5.79249863e-05, 4.06502549e-04, -1.35823707e-05, -4.13955189e-04,
5.29862793e-05, -1.98286005e-04, -2.22829175e-04, -8.88758230e-04,
5.62228710e-05, 1.36249752e-05, -2.00474996e-05, -2.10090068e-05,
1.00007518e+00, 1.00007569e+00, -4.44597417e-05, -2.93724453e-04,
1.00007513e+00, 1.00007496e+00, 1.00007532e+00, -1.22357142e-03,
3.27903892e-06, 1.00007592e+00, 1.00007468e+00, 1.00007558e+00,
2.09869172e-05, -1.97610235e-05, 1.00007529e+00, 1.00007530e+00,
1.00007503e+00, -2.68725642e-05, -3.00372853e-03, 1.00007386e+00,
1.00007443e+00, 1.00007388e+00, 5.86993822e-05, -8.69989983e-06,
1.00007590e+00, 1.00007488e+00, 1.00007515e+00, 8.81850779e-04,
2.03875532e-05, 1.00007480e+00, 1.00007425e+00, 1.00007517e+00,
-2.44678912e-05, -4.36556267e-08, 1.00007436e+00, 1.00007558e+00,
1.00007571e+00, -5.42990711e-04, 1.45517859e-04, 1.00007522e+00,
1.00007469e+00, 1.00007575e+00, -2.52271817e-05, -7.46339417e-05,
1.00007427e+00]]
rounded_x = [0 if abs(i) < abs(1-i) else 1 for i in x[0]]
print(rounded_x)
Output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1]
answered 2 days ago
Filip Młynarski
1,107111
1,107111
add a comment |
add a comment |
up vote
3
down vote
Here's a simple generalization for any arbitrary numbers a
and b
, instead of just 0
and 1
:
def closerab(l, a=0, b=1):
l = np.asarray(l)
boolarr = (np.abs(l - b) > np.abs(l - a))
# returns two lists of indices, one for numbers closer to a and one for numbers closer to b
return boolarr.nonzero()[0], (boolarr==0).nonzero()[0]
This'll return two lists, one with the indices of the numbers closer to a
, and one with the indices of the numbers closer to b
.
Testing it out:
l = [
-2.10044520e-04, 1.72314372e-04, 1.77235336e-04, 1.06613465e-04,
6.76617611e-07, 2.71623057e-03, 3.32789944e-05, 1.44899758e-05,
5.79249863e-05, 4.06502549e-04, 1.35823707e-05, 4.13955189e-04,
5.29862793e-05, 1.98286005e-04, 2.22829175e-04, 8.88758230e-04,
5.62228710e-05, 1.36249752e-05, 2.00474996e-05, 2.10090068e-05,
1.00007518e+00, 1.00007569e+00, 4.44597417e-05, 2.93724453e-04,
1.00007513e+00, 1.00007496e+00, 1.00007532e+00, 1.22357142e-03,
3.27903892e-06, 1.00007592e+00, 1.00007468e+00, 1.00007558e+00,
2.09869172e-05, 1.97610235e-05, 1.00007529e+00, 1.00007530e+00,
1.00007503e+00, 2.68725642e-05, 3.00372853e-03, 1.00007386e+00,
1.00007443e+00, 1.00007388e+00, 5.86993822e-05, 8.69989983e-06,
1.00007590e+00, 1.00007488e+00, 1.00007515e+00, 8.81850779e-04,
2.03875532e-05, 1.00007480e+00, 1.00007425e+00, 1.00007517e+00,
-2.44678912e-05, 4.36556267e-08, 1.00007436e+00, 1.00007558e+00,
1.00007571e+00, 5.42990711e-04, 1.45517859e-04, 1.00007522e+00,
1.00007469e+00, 1.00007575e+00, 2.52271817e-05, 7.46339417e-05,
1.00007427e+00
]
print(closerab(l, 0, 1))
This outputs:
(array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 22, 23, 27, 28, 32, 33, 37, 38, 42, 43, 47, 48, 52, 53,
57, 58, 62, 63]),
array([20, 21, 24, 25, 26, 29, 30, 31, 34, 35, 36, 39, 40, 41, 44, 45, 46,
49, 50, 51, 54, 55, 56, 59, 60, 61, 64]))
add a comment |
up vote
3
down vote
Here's a simple generalization for any arbitrary numbers a
and b
, instead of just 0
and 1
:
def closerab(l, a=0, b=1):
l = np.asarray(l)
boolarr = (np.abs(l - b) > np.abs(l - a))
# returns two lists of indices, one for numbers closer to a and one for numbers closer to b
return boolarr.nonzero()[0], (boolarr==0).nonzero()[0]
This'll return two lists, one with the indices of the numbers closer to a
, and one with the indices of the numbers closer to b
.
Testing it out:
l = [
-2.10044520e-04, 1.72314372e-04, 1.77235336e-04, 1.06613465e-04,
6.76617611e-07, 2.71623057e-03, 3.32789944e-05, 1.44899758e-05,
5.79249863e-05, 4.06502549e-04, 1.35823707e-05, 4.13955189e-04,
5.29862793e-05, 1.98286005e-04, 2.22829175e-04, 8.88758230e-04,
5.62228710e-05, 1.36249752e-05, 2.00474996e-05, 2.10090068e-05,
1.00007518e+00, 1.00007569e+00, 4.44597417e-05, 2.93724453e-04,
1.00007513e+00, 1.00007496e+00, 1.00007532e+00, 1.22357142e-03,
3.27903892e-06, 1.00007592e+00, 1.00007468e+00, 1.00007558e+00,
2.09869172e-05, 1.97610235e-05, 1.00007529e+00, 1.00007530e+00,
1.00007503e+00, 2.68725642e-05, 3.00372853e-03, 1.00007386e+00,
1.00007443e+00, 1.00007388e+00, 5.86993822e-05, 8.69989983e-06,
1.00007590e+00, 1.00007488e+00, 1.00007515e+00, 8.81850779e-04,
2.03875532e-05, 1.00007480e+00, 1.00007425e+00, 1.00007517e+00,
-2.44678912e-05, 4.36556267e-08, 1.00007436e+00, 1.00007558e+00,
1.00007571e+00, 5.42990711e-04, 1.45517859e-04, 1.00007522e+00,
1.00007469e+00, 1.00007575e+00, 2.52271817e-05, 7.46339417e-05,
1.00007427e+00
]
print(closerab(l, 0, 1))
This outputs:
(array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 22, 23, 27, 28, 32, 33, 37, 38, 42, 43, 47, 48, 52, 53,
57, 58, 62, 63]),
array([20, 21, 24, 25, 26, 29, 30, 31, 34, 35, 36, 39, 40, 41, 44, 45, 46,
49, 50, 51, 54, 55, 56, 59, 60, 61, 64]))
add a comment |
up vote
3
down vote
up vote
3
down vote
Here's a simple generalization for any arbitrary numbers a
and b
, instead of just 0
and 1
:
def closerab(l, a=0, b=1):
l = np.asarray(l)
boolarr = (np.abs(l - b) > np.abs(l - a))
# returns two lists of indices, one for numbers closer to a and one for numbers closer to b
return boolarr.nonzero()[0], (boolarr==0).nonzero()[0]
This'll return two lists, one with the indices of the numbers closer to a
, and one with the indices of the numbers closer to b
.
Testing it out:
l = [
-2.10044520e-04, 1.72314372e-04, 1.77235336e-04, 1.06613465e-04,
6.76617611e-07, 2.71623057e-03, 3.32789944e-05, 1.44899758e-05,
5.79249863e-05, 4.06502549e-04, 1.35823707e-05, 4.13955189e-04,
5.29862793e-05, 1.98286005e-04, 2.22829175e-04, 8.88758230e-04,
5.62228710e-05, 1.36249752e-05, 2.00474996e-05, 2.10090068e-05,
1.00007518e+00, 1.00007569e+00, 4.44597417e-05, 2.93724453e-04,
1.00007513e+00, 1.00007496e+00, 1.00007532e+00, 1.22357142e-03,
3.27903892e-06, 1.00007592e+00, 1.00007468e+00, 1.00007558e+00,
2.09869172e-05, 1.97610235e-05, 1.00007529e+00, 1.00007530e+00,
1.00007503e+00, 2.68725642e-05, 3.00372853e-03, 1.00007386e+00,
1.00007443e+00, 1.00007388e+00, 5.86993822e-05, 8.69989983e-06,
1.00007590e+00, 1.00007488e+00, 1.00007515e+00, 8.81850779e-04,
2.03875532e-05, 1.00007480e+00, 1.00007425e+00, 1.00007517e+00,
-2.44678912e-05, 4.36556267e-08, 1.00007436e+00, 1.00007558e+00,
1.00007571e+00, 5.42990711e-04, 1.45517859e-04, 1.00007522e+00,
1.00007469e+00, 1.00007575e+00, 2.52271817e-05, 7.46339417e-05,
1.00007427e+00
]
print(closerab(l, 0, 1))
This outputs:
(array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 22, 23, 27, 28, 32, 33, 37, 38, 42, 43, 47, 48, 52, 53,
57, 58, 62, 63]),
array([20, 21, 24, 25, 26, 29, 30, 31, 34, 35, 36, 39, 40, 41, 44, 45, 46,
49, 50, 51, 54, 55, 56, 59, 60, 61, 64]))
Here's a simple generalization for any arbitrary numbers a
and b
, instead of just 0
and 1
:
def closerab(l, a=0, b=1):
l = np.asarray(l)
boolarr = (np.abs(l - b) > np.abs(l - a))
# returns two lists of indices, one for numbers closer to a and one for numbers closer to b
return boolarr.nonzero()[0], (boolarr==0).nonzero()[0]
This'll return two lists, one with the indices of the numbers closer to a
, and one with the indices of the numbers closer to b
.
Testing it out:
l = [
-2.10044520e-04, 1.72314372e-04, 1.77235336e-04, 1.06613465e-04,
6.76617611e-07, 2.71623057e-03, 3.32789944e-05, 1.44899758e-05,
5.79249863e-05, 4.06502549e-04, 1.35823707e-05, 4.13955189e-04,
5.29862793e-05, 1.98286005e-04, 2.22829175e-04, 8.88758230e-04,
5.62228710e-05, 1.36249752e-05, 2.00474996e-05, 2.10090068e-05,
1.00007518e+00, 1.00007569e+00, 4.44597417e-05, 2.93724453e-04,
1.00007513e+00, 1.00007496e+00, 1.00007532e+00, 1.22357142e-03,
3.27903892e-06, 1.00007592e+00, 1.00007468e+00, 1.00007558e+00,
2.09869172e-05, 1.97610235e-05, 1.00007529e+00, 1.00007530e+00,
1.00007503e+00, 2.68725642e-05, 3.00372853e-03, 1.00007386e+00,
1.00007443e+00, 1.00007388e+00, 5.86993822e-05, 8.69989983e-06,
1.00007590e+00, 1.00007488e+00, 1.00007515e+00, 8.81850779e-04,
2.03875532e-05, 1.00007480e+00, 1.00007425e+00, 1.00007517e+00,
-2.44678912e-05, 4.36556267e-08, 1.00007436e+00, 1.00007558e+00,
1.00007571e+00, 5.42990711e-04, 1.45517859e-04, 1.00007522e+00,
1.00007469e+00, 1.00007575e+00, 2.52271817e-05, 7.46339417e-05,
1.00007427e+00
]
print(closerab(l, 0, 1))
This outputs:
(array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 22, 23, 27, 28, 32, 33, 37, 38, 42, 43, 47, 48, 52, 53,
57, 58, 62, 63]),
array([20, 21, 24, 25, 26, 29, 30, 31, 34, 35, 36, 39, 40, 41, 44, 45, 46,
49, 50, 51, 54, 55, 56, 59, 60, 61, 64]))
answered 2 days ago
tel
3,4911427
3,4911427
add a comment |
add a comment |
up vote
2
down vote
Here is one simple way to do this:
>>> a = np.arange(-2, 2.1, 0.1)
>>> (a >= .5).astype(np.float)
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1.])
(Change np.float
to np.int
if you want integers.)
Cleanest and probably fastest solution. Pity it did not attract many votes so far.
– Luca Citi
2 days ago
You don't have to usenp.float
ornp.int
in.astype
, a regularfloat
orint
will do just fine. Numpy will interpret it as the equivalent numpy variant.
– Rob
yesterday
add a comment |
up vote
2
down vote
Here is one simple way to do this:
>>> a = np.arange(-2, 2.1, 0.1)
>>> (a >= .5).astype(np.float)
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1.])
(Change np.float
to np.int
if you want integers.)
Cleanest and probably fastest solution. Pity it did not attract many votes so far.
– Luca Citi
2 days ago
You don't have to usenp.float
ornp.int
in.astype
, a regularfloat
orint
will do just fine. Numpy will interpret it as the equivalent numpy variant.
– Rob
yesterday
add a comment |
up vote
2
down vote
up vote
2
down vote
Here is one simple way to do this:
>>> a = np.arange(-2, 2.1, 0.1)
>>> (a >= .5).astype(np.float)
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1.])
(Change np.float
to np.int
if you want integers.)
Here is one simple way to do this:
>>> a = np.arange(-2, 2.1, 0.1)
>>> (a >= .5).astype(np.float)
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1.])
(Change np.float
to np.int
if you want integers.)
answered 2 days ago
NPE
344k59733866
344k59733866
Cleanest and probably fastest solution. Pity it did not attract many votes so far.
– Luca Citi
2 days ago
You don't have to usenp.float
ornp.int
in.astype
, a regularfloat
orint
will do just fine. Numpy will interpret it as the equivalent numpy variant.
– Rob
yesterday
add a comment |
Cleanest and probably fastest solution. Pity it did not attract many votes so far.
– Luca Citi
2 days ago
You don't have to usenp.float
ornp.int
in.astype
, a regularfloat
orint
will do just fine. Numpy will interpret it as the equivalent numpy variant.
– Rob
yesterday
Cleanest and probably fastest solution. Pity it did not attract many votes so far.
– Luca Citi
2 days ago
Cleanest and probably fastest solution. Pity it did not attract many votes so far.
– Luca Citi
2 days ago
You don't have to use
np.float
or np.int
in .astype
, a regular float
or int
will do just fine. Numpy will interpret it as the equivalent numpy variant.– Rob
yesterday
You don't have to use
np.float
or np.int
in .astype
, a regular float
or int
will do just fine. Numpy will interpret it as the equivalent numpy variant.– Rob
yesterday
add a comment |
up vote
1
down vote
From the Python built-in function docs round(number[, ndigits])
:
Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, it defaults to zero. The result is a floating point number. Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done away from 0 (so, for example,
round(0.5)
is1.0
andround(-0.5)
is-1.0
).
For numpy arrays in particular, you can use the numpy.round_
function.
New contributor
What about numbers that are not in range of 0 and 1?
– Filip Młynarski
2 days ago
1
Round works outside of the[0, 1]
range. Soround(2.2)
would be2.0
,round(-1.2)
would be-1.0
, andround(3.141, 2)
would be3.14
.
– Andrew Fiorillo
2 days ago
@AndrewFiorillo But I just wanted an output 0 and 1 :)
– Eliyah
2 days ago
Then @FilipMłynarski 's answer above is probably the most robust.
– Andrew Fiorillo
2 days ago
add a comment |
up vote
1
down vote
From the Python built-in function docs round(number[, ndigits])
:
Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, it defaults to zero. The result is a floating point number. Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done away from 0 (so, for example,
round(0.5)
is1.0
andround(-0.5)
is-1.0
).
For numpy arrays in particular, you can use the numpy.round_
function.
New contributor
What about numbers that are not in range of 0 and 1?
– Filip Młynarski
2 days ago
1
Round works outside of the[0, 1]
range. Soround(2.2)
would be2.0
,round(-1.2)
would be-1.0
, andround(3.141, 2)
would be3.14
.
– Andrew Fiorillo
2 days ago
@AndrewFiorillo But I just wanted an output 0 and 1 :)
– Eliyah
2 days ago
Then @FilipMłynarski 's answer above is probably the most robust.
– Andrew Fiorillo
2 days ago
add a comment |
up vote
1
down vote
up vote
1
down vote
From the Python built-in function docs round(number[, ndigits])
:
Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, it defaults to zero. The result is a floating point number. Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done away from 0 (so, for example,
round(0.5)
is1.0
andround(-0.5)
is-1.0
).
For numpy arrays in particular, you can use the numpy.round_
function.
New contributor
From the Python built-in function docs round(number[, ndigits])
:
Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, it defaults to zero. The result is a floating point number. Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done away from 0 (so, for example,
round(0.5)
is1.0
andround(-0.5)
is-1.0
).
For numpy arrays in particular, you can use the numpy.round_
function.
New contributor
New contributor
answered 2 days ago
Andrew Fiorillo
685
685
New contributor
New contributor
What about numbers that are not in range of 0 and 1?
– Filip Młynarski
2 days ago
1
Round works outside of the[0, 1]
range. Soround(2.2)
would be2.0
,round(-1.2)
would be-1.0
, andround(3.141, 2)
would be3.14
.
– Andrew Fiorillo
2 days ago
@AndrewFiorillo But I just wanted an output 0 and 1 :)
– Eliyah
2 days ago
Then @FilipMłynarski 's answer above is probably the most robust.
– Andrew Fiorillo
2 days ago
add a comment |
What about numbers that are not in range of 0 and 1?
– Filip Młynarski
2 days ago
1
Round works outside of the[0, 1]
range. Soround(2.2)
would be2.0
,round(-1.2)
would be-1.0
, andround(3.141, 2)
would be3.14
.
– Andrew Fiorillo
2 days ago
@AndrewFiorillo But I just wanted an output 0 and 1 :)
– Eliyah
2 days ago
Then @FilipMłynarski 's answer above is probably the most robust.
– Andrew Fiorillo
2 days ago
What about numbers that are not in range of 0 and 1?
– Filip Młynarski
2 days ago
What about numbers that are not in range of 0 and 1?
– Filip Młynarski
2 days ago
1
1
Round works outside of the
[0, 1]
range. So round(2.2)
would be 2.0
, round(-1.2)
would be -1.0
, and round(3.141, 2)
would be 3.14
.– Andrew Fiorillo
2 days ago
Round works outside of the
[0, 1]
range. So round(2.2)
would be 2.0
, round(-1.2)
would be -1.0
, and round(3.141, 2)
would be 3.14
.– Andrew Fiorillo
2 days ago
@AndrewFiorillo But I just wanted an output 0 and 1 :)
– Eliyah
2 days ago
@AndrewFiorillo But I just wanted an output 0 and 1 :)
– Eliyah
2 days ago
Then @FilipMłynarski 's answer above is probably the most robust.
– Andrew Fiorillo
2 days ago
Then @FilipMłynarski 's answer above is probably the most robust.
– Andrew Fiorillo
2 days ago
add a comment |
up vote
1
down vote
your_list=[[-2.10044520e-04, 1.72314372e-04, 1.77235336e-04, 1.06613465e-04,
6.76617611e-07, 2.71623057e-03, 3.32789944e-05, 1.44899758e-05,
5.79249863e-05, 4.06502549e-04, 1.35823707e-05, 4.13955189e-04,
5.29862793e-05, 1.98286005e-04, 2.22829175e-04, 8.88758230e-04,
5.62228710e-05, 1.36249752e-05, 2.00474996e-05, 2.10090068e-05,
1.00007518e+00, 1.00007569e+00, 4.44597417e-05, 2.93724453e-04,
1.00007513e+00, 1.00007496e+00, 1.00007532e+00, 1.22357142e-03,
3.27903892e-06, 1.00007592e+00, 1.00007468e+00, 1.00007558e+00,
2.09869172e-05, 1.97610235e-05, 1.00007529e+00, 1.00007530e+00,
1.00007503e+00, 2.68725642e-05, 3.00372853e-03, 1.00007386e+00,
1.00007443e+00, 1.00007388e+00, 5.86993822e-05, 8.69989983e-06,
1.00007590e+00, 1.00007488e+00, 1.00007515e+00, 8.81850779e-04,
2.03875532e-05, 1.00007480e+00, 1.00007425e+00, 1.00007517e+00,
-2.44678912e-05, 4.36556267e-08, 1.00007436e+00, 1.00007558e+00,
1.00007571e+00, 5.42990711e-04, 1.45517859e-04, 1.00007522e+00,
1.00007469e+00, 1.00007575e+00, 2.52271817e-05, 7.46339417e-05,
1.00007427e+00]]
close_to_one_or_zero=[1 if x > 0.5 else 0 for x in your_list[0]]
close_to_one_or_zero
[0, 0, 0, 0, 0,....... 1, 1, 1, 0, 0, 1]
add a comment |
up vote
1
down vote
your_list=[[-2.10044520e-04, 1.72314372e-04, 1.77235336e-04, 1.06613465e-04,
6.76617611e-07, 2.71623057e-03, 3.32789944e-05, 1.44899758e-05,
5.79249863e-05, 4.06502549e-04, 1.35823707e-05, 4.13955189e-04,
5.29862793e-05, 1.98286005e-04, 2.22829175e-04, 8.88758230e-04,
5.62228710e-05, 1.36249752e-05, 2.00474996e-05, 2.10090068e-05,
1.00007518e+00, 1.00007569e+00, 4.44597417e-05, 2.93724453e-04,
1.00007513e+00, 1.00007496e+00, 1.00007532e+00, 1.22357142e-03,
3.27903892e-06, 1.00007592e+00, 1.00007468e+00, 1.00007558e+00,
2.09869172e-05, 1.97610235e-05, 1.00007529e+00, 1.00007530e+00,
1.00007503e+00, 2.68725642e-05, 3.00372853e-03, 1.00007386e+00,
1.00007443e+00, 1.00007388e+00, 5.86993822e-05, 8.69989983e-06,
1.00007590e+00, 1.00007488e+00, 1.00007515e+00, 8.81850779e-04,
2.03875532e-05, 1.00007480e+00, 1.00007425e+00, 1.00007517e+00,
-2.44678912e-05, 4.36556267e-08, 1.00007436e+00, 1.00007558e+00,
1.00007571e+00, 5.42990711e-04, 1.45517859e-04, 1.00007522e+00,
1.00007469e+00, 1.00007575e+00, 2.52271817e-05, 7.46339417e-05,
1.00007427e+00]]
close_to_one_or_zero=[1 if x > 0.5 else 0 for x in your_list[0]]
close_to_one_or_zero
[0, 0, 0, 0, 0,....... 1, 1, 1, 0, 0, 1]
add a comment |
up vote
1
down vote
up vote
1
down vote
your_list=[[-2.10044520e-04, 1.72314372e-04, 1.77235336e-04, 1.06613465e-04,
6.76617611e-07, 2.71623057e-03, 3.32789944e-05, 1.44899758e-05,
5.79249863e-05, 4.06502549e-04, 1.35823707e-05, 4.13955189e-04,
5.29862793e-05, 1.98286005e-04, 2.22829175e-04, 8.88758230e-04,
5.62228710e-05, 1.36249752e-05, 2.00474996e-05, 2.10090068e-05,
1.00007518e+00, 1.00007569e+00, 4.44597417e-05, 2.93724453e-04,
1.00007513e+00, 1.00007496e+00, 1.00007532e+00, 1.22357142e-03,
3.27903892e-06, 1.00007592e+00, 1.00007468e+00, 1.00007558e+00,
2.09869172e-05, 1.97610235e-05, 1.00007529e+00, 1.00007530e+00,
1.00007503e+00, 2.68725642e-05, 3.00372853e-03, 1.00007386e+00,
1.00007443e+00, 1.00007388e+00, 5.86993822e-05, 8.69989983e-06,
1.00007590e+00, 1.00007488e+00, 1.00007515e+00, 8.81850779e-04,
2.03875532e-05, 1.00007480e+00, 1.00007425e+00, 1.00007517e+00,
-2.44678912e-05, 4.36556267e-08, 1.00007436e+00, 1.00007558e+00,
1.00007571e+00, 5.42990711e-04, 1.45517859e-04, 1.00007522e+00,
1.00007469e+00, 1.00007575e+00, 2.52271817e-05, 7.46339417e-05,
1.00007427e+00]]
close_to_one_or_zero=[1 if x > 0.5 else 0 for x in your_list[0]]
close_to_one_or_zero
[0, 0, 0, 0, 0,....... 1, 1, 1, 0, 0, 1]
your_list=[[-2.10044520e-04, 1.72314372e-04, 1.77235336e-04, 1.06613465e-04,
6.76617611e-07, 2.71623057e-03, 3.32789944e-05, 1.44899758e-05,
5.79249863e-05, 4.06502549e-04, 1.35823707e-05, 4.13955189e-04,
5.29862793e-05, 1.98286005e-04, 2.22829175e-04, 8.88758230e-04,
5.62228710e-05, 1.36249752e-05, 2.00474996e-05, 2.10090068e-05,
1.00007518e+00, 1.00007569e+00, 4.44597417e-05, 2.93724453e-04,
1.00007513e+00, 1.00007496e+00, 1.00007532e+00, 1.22357142e-03,
3.27903892e-06, 1.00007592e+00, 1.00007468e+00, 1.00007558e+00,
2.09869172e-05, 1.97610235e-05, 1.00007529e+00, 1.00007530e+00,
1.00007503e+00, 2.68725642e-05, 3.00372853e-03, 1.00007386e+00,
1.00007443e+00, 1.00007388e+00, 5.86993822e-05, 8.69989983e-06,
1.00007590e+00, 1.00007488e+00, 1.00007515e+00, 8.81850779e-04,
2.03875532e-05, 1.00007480e+00, 1.00007425e+00, 1.00007517e+00,
-2.44678912e-05, 4.36556267e-08, 1.00007436e+00, 1.00007558e+00,
1.00007571e+00, 5.42990711e-04, 1.45517859e-04, 1.00007522e+00,
1.00007469e+00, 1.00007575e+00, 2.52271817e-05, 7.46339417e-05,
1.00007427e+00]]
close_to_one_or_zero=[1 if x > 0.5 else 0 for x in your_list[0]]
close_to_one_or_zero
[0, 0, 0, 0, 0,....... 1, 1, 1, 0, 0, 1]
answered 2 days ago
cph_sto
552214
552214
add a comment |
add a comment |
up vote
1
down vote
You can use round
:
[round(i) for i in [0.1,0.2,0.3,0.8,0.9]]
add a comment |
up vote
1
down vote
You can use round
:
[round(i) for i in [0.1,0.2,0.3,0.8,0.9]]
add a comment |
up vote
1
down vote
up vote
1
down vote
You can use round
:
[round(i) for i in [0.1,0.2,0.3,0.8,0.9]]
You can use round
:
[round(i) for i in [0.1,0.2,0.3,0.8,0.9]]
edited yesterday
davnicwil
8,10444962
8,10444962
answered 2 days ago
Rudolf Morkovskyi
692113
692113
add a comment |
add a comment |
up vote
1
down vote
Alternatively, you can use a ternary operator.
x = [-0.2, 0.1, 1.1, 0.75, 0.4, 0.2, 1.5, 0.9]
a = 0
b = 1
[a if i <= (a+b)/2 else b for i in x]
add a comment |
up vote
1
down vote
Alternatively, you can use a ternary operator.
x = [-0.2, 0.1, 1.1, 0.75, 0.4, 0.2, 1.5, 0.9]
a = 0
b = 1
[a if i <= (a+b)/2 else b for i in x]
add a comment |
up vote
1
down vote
up vote
1
down vote
Alternatively, you can use a ternary operator.
x = [-0.2, 0.1, 1.1, 0.75, 0.4, 0.2, 1.5, 0.9]
a = 0
b = 1
[a if i <= (a+b)/2 else b for i in x]
Alternatively, you can use a ternary operator.
x = [-0.2, 0.1, 1.1, 0.75, 0.4, 0.2, 1.5, 0.9]
a = 0
b = 1
[a if i <= (a+b)/2 else b for i in x]
answered yesterday
Anastasiya-Romanova 秀
1,9111230
1,9111230
add a comment |
add a comment |
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%2f53467807%2fpython-function-that-identifies-if-the-numbers-in-a-list-or-array-are-closer-to%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
1
You may want to take a look at
numpy.rint
. Although it returns floats and not ints, for whatever reason, but you can cast the result to int after applying this function.– ForceBru
2 days ago
@ForceBru: It returns floats because float to int conversion can overflow.
– Kevin
2 days ago
5
What behaviour do you want for
0.5
?– Eric Towers
2 days ago
3
@ForceBru or even just (x >= 0.5).
– The Great Duck
2 days ago