机器学习笔记:线性回归

参考:https://yoyoyohamapi.gitbooks.io/mit-ml/content/线性回归

回归模型

输入:现有知识;输出:预测

一个预测问题在回归模型下的解决步骤:

1.训练集

2.学习算法

3.预测:当接受了新的数据输入之后,通过学习阶段获得的对应关系来预测输出

数学符号:

特征x_i;特征向量(输入)x;输出向量y;假设:预测函数/回归方程h_{\theta}(x)=\theta ^{T}x

误差评估(代价函数):用最小均方(LMS)来描述误差

J(\theta)={1\over 2m}\sum^m_{i=1}(h_\theta(x^{(i)})-y^{(i)})^2m为样本数

批量梯度下降(当学习效果不佳时的纠正策略)

调节\theta使代价函数足够小,在线性回归中通常使用梯度下降来调节

\theta_j=\theta_j-\alpha{\partial \over \partial \theta_j}J(\theta)

梯度是函数值下降最剧烈的方向,沿着J的梯度我开门就能接近最小值

\alpha 学习率 是沿梯度方向的学习速率,需要我们自己设置并且不断尝试

\theta调优直到收敛

def bgd(rate, maxLoop, epsilon, X, y):
    """批量梯度下降法

    Args:
        rate: 学习率
        maxLoop: 最大迭代次数
        epsilon: 收敛精度
        X: 样本矩阵
        y: 标签矩阵

    Returns:
        (theta, errors, thetas), timeConsumed
    """
    m,n = X.shape
    # 初始化theta
    theta = np.zeros((n,1))
    count = 0
    converged = False
    error = float('inf')
    errors = []
    thetas = {}
    for j in range(n):
        thetas[j] = [theta[j,0]]
    while count<=maxLoop:
        if(converged):
            break
        count = count + 1
        for j in range(n):
            deriv = (y-X*theta).T*X[:, j]/m
            theta[j,0] = theta[j,0]+rate*deriv
            thetas[j].append(theta[j,0])
        error = J(theta, X, y)
        errors.append(error[0,0])
        # 如果已经收敛
        if(error < epsilon):
            converged = True
    return theta,errors,thetas

随机梯度下降:

for i=1 to m:\theta_j=\theta_j+\alpha(y^{(i)}-h_\theta(x_(i)))x_j^{(i)}

def sgd(rate, maxLoop, epsilon, X, y):
    """随机梯度下降法
    Args:
        rate: 学习率
        maxLoop: 最大迭代次数
        epsilon: 收敛精度
        X: 样本矩阵
        y: 标签矩阵
    Returns:
        (theta, error, thetas), timeConsumed
    """
    m,n = X.shape
    # 初始化theta
    theta = np.zeros((n,1))
    count = 0
    converged = False
    error = float('inf')
    errors = []
    thetas = {}
    for j in range(n):
        thetas[j] = [theta[j,0]]
    while count <= maxLoop:
        if(converged):
            break
        count = count + 1
        errors.append(float('inf'))
        for i in range(m):
            if(converged):
                break
            diff = y[i,0]-h(theta, X[i].T)
            for j in range(n):
                theta[j,0] = theta[j,0] + rate*diff*X[i, j]
                thetas[j].append(theta[j,0])
            error = J(theta, X, y)
            errors[-1] = error[0,0]
            # 如果已经收敛
            if(error < epsilon):
                converged = True
    return theta, errors, thetas

正规方程:

\theta = (X^TX)^{-1}X^Ty

X为输入向量矩阵,x0=1

特征缩放:

将各个特征量化到统一的区间

方法一:Standardization(Z-score normalization) 量化后的特征将服从标准正态分布

z={x_i-\mu \over \delta}

\mu为均值 \delta为标准差 特征值分布在[-1,1]区间

方法二:Min-Max Scaling(normalization)

z={x_i-min(x_i)\over max(x_i)-min(x_i)}

量化到[0,1]空间

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值