[云炬python3玩转机器学习]5-5 衡量回归算法的标准,MSE vs MAE

[云炬python3玩转机器学习]5-5 衡量回归算法的标准,MSE vs MAE

[云炬python3玩转机器学习]5-5 衡量回归算法的标准,MSE vs MAE

[云炬python3玩转机器学习]5-5 衡量回归算法的标准,MSE vs MAE

[云炬python3玩转机器学习]5-5 衡量回归算法的标准,MSE vs MAE

05 衡量回归算法的标准,MSE vs MAE

In [3]:

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
import datetime;print("Run by CYJ,",datetime.datetime.now())
Run by CYJ, 2022-01-20 12:53:42.123449

波士顿房产数据

In [4]:

boston = datasets.load_boston()

In [5]:

boston.keys()

Out[5]:

dict_keys(['data', 'target', 'feature_names', 'DESCR', 'filename'])

In [6]:

print(boston.DESCR)

In [7]:

boston.feature_names

Out[7]:

array(['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD',
       'TAX', 'PTRATIO', 'B', 'LSTAT'], dtype='In [13]:x = boston.data[:,5] # 只使用房间数量这个特征
In [14]:x.shape
Out[14]:(506,)In [15]:y = boston.target
In [16]:y.shape
Out[16]:(506,)In [17]:plt.scatter(x, y)
plt.show()
In [18]:np.max(y)
Out[18]:50.0In [19]:x = x[y < 50.0]
y = y[y < 50.0]
In [20]:x.shape
Out[20]:(490,)In [21]:y.shape
Out[21]:(490,)In [22]:plt.scatter(x, y)
plt.show()
使用简单线性回归法In [23]:from playML.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(x, y, seed=666)
In [24]:x_train.shape
Out[24]:(392,)In [25]:y_train.shape
Out[25]:(392,)In [26]:x_test.shape
Out[26]:(98,)In [27]:y_test.shape
Out[27]:(98,)In [28]:from playML.SimpleLinearRegression import SimpleLinearRegression
In [29]:reg = SimpleLinearRegression()
reg.fit(x_train, y_train)
Out[29]:SimpleLinearRegression()In [30]:reg.a_
Out[30]:7.8608543562689555In [31]:reg.b_
Out[31]:-27.459342806705543In [32]:plt.scatter(x_train, y_train)
plt.plot(x_train, reg.predict(x_train), color='r')
plt.show()
In [35]:plt.scatter(x_train, y_train)
plt.scatter(x_test, y_test, color="c")
plt.plot(x_train, reg.predict(x_train), color='r')
plt.show()
In [36]:y_predict = reg.predict(x_test)
MSEIn [37]:mse_test = np.sum((y_predict - y_test)**2) / len(y_test)
mse_test
Out[37]:24.156602134387438RMSEIn [38]:from math import sqrt

rmse_test = sqrt(mse_test)
rmse_test
Out[38]:4.914936635846635MAEIn [39]:mae_test = np.sum(np.absolute(y_predict - y_test))/len(y_test)
mae_test
Out[39]:3.5430974409463873封装我们自己的评测函数代码参见 这里In [40]:from playML.metrics import mean_squared_error
from playML.metrics import root_mean_squared_error
from playML.metrics import mean_absolute_error
In [41]:mean_squared_error(y_test, y_predict)
Out[41]:24.156602134387438In [42]:root_mean_squared_error(y_test, y_predict)
Out[42]:4.914936635846635In [43]:mean_absolute_error(y_test, y_predict)
Out[43]:3.5430974409463873scikit-learn中的MSE和MAEIn [44]:from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
In [45]:mean_squared_error(y_test, y_predict)
Out[45]:24.156602134387438In [46]:mean_absolute_error(y_test, y_predict)
Out[46]:3.5430974409463873MSE v.s. MAE

Original: https://blog.csdn.net/qq_39154376/article/details/122596990
Author: 科大云炬
Title: [云炬python3玩转机器学习]5-5 衡量回归算法的标准,MSE vs MAE

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/635873/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球