Python function that identifies if the numbers in a list or array are closer to 0 or 1
Clash Royale CLAN TAG#URR8PPP
up vote
12
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
12
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
Nov 25 at 13:15
@ForceBru: It returns floats because float to int conversion can overflow.
– Kevin
Nov 25 at 15:31
5
What behaviour do you want for0.5
?
– Eric Towers
Nov 25 at 16:46
3
@ForceBru or even just (x >= 0.5).
– The Great Duck
Nov 26 at 0:50
add a comment |
up vote
12
down vote
favorite
up vote
12
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 Nov 26 at 7:51
timgeb
46.4k106287
46.4k106287
asked Nov 25 at 13:12
Eliyah
3061525
3061525
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
Nov 25 at 13:15
@ForceBru: It returns floats because float to int conversion can overflow.
– Kevin
Nov 25 at 15:31
5
What behaviour do you want for0.5
?
– Eric Towers
Nov 25 at 16:46
3
@ForceBru or even just (x >= 0.5).
– The Great Duck
Nov 26 at 0:50
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
Nov 25 at 13:15
@ForceBru: It returns floats because float to int conversion can overflow.
– Kevin
Nov 25 at 15:31
5
What behaviour do you want for0.5
?
– Eric Towers
Nov 25 at 16:46
3
@ForceBru or even just (x >= 0.5).
– The Great Duck
Nov 26 at 0:50
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
Nov 25 at 13:15
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
Nov 25 at 13:15
@ForceBru: It returns floats because float to int conversion can overflow.
– Kevin
Nov 25 at 15:31
@ForceBru: It returns floats because float to int conversion can overflow.
– Kevin
Nov 25 at 15:31
5
5
What behaviour do you want for
0.5
?– Eric Towers
Nov 25 at 16:46
What behaviour do you want for
0.5
?– Eric Towers
Nov 25 at 16:46
3
3
@ForceBru or even just (x >= 0.5).
– The Great Duck
Nov 26 at 0:50
@ForceBru or even just (x >= 0.5).
– The Great Duck
Nov 26 at 0:50
add a comment |
10 Answers
10
active
oldest
votes
up vote
13
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
Nov 25 at 13:23
2
@Eliyah thennp.where
!
– timgeb
Nov 25 at 13:29
np.minimum(np.rint(a),1) returns 0 for a<0.5 and 1 for a>=0.5.
– Tls Chris
Nov 25 at 13:31
@TlsChris On my machine:np.minimum(np.rint(0.5),1)
->0
becausenp.rint(0.5)
->0
.
– timgeb
Nov 25 at 13:34
1
@timgeb. np.rint(0.5) rounds down on my machine too. I shouldn't make assumptions :-).
– Tls Chris
Nov 25 at 15:27
|
show 2 more comments
up vote
20
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
Nov 25 at 13:22
1
Did not mention rounding solutions, as no range was defined.
– Dinari
Nov 25 at 13:24
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
3
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
Nov 26 at 0:30
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
Nov 26 at 10:47
add a comment |
up vote
2
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
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.
What about numbers that are not in range of 0 and 1?
– Filip Młynarski
Nov 25 at 13:19
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
Nov 25 at 13:22
@AndrewFiorillo But I just wanted an output 0 and 1 :)
– Eliyah
Nov 25 at 13:24
Then @FilipMłynarski 's answer above is probably the most robust.
– Andrew Fiorillo
Nov 25 at 13:30
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 |
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
13
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
Nov 25 at 13:23
2
@Eliyah thennp.where
!
– timgeb
Nov 25 at 13:29
np.minimum(np.rint(a),1) returns 0 for a<0.5 and 1 for a>=0.5.
– Tls Chris
Nov 25 at 13:31
@TlsChris On my machine:np.minimum(np.rint(0.5),1)
->0
becausenp.rint(0.5)
->0
.
– timgeb
Nov 25 at 13:34
1
@timgeb. np.rint(0.5) rounds down on my machine too. I shouldn't make assumptions :-).
– Tls Chris
Nov 25 at 15:27
|
show 2 more comments
up vote
13
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
Nov 25 at 13:23
2
@Eliyah thennp.where
!
– timgeb
Nov 25 at 13:29
np.minimum(np.rint(a),1) returns 0 for a<0.5 and 1 for a>=0.5.
– Tls Chris
Nov 25 at 13:31
@TlsChris On my machine:np.minimum(np.rint(0.5),1)
->0
becausenp.rint(0.5)
->0
.
– timgeb
Nov 25 at 13:34
1
@timgeb. np.rint(0.5) rounds down on my machine too. I shouldn't make assumptions :-).
– Tls Chris
Nov 25 at 15:27
|
show 2 more comments
up vote
13
down vote
accepted
up vote
13
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 Nov 25 at 13:28
answered Nov 25 at 13:22
timgeb
46.4k106287
46.4k106287
1
What if my numbers is above 2?
– Eliyah
Nov 25 at 13:23
2
@Eliyah thennp.where
!
– timgeb
Nov 25 at 13:29
np.minimum(np.rint(a),1) returns 0 for a<0.5 and 1 for a>=0.5.
– Tls Chris
Nov 25 at 13:31
@TlsChris On my machine:np.minimum(np.rint(0.5),1)
->0
becausenp.rint(0.5)
->0
.
– timgeb
Nov 25 at 13:34
1
@timgeb. np.rint(0.5) rounds down on my machine too. I shouldn't make assumptions :-).
– Tls Chris
Nov 25 at 15:27
|
show 2 more comments
1
What if my numbers is above 2?
– Eliyah
Nov 25 at 13:23
2
@Eliyah thennp.where
!
– timgeb
Nov 25 at 13:29
np.minimum(np.rint(a),1) returns 0 for a<0.5 and 1 for a>=0.5.
– Tls Chris
Nov 25 at 13:31
@TlsChris On my machine:np.minimum(np.rint(0.5),1)
->0
becausenp.rint(0.5)
->0
.
– timgeb
Nov 25 at 13:34
1
@timgeb. np.rint(0.5) rounds down on my machine too. I shouldn't make assumptions :-).
– Tls Chris
Nov 25 at 15:27
1
1
What if my numbers is above 2?
– Eliyah
Nov 25 at 13:23
What if my numbers is above 2?
– Eliyah
Nov 25 at 13:23
2
2
@Eliyah then
np.where
!– timgeb
Nov 25 at 13:29
@Eliyah then
np.where
!– timgeb
Nov 25 at 13:29
np.minimum(np.rint(a),1) returns 0 for a<0.5 and 1 for a>=0.5.
– Tls Chris
Nov 25 at 13:31
np.minimum(np.rint(a),1) returns 0 for a<0.5 and 1 for a>=0.5.
– Tls Chris
Nov 25 at 13:31
@TlsChris On my machine:
np.minimum(np.rint(0.5),1)
-> 0
because np.rint(0.5)
-> 0
.– timgeb
Nov 25 at 13:34
@TlsChris On my machine:
np.minimum(np.rint(0.5),1)
-> 0
because np.rint(0.5)
-> 0
.– timgeb
Nov 25 at 13:34
1
1
@timgeb. np.rint(0.5) rounds down on my machine too. I shouldn't make assumptions :-).
– Tls Chris
Nov 25 at 15:27
@timgeb. np.rint(0.5) rounds down on my machine too. I shouldn't make assumptions :-).
– Tls Chris
Nov 25 at 15:27
|
show 2 more comments
up vote
20
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
Nov 25 at 13:22
1
Did not mention rounding solutions, as no range was defined.
– Dinari
Nov 25 at 13:24
add a comment |
up vote
20
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
Nov 25 at 13:22
1
Did not mention rounding solutions, as no range was defined.
– Dinari
Nov 25 at 13:24
add a comment |
up vote
20
down vote
up vote
20
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 Nov 25 at 13:19
Dinari
1,176321
1,176321
3
Best to use NumPy for NumPy arrays, +1. Also worth mentioning other options if performance is an issue.
– jpp
Nov 25 at 13:22
1
Did not mention rounding solutions, as no range was defined.
– Dinari
Nov 25 at 13:24
add a comment |
3
Best to use NumPy for NumPy arrays, +1. Also worth mentioning other options if performance is an issue.
– jpp
Nov 25 at 13:22
1
Did not mention rounding solutions, as no range was defined.
– Dinari
Nov 25 at 13:24
3
3
Best to use NumPy for NumPy arrays, +1. Also worth mentioning other options if performance is an issue.
– jpp
Nov 25 at 13:22
Best to use NumPy for NumPy arrays, +1. Also worth mentioning other options if performance is an issue.
– jpp
Nov 25 at 13:22
1
1
Did not mention rounding solutions, as no range was defined.
– Dinari
Nov 25 at 13:24
Did not mention rounding solutions, as no range was defined.
– Dinari
Nov 25 at 13:24
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 Nov 25 at 13:23
Daniel Mesejo
9,1931923
9,1931923
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 Nov 25 at 13:25
Filip Młynarski
1,351111
1,351111
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 Nov 25 at 14:11
tel
3,5711428
3,5711428
add a comment |
add a comment |
up vote
3
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
Nov 26 at 0:30
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
Nov 26 at 10:47
add a comment |
up vote
3
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
Nov 26 at 0:30
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
Nov 26 at 10:47
add a comment |
up vote
3
down vote
up vote
3
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 Nov 25 at 19:22
NPE
344k60735866
344k60735866
Cleanest and probably fastest solution. Pity it did not attract many votes so far.
– Luca Citi
Nov 26 at 0:30
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
Nov 26 at 10:47
add a comment |
Cleanest and probably fastest solution. Pity it did not attract many votes so far.
– Luca Citi
Nov 26 at 0:30
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
Nov 26 at 10:47
Cleanest and probably fastest solution. Pity it did not attract many votes so far.
– Luca Citi
Nov 26 at 0:30
Cleanest and probably fastest solution. Pity it did not attract many votes so far.
– Luca Citi
Nov 26 at 0:30
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
Nov 26 at 10:47
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
Nov 26 at 10:47
add a comment |
up vote
2
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
2
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
2
down vote
up vote
2
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 Nov 26 at 9:41
Anastasiya-Romanova 秀
1,9211230
1,9211230
add a comment |
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.
What about numbers that are not in range of 0 and 1?
– Filip Młynarski
Nov 25 at 13:19
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
Nov 25 at 13:22
@AndrewFiorillo But I just wanted an output 0 and 1 :)
– Eliyah
Nov 25 at 13:24
Then @FilipMłynarski 's answer above is probably the most robust.
– Andrew Fiorillo
Nov 25 at 13:30
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.
What about numbers that are not in range of 0 and 1?
– Filip Młynarski
Nov 25 at 13:19
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
Nov 25 at 13:22
@AndrewFiorillo But I just wanted an output 0 and 1 :)
– Eliyah
Nov 25 at 13:24
Then @FilipMłynarski 's answer above is probably the most robust.
– Andrew Fiorillo
Nov 25 at 13:30
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.
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.
answered Nov 25 at 13:18
Andrew Fiorillo
1336
1336
What about numbers that are not in range of 0 and 1?
– Filip Młynarski
Nov 25 at 13:19
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
Nov 25 at 13:22
@AndrewFiorillo But I just wanted an output 0 and 1 :)
– Eliyah
Nov 25 at 13:24
Then @FilipMłynarski 's answer above is probably the most robust.
– Andrew Fiorillo
Nov 25 at 13:30
add a comment |
What about numbers that are not in range of 0 and 1?
– Filip Młynarski
Nov 25 at 13:19
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
Nov 25 at 13:22
@AndrewFiorillo But I just wanted an output 0 and 1 :)
– Eliyah
Nov 25 at 13:24
Then @FilipMłynarski 's answer above is probably the most robust.
– Andrew Fiorillo
Nov 25 at 13:30
What about numbers that are not in range of 0 and 1?
– Filip Młynarski
Nov 25 at 13:19
What about numbers that are not in range of 0 and 1?
– Filip Młynarski
Nov 25 at 13:19
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
Nov 25 at 13:22
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
Nov 25 at 13:22
@AndrewFiorillo But I just wanted an output 0 and 1 :)
– Eliyah
Nov 25 at 13:24
@AndrewFiorillo But I just wanted an output 0 and 1 :)
– Eliyah
Nov 25 at 13:24
Then @FilipMłynarski 's answer above is probably the most robust.
– Andrew Fiorillo
Nov 25 at 13:30
Then @FilipMłynarski 's answer above is probably the most robust.
– Andrew Fiorillo
Nov 25 at 13:30
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 Nov 25 at 13:30
cph_sto
562214
562214
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 Nov 26 at 8:00
davnicwil
8,10344962
8,10344962
answered Nov 25 at 13:20
Rudolf Morkovskyi
702115
702115
add a comment |
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%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
Nov 25 at 13:15
@ForceBru: It returns floats because float to int conversion can overflow.
– Kevin
Nov 25 at 15:31
5
What behaviour do you want for
0.5
?– Eric Towers
Nov 25 at 16:46
3
@ForceBru or even just (x >= 0.5).
– The Great Duck
Nov 26 at 0:50