79
83

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

今年覚えたnumpyの関数

Last updated at Posted at 2014-12-23

今年を振り返って、今年覚えたnumpyの関数を紹介します。

r_, c_

最初は行列の連結です。vstack, hstackをよく使っていたのですが、r_, c_がより短く簡単に書けるのでこっちを使ってます。

>>> a = np.arange(6).reshape(2, 3)
>>> b = np.arange(6, 12).reshape(2, 3)
>>> a
array([[0, 1, 2],
       [3, 4, 5]])
>>> b
array([[ 6,  7,  8],
       [ 9, 10, 11]])
>>> r_[a, b]
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
>>> c_[a, b]
array([[ 0,  1,  2,  6,  7,  8],
       [ 3,  4,  5,  9, 10, 11]])

bmat

グリッド状に結合します。

>>> A = np.matrix('1 1; 1 1')
>>> B = np.matrix('2 2; 2 2')
>>> C = np.matrix('3 4; 5 6')
>>> D = np.matrix('7 8; 9 0')
>>> np.bmat([[A, B], [C, D]])
matrix([[1, 1, 2, 2],
        [1, 1, 2, 2],
        [3, 4, 7, 8],
        [5, 6, 9, 0]])

vsplit, hsplit

分割のための関数です。

>>> a = np.arange(24).reshape(6, 4)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23]])
>>> np.vsplit(a, 2) # 行2分割
[array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]]),
 array([[12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23]])]
>>> np.vsplit(a, [3, 5]) # 行を3行目と5行目で分割
[array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]]),
 array([[12, 13, 14, 15],
       [16, 17, 18, 19]]),
 array([[20, 21, 22, 23]])]
>>> np.hsplit(a, [3]) # 列を3列目で分割
[array([[ 0,  1,  2],
       [ 4,  5,  6],
       [ 8,  9, 10],
       [12, 13, 14],
       [16, 17, 18],
       [20, 21, 22]]),
 array([[ 3],
       [ 7],
       [11],
       [15],
       [19],
       [23]])]

vectorize

スカラに対する関数をnp.sinのようなnumpyの配列で使えるようにします。

>>> a = np.arange(6).reshape(2, 3)
>>> f = lambda x: x * x
>>> vf = np.vectorize(f)
>>> vf(a)
array([[ 0,  1,  4],
       [ 9, 16, 25]])

multiply

行列(matrix)の要素同士の掛け算を行う関数multiplyです。matrix使ってる時にたまに使います。

>>> a = np.matrix(np.arange(4).reshape(2, 2))
>>> b = np.matrix(np.arange(4, 8).reshape(2, 2))
>>> a
matrix([[0, 1],
        [2, 3]])
>>> b
matrix([[4, 5],
        [6, 7]])
>>> np.multiply(a, b)
matrix([[ 0,  5],
        [12, 21]])

linalg.matrix_power

行列の乗数計算です。

>>> a = np.matrix(np.arange(4).reshape(2, 2))
>>> np.linalg.matrix_power(a, 0)
matrix([[1, 0],
        [0, 1]])
>>> np.linalg.matrix_power(a, 1)
matrix([[0, 1],
        [2, 3]])
>>> np.linalg.matrix_power(a, 2)
matrix([[ 2,  3],
        [ 6, 11]])

asscalar

要素数が1の配列をスカラーにします。

>>> np.asscalar(np.array([10.0]))
10.0
>>> np.asscalar(np.array([10.0, 20.0]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 463, in asscalar
    return a.item()
ValueError: can only convert an array of size 1 to a Python scalar

is_busday

平日かどうかを判定する関数です。

>>> import datetime
>>> np.is_busday(datetime.date(2014, 12, 25))
True
>>> np.is_busday(datetime.date(2014, 12, 27))
False
>>> np.is_busday(datetime.date(2014, 12, 23))
True
>>> np.is_busday(datetime.date(2014, 12, 23), holidays=[datetime.date(2014, 12, 23)])
False

piecewise

区分関数を作成します。

f(x) = \begin{cases}
f_1(x), & \text{if }condition_1(x)\text{ is true} \\
f_2(x), & \text{if }condition_2(x)\text{ is true} \\
...
\end{cases}
>>> x = np.linspace(-2.5, 2.5, 6)
>>> x
array([-2.5, -1.5, -0.5,  0.5,  1.5,  2.5])
>>> np.piecewise(x, [x < 0, x >= 0], [-1, 1])
array([-1., -1., -1.,  1.,  1.,  1.])
>>> np.piecewise(x, [x < 0, x >= 0], [lambda x: -x, lambda x: x])
array([ 2.5,  1.5,  0.5,  0.5,  1.5,  2.5])
79
83
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
79
83

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?