transposeの使い方(numpy)



Python
Pythonとは
基本的な使い方
IDLE
Jupyter Notebook
Markdown
コマンドプロンプトで実行
仮想環境の構築
仮想環境でIDLEを実行
ライブラリのインストール
pipの使い方
numpy , matplotlib等
graphviz
pytorch
Mecab
Pythonの関数:一覧
共通関数
append , extend
class
copy
csv.reader
csv.writer
def , return
dict , defaultdict
enumerate
exit
for
if
import
in
input
lambda
len
list
min/max
OrderedDict
open/close
os
pickle
print
range
re.split
read/readline
round/floor/ceil
split
sys.argv
time
while
write
zip
・特殊メソッド
 ・__name__
 ・__iter__ , __next__
正規表現、メタ文字
データの型の種類
四則演算 (+ , - , * , /)
コメントアウト (# , ''')
numpy
append
arange
argmax/argmin
array
asfarray
astype , dtype
digitize
dot
hstack/vstack
linalg.solve
linspace
mean
meshgrid
mgrid
ndim
ndmin
pad
poly1d
polyfit
prod
random
reshape
savetxt/loadtxt
shape
std
transpose
where
zeros/zeros_like
scipy
expit
imread
interpolate
matplotlib
imshow
figure
pcolormesh
plot
scatter
scikit-learn
GaussianNB
KMeans
KNeighborsClassifier
SVC
tree
keras
chainer
chainerrl
pytorch
BCELoss , MSELoss
Embedding
device
Sequential
Dataset, Dataloader
RNN, LSTM
OpenAI gym
Blackjack-v0
CartPole-v0
tkinter
frame, grid
画像表示
画像を切り取り表示
画像を保存
目的別
ステップ関数
1 of K 符号化法
線形補間
配列に番号をつける

公開日:2018/12/11          

■説明
配列を転置します。

■具体例
以下は各具体例で共通に記載する必要があります。

>> import numpy as np

例①

>> a=np.array([[2,3,4],[5,6,7]])
>> np.transpose(a)

  array([[2, 5],
     [3, 6],
     [4, 7]])


例②
転置は"T"という記載でも表現可能です。

>> a.T

  array([[2, 5],
     [3, 6],
     [4, 7]])


例③
1行の行列は以下の様にしても転置されません。

>> np.array([4,5,6]).T

  array([4, 5, 6])


この場合以下の様にします。カッコの数に注目です。

>> np.array([[4,5,6]]).T

  array([[4],
     [5],
     [6]])


または、以下の様にします。ndminとは配列の次元を定義するもので、1列の行列は2次元にする必要があります。

>> np.array([4,5,6],ndmin=2).T

 array([[4],
    [5],
    [6]])


例④ 3次元の場合

>> a = np.arange(24).reshape(2,3,4)
>> a

  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]]])

>> a.transpose(0, 2, 1)

  array([[[ 0, 4, 8],
      [ 1, 5, 9],
      [ 2, 6, 10],
      [ 3, 7, 11]],

      [[12, 16, 20],
      [13, 17, 21],
      [14, 18, 22],
      [15, 19, 23]]])


上記が意味するところは以下となります。


違う例だと以下となります。

>> a.transpose(1, 0, 2)

  array([[[ 0, 1, 2, 3],
      [12, 13, 14, 15]],

      [[ 4, 5, 6, 7],
      [16, 17, 18, 19]],

      [[ 8, 9, 10, 11],
      [20, 21, 22, 23]]])










サブチャンネルあります。⇒ 何かのお役に立てればと

関連記事一覧



Python
Pythonとは
基本的な使い方
IDLE
Jupyter Notebook
Markdown
コマンドプロンプトで実行
仮想環境の構築
仮想環境でIDLEを実行
ライブラリのインストール
pipの使い方
numpy , matplotlib等
graphviz
pytorch
Mecab
Pythonの関数:一覧
共通関数
append , extend
class
copy
csv.reader
csv.writer
def , return
dict , defaultdict
enumerate
exit
for
if
import
in
input
lambda
len
list
min/max
OrderedDict
open/close
os
pickle
print
range
re.split
read/readline
round/floor/ceil
split
sys.argv
time
while
write
zip
・特殊メソッド
 ・__name__
 ・__iter__ , __next__
正規表現、メタ文字
データの型の種類
四則演算 (+ , - , * , /)
コメントアウト (# , ''')
numpy
append
arange
argmax/argmin
array
asfarray
astype , dtype
digitize
dot
hstack/vstack
linalg.solve
linspace
mean
meshgrid
mgrid
ndim
ndmin
pad
poly1d
polyfit
prod
random
reshape
savetxt/loadtxt
shape
std
transpose
where
zeros/zeros_like
scipy
expit
imread
interpolate
matplotlib
imshow
figure
pcolormesh
plot
scatter
scikit-learn
GaussianNB
KMeans
KNeighborsClassifier
SVC
tree
keras
chainer
chainerrl
pytorch
BCELoss , MSELoss
Embedding
device
Sequential
Dataset, Dataloader
RNN, LSTM
OpenAI gym
Blackjack-v0
CartPole-v0
tkinter
frame, grid
画像表示
画像を切り取り表示
画像を保存
目的別
ステップ関数
1 of K 符号化法
線形補間
配列に番号をつける