Quantcast
Channel: Flipping zeroes and ones in one-dimensional NumPy array - Stack Overflow
Browsing all 6 articles
Browse latest View live

Answer by Mikolaj Buchwald for Flipping zeroes and ones in one-dimensional...

I also found a way to do it:In [1]: from numpy import arrayIn [2]: a = array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])In [3]: b = (~a.astype(bool)).astype(int)In [4]: print(a); print(b)[1 1 1 1 1...

View Article



Answer by YXD for Flipping zeroes and ones in one-dimensional NumPy array

A sign that you should probably be using a boolean datatypea = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=np.bool)# orb = ~ab = np.logical_not(a)

View Article

Answer by John Greenall for Flipping zeroes and ones in one-dimensional NumPy...

another superfluous option:numpy.logical_not(a).astype(int)

View Article

Answer by heltonbiker for Flipping zeroes and ones in one-dimensional NumPy...

answer = numpy.ones_like(a) - a

View Article

Answer by gboffi for Flipping zeroes and ones in one-dimensional NumPy array

There must be something in your Q that i do not understand...AnywayIn [2]: from numpy import arrayIn [3]: a = array((1,0,0,1,1,0,0))In [4]: b = 1-aIn [5]: print a ; print b[1 0 0 1 1 0 0][0 1 1 0 0 1...

View Article


Flipping zeroes and ones in one-dimensional NumPy array

I have a one-dimensional NumPy array that consists of zeroes and ones like so:array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])I'd like a quick way to just "flip" the values such that zeroes become...

View Article
Browsing all 6 articles
Browse latest View live




Latest Images