Create a ndarray objects is as simple as a list as a parameter.
E.g
import numpy as np # introduced numpy library
# Create a dimensional objects narray
a = np.array ([1,2,3,4,5])
# Narray create two-dimensional objects
a2 = np.array ([[1,2,3,4,5] , [6,7,8,9,10]])
# Created by analogy with its multidimensional objects
Gets the number of columns of the matrix rows (two dimensions)
accustomed to using matlab for digital to analog programming, when you want to traverse the matrix, generally the first to obtain the number of rows and columns of the matrix. To obtain the length of each dimension of the object narray can shape object attributes narray
import numpy as np
a = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print (a.shape) # result tuple returns a tuple (2L, 5L)
Print (a.shape [0]) # get the number of rows returned 2
Print (a.shape [. 1]) # get the number of columns, return 5
Taken matrix
rows and columns taken
same matrix and taken list, by [] (brackets) to intercept
import numpy as np
a = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print (a [0: 1] ) # intercepting a first row, returns [[2. 1. 4. 3. 5]]
Print (A [1,2:. 5]) #, taken second line, the third, four, return [ 89]
print (a [1 ,:]) #, taken second line, return [678910]
Conditional intercepted
by interception condition is actually passed its own Boolean statements in [] (square brackets) in the
example
import numpy as np
np.array = a ([[1,2,3,4,5], [6,7,8,9,10]])
B = a [a> 6] # 6, taken matrix element is greater than a, range is the one-dimensional array
print (b) # returns [78910]
# In fact, Boolean statement first generate a Boolean matrix, the matrix incoming Boolean [] (square brackets) to achieve the interception
Print (A> 6)
# return
[[False False False False False]
[False True True True True]]
Conditional application is taken more matrix elements satisfying certain conditions becomes a specific value.
For example, a matrix element 6 becomes greater than 0.
import numpy as np
np.array = A ([[1,2,3,4,5], [6,7,8,9,10]])
Print (A)
# Start matrix
[[12345]
[6 78910]]
A [A> 6] = 0
Print (A)
after more than 6 # clear matrix
[[12,345]
[60,000]]
The combined matrix
combined matrix may be achieved by a method and vstack hstack method of numpy
import numpy as np
a1 = np.array([[1,2],[3,4]])
a2 = np.array([[5,6],[7,8]])
#! Note that the parameter list to be passed in the form of a list or an incoming tuple of the tuple
Print (np.hstack ([A1, A2]))
# laterally combined, return the following results
[[1256]
[347 8]]
Print (np.vstack ((A1, A2)))
# longitudinal combined and returns the result as follows
[[12]
[34]
[56]
[78]]
The combined matrix can also concatenatef method.
np.concatenate ((a1, a2), axis = 0) is equivalent to np.vstack ((a1, a2))
np.concatenate ((a1, a2), axis = 1) is equivalent to np.hstack ((a1, a2))
By function creates a matrix
comes with some function to create ndarray object numpy module, you can easily create a common or regular matrix.
arange
import numpy as np
a = np.arange (10) # default starting from 0 to 10 (excluding 10) in steps. 1
Print (A) returns # [0123456789]
a1 = np.arange (5,10) # 5 from the start to 10 (excluding 10) in steps. 1
Print (A1) returns # [56789]
a2 = np.arange (5,20,2) # 5 from the start to 20 (excluding 20) in steps of 2
Print (A2) returns # [5791113151719]
linspace
linspace () and very similar to the matlab linspace, for creating a sequence specified number of equally spaced, actually generate a arithmetic sequence.
import numpy as np
a = np.linspace (0,10,7) # 0 is the first generation, the last one is 10, the number of arithmetic series containing 7
Print (A)
# Results
[5. 0.5 1.66666667 3.33333333 6.66666667 8.33333333 10]
logspace
linspace for generating an arithmetic sequence, and for generating a geometric sequence logspace.
The following example is used to generate the first 100, 102 is the last one, containing 5 number of geometric series.
import numpy as np
np.logspace = A (0,2,5)
Print (A)
# Results
[1. 10. The 31.6227766 3.16227766 100.]
ones, zeros, eye, empty
ones to create a full matrix
zeros to create an all-zero matrix
eye matrix created
empty create an empty matrix (there are actually value)
import numpy as np
a_ones = np.ones ((3,4)) # 1 to create a full 3 * 4 matrix
Print (a_ones)
# Results
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1 1. 1. 1.]]
a_zeros = np.zeros ((3,4)) # 0 create a full 3 * 4 matrix
Print (a_zeros)
# Results
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0 0. 0. 0.]]
a_eye = np.eye (3) # create third-order unit matrix
Print (a_eye)
# Results
[0. 1. 0.]
[0. 1. 0.]
[0. 0. 1.]]
a_empty = np.empty ((3,4)) # Create an empty matrix of 3 * 4
Print (a_empty)
# Results
[[1.78006111e-306 -3.13259416e-294 4.71524461e-309 1.94927842e + 289]
[2.10230387e- 5.42870216e + 294 6.73606381e-309 310 3.82265219e-297]
[6.24242356e-309 1.07034394e-296 2.12687797e + 183 is 6.88703165e-315]]
fromString
fromString () method can be converted into a string ndarray object needs to be digitized character string This method is useful sequences can be obtained ascii code string.
= A "abcdef"
B = np.fromstring (A, dtype = np.int8) # 8 as a character, so as to specify dtype np.int8
Print (B) returns # [979,899,100,101,102]
fromfunction
fromfunction () method can be generated according to the elements of the matrix line number out of the matrix.
Such as creating a matrix, the matrix of each element for the row and column numbers and.
import numpy as np
def func(i,j):
return i+j
np.fromfunction = A (FUNC, (5,6))
# parameter specifies a first function, the second parameter is a tuple or tuple in list, matrix size described
Print (A)
# Returns
[[0.1 2. 3. 4. 5.]
[1. 2. 3. 4. 5. 6.]
[2. 3. 4. 5. 6. 7.]
[3. 4. 5. 6. 7.8 .]
[4. 5. 6. 7. 8. 9.]]
# Note that the column is the number of lines starting from 0
Matrix operations
used the matrix operator
ndarray numpy objects in many overloaded operators, these operators can be used to complete the matrix operation between the corresponding elements.
Operator Description
+ adding the corresponding elements of the matrix
- the matrix elements corresponding to subtraction
* corresponding element of the matrix multiplication
/ division matrix corresponding element, if providers are integers then take
the corresponding element of the matrix division% take the remainder
** of each matrix element the power of n are taken as 2 **: squaring each element
e.g.
import numpy as np
a1 = np.array([[4,5,6],[1,2,3]])
a2 = np.array([[6,5,4],[3,2,1]])
print (a1 + a2) # sum
# Results
[[101 010]
[444]]
(a1 / a2) # print an integer division takes List
# Results
[[011]
[013]]
print (a1% a2) # division take the remainder
# Results
[[402]
[100]]
Common matrix function
in the same manner, numpy also defines a number of functions, these functions can function on each element in the matrix.
The default table introduced numpy module, i.e.
Import numpy NP AS
A as ndarray object.
Matrix Function Description
np.sin (a) taking the sine of each element in a matrix, SiN (X)
np.cos (a) taking the cosine of each element in a matrix, COS (X)
np.tan (a) of each element of the matrix take a tangent, Tan (X)
np.arcsin (a) taking the arc sine of each element in a matrix, arcsin (X)
np.arccos (a) taking the inverse cosine of each element in a matrix , arccos (X)
np.arctan (a) a matrix each element taking the arctangent, arctan (X)
np.exp (a) taking an exponential function of each element in a matrix, EX
np.sqrt (a) each element in a matrix square root √x
e.g.
import numpy as np
np.array = A ([[l, 2,3], [4,5,6]])
Print (np.sin (A))
# Results
[[0.84147098 0.90929743 0.14112001]
[-0.7568025 -0.2794155 -0.95892427]]
print(np.arcsin(a))
# 结果
C:\Users\Administrator\Desktop\learn.py:6: RuntimeWarning: invalid value encountered in arcsin
print(np.arcsin(a))
[[ 1.57079633 nan nan]
[ nan nan nan]]
When the matrix element is not in the scope of the domain, generates RuntimeWarning, the result is nan (not a number).
Matrix multiplication (dot)
the number of rows of the matrix multiplication matrix multiplication must satisfy the conditions, i.e. the number of columns of a matrix is equal to the second matrix.
Dot matrix multiplication function for
e.g.
import numpy as np
a1 = np.array ([[1,2,3] , [4,5,6]]) # a1 is 2 * 3 matrix
a2 = np.array ([[1,2] , [3,4], [5,6]]) # a2 is 3 * 2 matrix
print (a1.shape [1] == a2.shape [0]) # True, satisfies the condition matrix multiplication
Print (a1.dot (A2))
# a1.dot (A2) corresponding to the matlab A2 * A1
# and the python matlab a1 * a2 corresponds to the A1. A2 *
# results
[[2228]
[4964]]
Transposed matrix
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
Print (A.transpose ())
# Results
[[14]
[25]
[36]]
Matrix transpose there's an easier way is to aT
import numpy as np
np.array = A ([[l, 2,3], [4,5,6]])
Print (the aT)
# Results
[[14]
[25]
[36]]
Inverse matrix
of Matrix Inverse needs to be imported numpy.linalg, with linalg to inv inverse function.
Matrix inversion conditions are the same number of rows and columns of the matrix.
import numpy as np
import numpy.linalg as lg
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
Print (lg.inv (A))
# Results
[[-4.50359963e 9.00719925e + 15 + 15 + 15 -4.50359963e]
[9.00719925e + 15 -1.80143985e 9.00719925e + 16 + 15]
[15 + 9.00719925 -4.50359963e e + 15 -4.50359963e + 15]]
a = np.eye (3) # 3-order unit matrix
print (lg.inv (a)) # inverse matrix for his own
# Results
[[0. 1. 0.]
[0. 1. 0.]
[ 0. 0. 1.]]
Matrix information acquisition (e.g., average)
maximum minimum
maximum minimum values of the matrix elements is a function of max and min, respectively, can be obtained throughout the matrix row or column maximum and minimum.
E.g
import numpy as np
np.array = A ([[l, 2,3], [4,5,6]])
Print (a.max ()) # obtain the maximum result the whole matrix:. 6
Print (a.min ()) # result: 1
# Keyword parameters can be obtained axis line maximum (minimum) value or column maximum (minimum) value
# axis = 0 the row direction of the maximum (minimum) value, i.e., each column of the maximum (minimum) value
# axis = 1 column direction the maximum (minimum) value, i.e., per row maximum (minimum) value
# e.g.
Print (a.max (Axis = 0))
# results [456]
Print (a.max (Axis =. 1))
# results [36]
To get the maximum and minimum position # element is located, the function can be obtained by argmax
Print ((Axis =. 1) a.argmax)
# results [22]
The average value of
the average value obtained in the elements of the matrix can function mean (). Likewise, the entire matrix may be obtained, the average value of the row or column.
import numpy as np
np.array = A ([[l, 2,3], [4,5,6]])
Print (a.mean ()) # results: 3.5
# Likewise, parameters can be specified by the keyword obtaining an average value axis in which direction
print (a.mean (axis = 0) ) # Results [3.5 of 2.5 4.5 of 5]
Print (a.mean (= axis. 1)) # Results [2 5.]
Variance
function of the variance of var (), a function of the variance var () function corresponding to mean (abs (x - x.mean ( )) ** 2), where x is the matrix.
import numpy as np
np.array = A ([[l, 2,3], [4,5,6]])
Print (a.var ()) results 2.91666666667 #
print (a.var (axis = 0) ) # Results [2.25 2.25 2.25]
Print (a.var (Axis =. 1)) # Results [0.66666667 0.66666667]
Standard deviation
function is the standard deviation std ().
STD () is equivalent to sqrt (mean (abs (x - x.mean ()) ** 2)), or the equivalent sqrt (x.var ()).
import numpy as np
np.array = A ([[l, 2,3], [4,5,6]])
Print (a.std ()) results 1.70782512766 #
print (a.std (axis = 0) ) # Results [for 1.5 for 1.5 for 1.5]
Print (a.std (Axis =. 1)) # Results [0.81649658 0.81649658]
The median
value refers to the sequence arranged in order of size, it ranked in the middle of that value, if there is an even number, the middle row is the average of the two numbers.
For example the sequence [5,2,6,4,2], arranged in order of size [2,2,4,5,6], in the middle row is four, so that the sequence of values is 4.
Another example is the sequence of [5,2,6,4,3,2], arranged in order of size [2,2,3,4,5,6], because there is an even number, the number is two in the middle row 3 4, so that the sequence of values is 3.5.
Median values are function (), call method numpy.median (x, [axis]), axis-axis direction can be specified, the default axis = None, to the number of all values.
import numpy as np
x = np.array([[1,2,3],[4,5,6]])
print (np.median (x)) # taking a median of all numbers
# Results
3.5
print (np.median (x, axis = 0)) # median dimension taken along a first direction
# Results
[2.5 3.5 4.5]
print (np.median (x, axis = 1)) # median dimension taken along a second direction
# Results
[2.5.3]
Summing
SUM is the sum function matrix (), can rows, columns, or entire matrix summation
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print (a.sum ()) # summation of the entire matrix of
results # 21
print (a.sum (axis = 0) ) # summation of the row direction
# Results [579]
print (a.sum (axis = 1) ) # column direction summation
# Results [615]
Accumulation and
Accumulation means and a position before the position (including the location) and all elements.
For example the sequence [1,2,3,4,5], which is accumulated and [1,3,6,10,15], i.e. the first element is 1, the second element is 1 + 2 = 3, ... ..., for the fifth element 1 + 2 + 3 + 4 + 5 = 15.
Function matrix and are accumulated cumsum (), can rows, columns, or the entire matrix and accumulation.
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print (a.cumsum ()) # demand and the accumulation of the entire matrix
# Results [136,101,521]
print (a.cumsum (axis = 0) ) # and the accumulation of the direction and the row
# Results
[[123]
[579]]
print (a.cumsum (axis = 1) ) # request and the column direction of accumulation
# Results
[[136]
[4915]]
------------------ ---
author: ShellCollector
source: CSDN
original: https: //blog.csdn.net/jacke121/article/details/76146884
copyright: This article is a blogger original article, reproduced, please attach Bowen link!