Transform from list in python to mat in numpy

来源:互联网 发布:linux c项目开发ide 编辑:程序博客网 时间:2024/05/29 16:19

Code_1

    import numpy as np    dataMat = []    dataMat.append([1,2,3],[4,5,6])    mat = np.mat(dataMat)    print np.shape(mat)

(2,3)

Code_2

    import numpy as np    labelMat = []    labelMat.append(1)    labelMat.append(2)    labelMat.append(3)    mat = np.mat(labelMat)    print np.shape(mat)

(1,3)

Code_3

    import numpy as np    labelMat = []    labelMat.append([1])    labelMat.append([2])    labelMat.append([3])    mat = np.mat(labelMat)    print np.shape(mat)

(3,1)

Notice the difference between Code_2 and Code_3. It is pretty important if we need to do some matrix computation.

0 0
原创粉丝点击