1 introduction

In the previous two articles, the author introduced how to model linear regression (including multivariable and polynomial regression) and how to build and solve the model through Sklearn. But how do we evaluate a solvable model? In other words, what about your model?

Taking the original housing price forecast as an example, now suppose you solved the two models shown herewithSo which one should I choose? Or how do you evaluate the good or bad of a model when you can’t visualize it?

2 Evaluation Indicators

Common metrics used in regression tasks (predictions of continuous values) are:Mean Absolute Error (MAE), Mean Square Error (MSE), Root Mean Square Error (RMSE) and Mean Absolute percentage Error (Mean Absolute Error) Percentage Error, MAPE), MAE and MSE are the most widely used. The following is a general introduction in turn, and for all the following calculation formulas:Are the number of samples,All said the firstThe true value of the sample,All said the firstThe predicted value of the sample.

2.1 Common Indicators

  • Mean Absolute Error (MAE)

    MAE is used to measure the average absolute error between the predicted value and the true value. The smaller MAE is, the better the model is, and its definition is as follows:


def MAE(y, y_pre):
    return np.mean(np.abs(y - y_pre))
Copy the code
  • Mean square Error (MSE)

    MSE is probably the most common evaluation index in regression. Smaller MSE means better model, which is defined as follows:


Now, surely one would say this is not the target function of linear regression? Yes, equation (2) can be used as both an objective function and an evaluation indicator.

def MSE(y, y_pre):
    return np.mean((y - y_pre) ** 2)
Copy the code
  • Root mean Square Error (RMSE)

    RMSE is derived from the square root of MSE. The smaller RMSE is, the better the model is. Its definition is as follows:


def RMSE(y, y_pre):
    return np.sqrt(MSE(y, y_pre))
Copy the code
  • Mean Absolute percentage error (MAPE)

    MAPE is similar to MAE, but it is standardized on the basis of MAE. The smaller MAPE is, the better the model is. Its definition is as follows:


def MAPE(y, y_pre):
    return np.mean(np.abs((y - y_pre) / y))
Copy the code
  • The evaluation index

    However, SkLearn implements linear regression by defaultIndicators,The larger the model is, the better the model is, which is defined as follows:


    Among themRepresents the average of the true values. mayThe advantage is that the results are normalized, making it easier to see gaps between models.

def R2(y, y_pre):
    u = np.sum((y - y_pre) ** 2)
    v = np.sum((y - np.mean(y_pre)) ** 2)
    return 1 - (u / v)
Copy the code

2.2 Code Examples

def load_data(a):
    data = load_boston()
    x = data.data
    y = data.target
    return x, y

def train(x, y):
    model = LinearRegression()
    model.fit(x, y)
    y_pre = model.predict(x)
    print("model score: ", model.score(x, y))
    print("MAE: ", MAE(y, y_pre))
    print("MSE: ", MSE(y, y_pre))
    print("MAPE: ", MAPE(y, y_pre))
    print("R^2: ", R2(y, y_pre))

if __name__ == '__main__':
    x, y = load_data()
    train(x, y)
    
# the results:
#model score:  0.7406426641094094
#MAE:  3.270862810900317
#MSE:  21.894831181729206
#MAPE:  0.16417298806489977
# R ^ 2, 0.7406426641094093
Copy the code

3 summary

Through the above two sections, the author introduces in detail how to evaluate the quality of a regression model, as well as some commonly used evaluation indicators and implementation methods. Finally, a housing price forecast example is given to demonstrate the use of evaluation indicators. In the next article, the author will continue to introduce how to solve the objective function by gradient descent algorithm and the origin of this objective function. This content is over, thank you for reading!

If you have any questions, please send email to [email protected] and attach the link to the article, castle peak does not change, green water flow, month to see the inn!

reference

  • Example code and exercises: github.com/moon-hotel/…

  • Ng machine learning

More content welcome to scan code to pay attention to the public number to the inn!