In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
1, get the number of rows and rows of the matrix
Import numpyasnp
# create two-dimensional naaray objects
A=np.array ([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])
Print (a.shape) # returns a shape, which is a tuple
Print (a.shape [0]) # gets the number of rows. Imagine if it is multidimensional, so you will understand why it is [0].
Print (a.shape [1]) # gets the number of columns
2, the interception of matrix
Importnumpyasnp
# create two-dimensional naaray objects
A=np.array ([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])
Print (a [0:1]) # I don't quite understand here, look below
Print (a [1JI 2 4]) # returns [89] and returns 2-3 numbers in the second row
Print (a [1BI 2 5]) # returns [8910] to prove the number of 2-4 taken from the second row.
3, intercept according to conditions
Importnumpyasnp
# create two-dimensional naaray objects
A=np.array ([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])
B = a [a > 6] # intercepts elements greater than 6 in matrix a, and the range is an one-dimensional array
Print (b)
Print (a > 6) # in fact, the Boolean statement first generates a Boolean matrix, and passes the Boolean matrix into [] (square brackets) to intercept
4. Elements that meet certain conditions become specific values
Importnumpyasnp
# create two-dimensional naaray objects
A=np.array ([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])
Print (a)
# the matrix after zero clearance is greater than 6.
A [a > 6] = 0
Print (a)
The results are as follows:
[[12345]
[678910]]
[[12345]
[60000]]
5, Matrix merging
Importnumpyasnp
A1=np.array ([[1, 2], [3, 4]])
A2=np.array ([5, 5, 6], [7, 8])
Print (np.hstack ([A1 Magi a2])) # Horizontal
Print (A1) # so I know why it's 125pr 2478.
Print (np.vstack ((A1 Magi a2) # vertical
Np.concatenate ((A1 Magi a2), axis=0) # is equivalent to np.vstack ((A1 Magi a2))
Np.conca
6, create the matrix through the function
Numpy comes with functions to create narray objects, which can easily create regular matrices that are commonly used.
Importnumpyasnp
A=np.arange (10) # defaults from 0 to 10 (excluding 10) with a step size of 1
Print (a) # returns [0123456789]
A1=np.arange (5 and 10) # starts from 5 to 10 (excluding 10) with a step size of 1
Print (A1) # returns [56789]
A2=np.arange (5 and 20) # starts from 5 to 20 (excluding 20) with a step size of 2
Print (a2) # returns [5791113151719]
7,linspace
Create a specified number of evenly spaced sequences and actually generate an arithmetic sequence
Importnumpyasnp
A=np.linspace (0Jing 10 and 7) # generates an arithmetic sequence with 7 numbers, the first bit is 0 and the last bit is 10.
Print (a)
8,logspace
Logspace is used to generate proportional sequences.
Importnumpyasnp
The first bit of a=np.logspace generation is 10 minutes 0, and the last bit is 10 minutes 4, a proportional sequence with five numbers.
Print (a)
9,ones,zeros,eye,empty
Ones creates all-1 Matrix
Zeros creates all-zero matrix
Eye creates a unit matrix
Empty creates an empty matrix (actual value)
Importnumpyasnp
One=np.ones ((3d4)) # create an all-1 matrix of 3D4
Print (one)
Zero=np.zeros ((3d4)) # create an all-zero matrix of 3D4
Print (zero)
Eye=np.eye (5) # create a fifth-order identity matrix
Print (eye)
Empty=np.empty ((3p4)) # create an empty matrix of 3D4 (actual value)
Print (empty)
10fromstring-- get the character ASCII code
The fromstring () method can convert a string into a ndarray object, which is useful when you need to digitize a string. You can get the ascii code sequence of the string and convert it to the Aska code of the corresponding character.
Importnumpyasnp
A = "abcdef"
B=np.fromstring (a dtypewriter np.int8) # specify dtype as np.int8 because a character is 8 bits
Print (b) # returned [979899100101102]
11,fromfunction
The fromfunction () method generates the elements of the matrix based on the row number and column number of the matrix.
For example, create a matrix in which each element is the sum of row and column numbers.
Importnumpyasnp
Deffunc (iMagnej):
Returni+j # here can also be something else, such as adding another 9
A=np.fromfunction (func, (5, 6))
# this is how the function is defined. The first parameter is the specified function, and the second parameter is list list or tuple tuple, indicating the size of the matrix.
Print (a)
12, commonly used matrix function
Similarly, there are a number of functions defined in numpy that can be used to act on each element of the matrix.
The numpy module, import numpy asnp, is imported by default in the table.
An is the ndarray object.
Np.sin (a) takes the sine of each element in the matrix a, sin (x)
Np.cos (a) cosines each element in matrix a, cos (x)
Np.tan (a) tangent to each element in matrix a, tan (x)
Np.arcsin (a) inverts the sine of each element in the matrix a, arcsin (x)
Np.arccos (a) takes the inverse cosine of each element in the matrix a, arccos (x)
Np.arctan (a) takes the inverse tangent of each element in the matrix a, arctan (x)
Np.exp (a) takes an exponential function for each element in the matrix a, ex.
Np.sqrt (a) opens the root sign √ x for each element in matrix a
Importnumpyasnp
A=np.array ([[1, 2, 3], [4, 5, 6])
Print (np.sin (a))
# result
[[0.841470980.909297430.14112001]
[- 0.7568025-0.95892427-0.2794155]]
Print (np.arcsin (a))
# result
# RuntimeWarning:invalidvalueencounteredinarcsin
Print (np.arcsin (a))
[[1.57079633nannan] # nan means not a number
[nannannan]]
13, matrix multiplication (point multiplication)
Condition: the number of columns of the first matrix is equal to the number of rows of the second matrix, and the function is dot.
Importnumpyasnp
A1=np.array ([[1, 2, 3], [4, 5, 6])
A2=np.array ([[1, 2], [3, 4], [5, 6]])
Ifa1.shape [1] = = a2.shape [0]: # if the number of columns equals the number of rows
Print (a1.dot (a2))
14, transpose of matrix
Transpose function
Importnumpyasnp
A=np.array ([[1, 2, 3], [4, 5, 6])
Print (a.transpose ())
15, the inverse of the matrix
To find the inverse of the matrix, it is necessary to import numpy.linalg and use the inv function of linalg to find the inverse.
The condition for finding the inverse of a matrix is that the number of rows and columns of the matrix is the same.
Importnumpyasnp
Importnumpy.linalgaslg
A=np.array ([[1, 2, 3], [4, 5, 6], [7, 8, 9])
Print (lg.inv (a))
# result
[[- 4.50359963e+159.00719925e+15-4.50359963e+15]
[9.00719925e+15-1.80143985e+169.00719925e+15]
[- 4.50359963e+159.00719925e+15-4.50359963e+15]]
A=np.eye (3) # Unit Matrix of order 3
Print (lg.inv (a)) # the inverse of the unit matrix is himself
# result
[[1.0.0.]
[0.1.0.]
[0.0.1.]]
16, matrix information acquisition (such as average)
The functions that obtain the maximum and minimum values of elements in a matrix are max and min, respectively, and you can get the maximum and minimum values of the entire matrix, row, or column.
Importnumpyasnp
A=np.array ([[1rem 3rem 9], [1pr 5je 6])
Print (a.max ())
Print (a.min ())
Print (a.max (axis=0)) # [456] the maximum (minimum) value of axis=0 row direction, that is, the maximum (minimum) value of each column
Print (a.min (axis=1)) # [14] axis=1 column direction maximum (minimum) value
# to get the location of the maximum and minimum element, you can get it through the argmax function
Print (a.argmax (axis=1))
17, average mean ()
The average value of the elements in the matrix can be obtained through the function mean (). Similarly, the average of the entire matrix, row, or column can be obtained
Importnumpyasnp
A=np.array ([[1, 2, 3], [4, 5, 6])
Print (a.mean ()) # result: 3.5
# similarly, you can specify the direction in which to get the average through the keyword axis parameter
Print (a.mean (axis=0)) # results [2.53.54.5]
Print (a.mean (axis=1)) # results [2.5.]
18, variance var ()
Importnumpyasnp
A=np.array ([[1, 2, 3], [4, 5, 6])
Print (a.var ())
Print (a.var (axis=0))
Print (a.var (axis=1))
19, standard deviation std ()
Importnumpyasnp
A=np.array ([[1, 2, 3], [4, 5, 6])
Print (a.std ())
Print (a.std (axis=0))
Print (a.std (axis=1))
20, median median ()
The calling method is numpy.median (x, [axis]). Axis can specify the axis direction, default is axis=none, and take the median value for all numbers.
Importnumpyasnp
X=np.array ([[1, 2, 3], [4, 5, 6])
Print (np.median (x)) # takes the median of all numbers
Print (np.median (xQuery axisym0)) # take the middle value along the first dimensional direction
Print (np.median (xQuery axisym1)) # take the middle value along the second dimension
21, summing sum ()
Importnumpyasnp
A=np.array ([[1, 2, 3], [4, 5, 6])
Print (a.sum ()) # sums the whole matrix
Print (a.sum (axis=0)) # summation of row directions
Print (a.sum (axis=1)) # summation of column directions
22, cumulative and cussum ()
The cumulative sum of a location refers to the sum of all elements before (including that location).
For example, the sequence [1, 2, 3, 4, and 5] has a cumulative sum of [1, 3, 6, 10, 15], that is, the first element is 1, the second element is 1, and the second element is 1, 2, and 3. The fifth element is 1 / 2 / 3 / 4 / 5 / 15.
The function of the cumulative sum of a matrix is cumsum (), which can accumulate the sum of rows, columns, or the entire matrix.
Importnumpyasnp
A=np.array ([[1, 2, 3], [4, 5, 6])
Print (a.cumsum ()) # accumulates the whole matrix
Print (a.cumsum (axis=0)) # accumulate the sum of opposite directions
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.