机器学习实战:回归

线性回归

矩阵X存放输入数据,矩阵w存放回归系数。误差是预测y值和真实y值的差值平方,最佳拟合曲线曲线就是误差最小的曲线。

Y_{1} = X_{1}^{T} * w

公式

平法误差   \sum_{i = 1}^{m} = (y_{i} - x_{i}^{T}* w)^{2}

回归系数  \hat{w} = (X^{T}X) ^ {-I} X^{T} y

 

示例:标准回归函数

(1) 导入数据函数

def loadDataSet(filename):
    fr = open(filename,'r')
    NumFea = len(fr.readline().strip().split('\t')) -1
    dataMat = []
    labelMat = []
    for line in fr.readlines():
        lineArr = []
        curLine = line.strip().split('\t')
        for i in range(NumFea):
            lineArr.append(float(curLine[i]))
        
        dataMat.append(lineArr)
        labelMat.append(float(curLine[-1]))
    
    return dataMat,labelMat

xArr,yArr = loadDataSet('./ex0.txt')

(2) 标准回归函数

def standRegres(xArr,yArr):
    xMat = np.mat(xArr)
    yMat = np.mat(yArr).T
    xTx = xMat.T * xMat
    if np.linalg.det(xTx) == 0:
        print("This matrix is singular,cannot do inverse")
        return
    ws = xTx.I *(xMat.T * yMat)
    return ws

ws = standRegres(xArr,yArr)

 

(3) 评价模型好坏

xMat = np.mat(xArr)
yMat = np.mat(yArr)
yHat = xMat * ws
yHat
np.corrcoef(yHat.T,yMat)

 

示例:局部加权线性回归

适用情况

线性回归欠拟合现象。给待预测点附近的每个点赋予一定的权重。

公式

回归系数   \hat{w} = (X^{T} WX)^{-l} X^{T} Wy

权重  w(i,i) = exp(\frac{|x^{(i)} - x|}{-2k^{2}})

def lwlr(testPoint,xArr,yArr,k = 1.0):
    xMat = np.mat(xArr)
    yMat = np.mat(yArr).T
    row = xMat.shape[0]
    weight = np.mat(np.eye((m)))
    
    for i in range(row):
        diffMat = testPoint - xMat[i,:]
        weight[i,i] = np.exp((diffMat * diffMat.T)/ (-2 * k ** 2))
    xTx = xMat.T  * (weight * xMat)
    if np.linalg.det(xTx) == 0 :
        print("This matrix is singular,cannot do inverse")
        return
    ws = xTx.I * (xMat.T * (weight * yMat))
    return testPoint * ws


def lwlrTest(testArr,xArr,yArr,k = 1.0):
    row = xArr.shape[0]
    yHat = np.zeros(row)
    for i in range(row):
        yHat[i] = lwlr(testArr[i],xArr,yArr,k)
    return yHat

 

示例: 岭回归

适用情况

数据的特征比样本点多。

公式

回归系数 \hat{w} = (X^{T}X + \lambda I^{-l})X^{T}y

\lambda是一个用户定义的数值;I是一个mxm的矩阵,对角线上元素全为1,其他元素全为0。

(1) 计算回归系数

def ridgeRegres(xMat,yMat,lam=0.2):
    xTx = xMat.T * xMat
    denom = np.eye((xMat.shape[1])) * lam + xTx
    if np.linalg.det(denom) == 0:
        print("This matrix is singular,cannot do inverse")
        return
    ws = denom.I * (xMat.T * yMat)
    return ws

(2) 测试

def ridgeTest(xArr,yArr):
    xMat = np.mat(xArr)
    yMat = np.mat(yArr).T
    yMean = np.mean(yMat,0)
    yMat = yMat - yMean
    xMean = np.mean(xMat,0)
    xVar = np.var(xMat,0)
    xMat = (xMat - xMean) / xVar
    TestTime = 30
    wMat = np.zeros((TestTime,xMat.shape[1]))
    
    for i in range(TestTime):
        ws = ridgeRegres(xMat,yMat,exp(i - 10))
        wMat[i,:] = ws.T
    return wMat
        

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值