Usage of numpy matrix and multi-dimensional array in python, pythonnumpy

Source: Internet
Author: User

Usage of numpy matrix and multi-dimensional array in python, pythonnumpy

1. Introduction

Recently, I have been converting an algorithm from matlab to python. I am not familiar with python in many places. The general feeling is that it is easy to get started. In fact, it is quite difficult to use python elegantly. So far, I think matlab is quite comfortable to use in algorithm simulation research. It may be a bit familiar. Matlab directly integrates a lot of algorithm toolboxes. Function query, calling, and variable query are very convenient. Maybe python will feel very useful after a long time. Compared with python, you can directly select a code segment for execution, which is convenient, and python can also be implemented, that is, it is not very convenient.

Back to the truth, it takes a lot of vector and matrix operations to do algorithms. These operations are already familiar with matlab, but it is annoying to use python to find one, therefore, we will make a summary here, which will be updated based on the user experience in the subsequent use process.

Python matrix operations mainly rely on the numpy package. The scipy package is based on numpy and greatly extends the latter's computing capability.

2. Create a general multi-dimensional array

import numpy as np

a = np.array ([1,2,3], dtype = int) # create a 1 * 3-dimensional array array ([1,2,3])

type (a) # numpy.ndarray

a.shape # dimensional information (3L,)

a.dtype.name # 'int32'

a.size # Number of elements: 3

a.itemsize #Number of bytes occupied by each element: 4

b = np.array ([[1,2,3], [4,5,6]], dtype = int) # Create a 2 * 3-dimensional array array ([[1,2,3], [4,5 , 6]]))

b.shape # dimensional information (2L, 3L)

b.size # Number of elements: 6

b.itemsize # Number of bytes occupied by each element: 4


c = np.array ([[1,2,3], [4,5,6]], dtype = 'int16') # Create a 2 * 3-dimensional array array ([[1,2,3], [4 , 5,6]], dtype = int16)

c.shape # dimension information (2L, 3L)

c.size # Number of elements: 6

c.itemsize # Number of bytes occupied by each element: 2

c.ndim # number of dimensions

 
d = np.array ([[1,2,3], [4,5,6]], dtype = complex) # two-dimensional array of complex numbers

d.itemsize # Number of bytes occupied by each element: 16

d.dtype.name # Element type: 'complex128'

3. Create a special multi-dimensional array

a1 = np.zeros ((3,4)) # Create a 3 * 4 all-zero 2D array

Output:

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

  [0., 0., 0., 0.],

  [0., 0., 0., 0.]])

a1.dtype.name # Element type: 'float64'

a1.size # Number of elements: 12

a1.itemsize # Number of bytes occupied by each element: 8

 

 

a2 = np.ones ((2,3,4), dtype = np.int16) # create 2 * 3 * 4 all 1 3D array

a2 = np.ones ((2,3,4), dtype = 'int16') # create 2 * 3 * 4 all 1 3D array

Output:

array ([[[1, 1, 1, 1],

  [1, 1, 1, 1],

  [1, 1, 1, 1]],

 

  [[1, 1, 1, 1],

  [1, 1, 1, 1],

  [1, 1, 1, 1]]], dtype = int16)

 

 

a3 = np.empty ((2,3)) # create 2 * 3 uninitialized two-dimensional array

Output: (may vary)

array ([[1., 2., 3.],

  [4., 5., 6.]])



a4 = np.arange (10,30,5) # initial value 10, end value: 30 (not included), step size: 5

Output: array ([10, 15, 20, 25])

a5 = np.arange (0,2,0.3) # initial value 0, end value: 2 (not included), step size: 0.2

Output: array ([0., 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])


from numpy import pi

np.linspace (0, 2, 9) # initial value 0, end value: 2 (inclusive), number of elements: 9

Output:

array ([0., 0.25, 0.5, 0.75, 1., 1.25, 1.5, 1.75, 2.])

x = np.linspace (0, 2 * pi, 9)

Output:

array ([0., 0.78539816, 1.57079633, 2.35619449, 3.14159265,

  3.92699082, 4.71238898, 5.49778714, 6.28318531])

a = np.arange (6)

Output:

array ([0, 1, 2, 3, 4, 5])

b = np.arange (12) .reshape (4,3)

Output:

array ([[0, 1, 2],

  [3, 4, 5],

  [6, 7, 8],

  [9, 10, 11]])

c = np.arange (24) .reshape (2,3,4)

Output:

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

You can use numpy. set_printoptions to set the printing format of numpy variables.

In ipython, use help (numpy. set_printoptions) to query help and examples.

4. Basic operations on multi-dimensional arrays

Addition and subtraction operations require that the dimension information of both sides of the operation be consistent, and M * N is an array to perform the operation correctly.

a = np.arange (4)

Output:

array ([0, 1, 2, 3])

b = a ** 2

Output:

array ([0, 1, 4, 9])

c = 10 * np.sin (a)

Output:

  array ([0., 8.41470985, 9.09297427, 1.41120008])

 

 

n <35

Output:

array ([True, True, True, True], dtype = bool)

 

A = np.array ([[1,1], [0,1]])

B = np.array ([[2,0], [3,4]])

C = A * B # element multiplication

Output:

array ([[2, 0],

   [0, 4]])

D = A.dot (B) # matrix multiplication

Output:

array ([[5, 4],

   [3, 4]])

E = np.dot (A, B) # matrix multiplication

Output:

array ([[5, 4],

   [3, 4]])

Type conversion during multi-dimensional array operations

When operating with arrays of different types, the type of the resulting array corresponds to the more general or precise one (a behavior known as upcasting)

That is, when you operate on different types of multi-dimensional arrays, the results are automatically converted to arrays of higher precision types, that is, upcasting

a = np.ones ((2,3), dtype = int) # int32

b = np.random.random ((2,3)) # float64

b + = a # correct

a + = b # error
a = np.ones (3, dtype = np.int32)

b = np.linspace (0, pi, 3)

c = a + b

d = np.exp (c * 1j)

Output:

array ([0.54030231 + 0.84147098j, -0.84147098 + 0.54030231j,

   -0.54030231-0.84147098j])

d.dtype.name

Output:

  'complex128'

One-dimensional operations of multi-dimensional arrays, such as sum, minimum, and maximum

a = np.random.random ((2,3))

a.sum ()

a.min ()

a.max ()

 

 

b = np.arange (12) .reshape (3,4)

Output:

array ([[0, 1, 2, 3],

   [4, 5, 6, 7],

   [8, 9, 10, 11]])

b.sum (axis = 0) # Sum by column

Output:

array ([12, 15, 18, 21])

b.sum (axis = 1) # Sum by row

Output:

array ([6, 22, 38])

b.cumsum (axis = 0) # Accumulate elements by column

Output:

array ([[0, 1, 2, 3],

   [4, 6, 8, 10],

   [12, 15, 18, 21]])

b.cumsum (axis = 1) # Accumulate elements by row

Output:

array ([[0, 1, 3, 6],

   [4, 9, 15, 22],

   [8, 17, 27, 38]])


universal functions


B = np.arange (3)

np.exp (B)

np.sqrt (B)

C = np.array ([2.,-1., 4.])

np.add (B, C)

Other ufunc functions include:

All, any, apply_along_axis, argmax, argmin, argsort, average, bincount, ceil, clip, conj, partition coef, cov, cross, cumprod, cumsum, diff, dot, floor, inner, lexsort, max, maximum, mean, median, min, minimum, nonzero, outer, prod, re, round, sort, std, sum, trace, transpose, var, vdot, vectorize, where

5. array indexing, slicing, and iteration

a = np.arange (10) ** 3

a [2]

a [2: 5]

a [::-1] # reverse order output

for i in a:

 print (i ** (1/3.))

def f (x, y):

 return 10 * x + y

b = np.fromfunction (f, (5,4), dtype = int)

b [2,3]

b [0: 5,1]

b [:, 1]

b [1: 3 ,:]

b [-1]
c = np.array ([[[0,1,2], [10,11,12]], [[100,101,102], [110,111,112]]])

Output:

array ([[[0, 1, 2],

  [10, 11, 12]],

 

  [[100, 101, 102],

  [110, 111, 112]]])

c.shape

Output:

(2L, 2L, 3L)

c [0, ...]

c [0,:,:]

Output:

array ([[0, 1, 2],

  [10, 11, 12]])

c [:,:, 2]

c [..., 2]

Output:

array ([[2, 12],

  [102, 112]])

 

for row in c:

 print (row)

 

for element in c.flat:

 print (element)

a = np.floor (10 * np.random.random ((3,4)))

Output:

array ([[3., 9., 8., 4.],

  [2., 1., 4., 6.],

  [0., 6., 0., 2.]])

a.ravel ()

Output:

array ([3., 9., 8., ..., 6., 0., 2.])

a.reshape (6,2)

Output:

array ([[3., 9.],

  [8., 4.],

  [ twenty one.],

  [4., 6.],

  [0., 6.],

  [0., 2.]])

a.T

Output:

array ([[3., 2., 0.],

  [9., 1., 6.],

  [8., 4., 0.],

  [4., 6., 2.]])

a.T.shape

Output:

(4L, 3L)

a.resize ((2,6))

Output:

array ([[3., 9., 8., 4., 2., 1.],

  [4., 6., 0., 6., 0., 2.]])

a.shape

Output:

(2L, 6L)

a.reshape (3, -1)

Output:

array ([[3., 9., 8., 4.],

  [2., 1., 4., 6.],

  [0., 6., 0., 2.]])

Query the following functions:

Ndarray. shape, reshape, resize, ravel

6. combine different multi-dimensional arrays

a = np.floor (10 * np.random.random ((2,2)))

Output:

array ([[5., 2.],

   [6., 2.]]))

b = np.floor (10 * np.random.random ((2,2)))

Output:

array ([[0., 2.],

   [4., 1.]]))

np.vstack ((a, b))

Output:

array ([[5., 2.],

   [6., 2.],

   [0., 2.],

   [4., 1.]]))

np.hstack ((a, b))

Output:

array ([[5., 2., 0., 2.],

   [6., 2., 4., 1.]])

 

 

from numpy import newaxis

np.column_stack ((a, b))

Output:

array ([[5., 2., 0., 2.],

   [6., 2., 4., 1.]])

 

 

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

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

a [:, newaxis]

Output:

array ([[4.],

   [ 2.]])

b [:, newaxis]

Output:

array ([[2.],

   [ 8.]])

np.column_stack ((a [:, newaxis], b [:, newaxis]))

Output:

array ([[4., 2.],

   [2., 8.]]))

np.vstack ((a [:, newaxis], b [:, newaxis]))

Output:

array ([[4.],

   [ 2.],

   [ 2.],

   [ 8.]])

np.r_ [1: 4,0,4]

Output:

array ([1, 2, 3, 0, 4])

np.c_ [np.array ([[1,2,3]]), 0,0,0, np.array ([[4,5,6]])]

Output:

array ([[1, 2, 3, 0, 0, 0, 4, 5, 6]])

For more information, see the following functions:

Hstack, vstack, column_stack, concatenate, c _, r _

7. Split a large multi-dimensional array into a small multi-dimensional array

a = np.floor (10 * np.random.random ((2,12)))

Output:

array ([[9., 7., 9., ..., 3., 2., 4.],

   [5., 3., 3., ..., 9., 7., 7.]])

np.hsplit (a, 3)

Output:

[array ([[9., 7., 9., 6.],

   [5., 3., 3., 1.]]), array ([[7., 2., 1., 6.],

   [7., 5., 0., 2.]]), array ([[9., 3., 2., 4.],

   [3., 9., 7., 7.]]]]

np.hsplit (a, (3,4))

Output:

[array ([[9., 7., 9.],

   [5., 3., 3.]]), array ([[6.],

   [1.]]), array ([[7., 2., 1., ..., 3., 2., 4.],

   [7., 5., 0., ..., 9., 7., 7.]])]

Functions that implement similar functions include:

Heatmap, vsplit, array_split

8. Multi-dimensional array copying

a = np.arange (12)

Output:

array ([0, 1, 2, ..., 9, 10, 11])

 

not copy at all

 

b = a

b is a # True

b.shape = 3,4

a.shape # (3L, 4L)

 

def f (x) # Python passes mutable objects as references, so function calls make no copy.

  print (id (x)) # id is the unique identifier of the python object

 

id (a) # 111833936L

id (b) # 111833936L

f (a) # 111833936L

 

 

Shallow copy
c = a.view ()

c is a # False

c.base is a # True

c.flags.owndata # False

c.shape = 2,6

a.shape # (3L, 4L)

c [0,4] = 1234

print (a)

Output:

array ([[0, 1, 2, 3],

   [1234, 5, 6, 7],

   [8, 9, 10, 11]])

s = a [:, 1: 3]

s [:] = 10

print (a)

Output:

array ([[0, 10, 10, 3],

   [1234, 10, 10, 7],

   [8, 10, 10, 11]])

 

 

Deep copy
d = a.copy ()

d is a # False

d.base is a # False

d [0,0] = 9999

print (a)

Output:

array ([[0, 10, 10, 3],

   [1234, 10, 10, 7],

   [8, 10, 10, 11]])

Numpy basic functions and methods

Arange, array, copy, empty, empty_like, eye, fromfile, fromfunction, identity, linspace, logspace, mgrid, ogrid, ones, ones_like,R, Zeros, zeros_like

Conversions

Ndarray. astype, atleast_1d, atleast_2d, atleast_3d, mat

Manipulations

Array_split, column_stack,Concatenate, Diagonal, dsp1_, dstack, heat map, hstack, ndarray. item, newaxis, ravel, repeat, reshape, resize, squeeze, swapaxes, take, transpose, vsplit, vstack

Questionsall, any, nonzero, where

Ordering

Argmax, argmin, argsort, max, min, ptp, searchsorted, sort

Operations

Choose, compress, cumprod, cumsum, inner, ndarray. fill, imag, prod, put, putmask, real, sum

Basic Statistics

Cov, mean, std, var

Basic Linear Algebra

Cross, dot, outer, linalg. svd, vdot

Link to the complete list of functions and methods:

Https://docs.scipy.org/doc/numpy-dev/reference/routines.html#routines

9. Special indexing skills

a = np.arange (12) ** 2
Output:
array ([0, 1, 4, ..., 81, 100, 121])
i = np.array ([1,1,3,8,5])
a [i]
Output:
array ([1, 1, 9, 64, 25])

j = np.array ([[3,4], [9,7]])
a [j]
Output:
array ([[9, 16],
   [81, 49]])


palette = np.array ([[0,0,0], [255,0,0], [0,255,0], [0,0,255], [255,255,255]])
image = np.array ([[0,1,2,0], [0,3,4,0]])
palette [image]
Output:
array ([[[0, 0, 0],
   [255, 0, 0],
   [0, 255, 0],
   [0, 0, 0]],

   [[0, 0, 0],
   [0, 0, 255],
   [255, 255, 255],
   [0, 0, 0]]])


i = np.array ([[0,1], [1,2]])
j = np.array ([[2,1], [3,3]])
a [i, j]
Output:
array ([[2, 5],
   [7, 11]])
l = [i, j]
a [l]
Output:
array ([[2, 5],
   [7, 11]])


a [i, 2]
Output:
array ([[2, 6],
   [6, 10]])

a [:, j]
Output:
array ([[[2, 1],
   [3, 3]],

   [[6, 5],
   [7, 7]],

   [[10, 9],
   [11, 11]]]))

s = np.array ([i, j])
print (s)
array ([[[0, 1],
   [1, 2]],

   [[twenty one],
   [3, 3]]]))

a [tuple (s)]
Output:
array ([[2, 5],
   [7, 11]])
print (tupe (s))
Output:
(array ([[0, 1],
   [1, 2]]), array ([[2, 1],
   [3, 3]]))

10. Search for the Maximum/minimum values and their corresponding index values

time = np.linspace (20, 145, 5)
Output:
 array ([20., 51.25, 82.5, 113.75, 145.])

data = np.sin (np.arange (20)). reshape (5,4)
Output:
array ([[0., 0.84147098, 0.90929743, 0.14112001],
  [-0.7568025, -0.95892427, -0.2794155, 0.6569866],
  [0.98935825, 0.41211849, -0.54402111, -0.99999021],
  [-0.53657292, 0.42016704, 0.99060736, 0.65028784],
  [-0.28790332, -0.96139749, -0.75098725, 0.14987721]])

ind = data.argmax (axis = 0)
Output:
array ([2, 0, 3, 1], dtype = int64)

time_max = time [ind]
Output:
array ([82.5, 20., 113.75, 51.25])

data_max = data [ind, xrange (data.shape [1])]
Output:
array ([0.98935825, 0.84147098, 0.99060736, 0.6569866])

np.all (data_max == data.max (axis = 0))
Output:
True

 

a = np.arange (5)
a [[1,3,4]] = 0
print (a)
Output:
array ([0, 0, 2, 0, 0])
a = np.arange (5)
a [[0,0,2]] = [1,2,3]
print (a)
Output:
array ([2, 1, 3, 3, 4])


a = np.arange (5)
a [[0,0,2]] + = 1
print (a)
Output:
array ([1, 1, 3, 3, 4])
a = np.arange (12) .reshape (3,4)
 b = a> 4
Output:
array ([[False, False, False, False],
  [False, True, True, True],
  [True, True, True, True]], dtype = bool)

a [b]
Output:
array ([5, 6, 7, 8, 9, 10, 11])

a [b] = 0
print (a)
Output:
array ([[0, 1, 2, 3],
  [4, 0, 0, 0],
  [0, 0, 0, 0]])
a = np.arange (12) .reshape (3,4)
b1 = np.array ([False, True, True])
b2 = n.array ([True, False, True, False])
a [b1 ,:]
Output:
array ([[4, 5, 6, 7],
  [8, 9, 10, 11]])

a [b1]
Output:
array ([[4, 5, 6, 7],
  [8, 9, 10, 11]])

a [:, b2]
Output:
array ([[0, 2],
  [4, 6],
  [8, 10]])

a [b1, b2]
Output:
array ([4, 10])

11. ix _ () function

a = np.array ([2,3,4,5])
b = np.array ([8,5,4])
c = np.array ([5,4,6,8,3])
ax, bx, cx = np.ix_ (a, b, c)
print (ax) # (4L, 1L, 1L)
Output:
array ([[[2]],

   [[3]],

   [[4]],

   [[5]]]))
print (bx) # (1L, 3L, 1L)
Output:
array ([[[8],
   [5],
   [4]]]))
print (cx) # (1L, 1L, 5L)
Output:
array ([[[5, 4, 6, 8, 3]]])


result = ax + bx * cx
Output:
array ([[[42, 34, 50, 66, 26],
   [27, 22, 32, 42, 17],
   [22, 18, 26, 34, 14]],

   [[43, 35, 51, 67, 27],
   [28, 23, 33, 43, 18],
   [23, 19, 27, 35, 15]],

   [[44, 36, 52, 68, 28],
   [29, 24, 34, 44, 19],
   [24, 20, 28, 36, 16]],

   [[45, 37, 53, 69, 29],
   [30, 25, 35, 45, 20],
   [25, 21, 29, 37, 17]]])

result [3,2,4]
Output: 17

12. Linear Algebra

a = np.array ([[1., 2.], [3., 4.]])
a.transpose () # transpose
np.linalg.inv (a) # find the inverse
u = np.eye (2) # generate identity matrix
np.dot (a, a) # matrix product
np.trace (a) # find the trace of the matrix
y = np.array ([5.], [7.]])
np.linalg.solve (a, y) # solve linear system of equations
np.linalg.eig (a) # feature decomposition

"Automatic" Reshaping

a = np.arange (30)
a.shape = 2, -1,3
a.shape # (2L, 5L, 3L)
print (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],
   [24, 25, 26],
   [27, 28, 29]]])
x = np.arange (0,10,2)
y = np.arange (5)
m = np.vstack ([x, y])
Output:
array ([[0, 2, 4, 6, 8],
   [0, 1, 2, 3, 4]])
n = np.hstack ([x, y])
Output:
array ([0, 2, 4, 6, 8, 0, 1, 2, 3, 4])

13. Create a Matrix

a = np.array ([1,2,3])
a1 = np.mat (a)
Output:
matrix ([[1, 2, 3]])
type (a1)
Output:
numpy.matrixlib.defmatrix.matrix
a1.shape
Output:
(1L, 3L)
a.shape
Output:
(3L,)

b = np.matrix ([1,2,3])
Output:
matrix ([[1, 2, 3]])

from numpy import *
data1 = mat (zeros ((3,3)))
data2 = mat (ones ((2,4)))
data3 = mat (random.rand (2,2))
data4 = mat (random.randint (2,8, size = (2,5)))
data5 = mat (eye (2,2, dtype = int))

14. Common matrix operations

a1 = mat ([1,2])
a2 = mat ([[1], [2]])
a3 = a1 * a2
print (a3)
Output:
matrix ([[5]])

print (a1 * 2)
Output:
matrix ([[2, 4]])

a1 = mat (eye (2,2) * 0.5)
print (a1.I)
Output:
matrix ([[2., 0.],
   [0., 2.]])


a1 = mat ([[1,2], [2,3], [4,2]])
a1.sum (axis = 0)
Output:
matrix ([[7, 7]])
a1.sum (axis = 1)
Output:
matrix ([[3],
   [5],
   [6]])
a1.max () # Find the maximum value of matrix elements
Output:
4
a1.min () # Find the minimum value of matrix elements
Output:
1

np.max (a1,0) # find the maximum element of each column of the matrix
Output:
matrix ([[4, 3]])
np.max (a1,1) # find the maximum element of each row of the matrix
Output:
matrix ([[2],
   [3],
   [4]])


a = mat (ones ((2,2)))
b = mat (eye ((2)))
c = hstack ((a, b))
Output:
matrix ([[1., 1., 1., 0.],
   [1., 1., 0., 1.]])
d = vstack ((a, b))
Output:
matrix ([[1., 1.],
   [1., 1.],
   [1., 0.],
   [0., 1.]])

15. Mutual conversion between matrices, arrays, and lists

aa = [[1,2], [3,4], [5,6]]
bb = array (aa)
cc = mat (bb)

cc.getA () # matrix to array
cc.tolist () # matrix to list
bb.tolist () # convert array to list


# When the list is one-dimensional, the situation is a bit special
aa = [1,2,3,4]
bb = array (aa)
Output:
array ([1, 2, 3, 4])
cc = mat (bb)
Output:
matrix ([[1, 2, 3, 4]])

cc.tolist ()
Output:
[[1, 2, 3, 4]]

bb.tolist ()
Output:
[1, 2, 3, 4]

cc.tolist () [0]
Output:
[1, 2, 3, 4] 

The content sorting Reference Links are as follows:

Https://docs.scipy.org/doc/numpy-dev/user/quickstart.html

Http://python.usyiyi.cn/translate/NumPy_v111/reference/arrays.scalars.html#arrays-scalars-built-in

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.