- 追加された行はこの色です。
- 削除された行はこの色です。
- numpy入門 へ行く。
#mynavi(Python覚え書き);
#setlinebreak(on);
#html(<script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-MML-AM_CHTML"></script>)
* 目次 [#r3ea660f]
#contents
-- 関連
--- [[Python]]
--- [[Python覚え書き]]
--- [[ディープラーニング入門]]
--- [[TensorFlow入門]]
--- [[Keras入門]]
--- [[Chainer入門]]
--- [[PyTorch入門]]
--- [[pandas入門]]
--- [[R言語入門]]
--- [[機械学習の為の数学の基礎]]
--- [[統計学の基礎知識]]
--- [[numpyを使用した重回帰分析]]
* 行列の作成 [#fb894e40]
#html(<div style="padding-left:10px;">)
** 0〜の連番を値に持つ配列の作成 [#jc9af6de]
#html(<div style="padding-left:10px;">)
#mycode2(python){{
import numpy as np
x = np.arange(10)
print(x)
}}
結果
#myterm2(){{
[0 1 2 3 4 5 6 7 8 9]
}}
#html(</div>)
** 指定した範囲の値を指定個数に分割する [#r049311f]
#html(<div style="padding-left:10px;">)
#mycode2(){{
np.linspace(0, 10, 5) # ... array([ 0. , 2.5, 5. , 7.5, 10. ])
np.linspace(0, 100, 5) # ... array([ 0., 25., 50., 75., 100.])
np.linspace(0, 100, 11) # ... array([ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., 100.])
}}
#html(</div>)
** リストからベクトル(1次元配列)を作成する [#r8170824]
#html(<div style="padding-left:10px;">)
#mycode2(python){{
import numpy as np
x = np.array([1, 2, 3])
print(x)
}}
結果
#myterm2(){{
[1 2 3]
}}
#html(</div>)
** 要素が0の行列の作成 [#hf5024a4]
#html(<div style="padding-left:10px;">)
#mycode2(python){{
import numpy as np
x = np.zeros(10)
print(x)
}}
結果
#myterm2(){{
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
}}
#html(</div>)
** 乱数の生成 [#pb3d9cd9]
#html(<div style="padding-left:10px;">)
*** サイズ=10の乱数配列を作成 [#pe11c08d]
#html(<div class="pl10">)
#mycode2(python){{
np.random.seed()
np.random.rand(10) # 値は 0〜1 の間の実数
}}
結果
#myterm2(){{
array([0.66665716, 0.53671533, 0.66012721, 0.9495542 , 0.78441076,
0.26098457, 0.06754334, 0.46535488, 0.48764828, 0.68502518])
}}
#html(</div>)
*** 指定範囲の値を指定個数分生成する [#ab283d38]
#html(<div class="pl10">)
#mycode2(python){{
np.random.seed()
np.random.uniform(-10, 10, 10)
np.random.uniform(-10, 10, (2, 5))
}}
結果
#myterm2(){{
array([ 6.63545572, 4.95265166, 5.72494723, 6.38053746, 5.04741888,
6.10112906, -6.74466612, 5.11187673, -4.07671305, 3.30998199])
array([[-6.6427769 , -1.42335212, 2.81080724, 0.39621999, 3.01289381],
[ 6.49264639, 3.05848916, -7.60294646, 0.42769374, 9.08846494]])
}}
#html(</div>)
*** 指定範囲の値を指定個数分生成する(実数) [#q8dc95dd]
*** 指定範囲の値を指定個数分生成する(整数) [#q8dc95dd]
#html(<div class="pl10">)
#mycode2(python){{
np.random.seed()
np.random.randint(2, 5, 10)
np.random.randint(2, 5, (5, 2))
print(x2)
}}
結果
#myterm2(){{
array([2, 2, 2, 4, 2, 3, 4, 2, 4, 2])
array([[4, 3],
[3, 2],
[2, 3],
[2, 3],
[3, 4]])
}}
#html(</div>)
*** サンプル [#h000bfe2]
#html(<div style="padding-left: 10px;">)
150 から 180cm のランダムな身長データを整数で生成
#mycode2(python){{
height = np.random.randint(150, 180, 50)
}}
150 から 180cm のランダムな身長データを小数点以下2桁で生成。
#mycode2(python){{
height = np.round(np.random.uniform(150, 180, 50), 2)
}}
#html(</div>)
他にも 正規分布、ベータ分布などいろいろある。
https://docs.scipy.org/doc/numpy/reference/routines.random.html
#html(</div>)
#html(</div>)
* 要素の参照 [#ia2554ad]
#html(<div style="padding-left:10px;">)
#mycode2(python){{
import numpy as np
x = np.array([[1,2,3],[4,5,6]])
print(x[1,1])
}}
結果
#myterm2(){{
5
}}
#html(</div>)
* 行列のスライシング [#o10f4553]
#html(<div style="padding-left:10px;">)
#mycode2(python){{
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おきに
}}
結果
#myterm2(){{
[1 2 3 4 5]
[2 3]
[1 3 5]
}}
#html(</div>)
* 要素の書き換え [#qa076abb]
#html(<div style="padding-left:10px;">)
#mycode2(python){{
import numpy as np
x = np.array([[1,2,3],[4,5,6]])
print(x)
x[1,1] = 10
print(x)
}}
結果
#myterm2(){{
[[1 2 3]
[4 5 6]]
[[ 1 2 3]
[ 4 10 6]]
}}
#html(</div>)
* 行列のサイズ変更 [#r96695d9]
#html(<div style="padding-left:10px;">)
numpy.reshape で行列のサイズ変更を行う事ができる。
※ https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html
#mycode2(python){{
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)
}}
結果
#myterm2(){{
[ 1 2 3 4 5 6 7 8 9 10]
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
}}
#html(</div>)
* 行列の追加 [#h320c2aa]
#html(<div style="padding-left:10px;">)
numpy.append で行列の末尾に要素を追加する事ができる
※ https://docs.scipy.org/doc/numpy/reference/generated/numpy.append.html
#mycode2(python){{
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)
}}
結果
#myterm2(){{
[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
#mycode2(python){{
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)
}}
結果
#myterm2(){{
[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]]
}}
#html(</div>)
* 行列の結合 [#a6d1cda9]
#html(<div style="padding-left:10px;">)
** hstack [#rfdce9bc]
#html(<div style="padding-left:10px;">)
ベクトル(1次元配列)の結合
#mycode2(python){{
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)
}}
結果
#myterm2(){{
[1 2 3]
[4 5 6]
[1 2 3 4 5 6]
}}
行列(2次元配列)の結合
#mycode2(python){{
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)
}}
結果
#myterm2(){{
[[1 2 3]
[4 5 6]]
[[1 1 1]
[2 2 2]]
[[1 2 3 1 1 1]
[4 5 6 2 2 2]]
}}
#html(</div>)
** vstack [#ibf332b1]
#html(<div style="padding-left:10px;">)
ベクトル(1次元配列)の結合
#mycode2(python){{
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)
}}
結果
#myterm2(){{
[1 2 3]
[4 5 6]
[[1 2 3]
[4 5 6]]
}}
行列(2次元配列)の結合
#mycode2(python){{
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)
}}
結果
#myterm2(){{
[[1 2 3]
[4 5 6]]
[[1 1 1]
[2 2 2]]
[[1 2 3]
[4 5 6]
[1 1 1]
[2 2 2]]
}}
#html(</div>)
#html(</div>)
* 行列のファイルへの保存と読み出し [#h4ac3fce]
#html(<div style="padding-left:10px;">)
#mycode2(python){{
import numpy as np
i = np.array([[1,2,3],[4,5,6]])
# 保存
np.save('data1.npy', i)
# 読み出し
o = np.load('data1.npy')
# CSVの読み出し
csvdata = np.loadtxt("sample.csv", delimiter=",", skiprows=1)
print(i)
print(o)
}}
結果
#myterm2(){{
[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]
}}
#html(</div>)
* 行列の演算 [#f3649594]
#html(<div style="padding-left:10px;">)
** 行列同士の演算 [#da34d421]
#html(<div style="padding-left:10px;">)
#mycode2(python){{
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)
}}
結果
#myterm2(){{
[1 2 3]
[4 5 6]
加算
[5 7 9]
乗算
[ 4 10 18]
}}
#html(</div>)
** 行列とスカラーの演算 [#k13aa43c]
#html(<div style="padding-left:10px;">)
#mycode2(python){{
import numpy as np
a = np.array([1,2,3])
print(a)
print('加算(+5)')
print(a + 5)
print('乗算(*5)')
print(a * 5)
}}
結果
#myterm2(){{
[1 2 3]
加算(+5)
[6 7 8]
乗算(*5)
[ 5 10 15]
}}
#html(</div>)
** 行列積の計算 [#vd9870ad]
#html(<div style="padding-left:10px;">)
行列積は ndarray.dot を使用して計算する
#mycode2(python){{
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))
}}
結果
#myterm2(){{
[[22 28]
[49 64]]
}}
一応、数学の復習を兼ねて確認
#html(<div>)
#html(<table style="display:inline-block;vertical-align:middle;"><tr><td rowspan="2" style="font-size:1.5rem;vertical-align: middle;">(</td><td>1</td><td>2</td><td>3</td><td rowspan="2" style="font-size:1.5rem;vertical-align: middle;">)</td></tr><tr><td>4</td><td>5</td><td>6</td></tr></table>)
#html(<div style="padding:10px;display: inline-block">*</div>)
#html(<table style="display:inline-block;vertical-align:middle;"><tr><td rowspan="3" style="font-size:2rem;vertical-align: middle;">(</td><td>1</td><td>2</td><td rowspan="3" style="font-size:2rem;vertical-align: middle;">)</td></tr><tr><td>3</td><td>4</td></tr><tr><td>5</td><td>6</td></tr></table>)
#html(<span> = </span>)
#html(<table style="display:inline-block;vertical-align:middle;"><tr><td rowspan="2" style="font-size:1.5rem;vertical-align: middle;">(</td><td>1*1 + 2*3 + 3*5</td><td style="padding-left: 20px;">1*2 + 2*4 + 3*6</td><td rowspan="2" style="font-size:1.5rem;vertical-align: middle;">)</td></tr><tr><td>4*1 + 5*3 + 6*5</td><td style="padding-left: 20px;">4*2 + 5*4 + 6*6</td></tr></table>)
#html(<span> = </span>)
#html(<table style="display:inline-block;vertical-align:middle;"><tr><td rowspan="2" style="font-size:1.5rem;vertical-align: middle;">(</td><td>22</td><td style="padding-left: 20px;">28</td><td rowspan="2" style="font-size:1.5rem;vertical-align: middle;">)</td></tr><tr><td>49</td><td style="padding-left: 20px;">64</td></tr></table>)
合ってそう。
#html(</div>)
#html(</div>)
#html(</div>)
#html(</div>)
* 統計関数など [#o1420a5e]
#html(<div style="padding-left: 10px;">)
** 合計 [#u0c8b9b0]
#html(<div style="padding-left: 10px;">)
合計値は sum で求める。
#mycode2(python){{
import numpy as np
x = np.array([1, 2, 3, 4, 5])
np.sum(x) # 15
}}
#html(</div>)
** 平均値 [#j87931cb]
#html(<div style="padding-left: 10px;">)
平均値 はmeanで求める。
#mycode2(python){{
import numpy as np
x = np.array([1, 2, 3, 4, 5])
np.mean(x) # 3.0
}}
#html(</div>)
//** 加重平均 [#g52e339c]
//#html(<div style="padding-left: 10px;">)
//#TODO
// average
//#html(</div>)
** 中央値 [#efde7689]
#html(<div style="padding-left: 10px;">)
中央値は medianで求める。
#mycode2(python){{
import numpy as np
x = np.array([1, 2, 3, 4, 5])
np.median(x) # 3.0
}}
#html(</div>)
** 最頻値 [#u1e50123]
#html(<div style="padding-left: 10px;">)
#mycode2(python){{
import numpy as np
x = np.array([1, 2, 3, 4, 5, 1, 3, 5, 4, 5, 6])
np.argmax(np.bincount(x)) # 5
}}
もしくは
#mycode2(python){{
from statistics import mode
x = np.array([1, 2, 3, 4, 5, 1, 3, 5, 4, 5, 6])
mode(x) # 5
}}
#html(</div>)
** 分散 [#uf97af68]
#html(<div style="padding-left: 10px;">)
分散はvarで求める。
#mycode2(python){{
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
}}
#html(</div>)
** 標準偏差 [#e513ad04]
#html(<div style="padding-left: 10px;">)
標準偏差はstdで求める。
#mycode2(python){{
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
}}
#html(</div>)
** 四分位点、四分位範囲 [#k400a332]
#html(<div style="padding-left: 10px;">)
#mycode2(python){{
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 )
}}
#html(</div>)
** 単位行列 [#w3be82ce]
#html(<div style="padding-left: 10px;">)
#mycode2(python){{
import numpy as np
np.eye(3)
}}
結果
#myterm2(){{
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
}}
#html(</div>)
** 逆行列 [#zf211bd2]
#html(<div style="padding-left: 10px;">)
AB=I, BA=I を満たす行列B(Aの逆行列)を求める。
#mycode2(python){{
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))
}}
結果
#myterm2(){{
a: [[2 3]
[4 5]]
b: [[-2.5 1.5]
[ 2. -1. ]]
ba: [[1. 0.]
[0. 1.]]
ab: [[1. 0.]
[0. 1.]]
}}
#html(</div>)
** 転置行列 [#f224c718]
#html(<div style="padding-left: 10px;">)
#mycode2(python){{
x = np.array([[1,2,3],[4,5,6]])
print(x.T)
}}
結果
#myterm2(){{
[[1 4]
[2 5]
[3 6]]
}}
#html(</div>)
** 内積、行列積の計算 [#abf69d76]
#html(<div style="padding-left: 10px;">)
行列積は np.dot で求める事ができる。
#mycode2(python){{
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)
}}
結果
#myterm2(){{
array([[ 9, 12, 15],
[19, 26, 33],
[29, 40, 51]])
}}
#html(</div>)
** 行列積を利用して仮説の計算を行う [#m68c16f3]
#html(<div style="padding-left: 10px;">)
h(x) = n + m*y という仮説は行列積を利用して計算する事ができる。
以下の3つの仮説がある場合。
#html(){{
<div style="padding-left: 10px;">
<math>
<mrow>
<msub><mo>h</mo><mo>θ</mo></msub><mo>(x)</mo> <mo>=</mo> <mi>2</mi> <mo>-</mo> <mi>3y</mi>
</mrow>
</math>
<br/>
<math>
<mrow>
<msub><mo>h</mo><mo>θ</mo></msub><mo>(x)</mo> <mo>=</mo> <mi>3</mi> <mo>-</mo> <mi>4y</mi>
</mrow>
</math>
<br/>
<math>
<mrow>
<msub><mo>h</mo><mo>θ</mo></msub><mo>(x)</mo> <mo>=</mo> <mi>5</mi> <mo>-</mo> <mi>10y</mi>
</mrow>
</math>
</div>
}}
y = [10, 20, 30] というベクトルに対して上記の各仮説の計算を行うには、以下の通り行列積を計算する。
#html(){{
<div style="padding: 10px;">
<math>
<mfenced>
<mtable>
<mtr>
<mtd>
<mn>1</mn>
</mtd>
<mtd>
<mn>10</mn>
</mtd>
</mtr>
<mtr>
<mtd>
<mn>1</mn>
</mtd>
<mtd>
<mn>20</mn>
</mtd>
</mtr>
<mtr>
<mtd>
<mn>1</mn>
</mtd>
<mtd>
<mn>30</mn>
</mtd>
</mtr>
</mtable>
</mfenced>
</math>
*
<math>
<mfenced>
<mtable>
<mtr>
<mtd>
<mn>2</mn>
</mtd>
<mtd>
<mn>3</mn>
</mtd>
<mtd>
<mn>5</mn>
</mtd>
</mtr>
<mtr>
<mtd>
<mn>3</mn>
</mtd>
<mtd>
<mn>4</mn>
</mtd>
<mtd>
<mn>10</mn>
</mtd>
</mtr>
</mtable>
</mfenced>
<mo> = </mo>
<mfenced>
<mtable>
<mtr>
<mtd>
<mn>(1 * 2 + 10 * 3)</mn>
</mtd>
<mtd>
<mn>(1 * 3 + 10 * 4)</mn>
</mtd>
<mtd>
<mn>(1 * 5 + 10 * 10)</mn>
</mtd>
</mtr>
<mtr>
<mtd>
<mn>(1 * 2 + 20 * 3)</mn>
</mtd>
<mtd>
<mn>(1 * 3 + 20 * 4)</mn>
</mtd>
<mtd>
<mn>(1 * 5 + 20 * 10)</mn>
</mtd>
</mtr>
<mtr>
<mtd>
<mn>(1 * 2 + 30 * 3)</mn>
</mtd>
<mtd>
<mn>(1 * 3 + 30 * 4)</mn>
</mtd>
<mtd>
<mn>(1 * 5 + 30 * 10)</mn>
</mtd>
</mtr>
</mtable>
</mfenced>
<mo> = </mo>
<mfenced>
<mtable>
<mtr>
<mtd>
<mn>32</mn>
</mtd>
<mtd>
<mn>43</mn>
</mtd>
<mtd>
<mn>105</mn>
</mtd>
</mtr>
<mtr>
<mtd>
<mn>62</mn>
</mtd>
<mtd>
<mn>83</mn>
</mtd>
<mtd>
<mn>205</mn>
</mtd>
</mtr>
<mtr>
<mtd>
<mn>92</mn>
</mtd>
<mtd>
<mn>123</mn>
</mtd>
<mtd>
<mn>305</mn>
</mtd>
</mtr>
</mtable>
</mfenced>
</math>
</div>
}}
実装例
#mycode2(python){{
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)
}}
結果
#myterm2(){{
array([[ 32, 43, 105],
[ 62, 83, 205],
[ 92, 123, 305]])
}}
#html(</div>)
#html(</div>)