python 语言中 np.array ([[1,2]]).T 是进行什么运算?尝试了一下,跑不通

我在某课程看到一个用法c = np.array([[1,2]]).T 我想看看.T起什么作用。于是想跑跑这段代码,但是报错。还请高手指教,谢谢!

import numpy as np
b = np.array([1,2,3,0,6],[15,8,9,10,9])
c = np.array([[1,2]]).T
print (b)
print (c)

报错信息如下:
TypeError Traceback (most recent call last)
in
2 #!--coding:UTF-8 --
3 import numpy as np
—-> 4 b = np.array([1,2,3,0,6],[15,8,9,10,9])
5 c = np.array([[1,2]]).T
6 print (b)

TypeError: data type not understood

讨论数量: 1
pardon110

与T无关,T只是返回ndarray类型,与np.array() 参数相关, 换而言之,你第二行代码入参数据类型不对。numpy.array第一个位置参数必须是有序序列,其方法原型如下

array(object: Sequence[Any], dtype: Union[dtype, None, Union[int, float, bool], str, Tuple[Any, int], Tuple[Any, Union[int, Sequence[int]]], List[Union[Tuple[Union[str, Tuple[str, str]], Any], Tuple[Union[str, Tuple[str, str]], Any, Union[int, Sequence[int]]]]], Dict[str, Union[Sequence[str], Sequence[Any], Sequence[int], Sequence[Union[bytes, Unknown, None]], int]], Dict[str, Tuple[Any, int]], Tuple[Any, Any]], copy: bool, subok: bool, ndmin: int) -> ndarray
import numpy as np
b = np.array([[1,2,3,0,6],[15,8,9,10,9]])
c = np.array([[1,2]]).T
print (b)
print (c)

输出

[[ 1  2  3  0  6]
 [15  8  9 10  9]]
[[1]
 [2]]
3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!