[PYTHON] Numpy functions I learned this year

Looking back on this year, I will introduce the numpy functions that I learned this year.

r_, c_ The first is matrix concatenation. I used to use vstack and hstack a lot, but I use this because r_ and c_ are shorter and easier to write.

>>> 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 Join in a grid.

>>> 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 function for splitting.

>>> 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) #Split into two lines
[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]) #Split line into 3rd and 5th line
[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]) #Split column by third column
[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 Allows functions for scalars to be used in numpy arrays like np.sin.

>>> 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 function multiply that multiplies the elements of a matrix. I use it occasionally when using 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 Matrix multiplier calculation.

>>> 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 Makes an array with 1 element a scalar.

>>> 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 This function determines whether it is a weekday.

>>> 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 Create a piecewise function.

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

Recommended Posts

Numpy functions I learned this year
Summary of numpy functions I didn't know
NumPy universal functions
I studied this! !!
This time I learned Python I and II at Progate.
This time I learned python III and IV with Prorate
I wrote GP with numpy
A list of functions that I came across with 100 Numpy knocks and thought "This is convenient!"
What I learned about Linux
What I learned in Python
I learned Python basic grammar
Python functions learned in chemoinformatics