import numpy as np x = np.arange(10) print(x)
結果
[0 1 2 3 4 5 6 7 8 9]
import numpy as np x = np.array([1, 2, 3]) print(x)
結果
[1 2 3]
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]]
ベクトル(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]]
ベクトル(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]]
一応、数学の復習を兼ねて確認
( | 1 | 2 | 3 | ) |
4 | 5 | 6 |
( | 1 | 2 | ) |
3 | 4 | ||
5 | 6 |
( | 1*1 + 2*3 + 3*5 | 1*2 + 2*4 + 3*6 | ) |
4*1 + 5*3 + 6*5 | 4*2 + 5*4 + 6*6 |
( | 22 | 28 | ) |
49 | 64 |
合ってそう。
150 から 180cm のランダムな身長データを整数で生成。
height = np.random.randint(150, 180, 50)
150 から 180cm のランダムな身長データを小数点以下2桁で生成。
height = np.round(np.random.uniform(150, 180, 50), 2)
合計値は sum で求める。
import numpy as np x = np.array([1, 2, 3, 4, 5]) np.sum(x) # 15
平均値 はmeanで求める。
import numpy as np x = np.array([1, 2, 3, 4, 5]) np.mean(x) # 3.0
中央値は medianで求める。
import numpy as np x = np.array([1, 2, 3, 4, 5]) np.median(x) # 3.0
import numpy as np x = np.array([1, 2, 3, 4, 5, 1, 3, 5, 4, 5, 6]) np.argmax(np.bincount(x)) # 5
もしくは
from statistics import mode x = np.array([1, 2, 3, 4, 5, 1, 3, 5, 4, 5, 6]) mode(x) # 5
分散はvarで求める。
import numpy as np x = np.array([1, 2, 3, 4, 5]) np.var(x) # 2.0 # 確認 np.sum((x - x.mean()) ** 2) / np.size(x) # 2.0
標準偏差はstdで求める。
import numpy as np x = np.array([1, 2, 3, 4, 5]) np.std(x) # 1.41421356237 # 確認 np.sqrt(np.sum((x - x.mean()) ** 2) / np.size(x)) # 1.4142135623730951
import numpy as np data = np.array([2,69,67,75,84,92,80,77,65,78,84,95,71]) # 四分位点を求める q25, q75 = np.percentile(data, [25 ,75]) # 四分位範囲 iqr = q75 - q25 print(q25, q75, iqr )
import numpy as np np.eye(3)
結果
array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
AB=I, BA=I を満たす行列B(Aの逆行列)を求める。
a = np.array([[2,3],[4,5]]) b = np.linalg.inv(a) print("a: ", a) print("b: ", b) print("ba: ", np.dot(b, a)) print("ab: ", np.dot(a, b))
結果
a: [[2 3] [4 5]] b: [[-2.5 1.5] [ 2. -1. ]] ba: [[1. 0.] [0. 1.]] ab: [[1. 0.] [0. 1.]]
x = np.array([[1,2,3],[4,5,6]]) print(x.T)
結果
[[1 4] [2 5] [3 6]]
行列積は np.dot で求める事ができる。
import numpy as np x = np.array([[1,2],[3,4],[5,6]]) y = np.array([[1,2,3],[4,5, 6]]) np.dot(x, y)
結果
array([[ 9, 12, 15], [19, 26, 33], [29, 40, 51]])
h(x) = n + m*y という仮説は行列積を利用して計算する事ができる。
以下の3つの仮説がある場合。
y = [10, 20, 30] というベクトルに対して上記の各仮説の計算を行うには、以下の通り行列積を計算する。
実装例
import numpy as np a = np.array([[1, 10],[1, 20],[1, 30]]) b = np.array([[2, 3, 5],[3, 4, 10]]) np.dot(a, b)
結果
array([[ 32, 43, 105], [ 62, 83, 205], [ 92, 123, 305]])