Python function that identifies if the numbers in a list or array are closer to 0 or 1
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 |
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
4
@ForceBru or even just (x >= 0.5).
– The Great Duck
Nov 26 at 0:50
add a comment |
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
48.8k116390
48.8k116390
asked Nov 25 at 13:12
Eliyah
3391525
3391525
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
4
@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
4
@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
4
4
@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
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
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 |
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 |
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 |
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 |
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 |
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 |
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 F
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 F
Nov 25 at 13:30
add a comment |
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 |
You can use round
:
[round(i) for i in [0.1,0.2,0.3,0.8,0.9]]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
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
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
48.8k116390
48.8k116390
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
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 |
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 |
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,562422
1,562422
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 |
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 |
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 |
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
346k60740872
346k60740872
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 |
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 |
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 |
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
12k1924
12k1924
add a comment |
add a comment |
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 |
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 |
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,5401311
1,5401311
add a comment |
add a comment |
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 |
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 |
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
6,07011430
6,07011430
add a comment |
add a comment |
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 |
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 |
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,9311230
1,9311230
add a comment |
add a comment |
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 F
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 F
Nov 25 at 13:30
add a comment |
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 F
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 F
Nov 25 at 13:30
add a comment |
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 F
58829
58829
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 F
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 F
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 F
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 F
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 F
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 F
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 F
Nov 25 at 13:30
Then @FilipMłynarski 's answer above is probably the most robust.
– Andrew F
Nov 25 at 13:30
add a comment |
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 |
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 |
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
890218
890218
add a comment |
add a comment |
You can use round
:
[round(i) for i in [0.1,0.2,0.3,0.8,0.9]]
add a comment |
You can use round
:
[round(i) for i in [0.1,0.2,0.3,0.8,0.9]]
add a comment |
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,22845063
8,22845063
answered Nov 25 at 13:20
Rudolf Morkovskyi
724117
724117
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
4
@ForceBru or even just (x >= 0.5).
– The Great Duck
Nov 26 at 0:50