日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

拟合线性函数的几种方法

發(fā)布時間:2024/3/24 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 拟合线性函数的几种方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

基于最小二乘法擬合函數(shù)

A = np.vander(x, 2) #生成矩陣(數(shù)據(jù),輸出列數(shù)) C = np.diag(yerr * yerr) ATA = np.dot(A.T, A / (yerr ** 2)[:, None]) #獲取乘積 cov = np.linalg.inv(ATA) w = np.linalg.solve(ATA, np.dot(A.T, y / yerr ** 2))

np.diag(array) 中,array是一個1維數(shù)組時,結(jié)果形成一個以一維數(shù)組為對角線元素的矩陣,array是一個二維矩陣時,結(jié)果輸出矩陣的對角線元素。
np.linalg.inv():矩陣求逆
numpy.linalg.solve(a, b)以矩陣形式解一個線性矩陣方程,或線性標(biāo)量方程組。a為系數(shù)矩陣,b為縱坐標(biāo)或參數(shù)值
其中Y為Y的列矩陣

A為x的二維矩陣(結(jié)構(gòu)解釋在之后)

C為方差的對角矩陣

設(shè)[m b]T =X
則 AX=Y,即 mx+b=y
ATC-1AX=ATC-1Y
所以 X=[ATC-1A]-1 ATC-1Y
代碼中的ATA即ATC-1A
詳情參考:https://arxiv.org/abs/1008.4686

基于pyplot擬合函數(shù)

def fund(x,a,b):b=-bi=np.log10(x+a)n=b*ireturn n x=[] x = np.array(x) y = [] #使y變?yōu)閿?shù)組格式 y = np.array(y) #求lgy #y=np.log10(y) #y的誤差 yerr =[] yerr = np.array(yerr) #求lgyerr #yerr=np.log10(yerr) #擬合函數(shù),其中popt是參數(shù)結(jié)果集,pcov是方差集,curve_fit參數(shù)中,found是目標(biāo)函數(shù)格式,sigma是y的誤差值 popt, pcov = curve_fit(fund, x, y,sigma=yerr) #y2為求出擬合函數(shù)預(yù)測的y值,方便畫圖對比評估擬合效果 y2 = [fund(i,popt[0],popt[1]) for i in x] plt.axes(yscale = "log") plt.axes(xscale = "log") plt.loglog(x, y1,"k", marker='.') plt.loglog(x, y3,"g", 'r--') plt.show() print(popt) print(pcov)

基于神經(jīng)網(wǎng)絡(luò)擬合函數(shù)

缺點:無法求出具體參數(shù)值

x= x = np.array(x) y = y = np.log10(np.array(y)) X = np.reshape(x, (25, 1)) Y = np.reshape(y, (25, 1)) with tf.name_scope('Input'):xin = tf.placeholder('float', shape=[None, 1])yin = tf.placeholder('float', shape=[None, 1]) with tf.name_scope('Layer'):W1 = tf.Variable(tf.random_normal([1, 4], mean=1, stddev=0.2), name='w1')b1 = tf.Variable(tf.constant(0.1, shape=[4], dtype=tf.float32), name='b1')out1 = tf.nn.sigmoid(tf.add(tf.matmul(xin, W1), b1)) with tf.name_scope('Output'):W2 = tf.Variable(tf.random_normal([4, 1], mean=1, stddev=0.2), name='w2')b2 = tf.Variable(tf.constant(0.1, shape=[1], dtype=tf.float32), name='b2')out2 = tf.add(tf.matmul(out1, W2), b2) with tf.name_scope('Evaluate'):Loss = tf.reduce_mean(tf.square(out2 - yin))trainstep = tf.train.GradientDescentOptimizer(0.1).minimize(Loss) # %% Train with tf.Session() as sess:step = 0sess.run(tf.global_variables_initializer())while step < 5000:Tstep = sess.run(trainstep, feed_dict={xin: X, yin: Y})if step % 3 == 0:loss = sess.run(Loss, feed_dict={xin: X, yin: Y})print('Train step: ', Tstep)print('Loss: ', loss)step += 1# print(sess.run(W1))# print(sess.run(b1))# print(sess.run(W2))# print(sess.run(b2))# TestY_ = sess.run(out2, feed_dict={X_in: X})# print(sess.run(feed_dict={X_in: X})) plt.plot(X, Y, X, Y_) plt.show()

總結(jié)

以上是生活随笔為你收集整理的拟合线性函数的几种方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。