Skip to content

Instantly share code, notes, and snippets.

View bryangoodrich's full-sized avatar

Bryan Goodrich bryangoodrich

View GitHub Profile
@bryangoodrich
bryangoodrich / RegularizedGradientDescent.R
Created August 26, 2014 05:45
Regularized Gradient Descent Logistic Classifier with Decision Boundary
loadInput <- function() {
structure(c(0.051267, -0.092742, -0.21371, -0.375, -0.51325,
-0.52477, -0.39804, -0.30588, 0.016705, 0.13191, 0.38537, 0.52938,
0.63882, 0.73675, 0.54666, 0.322, 0.16647, -0.046659, -0.17339,
-0.47869, -0.60541, -0.62846, -0.59389, -0.42108, -0.11578, 0.20104,
0.46601, 0.67339, -0.13882, -0.29435, -0.26555, -0.16187, -0.17339,
-0.28283, -0.36348, -0.30012, -0.23675, -0.06394, 0.062788, 0.22984,
0.2932, 0.48329, 0.64459, 0.46025, 0.6273, 0.57546, 0.72523,
0.22408, 0.44297, 0.322, 0.13767, -0.0063364, -0.092742, -0.20795,
-0.20795, -0.43836, -0.21947, -0.13882, 0.18376, 0.22408, 0.29896,
@bryangoodrich
bryangoodrich / GradientDescent.R
Last active August 29, 2015 14:05
Gradient Descent for Logistic Classifier
gradientDescent <- function(X, y, initial_theta, method = "BFGS", ...) {
m <- nrow(y)
sigmoid <- function(x) 1 / (1 + exp(-x))
gradFunction <- function(theta) (1/m) * (t(X) %*% (sigmoid(X %*% theta)-y))
costFunction <- function(theta)
(1/m) * (t(-y) %*% log(sigmoid(X %*% theta)) - t(1-y) %*% log(1 - sigmoid(X %*% theta)))
optim(initial_theta, costFunction, gradFunction, method = method, ...)
}