在R中应用成本函数 [英] Applying Cost Functions in R
问题描述
我正处于R机器学习的起步阶段,我很难相信没有针对不同类型的回归算法来解决成本函数的软件包.例如,如果我想为Logistic回归求解成本函数,则手动方法如下:
I am in the beginning stages of machine learning in R and I find it hard to believe that there are no packages to solving the cost function for different types of regression algorithms. For example, if I want to solve the cost function for a logistic regression, the manual way would be below:
https: //www.r-bloggers.com/logistic-regression-with-r-step-by-step-implementation-part-2/
# Implement Sigmoid function
sigmoid <- function(z)
{
g <- 1/(1+exp(-z))
return(g)
}
#Cost Function
cost <- function(theta)
{
m <- nrow(X)
g <- sigmoid(X%*%theta)
J <- (1/m)*sum((-Y*log(g)) - ((1-Y)*log(1-g)))
return(J)
}
##Intial theta
initial_theta <- rep(0,ncol(X))
#Cost at inital theta
cost(initial_theta)
在glm功能中是否可以自动执行此操作?还是对于我应用的每种算法,都需要像这样手动进行操作吗?
In the glm function is there a way to automatically do this? Or for each algorithm that I apply, do I need to manually do it like this?
推荐答案
我们可以使用optim
进行优化,也可以直接使用glm
We could use optim
for optimization or use glm
directly
set.seed(1)
X <- matrix(rnorm(1000), ncol=10) # some random data
Y <- sample(0:1, 100, replace=TRUE)
# Implement Sigmoid function
sigmoid <- function(z) {
g <- 1/(1+exp(-z))
return(g)
}
cost.glm <- function(theta,X) {
m <- nrow(X)
g <- sigmoid(X%*%theta)
(1/m)*sum((-Y*log(g)) - ((1-Y)*log(1-g)))
}
X1 <- cbind(1, X)
optim(par=rep(0,ncol(X1)), fn = cost.glm, method='CG',
X=X1, control=list(trace=TRUE))
#$par
#[1] -0.067896075 -0.102393236 -0.295101743 0.616223350 0.124031764 0.126735986 -0.029509039 -0.008790282 0.211808300 -0.038330703 -0.210447146
#$value
#[1] 0.6255513
#$counts
#function gradient
# 53 28
glm(Y~X, family=binomial)$coefficients
# (Intercept) X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
#-0.067890451 -0.102411613 -0.295104858 0.616228141 0.124017980 0.126737807 -0.029523206 -0.008790988 0.211810613 -0.038319484 -0.210445717
下图显示了用optim
迭代计算的成本和系数如何收敛到用glm
计算的成本和系数.
The figure below shows how the cost and the coefficients iteratively computed with optim
converge to the ones computed with glm
.
这篇关于在R中应用成本函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!