Mean Squared error in Python
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle3
--
Chapters
00:00 Question
00:35 Accepted answer (Score 21)
01:01 Answer 2 (Score 2)
01:33 Answer 3 (Score -2)
01:52 Answer 4 (Score -2)
02:22 Thank you
--
Full question
https://stackoverflow.com/questions/3906...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #numpy #scikitlearn
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle3
--
Chapters
00:00 Question
00:35 Accepted answer (Score 21)
01:01 Answer 2 (Score 2)
01:33 Answer 3 (Score -2)
01:52 Answer 4 (Score -2)
02:22 Thank you
--
Full question
https://stackoverflow.com/questions/3906...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #numpy #scikitlearn
#avk47
ACCEPTED ANSWER
Score 22
You are modifying the index for no reason. A for loop increments it anyways. Also, you are not using the index, for example, you are not using any y[i] - y_pred[i], hence you don't need the loop at all.
Use the arrays
mse = np.mean((y - y_pred)**2)
ANSWER 2
Score 2
I would say :
def get_mse(y, y_pred):
d1 = y - y_pred
mse = (1/N)*d1.dot(d1) # N is int(len(y))
return mse
it would only work if y and y_pred are numpy arrays, but you would want them to be numpy arrays as long as you decide not to use other libraries so you can do math operations on it.
numpy dot() function is the dot product of 2 numpy arrays (you can also write np.dot(d1, d1) )