-
-
Save rvndbalaji/cfa827b06702806f0415d4d576a0cc1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sigmoid = function(z) | |
{ | |
return(1/(1 + exp(-z))) | |
} | |
cost = function(T) | |
{ | |
h = sigmoid(X%*%T) | |
m = nrow(X) | |
J = (1/m) * sum((-Y*log(h)) - (1-Y)*log(1-h)) | |
return(J) | |
} | |
init = rep(0,ncol(X)) | |
#Get the optimal theta values | |
theta = optim(par=init,fn=cost) | |
#Create a test matrix and append 1 to it. | |
Z = as.matrix(test[,-6]) | |
Z = cbind(1,Z) | |
#Predicted values | |
pred = sigmoid(Z %*% theta$par) | |
#From the graph, for all predictions greater than 0.7 ==> 1 else 0 | |
pred[pred>=0.7] = 1 | |
pred[pred<0.7] = 0 | |
pred = factor(pred) | |
plot(pred) | |
#Predict R-sqaured | |
perf(pred,test$Occupancy) | |
#Output | |
#[1] 0.9426025 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment