前言:
此刻我们对“python 数组转置”大概比较注意,大家都需要剖析一些“python 数组转置”的相关文章。那么小编也在网摘上网罗了一些有关“python 数组转置””的相关知识,希望朋友们能喜欢,朋友们一起来学习一下吧!Python 花式索引 指的是利用整数数组进行索引
In [6]: arr = np.empty((8, 4))
In [7]: arr
Out[7]:
array([[3.60332862e+252, 1.28156830e+213, 1.15824202e+171,
3.60313285e+252],
[6.32384870e-310, 1.09610677e-315, 1.09585729e-315,
1.09610606e-315],
[1.09610487e-315, 1.09645989e-315, 1.09645823e-315,
1.09609301e-315],
[1.09499445e-315, 1.09610179e-315, 1.09645965e-315,
1.09611104e-315],
[1.09645918e-315, 1.04832050e-315, 1.09585159e-315,
1.09586582e-315],
[1.09586155e-315, 1.09502591e-315, 1.09646013e-315,
1.09609373e-315],
[1.04832596e-315, 1.09502425e-315, 1.09610843e-315,
1.09646155e-315],
[1.09585539e-315, 1.09586013e-315, 1.09646416e-315,
1.09645847e-315]])
In [9]: for i in range(8):
...: arr[i] = i
In [10]: arr
Out[10]:
array([[0., 0., 0., 0.],
[1., 1., 1., 1.],
[2., 2., 2., 2.],
[3., 3., 3., 3.],
[4., 4., 4., 4.],
[5., 5., 5., 5.],
[6., 6., 6., 6.],
[7., 7., 7., 7.]])
##为了以特定顺序选取行子集,只需传入一个用于指定顺序的整数列表或ndarray即可
In [11]: arr[[4, 3, 0, 6]]
Out[11]:
array([[4., 4., 4., 4.],
[3., 3., 3., 3.],
[0., 0., 0., 0.],
[6., 6., 6., 6.]])
##使用负数索引将会从末尾开始选取行
In [12]: arr[[-3, -5, -7]]
Out[12]:
array([[5., 5., 5., 5.],
[3., 3., 3., 3.],
[1., 1., 1., 1.]])
In [13]: np.arange(32)
Out[13]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])
In [14]: np.arange(32).reshape((8, 4))
Out[14]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23],
[24, 25, 26, 27],
[28, 29, 30, 31]])
In [15]: arr = np.arange(32).reshape((8, 4))
##可以一次传入多个索引数组,对应的是arr[1][0],arr[5][3],arr[7][1],arr[2][2]
In [16]: arr[[1, 5, 7, 2], [0, 3, 1, 2]]
Out[16]: array([ 4, 23, 29, 10])
##方法1:arr[1][0],arr[1][3],...arr[5][0],arr[5][3],...依次类推
In [18]: arr[[1, 5, 7, 2]][:, [0, 3, 1, 2]]
Out[18]:
array([[ 4, 7, 5, 6],
[20, 23, 21, 22],
[28, 31, 29, 30],
[ 8, 11, 9, 10]])
##方法2:使用np.ix_()函数,它可以将两个一维整数数组转换为一个用于选取方形区域的索引器
In [19]: arr[np.ix_([1, 5, 7, 2], [0, 3, 1, 2])]
Out[19]:
array([[ 4, 7, 5, 6],
[20, 23, 21, 22],
[28, 31, 29, 30],
[ 8, 11, 9, 10]])
##花式索引跟切片不一样,它总是将数据复制到新数组中
数组转置和轴对换
##转置是重塑的一种特殊形式,它返回的是源数据的视图
##数组不仅有transpose方法,还有一个特殊的T属性
In [20]: arr = np.arange(15).reshape((3, 5))
In [21]: arr
Out[21]:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
In [22]: arr.T
Out[22]:
array([[ 0, 5, 10],
[ 1, 6, 11],
[ 2, 7, 12],
[ 3, 8, 13],
[ 4, 9, 14]])
##在进行矩阵计算时,经常需要用到该操作,比如利用np.dot计算矩阵内积XTX:
In [23]: arr = np.random.randn(6, 3)
In [24]: arr
Out[24]:
array([[ 1.43246019, -0.89622631, -0.90893883],
[ 1.5384252 , 2.2980319 , 0.05432292],
[ 1.21427005, -1.64032306, -0.99550839],
[-0.76845128, 0.29905102, -0.57052217],
[ 0.97445692, -0.64012016, -1.43677043],
[ 0.31091144, 1.27995381, 0.38438765]])
In [25]: np.dot(arr.T, arr)
Out[25]:
array([[ 7.52989561, -0.19587687, -3.26940483],
[-0.19587687, 10.91229902, 3.81349498],
[-3.26940483, 3.81349498, 4.35771641]])
##对于高维数组,transpose需要得到一个由轴编号组成的元组才能对这些轴进行转置:
In [26]: arr = np.arange(16).reshape((2, 2, 4))
In [27]: arr
Out[27]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7]],
[[ 8, 9, 10, 11],
[12, 13, 14, 15]]])
In [28]: arr.transpose((1, 0, 2))
Out[28]:
array([[[ 0, 1, 2, 3],
[ 8, 9, 10, 11]],
[[ 4, 5, 6, 7],
[12, 13, 14, 15]]])
##ndarray还有一个swapaxes方法,它需要接受一对轴编号:
In [29]: arr
Out[29]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7]],
[[ 8, 9, 10, 11],
[12, 13, 14, 15]]])
In [30]: arr.swapaxes(1, 2)
Out[30]:
array([[[ 0, 4],
[ 1, 5],
[ 2, 6],
[ 3, 7]],
[[ 8, 12],
[ 9, 13],
[10, 14],
[11, 15]]])
标签: #python 数组转置