numpy
    和
    行列積

 機能・要件
 構成・方式
 タスク
 ライブラリ
 導入
 Sample
 Error
 基礎他
 numpy (ナムパイ、ナンパイ)
 ・数値計算を行うためのライブラリ
ベクトル演算用に構築されたクラス
 ・np.ndarray (The N-dimensional array)
データ型
NumPyで使われる主となるPythonのクラス、多次元配列のデータ構造を持つ。
関数 numpy.array() で numpy.ndarray を作成
属性(attributes)
      base ndarray のベースとなるオブジェクト(どのメモリを参照しているのか)
data 配列のデータがどこから始まっているのかを示す。
dtype ndarray に含まれる要素が持つデータ型
flags Memory Layout についての情報
flat ndarray を1次元配列に変換するイテレータ
imag ndarray における虚数部分(imaginary part)
itemsize バイト単位での1つ1つの要素のメモリ消費量
nbytes ndarray の要素で占められるバイト単位のメモリ消費量
ndim ndarray に含まれる次元の数
real ndarray における実数部分(real part)
shape ndarray の形状(shape)をタプルで表す。
size ndarray に含まれる要素の数
strides 各次元方向に、1つ隣の要素に移動するに必要なバイト数をタプルで表示
ctypes ctypes モジュールで扱うためのイテレータ
T 転置を返す。ndim<2の場合は元の配列を返す。
 ・np.random.rand()
一様乱数 (0.0 – 1.0) の間のランダムな数値を出力     >>> np.random.rand(3, 4)     array([[0.42587049, 0.87472503, 0.56487433, 0.87732966],      [0.9585356 , 0.19595432, 0.27378322, 0.8721103 ],      [0.45706672, 0.14351054, 0.36885692, 0.53980196]])  ・np.random.normal()
正規乱数を出力
平均:50、標準偏差が 10 の正規分布の乱数を 3 x 4 の行列で出力     >>> np.random.normal(50, 10, (3, 4))     array([[49.29278399, 58.16619477, 46.0226885 , 25.08953415],      [41.49865668, 30.19008075, 41.54329693, 42.1655832 ],      [45.79807931, 40.48803217, 51.72822686, 40.9007963 ]])  ・np.random.seed(任意の数値)
引数を任意の数値に固定すると、毎回同じ値が生成     >>> np.random.randint(10)     8     >>> np.random.seed(10)     >>> np.random.randint(10)     9     >>> np.random.seed(10)     >>> np.random.randint(10)     9  ・値、最大値、標準偏差、配列の次元数
numpy.sum()、numpy.max()、numpy.std()、、numpy.ndim()
 ・listA = [4,5,6,7,8] とlistB = [1,2,3,4,5]の
  (for文を用いた場合の時間は20倍)     >>> import numpy as np     >>> listA = np.array([4,5,6,7,8])     >>> listB = np.array([1,2,3,4,5])     >>> listC = listA + listB     >>> print(listC)     [ 5 7 9 11 13]  ・リストの各要素に同じ数を足して、新しいリストを作る。     >>> import numpy as np     >>> listA = np.array([4,5,6,7,8])     >>> listC = listA + 5     >>> print(listC)     [ 9 10 11 12 13]  ・2次元配列(行列(matrix))     >>> import numpy as np     >>> B = np.array([[1,2], [3,4], [5,6]])     >>> print(B)     [[1 2]      [3 4]      [5 6]]     >>> np.ndim(B)  # 配列の次元数     2     >>> B.shape  # 配列の形状(shape):インスタンス変数     (3, 2)  # 結果はタプル(tuple)     >>> B.shape[0]     3  ・真偽リストによって配列の要素を参照     >>> import numpy as np     >>> listA = np.array([4,5,6,7,8])     >>> cond = np.array([True,False,True,True,False])     >>> print(listA[cond])     [4 6 7]  ・condの記述には比較演算子を用いる。     >>> import numpy as np     >>> listA = np.array([4,5,6,7,8])     >>> cond = listA > 6     >>> print(listA[cond])     [7 8]  ・condを数字のリストにする。     >>> import numpy as np     >>> listA = np.array([4,5,6,7,8])     >>> cond = [0,4,2]     >>> print(listA[cond])     [4 8 6]  ・1次元の配列を2次元にして行列のようにする。
 ・要素数10個の一次元配列を2行5列の行列に変える。(.reshape(2,5))     >>> import numpy as np     >>> listA = np.array([1,2,3,4,5,6,7,8,9,10])     >>> matrix = listA.reshape(2,5)     >>> print(matrix)     [[ 1 2 3 4 5]      [ 6 7 8 9 10]]  ・行列を縦、横に足して和を求める。     >>> import numpy as np     >>> listA = np.array([1,2,3,4,5,6,7,8,9,10])     >>> matrix = listA.reshape(2,5)     >>> print(matrix.sum(axis=0))  # axis=0を指定で行同士の和     [ 7 9 11 13 15]     >>> import numpy as np     >>> listA = np.array([1,2,3,4,5,6,7,8,9,10])     >>> matrix = listA.reshape(2,5)     >>> print(matrix.sum(axis=1))  # axis=1を指定で列同士の和     [15 40]  ・行列積     >>> import numpy as np     >>> listA = np.array([[1,2,3],[4,5,6]])     >>> listA.shape     (2, 3)     >>> listB = np.array([[1,2],[3,4],[5,6]])     >>> listB.shape     (3, 2)     >>> np.dot(listA, listB)# 被演算子(B, A)の順序が逆だと結果も異なる     array([[22, 28],      [49, 64]])