Sunday, 25 August 2013

SGD logistic regression in java

SGD logistic regression in java

Can someone tell me if this is the correct implementation of SGD logistic
regression in Java? I'm trying to follow this based on the description on
the coursera video. Thanks.
double intercept = 0.0;
double weights[] = new double[X[0].length];
for (int numIterations = 0; numIterations < 500000; numIterations++)
{
for (int item = 0; item < X.length; item++)
{
double[] currentItem = X[item];
double z = intercept;
for (int j = 0; j < currentItem.length; j++)
{
z += (weights[j] * currentItem[j]);
}
double adjust = 0.0001 * ((1.0 / (1.0 + Math.exp(-z))) -
y[item]); // alpha * (h(x) - y)
// adjust all of the weights at the same time
intercept = intercept - adjust;
for (int j = 0; j < currentItem.length; j++)
{
weights[j] = weights[j] - (adjust * currentItem[j]);
}
}
}

No comments:

Post a Comment