I have a CSV file with 20 columns and 785 rows. The 785th row for each column is a label describing the encoded image. The encoded image is either 3 or 5. So 1-784 row is the encoded image and 785th row is the label that names the image.
I loaded the CSV file 3_5_small.csv and segregated the labels from the encoded data.
which as you see is the image of number 3.
Now, I decided to use logistic regression to predict the images from the encoded data. I used Stochastic Gradient Descent as explained in the machine learning course by Andrew NG. But I do not think, I got it right. Before following the code, here are the steps I did:
- Transformed
train_labels_3_5
which contained only3
and5
to1
and0
respectively. So I want to predict the image of3
. If the output probability is < 0.5, it will be 5 and > 0.5 will be 3. - Randomly shuffled the
train_data_3_5
andtrain_labels_3_5
to the same degree. - Randomly generated the
theta
vector - Passed the
theta
vector andX
vector into thehypothesis
function - Updated the
theta
vector.
This is all I did. Here is the code to what I have done.
train <- function(data, labels, alpha = 0.001) {
#browser()
#Initialize the theta vector
theta <- seq(from = 0, to = 1, length.out = nrow(data))
number_of_iterations = 10
for(noi in 1:number_of_iterations) {
for(i in seq(1:ncol(data))) {
x = as.vector(data[,i]) #Create a x vector
h = hypothesis(x, theta) #Call the hypothesis function to get the probability
y = labels[1,i]
theta <- theta - (alpha * ((h - y) * x))
}
}
return(theta)
}
But on the test data and even on the training data, this does not predict correct at all. I do not know where have I gone wrong. I have revisited the algorithm, the lecture but cannot figure out, what am I doing incorrectly. It always predicts 1
no matter I pass the vector for 3 or 5!
labels
? $\endgroup$ – Michael M Apr 3 '18 at 18:40