Python覚え書き >

numpy入門

行列の作成

0〜の連番を値に持つ配列の作成

import numpy as np

x = np.arange(10)
print(x)

結果

[0 1 2 3 4 5 6 7 8 9]

リストからベクトル(1次元配列)を作成する

import numpy as np

x = np.array([1, 2, 3]) 
print(x)

結果

[1 2 3]

要素が0の行列の作成

import numpy as np

x = np.zeros(10)
print(x)

結果

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

ランダム値の行列の作成

import numpy as np

# サイズ=10の乱数配列を作成(値は 0〜1 の間の実数)
x1 = np.random.rand(10)
print(x1)

# 2〜5の範囲の乱数を 10*10 配列で作成
x2 = np.random.randint(2, 5, (10, 10))
print(x2)

結果

[0.16561915 0.09556958 0.46954353 0.90722789 0.27012909 0.69858166
 0.31817772 0.27222095 0.36353651 0.84831603]
[[2 3 3 4 2 4 4 4 4 3]
 [4 4 4 3 3 4 2 4 3 3]
 [2 2 4 3 2 4 2 2 4 3]
 [2 4 3 3 4 3 4 3 2 2]
 [2 4 2 3 3 4 4 4 3 3]
 [4 4 3 4 2 2 3 2 4 4]
 [3 3 4 4 3 4 4 2 4 4]
 [4 3 3 2 4 3 3 3 4 3]
 [3 3 3 2 2 2 3 4 3 4]
 [3 3 2 4 2 4 3 4 4 3]]

他にも 正規分布、ベータ分布などいろいろある。
https://docs.scipy.org/doc/numpy/reference/routines.random.html

要素の参照

import numpy as np

x = np.array([[1,2,3],[4,5,6]])
print(x[1,1])

結果

5

行列のスライシング

import numpy as np

x = np.array([1,2,3,4,5,6,7,8,9,10])
print(x[:5])
print(x[1:3])
print(x[0:5:2])  # 0〜4まで2おきに

結果

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

要素の書き換え

import numpy as np

x = np.array([[1,2,3],[4,5,6]])
print(x)

x[1,1] = 10
print(x)

結果

[[1 2 3]
 [4 5 6]]
[[ 1  2  3]
 [ 4 10  6]]

行列のサイズ変更

numpy.reshape で行列のサイズ変更を行う事ができる。
https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html

import numpy as np

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

# 1次元配列を2*5の行列に変更
y = x.reshape(2,5)
print(y)

結果

[ 1  2  3  4  5  6  7  8  9 10]
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]]

行列の追加

numpy.append で行列の末尾に要素を追加する事ができる
https://docs.scipy.org/doc/numpy/reference/generated/numpy.append.html

import numpy as np

x1 = np.array([1,2,3])
x2 = np.append(x1, [4,5,6])
print(x1)
print(x2)

y1 = np.array([[1,2,3],[4,5,6]])
y2 = np.append(y1, [[1,1,1],[2,2,2]])
y3 = np.append(y1, [[1,1,1],[2,2,2]], axis=0)
y4 = np.append(y1, [[1,1,1],[2,2,2]], axis=1)
print(y1)
print(y2)
print(y3)
print(y4)

結果

[1 2 3]
[1 2 3 4 5 6]
[[1 2 3]
 [4 5 6]]
[1 2 3 4 5 6 1 1 1 2 2 2]
[[1 2 3]
 [4 5 6]
 [1 1 1]
 [2 2 2]]
[[1 2 3 1 1 1]
 [4 5 6 2 2 2]]

numpy.insert を使用すると指定した位置に要素を挿入する事ができる。
https://docs.scipy.org/doc/numpy/reference/generated/numpy.insert.html

import numpy as np

x1 = np.array([1,2,3])
x2 = np.insert(x1, 1, [4,5,6])
print(x1)
print(x2)

y1 = np.array([[1,2,3],[4,5,6]])
y2 = np.insert(y1, 1, [9,9,9])
y3 = np.insert(y1, 1, [9,9,9], axis=0)
y4 = np.insert(y1, 1, [9,9], axis=1)
print(y1)
print(y2)
print(y3)
print(y4)

結果

[1 2 3]
[1 4 5 6 2 3]
[[1 2 3]
 [4 5 6]]
[1 9 9 9 2 3 4 5 6]
[[1 2 3]
 [9 9 9]
 [4 5 6]]
[[1 9 2 3]
 [4 9 5 6]]

行列の結合

hstack

ベクトル(1次元配列)の結合

import numpy as np

x1 = np.array([1,2,3])
x2 = np.array([4,5,6])
x3 = np.hstack((x1, x2))
print(x1)
print(x2)
print(x3)

結果

[1 2 3]
[4 5 6]
[1 2 3 4 5 6]

行列(2次元配列)の結合

import numpy as np

x1 = np.array([[1,2,3],[4,5,6]])
x2 = np.array([[1,1,1],[2,2,2]])
x3 = np.hstack((x1, x2))
print(x1)
print(x2)
print(x3)

結果

[[1 2 3]
 [4 5 6]]
[[1 1 1]
 [2 2 2]]
[[1 2 3 1 1 1]
 [4 5 6 2 2 2]]

vstack

ベクトル(1次元配列)の結合

import numpy as np

x1 = np.array([1,2,3])
x2 = np.array([4,5,6])
x3 = np.vstack((x1, x2))
print(x1)
print(x2)
print(x3)

結果

[1 2 3]
[4 5 6]
[[1 2 3]
 [4 5 6]]

行列(2次元配列)の結合

import numpy as np

x1 = np.array([[1,2,3],[4,5,6]])
x2 = np.array([[1,1,1],[2,2,2]])
x3 = np.vstack((x1, x2))
print(x1)
print(x2)
print(x3)

結果

[[1 2 3]
 [4 5 6]]
[[1 1 1]
 [2 2 2]]
[[1 2 3]
 [4 5 6]
 [1 1 1]
 [2 2 2]]

行列のファイルへの保存と読み出し

import numpy as np

i = np.array([[1,2,3],[4,5,6]])

# 保存
np.save('data1.npy', i)

# 読み出し
o = np.load('data1.npy')

print(i)
print(o)

結果

[[1 2 3]
 [4 5 6]]
[[1 2 3]
 [4 5 6]]

行列の演算

行列同士の演算

import numpy as np

a = np.array([1,2,3])
b = np.array([4,5,6])

print(a)
print(b)

print('加算')
print(a + b)

print('乗算')
print(a * b)

結果

[1 2 3]
[4 5 6]
加算
[5 7 9]
乗算
[ 4 10 18]

行列とスカラーの演算

import numpy as np

a = np.array([1,2,3])

print(a)

print('加算(+5)')
print(a + 5)

print('乗算(*5)')
print(a * 5)

結果

[1 2 3]
加算(+5)
[6 7 8]
乗算(*5)
[ 5 10 15]

行列積の計算

行列積は ndarray.dot を使用して計算する

import numpy as np

x1 = np.array([[1,2,3],[4,5,6]])
x2 = np.array([[1,2],[3,4],[5,6]])

print(x1.dot(x2))

結果

[[22 28]
 [49 64]]

一応、数学の復習を兼ねて確認

(123)
456
*
(12)
34
56
=
(1*1 + 2*3 + 3*51*2 + 2*4 + 3*6)
4*1 + 5*3 + 6*54*2 + 5*4 + 6*6
=
(2228)
4964

合ってそう。


トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS