简体   繁体   中英

How to add a dot in python numpy ndarray - data type issue

I have a NumPy ndarray that looks like:

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

but I would like to process it to the following form:

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

How would I achieve this?

It looks to me like you have an array of some integer type. You probably want to convert to an array of float:

array_float = array_int.astype(float)

eg:

>>> ones_i = np.ones(10, dtype=int)
>>> print ones_i
[1 1 1 1 1 1 1 1 1 1]
>>> ones_f = ones_i.astype(float)
>>> print ones_f
[ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]

With that said, I think that it is worth asking why you want to process the string representation of your array. There very well might be a better way to accomplish your goal.

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