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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

1.7 程序示例--多项式回归-机器学习笔记-斯坦福吴恩达教授

發布時間:2025/4/5 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1.7 程序示例--多项式回归-机器学习笔记-斯坦福吴恩达教授 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

程序示例–多項式回歸

下面,我們有一組溫度(temperature)和實驗產出量(yield)訓練樣本,該數據由博客 Polynomial Regression Examples 所提供:

temperatureyield
503.3
502.8
502.9
702.3
702.6
702.1
802.5
802.9
802.4
903.0
903.1
902.8
1003.3
1003.5
1003.0

我們先通過如下預測函數進行訓練:
h(θ)=θ0+θ1xh(θ)=θ_0+θ_1xh(θ)=θ0?+θ1?x

# coding: utf-8 # linear_regression/test_temperature_normal.py import regression from matplotlib import cm from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import matplotlib.ticker as mtick import numpy as npif __name__ == "__main__":X, y = regression.loadDataSet('data/temperature.txt');m,n = X.shapeX = np.concatenate((np.ones((m,1)), X), axis=1)rate = 0.0001maxLoop = 1000epsilon =0.01result, timeConsumed = regression.bgd(rate, maxLoop, epsilon, X, y)theta, errors, thetas = result# 繪制擬合曲線fittingFig = plt.figure()title = 'bgd: rate=%.3f, maxLoop=%d, epsilon=%.3f \n time: %ds'%(rate,maxLoop,epsilon,timeConsumed)ax = fittingFig.add_subplot(111, title=title)trainingSet = ax.scatter(X[:, 1].flatten().A[0], y[:,0].flatten().A[0])xCopy = X.copy()xCopy.sort(0)yHat = xCopy*thetafittingLine, = ax.plot(xCopy[:,1], yHat, color='g')ax.set_xlabel('temperature')ax.set_ylabel('yield')plt.legend([trainingSet, fittingLine], ['Training Set', 'Linear Regression'])plt.show()# 繪制誤差曲線errorsFig = plt.figure()ax = errorsFig.add_subplot(111)ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.4f'))ax.plot(range(len(errors)), errors)ax.set_xlabel('Number of iterations')ax.set_ylabel('Cost J')plt.show()

得到的擬合圖像為:

接下來,我們使用了多項式回歸,添加了 2 階項:

h(θ)=θ0+θ1x+θ2x2h(θ)=θ_0+θ_1x+θ_2x^2h(θ)=θ0?+θ1?x+θ2?x2

因為 xxxx2x2x2 數值差異較大,所以我們會先做一次特征標準化,將各個特征縮放到 [?1,1] 區間

z=x?μδz=\frac {x?μ}δz=δx?μ?

# coding: utf-8 # linear_regression/test_temperature_polynomial.pyimport regression import matplotlib.pyplot as plt import matplotlib.ticker as mtick import numpy as npif __name__ == "__main__":srcX, y = regression.loadDataSet('data/temperature.txt');m,n = srcX.shapesrcX = np.concatenate((srcX[:, 0], np.power(srcX[:, 0],2)), axis=1)# 特征縮放X = regression.standardize(srcX.copy())X = np.concatenate((np.ones((m,1)), X), axis=1)rate = 0.1maxLoop = 1000epsilon = 0.01result, timeConsumed = regression.bgd(rate, maxLoop, epsilon, X, y)theta, errors, thetas = result# 打印特征點fittingFig = plt.figure()title = 'polynomial with bgd: rate=%.2f, maxLoop=%d, epsilon=%.3f \n time: %ds'%(rate,maxLoop,epsilon,timeConsumed)ax = fittingFig.add_subplot(111, title=title)trainingSet = ax.scatter(srcX[:, 1].flatten().A[0], y[:,0].flatten().A[0])print theta# 打印擬合曲線xx = np.linspace(50,100,50)xx2 = np.power(xx,2)yHat = []for i in range(50):normalizedSize = (xx[i]-xx.mean())/xx.std(0)normalizedSize2 = (xx2[i]-xx2.mean())/xx2.std(0)x = np.matrix([[1,normalizedSize, normalizedSize2]])yHat.append(regression.h(theta, x.T))fittingLine, = ax.plot(xx, yHat, color='g')ax.set_xlabel('Yield')ax.set_ylabel('temperature')plt.legend([trainingSet, fittingLine], ['Training Set', 'Polynomial Regression'])plt.show()# 打印誤差曲線errorsFig = plt.figure()ax = errorsFig.add_subplot(111)ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.2e'))ax.plot(range(len(errors)), errors)ax.set_xlabel('Number of iterations')ax.set_ylabel('Cost J')plt.show()

得到的擬合曲線更加準確:

總結

以上是生活随笔為你收集整理的1.7 程序示例--多项式回归-机器学习笔记-斯坦福吴恩达教授的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。