In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to add fitting line and display fitting equation and R square in python scatter chart". In daily operation, it is believed that many people have doubts about how to add fitting line and show fitting equation and R square problem in python scatter chart. The editor consulted all kinds of data and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the doubt of "how to add fitting line and display fitting equation and R square in python scatter chart"! Next, please follow the editor to study!
The polyfit () function can use the least square method to fit some points into a curve.
Numpy.polyfit (x, y, deg, rcond=None, full=False, w=None, cov=False) # x: Abscissa to fit point # y: ordinate to fit point # deg: degrees of freedom. For example, if the degree of freedom is 2, then the fitted curve is a quadratic function, and the degree of freedom is 3, and the fitted curve is a cubic function.
First of all, let's construct the scattered points that need to be fitted.
# solve axis scale negative garbled plt.rcParams ['axes.unicode_minus'] = False # solve the problem of Chinese garbled plt.rcParams [' font.sans-serif'] = ['Simhei'] import numpy as np import matplotlib.pyplot as plt x = np.arange (- 1,1,0.02) y = 2 * np.sin (x * 2.3) + np.random.rand (len (x))
Then print it and have a look
Plt.scatter (x, y) plt.show ()
Then use polyfit function to fit these points into a cubic curve.
Parameter = np.polyfit (x, y, 3)
The output is the parameters of the cubic equation, and we can piece together the equation as follows
Y2 = parameter [0] * x * * 3 + parameter [1] * x * * 2 + parameter [2] * x + parameter [3]
Print the fitted result.
Plt.scatter (x, y) plt.plot (x, y2, color='g') plt.show ()
You can also use the poly1d () function to help us piece together the equation, and the result is the same.
P = np.poly1d (parameter) plt.scatter (x, y) plt.plot (x, p (x), color='g') plt.show ()
Evaluation indicator R
Least square fitting of arbitrary function by two-dimensional scattered points
Derivation of the relationship between correlation coefficient and R Party in least Squares
Among them
Using correlation coefficient Matrix to calculate R Square
Correlation = np.corrcoef (y, y2) [0meme 1] # correlation coefficient correlation**2 # R
First, let's take a look at the output of the poly1d function.
P = np.poly1d (parameter,variable='x') print (p)
Here the result is output to two lines, but it is very inconvenient to output to two lines
Try to write your own function to make the output to one line.
Parameter= [- 2.44919641,-0.01856314, 4.12010434, 0.47296566] # coefficient aa='' deg=3 for i in range (deg+1): bb=round (parameter [I] 2) # bb is the I term coefficient if bb > = 0: if items0: bb=str (bb) else: bb=' +'+ str (bb) else: bb=''+ str (bb) if deg==i: aaaa=aa+bb else: aaaa=aa+bb+' x ^'+ str (deg-i) print (aa)
Encapsulated into a function
Def Curve_Fitting: parameter = np.polyfit (xrecedence) # fitting deg degree polynomial p = np.poly1d (parameter) # fitting deg degree polynomial aa='' # equation splicing-for i in range (deg+1): bb=round (parameter [I] 2) if bb > 0: if iTunes 0: bb=str (bb) else: bb='+'+str (bb) else: bb=str (bb) if deg==i: aaaa=aa+bb else: aaaa=aa+bb+' x ^'+ str (deg-i) # equation splicing- -plt.scatter (x Y) # Raw data scatter plot plt.plot (x, p (x), color='g') # draw a fitting curve # plt.text (- 1d0) plt.legend ([aa,round (np.corrcoef (y) P (x)) [0jue 1] * * 2jue 2)) # the spliced equation and R side are put into the legend plt.show () # print ('curve equation is:', aa) # print ('r ^ 2 is:', round (np.corrcoef (y, p (x)) [0jue 1] * * 2)
Redrawing using encapsulated functions
Curve_Fitting (xmeme yp3)
At this point, the study of "how to add fitting lines and display fitting equations and R squares in python scatter chart" is over. 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.