数组的转置和换轴

转置是一种特殊的

数据重组形式,可以返回底层数据的视图而不需要复制任何内容。数组拥有transpose方法,也有特殊的 T 属性:

import numpy as np
arr = np.arange(15).reshape((3,5))
print(arr)
print('------------|')
arr=arr.T #把arr数组给转置了,行列互换
print(arr)
#计算矩阵内积会使用 np.dot

arr1 = np.random.randn(6,3)
print(arr1)
print('------------------')
arr2 =np.dot(arr1.T, arr1
print(arr2)
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
------------|
[[ 0  5 10]
 [ 1  6 11]
 [ 2  7 12]
 [ 3  8 13]
 [ 4  9 14]]
[[-0.10721531 -0.9623694   2.40925637]
 [ 1.37846812 -1.08492064  1.52178665]
 [ 2.27103816  0.05325764  0.06673496]
 [-0.07155395  0.21708365 -0.7271715 ]
 [-1.54946414 -0.10253511  0.54530747]
 [-0.61156295 -1.6659128  -0.30243878]]
------------------
[[ 9.84925209 -0.10924581  1.38304084]
 [-0.10924581  4.93894824 -3.67599176]
 [ 1.38304084 -3.67599176  9.04241226]]

关于数组的换轴,在下一篇博客有详解。

原文地址:https://www.cnblogs.com/chanyuli/p/11762416.html