2

I've got a list containing numpy matrices. Anyway that I could turn the whole thing into a nice clean numpy array?

From:

[matrix([[1]]), matrix([[ 1.99387871]]), matrix([[ 2.53564618]]), matrix([[ 4.39125807]]), matrix([[ 4.246309]]), matrix([[ 5.21571607]]), matrix([[ 6.17408811]]), matrix([[ 4.75146571]]), matrix([[ 6.19319742]]), matrix([[ 6.1277607]]), matrix([[ 7.43821216]])]

To:

[[1 1.99387871 2.53564618 4.39125807 4.246309 5.21571607 6.17408811 4.75146571 6.19319742 6.1277607 7.43821216]]

1 Answer 1

6
b = np.asarray(a, dtype=float)
#to get the same shape do.
b = b.reshape(-1, len(b)) 
#to just get one dimmension do. 
b = np.asarray(a, dtype=float).reshape(len(a))
Sign up to request clarification or add additional context in comments.

3 Comments

Maybe add .reshape(-1, len(a)) if the OP cares about the shape.
Also, I don't think you want the dtype=float. Without it, you preserve the dtype of the individual matrices—or, if they're different, as in the OP's example, the coerced union of the types. That's probably better than forcing float even if there were, say, float128 or complex128 values in the matrices.
@abarnert True, for the general case forcing float is not a good solution, but setting the dtype when you know what it is, usually a good idea. Sometimes numpy isn't smart enough to figure them out and will just set it as a generic object. which can cause problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.