<jupyter_start><jupyter_text>02 衡量回归算法的标准<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets<jupyter_output><empty_output><jupyter_text>波士顿房产数据<jupyter_code>boston = datasets.load_boston()
print(boston.DESCR)
boston.feature_names
x = boston.data[:, 5] # 只使用房间数量这个特征
x.shape
y = boston.target
y.shape
plt.scatter(x, y)
plt.show
np.max(y)
x = x[y < 50.0]
y = y[y < 50.0]
plt.scatter(x, y)
plt.show<jupyter_output><empty_output><jupyter_text>使用简单线性回归法<jupyter_code>%run ../playML/model_selection.py
x_train, x_test, y_train, y_test = train_test_split(x, y, seed=666)
x_train.shape
x_test.shape
%run ../playML/SimpleLinearRegression.py
reg = SimpleLinearRegression2()
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')
plt.show()<jupyter_output><empty_output><jupyter_text>MSE<jupyter_code>y_predict = reg.predict(x_test)
mse_test = np.sum((y_predict - y_test) ** 2 / len(y_test))
mse_test<jupyter_output><empty_output><jupyter_text>RMSE<jupyter_code>from math import sqrt
rmse_test = sqrt(mse_test)
rmse_test<jupyter_output><empty_output><jupyter_text>MAE<jupyter_code>mae_test = np.sum(np.absolute(y_predict - y_test)) / len(y_test)
mae_test
%run ../playML/metrics.py
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>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>R Square<jupyter_code>1 - mean_squared_error(y_test, y_predict) / np.var(y_test)
r2_score(y_test, y_predict)
from sklearn.metrics import r2_score
r2_score(y_test, y_predict)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12038014783526928, 'fraction_numerical': 0.009503695881731784, 'mean_word_length': 9.298913043478262, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 33, 'https://': 0, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '50800', 'license': 'Apache-2.0', 'n_tokens_mistral': 856, 'n_tokens_neox': 833, 'n_words': 109, 'path': 'data-science/scikit-learn/03/02-MSE-VS-MAE.ipynb', 'repo_name': 'le3t/ko-repo', 'size': '1894'}