PythonDay 25

# 今年覚えたnumpyの関数

More than 1 year has passed since last update.

## 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

>>> 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

>>> 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])