Numpy入門編の続きです。
Numpyは、Pythonにおける数値計算処理の土台になっているライブラリで、続くPandasやAIなどへの基礎にもなっているのでぜひともモノにしておきたいところです。
仕事でのデータ処理を行う上でも基本として身につけておきたいですね。
今回も相変わらずNumpyの公式ページの入門編をなぞっていきます。
配列の基本操作
a = np.array(
[1, 2]
)
b = np.ones(2, dtype='int32')
print('a = ', a)
print('b = ', b)
print('a + b = ', a + b)
print('a - b = ', a - b)
print('a * b = ', a * b)
print('a / a = ', a / a)
print('a / b = ', a / b)
c = np.array(
[
[1, 2],
[3, 4]
]
)
d = np.array(
[
[5, 6],
[7, 8]
]
)
print('c + d = ', c + d)
print('c - d = ', c - d)
print('c * d = ', c * d)
e = np.array(
[1, 2, 3, 4]
)
print('e.sum() = ', e.sum())
f = np.array(
[
[1, 1],
[2, 2]
]
)
print('f.sum(axis=0) = ', f.sum(axis=0))
print('f.sum(axis=1) = ', f.sum(axis=1))
g = f * 2
print(g)
合計値・最大値・最小値
a = np.array(
[
[0.1234, 0.2345, 0.3456, 0.4567],
[0.2345, 0.3456, 0.4567, 0.5678],
[0.3456, 0.4567, 0.5678, 0.6789]
]
)
print('a.sum() = ', a.sum())
print('a.min() = ', a.min())
print('a.max() = ', a.max())
print('a.max(axis=0) = ', a.max(axis=0))
print('a.max(axis=1) = ', a.max(axis=1))
行列
a = np.array(
[
[1, 2],
[3, 4]
]
)
print('a = ', a)
print('a[0, 0] = ', a[0, 0])
print('a[0, 1] = ', a[0, 1])
print('a[1, 0] = ', a[1, 0])
print('a[1, 1] = ', a[1, 1])
print('a[1:3] = ', a[1:3])
print('a[0:] = ', a[0:])
print('a[0] = ', a[0])
print('a[:, 0] = ', a[:, 0])
print('a.max() = ', a.max())
print('a.min() = ', a.min())
print('a.sum() = ', a.sum())
print('a.max(axis=0) = ', a.max(axis=0))
print('a.max(axis=1) = ', a.max(axis=1))
b = np.ones([2, 2])
print('b = ', b)
print('a + b = ', a + b)
print('a * b = ', a * b)
print('np.matmul(a, b) = ', np.matmul(a, b))
print('np.dot(a, b) = ', np.dot(a, b))
c = np.matrix(
[
[1, 2],
[3, 4]
]
)
d = np.matrix(
[
[1, 1],
[1, 1]
]
)
print('c = ', c)
print('d = ', d)
print('c * d = ', c * d)
乱数
a = np.random.default_rng(0)
print('a = ', a)
b = a.integers(5, size=(2, 4))
print('b = ', b)
c = a.integers(10, size=(2, 4))
print('c = ', c)
配列からユニークな値を取り出す
a = np.array(
[11, 11, 12, 13, 14, 15, 16, 17, 12, 13, 11, 14, 18, 19, 20]
)
b = np.unique(a)
print('b = ', b)
c, d = np.unique(a, return_index=True)
print('c = ', c)
print('d = ', d)
print('a[d] = ', a[d])
e, f = np.unique(a, return_counts=True)
print('f = ', f)
g = np.array(
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[1, 2, 3, 4],
[1, 2, 3, 5],
]
)
print('g = ', g)
print('np.unique(g) = ', np.unique(g))
print('np.unique(g, axis=0) = ', np.unique(g, axis=0))
配列の組み換え
a = np.array(
[1, 2, 3, 4, 5, 6]
)
print('a = ', a)
b = a.reshape(2, 3)
print('b = ', b)
c = a.reshape(3, 2)
print('c = ', c)
print('b.T = ', b.T)
要素を逆順に並べ替える
a = np.array(
[1, 2, 3, 4, 5, 6, 7, 8]
)
b = np.flip(a)
print('b = ', b)
c = np.array(
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]
)
d = np.flip(c)
print('d = ', d)
e = np.flip(d, axis=0)
print('e = ', e)
f = np.flip(d, axis=1)
print('f = ', f)
多重配列から1次元配列への変換
a = np.array(
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]
)
print('a.flatten() = ', a.flatten())
b = np.array(
[
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]
],
[
[13, 14, 15],
[16, 17, 18],
[19, 20, 21],
[22, 23, 24],
]
]
)
print('b.flatten() = ', b.flatten())
今回はここまで。
Numpyについて、もっと詳しく知りたいなら公式ページへGoです。
次回は、Pandasの使い方を見ていきます。
コメント