日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

numpy T、transpose()函数、swapaxes()函数

發布時間:2023/12/2 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 numpy T、transpose()函数、swapaxes()函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

1? 矩陣轉置T,既線性代數中矩陣轉置

2 ?transpose()函數

3??swapaxes()函數


?

1? 矩陣轉置T,既線性代數中矩陣轉置

示例程序如下:

import numpy as npa = np.arange(0,24,2).reshape(3, 4)print('*****a****') print('a: \n', a) print('a.shape: ', a.shape)print('\n*****a.T****') print('a.T:\n', a.T) print('a.T: ', a.T.shape)

?

運行結果

*****a****
a:?
?[[ 0 ?2 ?4 ?6]
?[ 8 10 12 14]
?[16 18 20 22]]
a.shape: ?(3, 4)

*****a.T****
a.T:
?[[ 0 ?8 16]
?[ 2 10 18]
?[ 4 12 20]
?[ 6 14 22]]
a.T: ?(4, 3)

?

?

2 ?transpose()函數

以維度為(2,3,4)的數組a為例進行說明:

? ? ? a有3個,第0維大小為2,第1維大小為3,第2維大小為4

? ? ? a.transpose(2,1,0)既將數組a第0維大小2與第2維大小4進行替換,返回第0維大小為4,第1維大小為3,第2維大小為2

示例程序如下:

import numpy as npa = np.arange(24).reshape(2, 3, 4)print('*****a****') print('a: \n', a) print('a.shape: ', a.shape)print('\n*****a.transpose(2,1,0)****') print('a.transpose(2,1,0):\n', a.transpose(2,1,0)) print('a.transpose(2,1,0).shape: ', a.transpose(2,1,0).shape)

?

運行結果:

*****a**** a:?[[[ 0 ?1 ?2 ?3][ 4 ?5 ?6 ?7][ 8 ?9 10 11]][[12 13 14 15][16 17 18 19][20 21 22 23]]] a.shape: ?(2, 3, 4)*****a.transpose(2,1,0)**** a.transpose(2,1,0):[[[ 0 12][ 4 16][ 8 20]][[ 1 13][ 5 17][ 9 21]][[ 2 14][ 6 18][10 22]][[ 3 15][ 7 19][11 23]]] a.transpose(2,1,0).shape: ?(4, 3, 2)

3??swapaxes()函數

swapaxes()函數接受的參數是一對軸編號

? ? 以維度為(2,3,4)的數組a為例進行說明:

? ? ? ? ?a有3個,第0維大小為2,第1維大小為3,第2維大小為4

? ? ? ? ?a. swapaxes (2,0)既將數組a第0維大小2與第2維大小4進行替換,返回第0維大小為4,第2維大小為2;第1維大小不變。

import numpy as npa = np.arange(24).reshape(2, 3, 4)print('*****a****') print('a: \n', a) print('a.shape: ', a.shape)print('\n*****a.swapaxes (2,1,0)****') print('a.swapaxes(2,0):\n', a.swapaxes(2,0)) print('a.swapaxes(2,0).shape: ', a.swapaxes(2,0).shape)

?

運行結果:

*****a****
a:?
?[[[ 0 ?1 ?2 ?3]
? [ 4 ?5 ?6 ?7]
? [ 8 ?9 10 11]]

?[[12 13 14 15]
? [16 17 18 19]
? [20 21 22 23]]]
a.shape: ?(2, 3, 4)

*****a.swapaxes (2,1,0)****
a.swapaxes(2,0):
?[[[ 0 12]
? [ 4 16]
? [ 8 20]]

?[[ 1 13]
? [ 5 17]
? [ 9 21]]

?[[ 2 14]
? [ 6 18]
? [10 22]]

?[[ 3 15]
? [ 7 19]
? [11 23]]]
a.swapaxes(2,0).shape: ?(4, 3, 2)

總結

以上是生活随笔為你收集整理的numpy T、transpose()函数、swapaxes()函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。