<jupyter_start><jupyter_text>衡量回归算法标准<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets<jupyter_output><empty_output><jupyter_text>1. 波士顿房产数据<jupyter_code>boston = datasets.load_boston()
print(boston.DESCR)
# 在这里我们只使用 RM(房间数量) 属性
# - RM       average number of rooms per dwelling
x = boston.data[:, 5]
x.shape
y = boston.target
y.shape
plt.scatter(x, y)<jupyter_output><empty_output><jupyter_text>去除最大点<jupyter_code>np.max(y)
x = x[y < 50.0]
y = y[y < 50.0]
plt.scatter(x, y)<jupyter_output><empty_output><jupyter_text>2. 调用自己的模型训练数据<jupyter_code>from LR.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, seed=666)
x_train.shape
x_test.shape
from LR.SimpleLinearRefression import SimpleLinearRegression
reg = SimpleLinearRegression()
reg.fit(x_train, y_train)
reg.a_
reg.b_
plt.scatter(x_train, y_train)
plt.plot(x_train, reg.predict(x_train), color="r")
y_predict = reg.predict(x_test)<jupyter_output><empty_output><jupyter_text>3. MSE（Mean Squared Error）<jupyter_code>mse_test = np.sum((y_predict - y_test) ** 2) / len(y_test)
mse_test<jupyter_output><empty_output><jupyter_text>4. RMSE **均方根误差**[![LR4.png](https://i.postimg.cc/tRsJSRDr/LR4.png)](https://postimg.cc/kDd7Dm08)<jupyter_code>from math import sqrt

rmse_test = sqrt(mse_test)
rmse_test<jupyter_output><empty_output><jupyter_text>5. MAE **平均绝对误差**[![LR5.png](https://i.postimg.cc/fRmw4J0K/LR5.png)](https://postimg.cc/TK28g20L)<jupyter_code>mae_test = np.sum(np.abs(y_predict - y_test)) / len(y_test)
mae_test<jupyter_output><empty_output><jupyter_text>6. 手动实现MSE、RMSE、MAE<jupyter_code>from LR.metrics import mean_squared_error
from LR.metrics import root_mean_squared_error
from LR.metrics import mean_absolute_error
mean_squared_error(y_test, y_predict)
root_mean_squared_error(y_test, y_predict)
mean_absolute_error(y_test, y_predict)<jupyter_output><empty_output><jupyter_text>7. scikit-learn中的MSE和MAE<jupyter_code>from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
mean_squared_error(y_test, y_predict)
mean_absolute_error(y_test, y_predict)<jupyter_output><empty_output><jupyter_text>8. R Squared [![LR6.png](https://i.postimg.cc/P5dnpKRg/LR6.png)](https://postimg.cc/tYvM8dyk)[![LR7.png](https://i.postimg.cc/26GZY0gC/LR7.png)](https://postimg.cc/HcJx49pN)**R Squared 还可以有如下变形**[![LR8.png](https://i.postimg.cc/J7dsQHLY/LR8.png)](https://postimg.cc/3Wmr8RW2)- 即是 1 减去 MSE除以 y 的方差  R Squared 编程实现<jupyter_code>1 - mean_squared_error(y_test, y_predict) / np.var(y_test)
from LR.metrics import r2_score
r2_score(y_test, y_predict)
reg.score(x_test, y_test)<jupyter_output><empty_output><jupyter_text>scikit-learn 中的 R Squared<jupyter_code>from sklearn.metrics import r2_score
r2_score(y_test, y_predict)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13500176242509693, 'fraction_numerical': 0.019386676066267185, 'mean_word_length': 8.428571428571429, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 45, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '319681', 'license': 'MIT', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1284, 'n_words': 157, 'path': 'ML-Base-MOOC/chapt-3 Linear Regression/02-Measure Linear Regression.ipynb', 'repo_name': 'silence1016/machine-learning', 'size': '2837'}