I want to convert a list of lists into a numpy matrix and then transpose it. How can I do this?
Asked
Active
Viewed 88 times
2 Answers
0
Simply use np.array
x = [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]
a = np.array(x)
a.transpose()
Out:
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]])

SRT HellKitty
- 577
- 3
- 10
0
Try this:
import numpy as np
from pprint import pprint
a = [[0.2, 0.4],[0.4, 0.6],[0.4, 0.6],[0.20, 1.]]
pprint(a)
b = np.array(a)
b_t = b.T
pprint(b)
pprint(b_t)
The output is:
[[0.2, 0.4], [0.4, 0.6], [0.4, 0.6], [0.2, 1.0]]
array([[0.2, 0.4],
[0.4, 0.6],
[0.4, 0.6],
[0.2, 1. ]])
array([[0.2, 0.4, 0.4, 0.2],
[0.4, 0.6, 0.6, 1. ]])

Bill Chen
- 1,699
- 14
- 24