Transforming a list to a column vector in Python

前端 未结 4 1974
面向向阳花
面向向阳花 2021-01-21 05:33

I was wondering if there\'s a function that takes a list X as input and outputs a column vector corresponding to X?

(I looked around and I susp

4条回答
  •  清歌不尽
    2021-01-21 05:38

    A list is a native type of Python, whereas a numpy array is a numpy object. You will need to convert your list to a numpy array first. You can do something like the following.

    x = list(range(5))
    print(x)
    

    [0, 1, 2, 3, 4]

    x_vector = np.asarray(x)
    

    array([0, 1, 2, 3, 4])

    Now, Python does not know whats the difference between a row vector and a column vector, that is up to how you will use the vector. The current vector is 1x5. If you want 5x1, you can take the transpose by either

    x_vector.T
    

    or

    np.transpose(x_vector)
    

    However, since this is a 1D matrix, the transpose is the same as the non-transposed vector in memory.

提交回复
热议问题