3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

簡単に試せるNumpyでの線形計算コード➀(初級編)

3
Posted at

Numpyとは?

数値計算を行うための関数を提供するパッケージです。機械学習やディープラーニングの処理にはベクトル(配列)や行列の計算が頻繁に必要になります。

実際に計算をやってみる

・Numpyのインポート

Numpyで計算を行うにあたり、まずNumpyライブラリをインポートします。
(※Numpyがインストール済みと仮定し、筆者はJupyterNotebookで実装しています)

import numpy as np

・まず1(行)×3(列)の行列を出力してみる

入力

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

出力

a = [1 2 3]
b = [4 5 6]

・足し算してみると

入力

c = a + b
print('c =',c)

出力

c = [5 7 9]

となり、aとbが足し算されていることが確認できます。

・3(行)×3(列)の行列を足し算する

入力

x = np.array([[1,2,3],[4,5,6],[7,8,9]])
y = np.array([[7,8,9],[1,2,3],[4,5,6]])
z = x + y

print('x =', x)
print('y =', y)
print('z =', z)

出力

x = [[1 2 3]
 [4 5 6]
 [7 8 9]]
y = [[7 8 9]
 [1 2 3]
 [4 5 6]]
z = [[ 8 10 12]
 [ 5  7  9]
 [11 13 15]]

下記のような行列が出力されていることを確認しました。

$$
x =
\begin{pmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \
7 & 8 & 9
\end{pmatrix}
y =
\begin{pmatrix}
7 & 8 & 9 \\
1 & 2 & 3 \
4 & 5 & 6
\end{pmatrix}
z=
\begin{pmatrix}
8 & 10 & 12 \\
5 & 7 & 9 \
11 & 13 & 15
\end{pmatrix}
$$

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?