有监督学习--简单线性回归模型(梯度下降法代码实现)0.引入依赖1.导入数据(data.csv)2.定义损失函数3.定义模型的超参数4.定义核心梯度下降模型函数5.测试:运行梯度下降算法,计算最优的 w 和 b6.画出拟合曲线7.附录-测试数据
有监督学习--简单线性回归模型(梯度下降法代码实现)
0.引入依赖
import numpy as npimport matplotlib.pyplot as plt
1.导入数据(data.csv)
points = np.genfromtxt(‘data.csv‘, delimiter=‘,‘)
# points# 提取 points 中的两对数据,分别作为 x, y# points[0][0] 等价于# points[0,0] # 第一行第一列数据# points[0,0:1] # array([32.50234527])# points[0,0:2] # 第一行数据 array([32.50234527, 31.70700585])# points[0,0:] # 第一行数据 array([32.50234527, 31.70700585])x = points[:,0] # 第一列数据y = points[:,1] # 第二列数据
# 用 scatter 画出散点图plt.scatter(x, y)plt.show()
作图如下:
2.定义损失函数
# 损失函数是模型系数的函数,还需要传入数据的 x,ydef compute_cost(w, b, points): total_cost = 0 M = len(points) # 逐点计算【实际数据 yi 与 模型数据 f(xi) 的差值】的平方,然后求平均 for i in range(M): x = points[i, 0] y = points[i, 1] total_cost += (y - w * x - b) ** 2
return total_cost / M
3.定义模型的超参数
alpha = 0.0001initial_w = 0initial_b = 0num_iter = 10
4.定义核心梯度下降模型函数
def grad_desc(points, initial_w, initial_b, alpha, num_iter): w = initial_w b = initial_b # 定义一个list保存所有的损失函数值,用来显示下降的过程 cost_list = []
for i in range(num_iter): # 先计算初始值的损失函数的值 cost_list.append(compute_cost(w, b, points)) w, b = step_grad_desc(w, b, alpha, points)
return [w, b, cost_list]
def step_grad_desc(current_w, current_b, alpha, points): sum_grad_w = 0 sum_grad_b = 0 M = len(points)
# 对每一个点带入公式求和 for i in range(M): x = points[i, 0] y = points[i, 1] sum_grad_w += (current_w * x + current_b - y) * x sum_grad_b += (current_w * x + current_b - y)
# 用公式求当前梯度 grad_w = 2 / M * sum_grad_w grad_b = 2 / M * sum_grad_b
# 梯度下降,更新当前的 w 和 b updated_w = current_w - alpha * grad_w updated_b = current_b - alpha * grad_b
return updated_w, updated_b
5.测试:运行梯度下降算法,计算最优的 w 和 b
w, b, cost_list = grad_desc(points, initial_w, initial_b, alpha, num_iter)
print(‘w is:‘, w)print(‘b is:‘, b)
cost = compute_cost(w, b, points)print(‘cost is:‘, cost)
plt.plot(cost_list)plt.show()
输出结果如下:
w is: 1.4774173755483797b is: 0.02963934787473238cost is: 112.65585181499748
作图如下:
6.画出拟合曲线
# 先用 scatter 画出2维散点图plt.scatter(x, y)
# 针对每一个x,计算出预测的值pred_y = w * x + b# 再用 plot 画出2维直线图plt.plot(x, pred_y, c=‘r‘)plt.show()
作图如下:
7.附录-测试数据
测试数据 data.csv 如下:
32.502345269453031,31.7070058465699253.426804033275019,68.7775959816389161.530358025636438,62.56238229794580347.475639634786098,71.54663223356777759.813207869512318,87.23092513368739355.142188413943821,78.21151827079923252.211796692214001,79.6419730498087439.299566694317065,59.17148932186950848.10504169176825,75.33124229706305652.550014442733818,71.30087988685035345.419730144973755,55.16567714595912354.351634881228918,82.47884675749791944.164049496773352,62.00892324572582558.16847071685779,75.39287042599495756.727208057096611,81.4361921588786448.955888566093719,60.72360244067396544.687196231480904,82.89250373145371560.297326851333466,97.37989686216607845.618643772955828,48.84715331735507238.816817537445637,56.87721318626850666.189816606752601,83.87856466460276365.41605174513407,118.5912173025224947.48120860786787,57.25181946226896941.57564261748702,51.39174407983230751.84518690563943,75.38065166531235759.370822011089523,74.76556403215137457.31000343834809,95.45505292257473763.615561251453308,95.22936601755530746.737619407976972,79.05240616956558650.556760148547767,83.43207142132371252.223996085553047,63.35879031749787835.567830047746632,41.41288530370056342.436476944055642,76.61734128007404458.16454011019286,96.76956642610819957.504447615341789,74.08413011660252345.440530725319981,66.58814441422859461.89622268029126,77.76848241779302433.093831736163963,50.71958891231208436.436009511386871,62.12457081807178137.675654860850742,60.81024664990221144.555608383275356,52.68298336638778143.318282631865721,58.56982471769286750.073145632289034,82.90598148507051243.870612645218372,61.42470980433912362.997480747553091,115.2441528007952932.669043763467187,45.57058882337608540.166899008703702,54.08405479622361253.575077531673656,87.99445275811041333.864214971778239,52.72549437590042564.707138666121296,93.57611869265824138.119824026822805,80.16627544737096444.502538064645101,65.10171157056032640.599538384552318,65.56230126040037541.720676356341293,65.28088692082282351.088634678336796,73.43464154632430155.078095904923202,71.1397278586189441.377726534895203,79.10282968354985762.494697427269791,86.52053844034715349.203887540826003,84.74269780782621841.102685187349664,59.35885024862493341.182016105169822,61.68403752483362750.186389494880601,69.84760415824918352.378446219236217,86.09829120577410350.135485486286122,59.10883926769964333.644706006191782,69.8996816436276339.557901222906828,44.86249071116439856.130388816875467,85.49806777884022357.362052133238237,95.53668684646721960.269214393997906,70.25193441977158735.678093889410732,52.72173496477498831.588116998132829,50.39267013507989653.66093226167304,63.64239877565775346.682228649471917,72.24725106866236543.107820219102464,57.81251297618140270.34607561504933,104.2571015854382244.492855880854073,86.64202031882200657.50453330326841,91.48677800011013536.930076609191808,55.23166088621283655.805733357942742,79.55043667850760938.954769073377065,44.84712424246760156.901214702247074,80.20752313968276356.868900661384046,83.1427497920434634.33312470421609,55.72348926054391459.04974121466681,77.63418251167786457.788223993230673,99.05141484174826954.282328705967409,79.12064627468002751.088719898979143,69.58889785111847550.282836348230731,69.51050331149438944.211741752090113,73.68756431831728538.005488008060688,61.36690453724013132.940479942618296,67.17065576899511853.691639571070056,85.66820314500154268.76573426962166,114.8538712339139446.230966498310252,90.12357206996742368.319360818255362,97.91982103524284850.030174340312143,81.53699078301502849.239765342753763,72.11183246961566350.039575939875988,85.23200734232567348.149858891028863,66.22495788805463225.128484647772304,53.454394214850524
原文地址:https://www.cnblogs.com/chenmingjun/p/10884542.html
时间: 2024-11-06 09:48:15