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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

机器学习 回归篇(1)——多元线性回归

發布時間:2025/3/21 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 机器学习 回归篇(1)——多元线性回归 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

機器學習 回歸篇(1)——多元線性回歸

  • 摘要
  • 線性回歸簡介
  • python實現
  • 運行結果及可視化

摘要

本文介紹了最基礎的回歸問題——多元線性回歸,并通過python進行實現及可視化展示運行結果。

線性回歸簡介

線性回歸問題的重點在于如何求解回歸函數的截距和系數。
1、構建代價函數(也叫損失函數):平均平方誤差。
2、通過最小二乘法或其他優化算法進行求解,因為線性回歸的代價函數為凸函數,所以一般的經典優化算法用于求解都是適用的,如梯度下降法、單純形法等等。

python實現

CyrusLinearRegression類的有如下方法和屬性:
1、fit():用于擬合模型。
2、predict():用于模型預測
3、coef:回歸函數的系數
4、intercept:回歸函數的截距

from sympy import * import numpy as np import matplotlib.pyplot as plt from matplotlib import font_manager ft = font_manager.FontProperties(fname = "C:\Windows\Fonts\simsun.ttc") # 設置matplotlib的中文顯示字體 class CyrusLinearRegression(object):def __init__(self):self.x = Noneself.y = Noneself.coef = Noneself.intercept = None# 自定義回歸函數形式def regression_func(self,x):func_ = Symbol("a0")func = []for i in range(self.x.shape[1]):func.append(Symbol("a"+str(i+1)))return (Matrix(func).transpose() * Matrix(x) + Matrix([func_]))[0]# 計算損失函數def cal_loss_function(self):loss_func = 0for i in range(self.x.shape[0]):loss_func += (self.regression_func(self.x[i,:]) - self.y[i,0])**2return loss_func/(2*int(self.x.shape[0]))# 定義損失函數def loss_function(self,a):loss_func = self.cal_loss_function()for i in range(self.x.shape[1]+1):temp_sym = Symbol("a"+str(i))loss_func = loss_func.subs(temp_sym,a[i])return loss_funcdef fit(self,x,y):# 1、初始化賦值self.x = np.array(x)self.y = np.array(y).reshape(-1,1)# 2、采用nelder-mead(單純形)優化算法最小化損失函數loss_func = self.loss_functionfrom scipy.optimize import minimizea = minimize(loss_func, [0 for i in range(self.x.shape[1]+1)], method='nelder-mead',options={'xatol': 1e-8, 'disp': True})result = a.xself.coef = result[1:]self.intercept = result[0]def predict(self,x):x = np.array(x)y = []for i in range(x.shape[0]):value = self.interceptfor index,item in enumerate(self.coef):value += x[i,index]*itemy.append(value)return np.array(y).reshape(-1,1)if __name__ == "__main__":x = np.random.randint(1,100,60).reshape(20,3)y = np.array([5*x[i,0]-3*x[i,0]+3.5*x[i,0]-3+np.random.randn(1) for i in range(x.shape[0])]).reshape(-1,1)lr_model = CyrusLinearRegression()lr_model.fit(x,y)# 打印結果print("*"*10,'LinearRegression',"*"*10)print('coef:',lr_model.coef)print('intercept:',lr_model.intercept)# 模型預測并與真實值y_predict = lr_model.predict(x)fig = plt.figure()fig.add_subplot(111)plt.scatter(y_predict,y,marker = "*",s = 18)plt.xlabel("真實值",fontproperties = ft,size = 18)plt.ylabel("預測值",fontproperties = ft,size = 18)

運行結果及可視化

********** LinearRegression ********** coef: [ 5.50424056 0.00784916 -0.00707917] intercept: -3.1227798356667718


by CyrusMay 2020 05 08

天雨粟
鬼夜哭
思念漫太古

——五月天(倉頡)——

總結

以上是生活随笔為你收集整理的机器学习 回归篇(1)——多元线性回归的全部內容,希望文章能夠幫你解決所遇到的問題。

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