python - How to convert a boolean array to an int array -
i use scilab, , want convert array of booleans array of integers:
>>> x = np.array([4, 3, 2, 1]) >>> y = 2 >= x >>> y array([false, false, true, true], dtype=bool)
in scilab can use:
>>> bool2s(y) 0. 0. 1. 1.
or multiply 1:
>>> 1*y 0. 0. 1. 1.
is there simple command in python, or have use loop?
numpy arrays have astype
method. y.astype(int)
.
note might not necessary this, depending on you're using array for. bool autopromoted int in many cases, can add int arrays without having explicitly convert it:
>>> x array([ true, false, true], dtype=bool) >>> x + [1, 2, 3] array([2, 2, 4])
Comments
Post a Comment