[PYTHON] Things to keep in mind when converting row vectors to column vectors with ndarray

This is a story about converting a row vector to a column vector with python's ndarray. Involuntarily, it's easy to do the following. print(np.array([1,2,3,4,5]).T) But with this, the result is

[1 2 3 4 5]

And cannot be converted to a row vector. This is because the row vector is represented by a one-dimensional array. This is because a column vector can only be represented by an array of two or more dimensions. In fact, print(np.array([[1,2,3,4,5]]).T) If you pass a row vector in a two-dimensional array like

[[1]
 [2]
 [3]
 [4]
 [5]]

I get the result.

By the way print(np.transpose(np.array([1,2,3,4,5]))) The same is true for, and the column vector cannot be obtained.

If you want to get the column vector m (as a two-dimensional array) from the row vector v as a one-dimensional array,

m = np.array([v]).T

Let's say

Recommended Posts

Things to keep in mind when converting row vectors to column vectors with ndarray
Things to keep in mind when using Python with AtCoder
Things to keep in mind when using cgi with python.
Things to keep in mind when developing crawlers in Python
Things to keep in mind when processing strings in Python2
Things to keep in mind when processing strings in Python3
Things to keep in mind when using Python for those who use MATLAB
Things to keep in mind when doing Batch Prediction on GCP ML Engine
Assign to any column in each row with np.array
Things to keep in mind when building automation tools for the manufacturing floor in Python
How to access with cache when reading_json in pandas
Things to note when initializing a list in Python
Stumble when converting bidirectional list to JSON in Go
Things to do when you start developing with Django
Things to watch out for when migrating with Django
I briefly summarized what you should keep in mind when learning with or without supervised learning
Summary of points to keep in mind when writing a program that runs on Python 2.5
Things to watch out for when using default arguments in Python
How to not escape Japanese when dealing with json in python
Things to watch out for when naming dynamic routing in nuxt.js
Timezone specification when converting a string to datetime type in python