Skip to content

Instantly share code, notes, and snippets.

@rvndbalaji
Created July 24, 2018 14:46
Show Gist options
  • Save rvndbalaji/cfa827b06702806f0415d4d576a0cc1a to your computer and use it in GitHub Desktop.
Save rvndbalaji/cfa827b06702806f0415d4d576a0cc1a to your computer and use it in GitHub Desktop.
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