#8 Sklearn - Python package - Model evaluate metrics for regression
One of the most important steps in building prediction model is evaluating how accurate our model is by the way using metrics to calculate the amount of deviation between predicted value and actual value. In this journal, we will dive into 4 metrics, they are Mean Absolute Error, Mean Square Error, Root Mean Spare Error, and R-squared score. All the functions we use that come from sklearn Python package (from sklearn import metrics).
Mean Absolute Error (MAE) is the mean of the absolute value of the errors. In other words, we use the differences of each actual value with predicted value then divide by its number of observations. It measures the average of the residuals in the dataset. That means the weight of all the errors are the same. For example, the difference of first pair yi1 and y^i1 is 5, the difference of second pair yi2 and y^i2 is 1. The average of them will be 3. Then the error will be off by 3. So, "MAE is best used in scenarios where the magnitude of every error is not important". The range value of MAE is from 0 to infinity. The lower score, the better model. It is calculated by the formula below:
Code format:
from sklearn.metrics import mean_absolute_error
mean_absolute_error(actual value, predicted value)
Mean Squared Error (MSE) is the mean of the squared errors. MSE is more popular than MAE because it "punishes" the large error. MSE is more useful when the dataset contains a lot of noises, outliers, or unexpected values. It measures the variance of the residuals. For example, the errors are 1, 0, 2, 1, 8 (the result will be 2.4), the square errors in turn are 1, 0, 4, 1, 64 (13.1). Then we can see we have an outlier value here is 8, it makes the square error overall is bigger. The single high value leads the high mean score. So, we can say that the MAE is affected by large deviator or outliers. The range value of MAE is from 0 to infinity. The lower score, the better model.
from sklearn.metrics import mean_squared_error
mean_squared_error(actual value, predicted value)
from sklearn.metrics import root_mean_squared_error
root_mean_squared_error(actual value, predicted value)
from sklearn.metrics import r2_score
r2_score(actual value, predicted value) or
modelName.score(X_test, y_test)
During the time I study for this journal, I also found the answer for my #7 journal. In #7 journal, I typed model3.score(X_test7, test_pred3) instead of model3.score(X_test7, test_y), that's the reason why I got the result is 1. And because I got the testing score = 1, so I thought model 3 must be the best model. But I was wrong. The actual testing score (also r-squared score) is -5973.93, when the training score is 0.32, that's a type of overfitting model. Thus, our best model in journal #7 is model 1 - linear regression. This answer leaves me a question that linear regression is better with or without polynomial feature? Why the curve is worse than a line in some cases or how?
Bao,
ReplyDeleteExcellent review, I wish I had the answer for you. I look forward to hearing the solution!