简体   繁体   中英

Remove Decimals from Array

I have 2 arrays containing zeros & ones. I want to perform hstack() on them but not getting the desired output.

Python Code..

 import numpy as np
 zeros = np.zeros(8)
 ones = np.ones(8)
 zerosThenOnes = np.hstack((zeros, ones))   # A 1 by 16 array

Current Output..

 [ 0.  0.  0.  0.  0.  0.  0.  0.  1.  1.  1.  1.  1.  1.  1.  1.]

Expected Output..

 [ 0   0   0   0   0   0   0   0   1   1   1   1   1   1   1   1 ]

I can't understand what silly mistake I'm doing.

You must tell numpy to return the values as integers

import numpy as np

zeros = np.zeros((8,), dtype=np.int)
ones = np.ones((8,), dtype=np.int)
zerosThenOnes = np.hstack((zeros, ones))

To print out zerosThenOnes like this [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]

Use:

print([x for x in zerosThenOnes])

Numpy Zeros

np.hstack((np.zeros(8), np.ones(8))).astype(int)

for np.array output, or

map( int, np.hstack((np.zeros(8), np.ones(8))) )

for list output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM