python: list of matrices to numpy array?

泄露秘密 提交于 2020-01-05 14:12:13

问题


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:


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))


来源:https://stackoverflow.com/questions/20749843/python-list-of-matrices-to-numpy-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!