這篇文章給大家分享的是有關(guān)python怎么實現(xiàn)三維擬合的方法的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
從事綿陽電信機房機柜租用,服務器租用,云主機,網(wǎng)站空間,域名與空間,CDN,網(wǎng)絡(luò)代維等服務。如下所示:
from matplotlib import pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = Axes3D(fig) #列出實驗數(shù)據(jù) point=[[2,3,48],[4,5,50],[5,7,51],[8,9,55],[9,12,56]] plt.xlabel("X1") plt.ylabel("X2") #表示矩陣中的值 ISum = 0.0 X1Sum = 0.0 X2Sum = 0.0 X1_2Sum = 0.0 X1X2Sum = 0.0 X2_2Sum = 0.0 YSum = 0.0 X1YSum = 0.0 X2YSum = 0.0 #在圖中顯示各點的位置 for i in range(0,len(point)): x1i=point[i][0] x2i=point[i][1] yi=point[i][2] ax.scatter(x1i, x2i, yi, color="red") show_point = "["+ str(x1i) +","+ str(x2i)+","+str(yi) + "]" ax.text(x1i,x2i,yi,show_point) ISum = ISum+1 X1Sum = X1Sum+x1i X2Sum = X2Sum+x2i X1_2Sum = X1_2Sum+x1i**2 X1X2Sum = X1X2Sum+x1i*x2i X2_2Sum = X2_2Sum+x2i**2 YSum = YSum+yi X1YSum = X1YSum+x1i*yi X2YSum = X2YSum+x2i*yi # 進行矩陣運算 # _mat1 設(shè)為 mat1 的逆矩陣 m1=[[ISum,X1Sum,X2Sum],[X1Sum,X1_2Sum,X1X2Sum],[X2Sum,X1X2Sum,X2_2Sum]] mat1 = np.matrix(m1) m2=[[YSum],[X1YSum],[X2YSum]] mat2 = np.matrix(m2) _mat1 =mat1.getI() mat3 = _mat1*mat2 # 用list來提取矩陣數(shù)據(jù) m3=mat3.tolist() a0 = m3[0][0] a1 = m3[1][0] a2 = m3[2][0] # 繪制回歸線 x1 = np.linspace(0,9) x2 = np.linspace(0,12) y = a0+a1*x1+a2*x2 ax.plot(x1,x2,y) show_line = "y="+str(a0)+"+"+str(a1)+"x1"+"+"+str(a2)+"x2" plt.title(show_line) plt.show()
感謝各位的閱讀!關(guān)于“python怎么實現(xiàn)三維擬合的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!