In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what is the method of python matrix transposition". In the daily operation, I believe that many people have doubts about what the method of python matrix transposition is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the method of python matrix transposition?" Next, please follow the editor to study!
The ordinary least square method of linear regression. The ordinary least square method (OLS) is a method to estimate parameter β in simple linear regression, X β = y, where X is the characteristic matrix and y is the dependent variable (or target). By minimizing the difference between the dependent variables observed in a given data set and the dependent variables predicted by the linear function.
So without saying much, let's take a look at the following code:
Import numpy as np
Import numpy as np
Xn, xp = [2jue 2]
Array = []
For i in range (2):
Array = [1, 0, 0, 2]
X = np.array (array) .reshape (2,2)
Y = np.array ([2prime3])
T means transpose the matrix a = np.dot (np.dot (np.linalg.inv (np.dot (x.T, x)), x.T), y) .round (2)
Print (a)
# the output is: [2.1.5]
In the code "a = np.dot (np.dot (np.linalg.inv (np.dot (x.T, x)), x.T), y) .round (2)", the dot function is matrix multiplication, so for "*" it is expressed as element-by-element multiplication. We can look at the following code:
The dot function uses:
A = np.array ([[1djue 1], [1mai 1]]) b = np.array ([1rect 0], [Omel 1]]) np.dot (aMague b) # matrix multiplication
Results: array ([[1pd1], [1pd1])
"*" use:
A = np.array ([[1djue 1], [1pr 1]]) b = np.array ([1pr 0], [OJI 1]) c = multiply the corresponding elements of astatb #
Results: array ([[1d0], [0JEI]])
There is a difference between the use of dot and "*" after we implement it in code.
Np.linalg.inv () means matrix inversion. Besides, there are np.linalg.det () matrix determinant, np.linalg.norm () norm and np.linalg.eigh matrix eigenvector.
The code of the matrix transposition method is shared as follows:
# Python's matrix transpose matrix = [1Jing 2je 3jue 4], [5Jing 6je 7je 8], [9Jing 10je 11je 12]] def printmatrix (m): for ele in m: for i in ele: print ("% 2d"% iGrad end = ") print () # 1, transpose def transformMatrix (m) using the characteristics of meta-ancestor: # here cleverly according to the number of columns passed by meta-ancestor m The number of rows of r is generated r = [[] for i in m [0]] for ele in m: for i in range (len (ele)): # [emphasis]: here the ele row I column of m is used and the value is appended to the line I of r Ingenious use of I r [I] .append (Elel [I]) # printmatrix (r) # to see how the array is assigned If you do not need to comment out # print ("*" * 20) # print separator return r # 2, use zip function to generate transpose matrix def transformMatrix1 (m): return zip (* m) # 3, Use the transpose method def transformMatrix2 (m): import numpy return numpy.transpose (m) of the numpy module. Tolist () print ("first method result presentation") printmatrix (transformMatrix (matrix)) print ("second method result presentation") printmatrix (transformMatrix1 (matrix)) print ("concise code presentation of the second method") printmatrix (zip (* matrix)) # for code simplicity You can print print ("result presentation of the third method") printmatrix (transformMatrix2 (matrix)) directly without the transformMatrix1 function.
The running results are as follows:
The results of the first method show 1 5 9 2 6 10 3 7 11 4 8 12 the second method shows 1 5 9 2 6 10 3 7 11 4 8 12 the concise code of the second method shows 1 5 9 2 6 10 3 7 11 4 8 12 the results of the third method shows 1 5 9 2 6 10 3 7 11 4 8 12, this is the end of the study on "what is the method of python matrix transposition". I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.