Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mean Squared error in Python

I'm trying to made function that will calculate mean squared error from y (true values) and y_pred (predicted ones) not using sklearn or other implementations.

I'll try next:

def mserror(y, y_pred):
    i=0
    for i in range (len(y)):
        i+=1
        mse = ((y - y_pred) ** 2).mean(y)   
        return mse

Can you please correct me what I m doing wrong with the calculation and who it can be fixed?

like image 893
Keithx Avatar asked Aug 21 '16 13:08

Keithx


People also ask

How do you calculate MSE?

To find the MSE, take the observed value, subtract the predicted value, and square that difference. Repeat that for all observations. Then, sum all of those squared values and divide by the number of observations.

What is the mean square of error?

In statistics, the mean squared error (MSE) or mean squared deviation (MSD) of an estimator (of a procedure for estimating an unobserved quantity) measures the average of the squares of the errors—that is, the average squared difference between the estimated values and the actual value.

Is MSE same as L2?

L2 loss and MSE are related, but not the same. L2 loss is the loss for each example, whilst MSE is the cost function which is an aggregation of all the loss values in the dataset.

What is the difference between RMSE and MSE?

RMSE is the square root of MSE. MSE is measured in units that are the square of the target variable, while RMSE is measured in the same units as the target variable. Due to its formulation, MSE, just like the squared loss function that it derives from, effectively penalizes larger errors more severely.


2 Answers

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)
like image 145
percusse Avatar answered Sep 18 '22 15:09

percusse


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) )

like image 25
rotem Avatar answered Sep 21 '22 15:09

rotem