I have an specific array which each rows has to array. I want to reshape it. But, I don't know how to reshape it to a 2d array. Here is my array:
x = np.array([[[array([0.23006942, 0.29322573, 0.37292298]),
array([0.23006942])],
[array([0.29322573, 0.37292298, 0.23006942]),
array([0.32480389])],
[array([0.37292298, 0.23006942, 0.32480389]),
array([0.31427784])]],
[[array([0.09971349, 0.08827682, 0.0900638 ]),
array([0.02251595])],
[array([0.08827682, 0.0900638 , 0.02251595]),
array([0.0986413])],
[array([0.0900638 , 0.02251595, 0.0986413 ]),
array([0.02144376])]]], dtype=object)
Here is the desired output:
out = np.array(
[
[array([0.23006942, 0.29322573, 0.37292298]),array([0.23006942])],
[array([0.29322573, 0.37292298, 0.23006942]),array([0.32480389])],
[array([0.37292298, 0.23006942, 0.32480389]),array([0.31427784])],
[array([0.09971349, 0.08827682, 0.0900638 ]),array([0.02251595])],
[array([0.08827682, 0.0900638 , 0.02251595]),array([0.0986413])],
[array([0.0900638 , 0.02251595, 0.0986413 ]),array([0.02144376])]
])
Any help appreciated.
Thanks
I've tried to use the reshape. But, it does not solve.
CodePudding user response:
I think you can use reshape
(in a correct way):
# Your code
import numpy as np
x = np.array([[[np.array([0.23006942, 0.29322573, 0.37292298]),
np.array([0.23006942])],
[np.array([0.29322573, 0.37292298, 0.23006942]),
np.array([0.32480389])],
[np.array([0.37292298, 0.23006942, 0.32480389]),
np.array([0.31427784])]],
[[np.array([0.09971349, 0.08827682, 0.0900638 ]),
np.array([0.02251595])],
[np.array([0.08827682, 0.0900638 , 0.02251595]),
np.array([0.0986413])],
[np.array([0.0900638 , 0.02251595, 0.0986413 ]),
np.array([0.02144376])]]], dtype=object)
# What you need
out = x.reshape(6,2)
out
Output
array([
[array([0.23006942, 0.29322573, 0.37292298]), array([0.23006942])],
[array([0.29322573, 0.37292298, 0.23006942]), array([0.32480389])],
[array([0.37292298, 0.23006942, 0.32480389]), array([0.31427784])],
[array([0.09971349, 0.08827682, 0.0900638 ]), array([0.02251595])],
[array([0.08827682, 0.0900638 , 0.02251595]), array([0.0986413])],
[array([0.0900638 , 0.02251595, 0.0986413 ]), array([0.02144376])]
],
dtype=object)
CodePudding user response:
It's ragged, it's not concatenated. Is it a question of flattening the array to a list of arrays?
out = [x.tolist() for arr in x for x in arr]
Output:
[[array([0.23006942, 0.29322573, 0.37292298]), array([0.23006942])],
[array([0.29322573, 0.37292298, 0.23006942]), array([0.32480389])],
[array([0.37292298, 0.23006942, 0.32480389]), array([0.31427784])],
[array([0.09971349, 0.08827682, 0.0900638 ]), array([0.02251595])],
[array([0.08827682, 0.0900638 , 0.02251595]), array([0.0986413])],
[array([0.0900638 , 0.02251595, 0.0986413 ]), array([0.02144376])]]