首页 > 解决方案 > Numpy 转置向量

问题描述

如何用 numpy 转置向量?我在尝试

import numpy as np
center = np.array([1,2])
center_t = np.transpose(center)

但它不起作用,我该怎么办?

标签: pythonnumpytranspose

解决方案


重塑应该可以解决问题。

center = np.array([1,2])

print(center.reshape(-1,1))

数组([[1],[2]])

但是,对于 n 维数组,这将转置数组。

print(center.T)

例如:

a = np.array([['a','b','c'],['d','e','f'],['g','h','i']])


print(a)

array([['a', 'b', 'c'],
       ['d', 'e', 'f'],
       ['g', 'h', 'i']], dtype='<U1')

print(a.T)

array([['a', 'd', 'g'],
       ['b', 'e', 'h'],
       ['c', 'f', 'i']], dtype='<U1')

推荐阅读