Given this code:
arr = np.array(
[
[
np.array([1, 2, 3]),
np.array([4, 9])
],
[
np.array([1, 2, 5]),
np.array([5, 6])
],
[
np.array([1, 2, 5]),
np.array([85, 86])
]
]
)
(input, expected) = arr.T
input[:2]
I am getting this output:
array([array([1, 2, 3]), array([1, 2, 5])], dtype=object)
But I want this to be a 2d array of type dtype
float32
.
So if I create it using a different way:
np.array(
[
[1, 2, 3],
[1, 2, 5]
], dtype=np.float32
)
This is what I want the output to look like:
array([[1., 2., 3.],
[1., 2., 5.]], dtype=float32)
How do I convert input
into this 2d array, so I won't see dtype=object
, but dtype=float32
?
I've tried this:
np.array(input[:2], dtype=np.float32)
but it prints this error:
ValueError: setting an array element with a sequence.
I've also looked at other functions like view
, asarray
, all of them resulted in a similar error.
I found it eventually:
This doesn't work:
np.array(input, dtype=np.float32)
But copying it does:
np.array([x for x in input], dtype=np.float32)
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.