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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

模型训练 准确率下降_手写批量线性回归算法:在Python3中梯度下降方法实现模型训练

發布時間:2025/3/21 python 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 模型训练 准确率下降_手写批量线性回归算法:在Python3中梯度下降方法实现模型训练 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在這篇文章中,我們將看一個使用NumPy作為數據處理庫的Python3編寫的程序,來了解如何實現使用梯度下降法的(批量)線性回歸。

? 我將逐步解釋代碼的工作原理和代碼的每個部分的工作原理。

? 我們將使用此公式計算梯度。

? 在此,x(i)向量是一個點,其中N是數據集的大小。 n(eta)是我們的學習率。 y(i)向量是目標輸出。 f(x)向量是定義為f(x)= Sum(w * x)的回歸線性函數,這里sum是sigma函數。 另外,我們將考慮初始偏差w0 = 0并使得x0 =1。所有權重均初始化為0。

? 在此方法中,我們將平方誤差總和用作損失函數。

? 除了將SSE初始化為零外,我們將在每次迭代中記錄SSE的變化,并將其與在程序執行之前提供的閾值進行比較。 如果SSE低于閾值,程序將退出。

? 在該程序中,我們從命令行提供了三個輸入。 他們是:

threshold — 閾值,在算法終止之前,損失必須低于此閾值。

data — 數據集的位置。

learningRate — 梯度下降法的學習率。

? 因此,該程序的啟動應該是這樣的:

python3linearregr.py — datarandom.csv — learningRate 0.0001 — threshold 0.0001

? 在深入研究代碼之前我們確定最后一件事,程序的輸出將如下所示:

iteration_number,weight0,weight1,weight2,...,weightN,sum_of_squared_errors

? 該程序包括6個部分,我們逐個進行查看。

導入模塊

import argparse # to read inputs from command lineimport csv # to read the input data set fileimport numpy as np # to work with the data set

初始化部分

# initialise argument parser and read argumentsfrom command line with the respective flags and then call the main() functionif __name__ == '__main__': parser = argparse.ArgumentParser()parser.add_argument("-d", "--data", help="Data File")parser.add_argument("-l", "--learningRate", help="Learning Rate") parser.add_argument("-t", "--threshold", help="Threshold") main()

main函數部分

defmain():args = parser.parse_args()file, learningRate, threshold = args.data, float(args.learningRate), float(args.threshold) # save respective command line inputs into variables# read csv file and the last column is the target output and is separated from the input (X) as Ywith open(file) as csvFile:reader = csv.reader(csvFile, delimiter=',')X = []Y = []for row in reader:X.append([1.0] + row[:-1])Y.append([row[-1]])# Convert data points into float and initialise weight vector with 0s.n = len(X)X = np.array(X).astype(float)Y = np.array(Y).astype(float)W = np.zeros(X.shape[1]).astype(float)# this matrix is transposed to match the necessary matrix dimensions for calculating dot productW = W.reshape(X.shape[1], 1).round(4)# Calculate the predicted output valuef_x = calculatePredicatedValue(X, W)# Calculate the initial SSEsse_old = calculateSSE(Y, f_x)outputFile = 'solution_' + 'learningRate_' + str(learningRate) + '_threshold_' + str(threshold) + '.csv''''Output file is opened in writing mode and the data is written in the format mentioned in the post. After thefirst values are written, the gradient and updated weights are calculated using the calculateGradient function.An iteration variable is maintained to keep track on the number of times the batch linear regression is executedbefore it falls below the threshold value. In the infinite while loop, the predicted output value is calculated again and new SSE value is calculated. If the absolute difference between the older(SSE from previous iteration) and newer(SSE from current iteration) SSE is greater than the threshold value, then above process is repeated.The iteration is incremented by 1 and the current SSE is stored into previous SSE. If the absolute difference between the older(SSE from previous iteration) and newer(SSE from current iteration) SSE falls below the threshold value, the loop breaks and the last output values are written to the file.'''with open(outputFile, 'w', newline='') as csvFile:writer = csv.writer(csvFile, delimiter=',', quoting=csv.QUOTE_NONE, escapechar='')writer.writerow([*[0], *["{0:.4f}".format(val) for val in W.T[0]], *["{0:.4f}".format(sse_old)]])gradient, W = calculateGradient(W, X, Y, f_x, learningRate)iteration = 1whileTrue:f_x = calculatePredicatedValue(X, W)sse_new = calculateSSE(Y, f_x)if abs(sse_new - sse_old) > threshold:writer.writerow([*[iteration], *["{0:.4f}".format(val) for val in W.T[0]], *["{0:.4f}".format(sse_new)]])gradient, W = calculateGradient(W, X, Y, f_x, learningRate)iteration += 1sse_old = sse_newelse:breakwriter.writerow([*[iteration], *["{0:.4f}".format(val) for val in W.T[0]], *["{0:.4f}".format(sse_new)]])print("Output File Name: " + outputFile

? main函數的流程如下所示:

  • 將相應的命令行輸入保存到變量中
  • 讀取CSV文件,最后一列是目標輸出,與輸入(存儲為X)分開并存儲為Y
  • 將數據點轉換為浮點初始化權重向量為0s
  • 使用calculatePredicatedValue函數計算預測的輸出值
  • 使用calculateSSE函數計算初始SSE
  • 輸出文件以寫入模式打開,數據以文章中提到的格式寫入。寫入第一個值后,使用calculateGradient函數計算梯度和更新的權重。進行變量迭代以確定批線性回歸在損失函數低于閾值之前執行的次數。在無限while循環中,再次計算預測的輸出值,并計算新的SSE值。如果舊的(來自先前迭代的SSE)和較新的(來自當前迭代的SSE)之間的絕對差大于閾值,則重復上述過程。迭代次數增加1,當前SSE被存儲到先前的SSE中。如果較舊的(上一次迭代的SSE)和較新的(當前迭代的SSE)之間的絕對差值低于閾值,則循環中斷,并將最后的輸出值寫入文件。
  • calculatePredicatedValue函數

    ? 在此,通過執行輸入矩陣X和權重矩陣W的點積來計算預測輸出。

    # dot product of X(input) and W(weights) as numpy matrices and returning the result which is the predicted outputdefcalculatePredicatedValue(X, W):f_x = np.dot(X, W)return f_x

    calculateGradient函數

    ? 使用文章中提到的第一個公式計算梯度并更新權重。

    defcalculateGradient(W, X, Y, f_x, learningRate):gradient = (Y - f_x) * Xgradient = np.sum(gradient, axis=0)temp = np.array(learningRate * gradient).reshape(W.shape)W = W + tempreturn gradient, W

    calculateSSE函數

    ? 使用上述公式計算SSE。

    defcalculateSSE(Y, f_x): sse = np.sum(np.square(f_x - Y)) return sse

    ? 現在,看完了完整的代碼。 讓我們看一下程序的執行結果。

    ? 這是輸出的樣子:00.00000.00000.00007475.31491-0.0940-0.5376-0.25922111.51052-0.1789-0.7849-0.3766880.69803-0.2555-0.8988-0.4296538.86384-0.3245-0.9514-0.4533399.80925-0.3867-0.9758-0.4637316.16826-0.4426-0.9872-0.4682254.51267-0.4930-0.9926-0.4699205.84798-0.5383-0.9952-0.4704166.69329-0.5791-0.9966-0.4704135.029310-0.6158-0.9973-0.4702109.389211-0.6489-0.9978-0.470088.619712-0.6786-0.9981-0.469771.794113-0.7054-0.9983-0.469458.163114-0.7295-0.9985-0.469147.120115-0.7512-0.9987-0.468938.173816-0.7708-0.9988-0.468730.926117-0.7883-0.9989-0.468525.054418-0.8042-0.9990-0.468320.297519-0.8184-0.9991-0.468116.443820-0.8312-0.9992-0.468013.321821-0.8427-0.9993-0.467810.792522-0.8531-0.9994-0.46778.743423-0.8625-0.9994-0.46767.083324-0.8709-0.9995-0.46755.738525-0.8785-0.9995-0.46744.649026-0.8853-0.9996-0.46743.766327-0.8914-0.9996-0.46733.051228-0.8969-0.9997-0.46722.471929-0.9019-0.9997-0.46722.002630-0.9064-0.9997-0.46711.622431-0.9104-0.9998-0.46711.314432-0.9140-0.9998-0.46701.064833-0.9173-0.9998-0.46700.862634-0.9202-0.9998-0.46700.698935-0.9229-0.9998-0.46690.566236-0.9252-0.9999-0.46690.458737-0.9274-0.9999-0.46690.371638-0.9293-0.9999-0.46690.301039-0.9310-0.9999-0.46680.243940-0.9326-0.9999-0.46680.197641-0.9340-0.9999-0.46680.160142-0.9353-0.9999-0.46680.129743-0.9364-0.9999-0.46680.105144-0.9374-0.9999-0.46680.085145-0.9384-0.9999-0.46680.069046-0.9392-0.9999-0.46680.055947-0.9399-1.0000-0.46670.045348-0.9406-1.0000-0.46670.036749-0.9412-1.0000-0.46670.029750-0.9418-1.0000-0.46670.024151-0.9423-1.0000-0.46670.019552-0.9427-1.0000-0.46670.015853-0.9431-1.0000-0.46670.012854-0.9434-1.0000-0.46670.010455-0.9438-1.0000-0.46670.008456-0.9441-1.0000-0.46670.006857-0.9443-1.0000-0.46670.005558-0.9446-1.0000-0.46670.004559-0.9448-1.0000-0.46670.003660-0.9450-1.0000-0.46670.002961-0.9451-1.0000-0.46670.002462-0.9453-1.0000-0.46670.001963-0.9454-1.0000-0.46670.001664-0.9455-1.0000-0.46670.001365-0.9457-1.0000-0.46670.001066-0.9458-1.0000-0.46670.000867-0.9458-1.0000-0.46670.000768-0.9459-1.0000-0.46670.000569-0.9460-1.0000-0.46670.000470-0.9461-1.0000-0.46670.0004最終程序

    import argparse import csv import numpy as npdefmain():args = parser.parse_args()file, learningRate, threshold = args.data, float(args.learningRate), float(args.threshold) # save respective command line inputs into variables# read csv file and the last column is the target output and is separated from the input (X) as Ywith open(file) as csvFile:reader = csv.reader(csvFile, delimiter=',')X = []Y = []for row in reader:X.append([1.0] + row[:-1])Y.append([row[-1]])# Convert data points into float and initialise weight vector with 0s.n = len(X)X = np.array(X).astype(float)Y = np.array(Y).astype(float)W = np.zeros(X.shape[1]).astype(float)# this matrix is transposed to match the necessary matrix dimensions for calculating dot productW = W.reshape(X.shape[1], 1).round(4)# Calculate the predicted output valuef_x = calculatePredicatedValue(X, W)# Calculate the initial SSEsse_old = calculateSSE(Y, f_x)outputFile = 'solution_' + 'learningRate_' + str(learningRate) + '_threshold_' + str(threshold) + '.csv''''Output file is opened in writing mode and the data is written in the format mentioned in the post. After thefirst values are written, the gradient and updated weights are calculated using the calculateGradient function.An iteration variable is maintained to keep track on the number of times the batch linear regression is executedbefore it falls below the threshold value. In the infinite while loop, the predicted output value is calculated again and new SSE value is calculated. If the absolute difference between the older(SSE from previous iteration) and newer(SSE from current iteration) SSE is greater than the threshold value, then above process is repeated.The iteration is incremented by 1 and the current SSE is stored into previous SSE. If the absolute difference between the older(SSE from previous iteration) and newer(SSE from current iteration) SSE falls below the threshold value, the loop breaks and the last output values are written to the file.'''with open(outputFile, 'w', newline='') as csvFile:writer = csv.writer(csvFile, delimiter=',', quoting=csv.QUOTE_NONE, escapechar='')writer.writerow([*[0], *["{0:.4f}".format(val) for val in W.T[0]], *["{0:.4f}".format(sse_old)]])gradient, W = calculateGradient(W, X, Y, f_x, learningRate)iteration = 1whileTrue:f_x = calculatePredicatedValue(X, W)sse_new = calculateSSE(Y, f_x)if abs(sse_new - sse_old) > threshold:writer.writerow([*[iteration], *["{0:.4f}".format(val) for val in W.T[0]], *["{0:.4f}".format(sse_new)]])gradient, W = calculateGradient(W, X, Y, f_x, learningRate)iteration += 1sse_old = sse_newelse:breakwriter.writerow([*[iteration], *["{0:.4f}".format(val) for val in W.T[0]], *["{0:.4f}".format(sse_new)]])print("Output File Name: " + outputFiledefcalculateGradient(W, X, Y, f_x, learningRate):gradient = (Y - f_x) * Xgradient = np.sum(gradient, axis=0)# gradient = np.array([float("{0:.4f}".format(val)) for val in gradient])temp = np.array(learningRate * gradient).reshape(W.shape)W = W + tempreturn gradient, WdefcalculateSSE(Y, f_x):sse = np.sum(np.square(f_x - Y))return ssedefcalculatePredicatedValue(X, W):f_x = np.dot(X, W)return f_xif __name__ == '__main__':parser = argparse.ArgumentParser()parser.add_argument("-d", "--data", help="Data File")parser.add_argument("-l", "--learningRate", help="Learning Rate")parser.add_argument("-t", "--threshold", help="Threshold")main()

    ? 這篇文章介紹了使用梯度下降法進行批線性回歸的數學概念。 在此,考慮了損失函數(在這種情況下為平方誤差總和)。 我們沒有看到最小化SSE的方法,而這是不應該的(需要調整學習率),我們看到了如何在閾值的幫助下使線性回歸收斂。

    ? 該程序使用numpy來處理數據,也可以使用python的基礎知識而不使用numpy來完成,但是它將需要嵌套循環,因此時間復雜度將增加到O(n * n)。 無論如何,numpy提供的數組和矩陣的內存效率更高。 另外,如果您喜歡使用pandas模塊,建議您使用它,并嘗試使用它來實現相同的程序。

    ? 希望您喜歡這篇文章。 謝謝閱讀。

    總結

    以上是生活随笔為你收集整理的模型训练 准确率下降_手写批量线性回归算法:在Python3中梯度下降方法实现模型训练的全部內容,希望文章能夠幫你解決所遇到的問題。

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

    主站蜘蛛池模板: 国产一区二区精品在线 | 大尺度在线观看 | 91精品视频网站 | 中文字幕亚洲欧美 | 久久三| 午夜日韩福利 | 国产日韩精品视频 | 午夜一区在线 | 欧美成人精品在线观看 | 色呦呦在线免费观看 | 亚洲一区电影网 | 黄色在线观看国产 | 日韩美女视频在线 | 亚洲成人一区 | 日韩精品一区二区在线视频 | 久久综合久色欧美综合狠狠 | 欧美性猛交xxxxx水多 | 国产精品久久综合青草亚洲AV | 在线观看精品一区 | av噜噜在线观看 | 一本色道综合久久欧美日韩精品 | 欧美三日本三级少妇三 | 黄色大毛片 | 欧美精品播放 | 国产精品99久久久久久大便 | 草逼免费视频 | 成人午夜在线观看视频 | 99精品久久99久久久久 | 亚洲熟妇av一区二区三区 | 人人爽人人爽人人片 | 伊人热久久 | 名人明星三级videos | 久草麻豆 | 中文字幕无码毛片免费看 | 久久综合久久综合久久综合 | 99热| 97视频成人 | 色哟哟在线播放 | 日产精品久久久久久久蜜臀 | 天堂新版8中文在线8 | 国产成人精品三级麻豆 | 国产黄片毛片 | 精品亚洲国产成人av制服丝袜 | 91香蕉嫩草 | 最新毛片网 | 日韩免费一区二区三区 | 夜夜草网| 五月天社区 | 无遮挡边吃摸边吃奶边做 | 依人成人网 | 短视频在线观看 | 久久福利国产 | 综合色99 | 性欧美一区二区 | 中文字幕在线视频免费 | 玉势 (1v1 高h) | 狠狠丁香| 中文人妻一区二区三区 | 久久久久久久久久久久久国产 | 热99这里只有精品 | 天天综合网久久综合网 | 有机z中国电影免费观看 | 黄色一级视频网站 | 成人第一页 | 亚洲av久久久噜噜噜熟女软件 | 亚洲日日干 | 欧美 日韩 国产 中文 | 人人澡人人澡人人 | 快播日韩 | 少妇一区二区三区 | 国产真实伦对白全集 | 久久久久久久久久久电影 | 伊人免费 | 国产精品久久久久久妇女6080 | 91秘密入口 | 国产黄a三级三级三级 | 天堂精品一区 | 免费男女乱淫真视频免费播放 | 亚洲无毛 | 国产一级做a爰片在线看免费 | 国产三级久久 | 久久99日韩 | 国产 欧美 日本 | 激情综合丁香五月 | 杂技xxx裸体xxxx欧美 | 锦绣未央在线观看 | 情欲少妇人妻100篇 黄色一级片欧美 | 韩国午夜av | 国产破处av | 拔插拔插海外华人免费视频 | 日韩在线第二页 | 免费成人在线看 | 顶臀精品视频www | 日本高清一区二区视频 | 激情av一区 | 亚洲视频免费在线观看 | 国产美女永久无遮挡 | 黑人一级| 国产精品水嫩水嫩 |