はじめに
半年間かけてAIの勉強をします。生成AIに勉強すべき要素を教えてもらい勉強していきます。Pythonを使用していきます。
numpy
Pythonで大規模な多次元配列や行列の計算を高速かつ効率的に行うための、数値計算用オープンソースライブラリ。専用の配列(ndarray)によって高速演算ができる。
numpy機能(基礎)
import numpy as np
# 配列の生成(numpy配列)
a = np.array([1, 2, 3, 4, 5])
b = np.array([[1, 2, 3], [4, 5, 6]])
print(f"1D: shape={a.shape}, dtype={a.dtype}")# shapeは次元数(縦,横)、dtypeは型
print(f"2D: shape={b.shape}, dtype={b.dtype}")
# 特殊な配列
print(f"zeros:\n{np.zeros((3, 3))}")# 全部0の配列(3*3)
print(f"eye:\n{np.eye(3)}")# 単位行列(3*3)
print(f"arange: {np.arange(0, 10, 2)}")# 0~10未満で2刻み
print(f"linspace: {np.linspace(0, 1, 5)}")# 0~1を等間隔で5点
# インデキシングとスライシング
x = np.arange(12).reshape(3, 4)
print(f"\n元の配列:\n{x}")
print(f"x[1, 2] = {x[1, 2]}")# 1列目2行目(縦,横)
print(f"x[:, 1] = {x[:, 1]}")# 1列目すべて
print(f"x[0:2, 1:3] =\n{x[0:2, 1:3]}")# 0~1行目,1~2列目
# ブールインデキシング
print(f"x > 5: {x[x > 5]}")# x>5の要素がtrue,以外がfalseの配列作ってtrueのみ抽出1列で作る
# reshape, flatten, transpose
print(f"reshape(2,6):\n{x.reshape(2, 6)}")# 2行6列に整形
print(f"flatten: {x.flatten()}")# 1次元に展開
print(f"transpose:\n{x.T}")# 転置
1D: shape=(5,), dtype=int64
2D: shape=(2, 3), dtype=int64
zeros:
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
eye:
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
arange: [0 2 4 6 8]
linspace: [0. 0.25 0.5 0.75 1. ]
元の配列:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
x[1, 2] = 6
x[:, 1] = [1 5 9]
x[0:2, 1:3] =
[[1 2]
[5 6]]
x > 5: [ 6 7 8 9 10 11]
reshape(2,6):
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]]
flatten: [ 0 1 2 3 4 5 6 7 8 9 10 11]
transpose:
[[ 0 4 8]
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]
numpyの機能を試してみました。numpy配列の生成、確認。numpy特殊配列の生成。任意要素の抽出、配列の変換を行える。
import numpy as np
# ブロードキャスト
A = np.array([[1, 2, 3], [4, 5, 6]]) # (2, 3)
b = np.array([10, 20,30]) # (3,)
print(f"A + b (broadcast):\n{A + b}")# [A[0]*B,A[1]*B] 末尾の次元が同じかどちらも1であれば計算できる
# スカラーとのブロードキャスト
print(f"A * 2:\n{A * 2}")# 全要素*2
# ユニバーサル関数
x = np.array([0, np.pi/4, np.pi/2, np.pi])
print(f"sin: {np.sin(x)}")# 三角関数
print(f"cos: {np.cos(x)}")
print(f"tan: {np.tan(x)}")
print(f"exp: {np.exp([0, 1, 2])}")# 指数関数
print(f"log: {np.log([1, np.e, np.e**2])}")# 対数関数
# axis操作
M = np.array([[1, 2, 3], [4, 5, 6]])
print(f"\nM:\n{M}")
print(f"sum(axis=0) 列方向: {M.sum(axis=0)}")# 行方向に足す
print(f"sum(axis=1) 行方向: {M.sum(axis=1)}")# 列方向にすべて足す
print(f"mean(axis=0): {M.mean(axis=0)}")# 行方向の平均値
print(f"mean(axis=1): {M.mean(axis=1)}")
print(f"max(axis=0): {M.max(axis=0)}")
print(f"max(axis=1): {M.max(axis=1)}")# 列方向の最大値
print(f"argmax(axis=0): {M.argmax(axis=0)}")# 最大値のインデックスを返す。列ごとにどの行が最大値を持っているかを示す。
print(f"argmax(axis=1): {M.argmax(axis=1)}")
# 高度なブロードキャスト: 外積的な計算
#a = np.array([1, 2, 3])[:, np.newaxis] # (3,)を(3, 1)に変換
a = np.array([1, 2, 3]) # shape: (3,)
a = a[:, np.newaxis] # shape: (3, 1)
b = np.array([10, 20]) # (2,)
print(f"\n{a}")
print(f"\n外積的計算 a*b:\n{a * b}")# aの各要素とbの各要素の積を計算する。aは(3,1)でbは(2,)なので、ブロードキャストによりaの各行がbと掛け合わされる。結果は(3,2)の行列になる。
# where
x = np.array([-2, -1, 0, 1, 2])
print(f"np.where(x>0, x, 0): {np.where(x > 0, x, 0)}")# np.where(条件, 真の値, 偽の値),x>0の要素にあうものはそのまま合わないものはFalse
A + b (broadcast):
[[11 22 33]
[14 25 36]]
A * 2:
[[ 2 4 6]
[ 8 10 12]]
sin: [0.00000000e+00 7.07106781e-01 1.00000000e+00 1.22464680e-16]
cos: [ 1.00000000e+00 7.07106781e-01 6.12323400e-17 -1.00000000e+00]
tan: [ 0.00000000e+00 1.00000000e+00 1.63312394e+16 -1.22464680e-16]
exp: [1. 2.71828183 7.3890561 ]
log: [0. 1. 2.]
M:
[[1 2 3]
[4 5 6]]
sum(axis=0) 列方向: [5 7 9]
sum(axis=1) 行方向: [ 6 15]
mean(axis=0): [2.5 3.5 4.5]
mean(axis=1): [2. 5.]
max(axis=0): [4 5 6]
max(axis=1): [3 6]
argmax(axis=0): [1 1 1]
argmax(axis=1): [2 2]
[[1]
[2]
[3]]
外積的計算 a*b:
[[10 20]
[20 40]
[30 60]]
np.where(x>0, x, 0): [0 0 0 1 2]
関数、行列操作を行う。