Numpy personal tips

numpy is another indispensable tool for data analysis. I’ll leave a note as a personal reminder. For more information

Contents

github

Author’s environment

The author’s environment and import method are as follows.

! sw_vers
ProductName: Mac OS X
ProductVersion: 10.14.6
BuildVersion: 18G95
Python -V
Python 3.5.5 :: Anaconda, Inc.
%matplotlib inline
%config InlineBackend.figure_format = 'svg'

import numpy as np

np.__version__
'1.18.1'

Basic matrix computation.

np.identify(N)

Create unit matrix.

np.identity(3)
array([[1., 0., 0.]],
       [0., 1., 0.],
       [0., 0., 1.]])

np.eye(N,M=None,k=0)

Creates a unit matrix, which, unlike identify, is not limited to being a square matrix. Unlike identify, it is not restricted to be a square matrix, and the position of the diagonal one can be specified.

``python np.eye(3)





    array([[1., 0., 0.],
           [0., 1., 0.],
           [0., 0., 1.]])




```python
np.eye(4,3)
array([[1., 0., 0.]],
       [0., 1., 0.]],
       [0., 0., 1.],
       [0., 0., 0.]])
np.eye(4,5,1)
array([[0., 1., 0., 0., 0.]],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.]])

np.zeros((N,M,…))

Creates an array where all elements are zero. The argument is a tuple of shapes; int makes it a vector.

``python np.zeros((5,4))





    array([[0., 0., 0., 0.],
           [0., 0., 0., 0.],
           [0., 0., 0., 0.],
           [0., 0., 0., 0.],
           [0., 0., 0., 0.]])




```python
np.zeros(3)
array([0., 0., 0.])

np.zeros_like(x)

Creates an array with all zero elements of the same size as x.

a = np.array([i for i in range(6)]).reshape(2,3)
np.zeros_like(a) # Create a zero array of (2,3).
array([[0, 0, 0],
       [0, 0, 0]])

np.ones((N,M,…))

Creates an array with all elements being 1. The argument is a tuple of shapes; int makes it a vector.

np.ones((2,5))
array([[1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])

np.zeros_like(x)

Creates an array with all zero elements of the same size as x.

a = np.array([i for i in range(15)]).reshape(5,3)
np.ones_like(a) # Create a zero array of (5,3).
array([[1, 1, 1],
       [1, 1, 1],
       [1, 1, 1],
       [1, 1, 1],
       [1, 1, 1]])

np.dot(a,b)

Calculating the inner product. Let $\displaystyle {\bf a}=(a_1,a_2,a_3), {\bf b} = (b_1,b_2,b_3)$ be a vector (first-order tensor). $$. {\rm np.dot(a,b)} = a^{T}b = \sum_{k=1}^{3}a_kb_k $$. which is to calculate the In the case of matrices, it is the calculation of the product of matrices.

dot operator for tensors

For tensors of different sizes, the computation is a bit tedious, but the image of the computation is as follows

a = np.array([1,2])
b = np.array([4,3])

print('### For vectors ###')
print('a : ')
print(a)
print('b : ')
print(b)
print('Calculation result')
print(np.dot(a,b))
print()

a = np.array([[1,2],[3,4]])
b = np.array([[4,3],[2,1]])

print('### For matrices ###')
print('a : ')
print(a)
print('b : ')
print(b)
print('Calculation result')
print(np.dot(a,b))
### For vectors ###
a :
[1 2] b
b :
[4 3]
Calculation result
10

### For matrices ###
a :
[[1 2]]
 [3 4]]
b :
[[4 3]]
 [2 1]]
Calculation result
[[ 8 5]]
 [20 13]]

np.vdot(a,b)

Take the complex conjugate of $a$ and calculate the inner product. For real numbers, it is the same as np.dot. If the vector (first-order tensor) is $\displaystyle {\bf a}=(a_1,a_2,a_3), {\bf b} = (b_1,b_2,b_3)$, then $$. {\rm np.vdot(a,b)} = a^{*}b = \overline{a}^{T}b= \sum_{k=1}^{3}\overline{a_k}b_k $$.

a = np.array([1,2])
b = np.array([4,3])

print('equal to np.dot for real numbers')
print(np.dot(a,b))
print(np.vdot(a,b))
print('')

print('Equal to taking complex conjugate and calculating inner product')
a = np.array([1+2j,3+4j])
b = np.array([4+3j,2+1j])
print(np.vdot(a, b))

a = np.array([1-2j,3-4j])
b = np.array([4+3j,2+1j])
print(np.dot(a, b))

print((1-2j)*(4+3j)+(3-4j)*(2+1j))
For real numbers, it is equal to np.dot
10
10

Equivalent to taking complex conjugates and calculating the inner product
(20-10j)
(20-10j)
(20-10j)

np.outer(a,b)

Calculate the direct product. Let $\displaystyle {\bf a}=(a_1,a_2,a_3), {\bf b} = (b_1,b_2,b_3)$ be a vector (first-order tensor).

$$. {\rm np.outer(a,b)} = ab^{*}= a\overline{b}^{T}=\left( \begin{array}{ccc} a_1\overline{b_1} & a_1\overline{b_2} & a_1\overline{b_3} a_2\overline{b_1} & a_2\overline{b_2} & a_2\overline{b_3}\c a_3\overline{b_1} & a_3\overline{b_2} & a_3\overline{b_3} \cross\cross\cross\cross\cross\cross\cross\cross\cross\cross\cross\cross\b1} \right) $$.
. The following calculation is performed. Also, if $a$ and $b$ are not vectors, they are forced to be converted to vectors. The direct product does not come up much in data analysis or machine learning, but it is used in quantum computation. It is a basis for representing the state of two bits.
a = np.array([1,2])
b = np.array([4,3])
print('Example of calculating the outer of a vector')
print(np.outer(a,b))
print()

a = np.arange(1,5).reshape(-1,2)
b = np.arange(11,15).reshape(-1,2)

print('Example of matrix OUTER calculation')
print('a = \n',a)
print('b = \n',b)
print('np.outer(a,b) = \n',np.outer(a,b))
Example of calculating the outer of a vector
[[4 3]]
 [8 6]]

Example of computing outer of a matrix
a =
 [[1 2]]
 [3 4]]
b =
 [[11 12]]
 [13 14]]
np.outer(a,b) =
 [[11 12 13 14]]
 [22 24 26 28]
 [33 36 39 42]]
 [44 48 52 56]]

np.matmul(a,b)

This is the usual matrix product.

mutmal operator for tensors

If the sizes do not match

a = np.arange(1,5).reshape(-1,2)
b = np.arange(11,15).reshape(-1,2)

print('matmul calculation example')
print('a = \n',a)
print('b = \n',b)
print('np.matmul(a,b) = \n',np.matmul(a,b))
Example of matmul calculation
a =
 [[1 2]]
 [3 4]]
b =
 [[11 12]]
 [13 14]]
np.matmul(a,b) =
 [[37 40]]
 [85 92]]

Matmul is an important operation that often comes up in machine learning, so I will explain it in a little more detail. For more details, please refer to the official page .

When the dimensions of the two arguments are both 2 (when they are both matrices)

This is a matrix operation as usual. An example calculation is shown above.

When one of the dimensions of the arguments is greater than 2

The matrix operation will be performed as usual.

In the following case, the result is 7x4

In the following case, the result is 7x4.

a = np.ones([9, 5, 7, 4])
c = np.ones([9, 5, 4, 3])

print(np.dot(a, c).shape)
print(np.matmul(a, c).shape)

a = np.ones([1,2,1])
c = np.ones([1,1,2])

print(np.dot(a, c))
print(np.dot(a, c).shape)
print(np.matmul(a, c).shape)
(9, 5, 7, 9, 5, 3)
(9, 5, 7, 3)
[[[[1. 1.]]

  [[1. 1.]]]]
(1, 2, 1, 2)
(1, 2, 2)
a = np.array([.
  [[1,2],
   [[3,4]],
  [[10,20],
   [30,40]]
])

b = np.array([[.
  [[1],
   [[2]],
  [[10],
   [[20]],
  [[100],
   [200]]
])

print('For the dot operator')
print(a.shape)
print(b.shape)
print()

print(np.dot(a,b).shape)
print(np.dot(a,b))
print()
For the dot operator
(2, 2, 2)
(3, 2, 1)

(2, 2, 3, 1)
[[[[ 5]
   [ 50]
   [ 500]]

  [[ 11]]
   [ 110]
   [ 1100]]]


 [[ 50]]
   [ 500]
   [[ 5000]]

  [[ 110]]
   [ 1100]
   [11000]]]]
a = np.array([[
  [[1,2],
   [[3,4]],
  [[10,20],
   [30,40]]
])

b = np.array([[.
  [[1],
   [[2]],
  [[10],
   [[20]],
  [[100],
   [200]]
])

print('For the matmul operator')
print(a.shape)
print(b.shape)
print()

try:
  print(np.matmul(a,b).shape)
  print(np.matmul(a,b))
except Exception as e:
  print(e)
For the matmul operator
(2, 2, 2)
(3, 2, 1)

operands could not be broadcast together with remapped shapes [original->remapped]: (2,2,2)->(2,newaxis,newaxis) (3,2,1)->(3,newaxis,newaxis) and requested shape (2,1)
a = np.array([[1,2]])
  [[1,2],
   [[3,4]],
  [[10,20],
   [30,40]]
])

b = np.array([[.
  [[1],
   [2]]
])

print('For the matmul operator')
print(a.shape)
print(b.shape)
print()

print(np.matmul(a,b).shape)
print(np.matmul(a,b))
For the matmul operator
(2, 2, 2)
(1, 2, 1)

(2, 2, 1)
[[ 5]]
  [ 11]]

 [[ 50]]
  [110]]]