繁体   English   中英

R中的多元线性回归-梯度下降

[英]Multivariate Linear Regression - Gradient Descent in R

我正在学习机器学习。 因此,我对在网上找到的数据做了一些简单的练习。 现在,我尝试通过R中的梯度下降实现线性回归。运行它时,我意识到它没有收敛,并且我的成本无限高。 尽管我怀疑它在我计算梯度的部分中的某个位置,但是我找不到问题。 因此,让我们开始展示我的数据。

我的数据集包含4列: ROLL ~ UNEM, HGRAD, INC因此,目标是找到ROLL与其他对象之间的关系。

  • 让我介绍我的代码

     datavar <- read.csv("dataset.csv") attach(datavar) X <- cbind(rep(1, 29), UNEM,HGRAD,INC) y <- ROLL # function where I calculate my prediction h <- function(X, theta){ return(t(theta) %*% X) } # function where I calculate the cost with current values cost <- function(X, y, theta){ result <- sum((X %*% theta - y)^2 ) / (2*length(y)) return(result) } # here I calculate the gradient, #mathematically speaking I calculate derivetive of cost function at given points gradient <- function(X, y, theta){ m <- nrow(X) sum <- c(0,0,0,0) for (i in 1 : m) { sum <- sum + (h(X[i,], theta) - y[i]) * X[i,] } return(sum) } # The main algorithm gradientDescent <- function(X, y, maxit){ alpha <- 0.005 m <- nrow(X) theta <- c(0,0,0,0) cost_history <- rep(0,maxit) for (i in 1 : maxit) { theta <- theta - alpha*(1/m)*gradient(X, y, theta) cost_history[i] <- cost(X, y, theta) } plot(1:maxit, cost_history, type = 'l') return(theta) } 

我这样运行代码

 gradientDescent(X, y, 20)

这是我得到的输出:

-7.001406e+118  -5.427330e+119  -1.192040e+123  -1.956518e+122

所以,你能找到我错了吗。 我已经尝试过不同的Alpha值,没有什么不同。 顺便说一句,我感谢您提供的任何提示或好的做法,

谢谢

好吧,我想我终于找到了答案。 问题是我没有应用任何功能缩放。 因为虽然这是平稳运行算法的可选步骤,但请注意。 现在它可以按预期工作了。 您可以尝试使用R的scale()函数对缩放后的数据集运行代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM