从零开始的线性回归

时间:2018-09-21 12:21:09

标签: r algorithm machine-learning linear-regression

我正在尝试编写具有梯度下降法的简单线性回归的基本代码。 这是我的代码。

linear = function(x,y,lr)
{
theta0 = 0

theta1 = 0

m=length(x)

hypo = theta0 +theta1*x

costt = cost(hypo , y)
prev_cost = 1000000

while (prev_cost > cost)
{

prev_cost = cost

theta0 = theta0 - (lr/m)*(hypo - y)

theta1 = theta1 - (lr/m)*(hypo - y)*x

hypo = theta0 + theta1*x

New_cost = cost(hypo , y)

if(New_cost < cost)
{
cost = New_cost
}
}
theta = c(theta0 , theta1)
return( theta )
}  

cost = function(hypo , y)
{
interm = (hypo - y)^2

interm1 = sum(interm)

interm2 = interm1/(2 * m)

return(interm2)  
}

但是当我用数据对其进行测试时,它会生成一条警告消息。

There were 50 or more warnings (use warnings() to see the first 50)

并停止终止。 代码有什么问题? 当我使用警告时,我得到

Warning messages:
1: In while (prev_cost > cost) { ... :
the condition has length > 1 and only the first element will be used 
lr = 0.01,这是学习率。 这就是数据x和y的快照 enter image description here

1 个答案:

答案 0 :(得分:0)

这为我运行。它产生的向量是length(x)的两倍-这是您要的吗?

linear = function(x,y,lr)
{
    theta0 = 0

    theta1 = 0

    m=length(x)

    hypo = theta0 +theta1*x

    costt = cost(hypo , y, m)
    prev_cost = 1000000

    while (prev_cost > costt)
    {

        prev_cost = costt

        theta0 = theta0 - (lr/m)*(hypo - y)

        theta1 = theta1 - (lr/m)*(hypo - y)*x

        hypo = theta0 + theta1*x

        New_cost = cost(hypo , y, m)

        if(New_cost < costt)
        {
            costt = New_cost
        }
    }
    theta = c(theta0 , theta1)
    return( theta )
}  

cost = function(hypo , y, m)
{
    interm = (hypo - y)^2

    interm1 = sum(interm)

    interm2 = interm1/(2 * m)

    return(interm2)  
}

x <- rnorm(80)
y <- rnorm(80)
lr <- 0.01
linear(x, y, lr)