numpyを使う

  • 高速コンテナの例
import numpy as np
a = np.arange(1000)
%timeit a ** 2 # A = range(1000)よりずっと速い
  • 検索
np.lookfor('fourrier')
  • ベクトル、行列、アレイ
    • numpyで覚えるべきことは、まず、これ
a1 = np.arange(12)
a2 = a1.copy()
a2.shape = (3, 4)
a3 = a1.copy()
a3.shape = (2, 3, 2)
a1
a2
a3
In [116]: a1
Out[116]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

In [117]: a2
Out[117]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [118]: a3
Out[118]: 
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5]],

       [[ 6,  7],
        [ 8,  9],
        [10, 11]]])
  • シークエンス
    • ここからここまで、指定幅
In [122]: np.arange(0,10,0.3)
Out[122]: 
array([ 0. ,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8,  2.1,  2.4,  2.7,  3. ,
        3.3,  3.6,  3.9,  4.2,  4.5,  4.8,  5.1,  5.4,  5.7,  6. ,  6.3,
        6.6,  6.9,  7.2,  7.5,  7.8,  8.1,  8.4,  8.7,  9. ,  9.3,  9.6,
        9.9])
    • 点の数。[min,max]、[min,max) の両方ができる
In [119]: np.linspace(0, 1, 6)
Out[119]: array([ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ])

In [120]: np.linspace(0, 1, 6, endpoint=False)
Out[120]: 
array([ 0.        ,  0.16666667,  0.33333333,  0.5       ,  0.66666667,
        0.83333333])
    • 行列
In [123]: np.zeros((3,3))
Out[123]: 
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])

In [124]: np.eye(3)
Out[124]: 
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])

In [125]: np.diag([1,3,2,4])
Out[125]: 
array([[1, 0, 0, 0],
       [0, 3, 0, 0],
       [0, 0, 2, 0],
       [0, 0, 0, 4]])

In [126]: np.ones((3,3))
Out[126]: 
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])
  • 簡単なグラフを描く
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 3, 20)
y = np.linspace(0, 9, 20)
plt.plot(x,y)
plt.plot(x,y,'o')
plt.show()
    • ipythonの場合には ipython --pylab というモードで立ち上げれば、plt.show()は不要
  • ブロードキャストで、格子状データ
x = np.arange(5)
y = np.arange(5)
y2 = y[:, np.newaxis]
x + y2
>>> x + y2
array([[0, 1, 2, 3, 4],
       [1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6],
       [3, 4, 5, 6, 7],
       [4, 5, 6, 7, 8]])
  • np.ogrid()を使ってさらに便利に
>>> x,y = np.ogrid[0:5,0:6]
>>> x
array([[0],
       [1],
       [2],
       [3],
       [4]])
>>> y
array([[0, 1, 2, 3, 4, 5]])
>>> x.shape
(5, 1)
>>> y.shape
(1, 6)
>>> x + y
array([[0, 1, 2, 3, 4, 5],
       [1, 2, 3, 4, 5, 6],
       [2, 3, 4, 5, 6, 7],
       [3, 4, 5, 6, 7, 8],
       [4, 5, 6, 7, 8, 9]])
  • とは言え、なんとなくイメージしがたいので、列数1の2次元行列、行数1の2次元行列として作って、その行列演算と考えるのがよいように思う
    • ただし、xが列ベクトル行列、yが行ベクトル行列であるときに以下のように行列の積が以下のようになる
x = np.array([range(4)],ndmin=2)
x
y = np.array([range(4)],ndmin=2)
y2 = y.T
y2
x * y2
x + y2
>>> x = np.array([range(4)],ndmin=2)
>>> x
array([[0, 1, 2, 3]])
>>> y = np.array([range(4)],ndmin=2)
>>> y2 = y.T
>>> y2
array([[0],
       [1],
       [2],
       [3]])
>>> x * y2
array([[0, 0, 0, 0],
       [0, 1, 2, 3],
       [0, 2, 4, 6],
       [0, 3, 6, 9]])
>>> x + y2
array([[0, 1, 2, 3],
       [1, 2, 3, 4],
       [2, 3, 4, 5],
       [3, 4, 5, 6]])
>>> p = np.poly1d([3,2,-1])
>>> x = np.arange(0,10,0.1)
>>> y = p(x)
>>> plt.plot(x,y)
[<matplotlib.lines.Line2D object at 0x10e041410>]
>>> plt.show()
>>> 
  • テキストデータファイルを読み込んで行列型にする
data = np.loadtxt('file.txt')