Davidoff
Davidoff

Reputation: 21

Convert matrix to tuple python

I have a matrix called w and it looks like this:

in:  print(w)
out: [[0.0053691  0.00328692]]

it has a type <class 'numpy.matrix'>

in:  print(type(w))
out: <class 'numpy.matrix'>

I want it to be a tuple, but a simple w = tuple(w) is not doing the job done:

in:  w=tuple(w)
in:  print(w)
out: (matrix([[0.00624969, 0.00413867]]),)

But i want output to be like this:

in:  print(w)
out: (0.00624969, 0.00413867)

What am I doing wrong?

Upvotes: 0

Views: 960

Answers (2)

Pablo C
Pablo C

Reputation: 4761

You're "storing" the matrix into a tuple, not converting it. You can do the following

tuple(*w.A)
#(0.00624969, 0.00413867)

The starred expression (*w.A) unpackages outer dimension of the array values by iteration. Read more here.

Upvotes: 0

mdurant
mdurant

Reputation: 28684

You have a two-dimensional thing.

You can create a list like

>>> w.tolist()
[[0.0053691, 0.00328692]]

which is probably good enough. If you really wanted to extract a tulpe of the inner part

>>> tuple(w.tolist()[0])
(0.0053691, 0.00328692)

Upvotes: 1

Related Questions