您好,登錄后才能下訂單哦!
如下所示:
from numpy import * import numpy as np import matplotlib.pyplot as plt plt.close() fig=plt.figure() plt.grid(True) plt.axis([0,10,0,8]) #列出數據 point=[[1,2],[2,3],[3,6],[4,7],[6,5],[7,3],[8,2]] plt.xlabel("X") plt.ylabel("Y") #用于求出矩陣中各點的值 XSum = 0.0 X2Sum = 0.0 X3Sum = 0.0 X4Sum = 0.0 ISum = 0.0 YSum = 0.0 XYSum = 0.0 X2YSum = 0.0 #列出各點的位置 for i in range(0,len(point)): xi=point[i][0] yi=point[i][1] plt.scatter(xi,yi,color="red") show_point = "("+ str(xi) +","+ str(yi) + ")" plt.text(xi,yi,show_point) XSum = XSum+xi X2Sum = X2Sum+xi**2 X3Sum = X3Sum + xi**3 X4Sum = X4Sum + xi**4 ISum = ISum+1 YSum = YSum+yi XYSum = XYSum+xi*yi X2YSum = X2YSum + xi**2*yi # 進行矩陣運算 # _mat1 設為 mat1 的逆矩陣 m1=[[ISum,XSum, X2Sum],[XSum, X2Sum, X3Sum],[X2Sum, X3Sum, X4Sum]] mat1 = np.matrix(m1) m2=[[YSum], [XYSum], [X2YSum]] mat2 = np.matrix(m2) _mat1 =mat1.getI() mat3 = _mat1*mat2 # 用list來提取矩陣數據 m3=mat3.tolist() a = m3[0][0] b = m3[1][0] c = m3[2][0] # 繪制回歸線 x = np.linspace(0,10) y = a + b*x + c*x**2 plt.plot(x,y) show_line = "y="+str(a)+"+("+str(b)+"x)"+"+("+str(c)+"x2)"; plt.title(show_line) plt.show()
以上這篇Python實現二維曲線擬合的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。