多元线性回归模型和最小二乘法

xiaoxiao2021-02-28  103

为了加深映像,将笔记记录在此

P元线性模型为:

为了求参数我们的目标是让误差平方和最小

对各个参数求偏导

可得到正规方程组

写成矩阵形式为

从而得到参数的解

Python 代码实现:

[python] view plain copy import numpy as np    def loadDataSet(fileName):      dataMat=[]      labelMat=[]      fr=open(fileName)      for line in fr.readlines():          curLine=line.strip().split('\t')          dataMat.append(list(map(float,curLine[:-1])))          labelMat.append(float(curLine[-1]))      return dataMat,labelMat    def standRegress(xArr,yArr):      xMat=np.mat(xArr)      yMat=np.mat(yArr).T      xTx=xMat.T*xMat      if np.linalg.det(xTx)==0.0:                    return      ws=xTx.I*(xMat.T*yMat)      return ws    xArr,yArr=loadDataSet("ex0.txt")  ws=standRegress(xArr,yArr)                         数据集的样是为:

通常还可以用梯度下降算法来求参数的值。

上面的数据来源于《 机器学习实战》

原文地址:http://blog.csdn.net/sinat_16233463/article/details/37363183

转载请注明原文地址: https://www.6miu.com/read-52480.html

最新回复(0)