numpy タスク03

配列演算

形を変える

  • numpy.ndarray.shape は配列の次元を表し、長さが次元数、つまり ndim 属性 (ランク) であるタプルを返します。
import numpy as np

x = np.array([1,2,9,4,5,6,7,8])
print(x.shape)
x.shape = [2,4]
print(x)

(8,)
[[1 2 9 4]
[5 6 7 8]]

  • numpy.ndarray. flat は配列を 1 次元イテレータに変換し、for を使用して配列の各要素にアクセスできます。
import numpy as np
x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = x.flat

for i in y:
    print(i,end=',')

11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,

  • numpy.ndarray. flatten ([order='C']) 配列のコピーを 1 次元配列に変換して返します。
    [順序: 'C' - 行順、'F' - 列順、'A' - 元の順序、'k' - 要素がメモリ内に現れる順序。
import numpy as np
x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = x.flatten()
print(y)

[11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
]

  • numpy.ravel(a, order='C') は配列を 1 次元配列に変換します。上記とは異なり、このメソッドは配列のビューを返します。
  • reshape() 関数のパラメータ newshape = -1 の場合、配列を 1 次元に縮小することを意味します

配列の転置

  • numpy.transpose(a,axes=None)
  • numpy.ndarray.T
import numpy as np
x = np.random.rand(5,5) * 10
x = np.around(x ,2)
print(x)
print("++++++++++++++")
y = x.T
print(y)
print("++++++++++++++")
z = np.transpose(x)
print(z)

[[1.62 1.64 2.18 2.61 3.45]
[3.42 9.84 7.68 4.1 7.24]
[3.95 1.1 0.8 2.89 0.19]
[0.15 0.91 5.34 9.95 8.62]
[7.48 6.67 0.38 2.31 0.33]]
++++++++++++ +
[[1.62 3.42 3.95 0.15 7.48]
[1.64 9.84 1.1 0.91 6.67]
[2.18 7.68 0.8 5.34 0.38]
[2.61 4.1 2.89 9.95 2.31]
[3.45 7.24 0.19 8.6 2 0.33]]
++++++++++++ ++
[[1.62 3.42 3.95 0.15 7.48]
[1.64 9.84 1.1 0.91 6.67]
[2.18 7.68 0.8 5.34 0.38]
[2.61 4.1 2.89 9.95 2.31]
[3.45 7.24 0.19 8. 62 0.33]]

寸法を変更する

  • 配列を作成した後、行列計算でよく使用される次元を追加することもできます。
    [numpy.newaxis = なし なし]
import numpy as np
x = np.array([1,2,9,4,5,6,7,8])
print(x.shape)
print(x)
print("++++++++++++")
y = x[:,np.newaxis]
print(y.shape)
print(y)

(8,)
[1 2 9 4 5 6 7 8]
++++++++++++
(8, 1)
[[1]
[2]
[9]
[4]
[5]
[6]
[7]
[8]]

  • numpy.squeeze(a, axis = None) は、配列の形状から 1 次元のエントリを削除します。つまり、形状内の 1 の次元を削除します [ a は入力が配列であることを示します。axis は次元の指定に使用され
    ます削除する必要がありますが、指定された次元は単一次元である必要があります。そうでない場合はエラーが報告されます]
import numpy as np 
x = np.arange(10)
print(x.shape)
x = x[np.newaxis,:]
print(x.shape)
y = np.squeeze(x)
print(y.shape)

(10,)
(1,10)
(10,)

配列の組み合わせ

  • 既存の軸に沿って配列シーケンスを連結します (元の x、y はすべて 1 次元であり、結合された結果も 1 次元です)。
import numpy as np
x = np.array([1,2,3])
y = np.array([7,8,9])
z = np.concatenate([x,y])
print(z)

[1 2 3 7 8 9]

  • x と y は両方とも 2 次元であり、結合後の結果も 2 次元であることがわかります。
import numpy as np
x = np.array([1,2,3]).reshape(1,3)
y = np.array([7,8,9]).reshape(1,3)
z = np.concatenate([x,y])
print(z)
print('+++++++++++++++')
z = np.concatenate([x, y], axis=1)
print(z)

[[1 2 3]
[7 8 9]]
++++++++++++++
[[1 2 3 7 8 9]]

  • 新しい軸に沿って一連の配列 (スタック) を結合します。
import numpy as np

x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[7, 8, 9], [10, 11, 12]])
z = np.stack([x, y])
print(z.shape)
print(z)

(2、2、3)
[[[ 1 2 3]
[ 4 5 6]]

[[ 7 8 9]
[10 11 12]]]

配列の分割

  • numpy.split(ary, indices_orsections,sxis=0) 配列を分割します
import numpy as np
x = np.array([[11, 12, 13, 14],
              [16, 17, 18, 19],
              [21, 22, 23, 24]])
y = np.split(x,[1,3])
print(y)

[配列([[11, 12, 13, 14]])、配列([[16, 17, 18, 19], [21, 22, 23, 24]]), 配列([], 形状=(
0 、4)、dtype=int32)]

  • 垂直分割とは、配列を高さに応じて分割することです。
import numpy as np

x = np.array([[11, 12, 13, 14],
              [16, 17, 18, 19],
              [21, 22, 23, 24]])
y = np.vsplit(x, 3)
print(y)

[配列([[11, 12, 13, 14]]), 配列([[16, 17, 18, 19]]), 配列([[21, 22, 23, 24]])]

  • 水平スライスとは、配列を幅に従ってスライスすることです。
import numpy as np

x = np.array([[11, 12, 13, 14],
              [16, 17, 18, 19],
              [21, 22, 23, 24]])
y = np.hsplit(x, 2)
print(y)

[配列([[11, 12],
[16, 17],
[21, 22]]), 配列([[13, 14],
[18, 19],
[23, 24]])]

配列タイリング

  • numpy.tile(A, reps) は、元の行列を水平方向と垂直方向にコピーします。
import numpy as np

x = np.array([[1, 2], [3, 4]])
print(x)
print("++++++++++")
y = np.tile(x,(1,3))
print(y)

[[1 2]
[3 4]]
++++++++++
[[1 2 1 2 1 2]
[3 4 3 4 3 4]]

  • numpy.repeat(a, 繰り返し, axis=None) [axis=0、y 軸に沿ってコピー、行数を増やす; axis=1、x 軸に沿ってコピー、列数を増やす、繰り返し、可能数値にすることもできますが、行列にすることもできます。axis=None の場合、現在の行列が平坦化され、実際には行ベクトルになります]
import numpy as np

x = np.array([[1, 2], [3, 4]])
y = np.repeat(x, 2, axis=1)
print(y)

[[1 1 2 2]
[3 3 4 4]]

要素の追加と削除

  • numpy.unique(ar、return_index=False、return_inverse=False、return_counts=False、axis=None)

おすすめ

転載: blog.csdn.net/BigCabbageFy/article/details/109279502