-
回归问题的判定
-
目标值是连续性的值,而分类问题的目标值是离散的值
-
-
回归处理的问题为预测:
-
预测房价
-
销售额的预测
-
设定贷款额度
-
总结:在上述案例中,可以根据事物的相关特征预测出对应的结果值
-
-
线性回归在生活中的映射:生活案例【预测学生的期末成绩】:
-
期末成绩的制定根据平时成绩加考试成绩,该例子中,特征值为考试成绩和平时成绩,目标值为总成绩
-
回归算法预测出来的结果其实就是经过相关的算法计算出来的结果值
-
每一个特征需要有一个权重的占比,这个权重的占比明确后,则就可以得到最终的计算结果,也就是获得了最终预测的结果
-
-
最小二乘法
-
求解让rss最小化的参数向量w,这种通过最小化真实值和预测值之间的rss来求解参数的方法叫做最小二乘法
-
求解极值(最小值)的第一步往往是求解一阶导数并让一阶导数等于0,最小二乘法也不能免俗,因此,我们现在在残差平方和rss上对参数向量w求导
-
w表示的是一个列向量(矩阵),我们现在并非是对常熟求导,而是对列向量(矩阵)求导
使用
-
API:最小二乘(正规方程):from skleaen.lineat_modil import LinearRegression
-
使用最小二乘对加利福尼亚房价进行预测
-
特征介绍:
-
avebedrms:该街区平均的卧室数目
-
population:街区人口
-
aveoccup:街区的纬度
-
longitude:街区的经度
-
medinc:街区住户收入的中位数
-
houseage:房屋使用年数的中位数
-
averooms:街区平均房屋的数量
-
-
代码:
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.datasets import fetch_california_housing as fch
feature = fch().data
target = fch().target
print(feature.shape)
print(target.shape)
print("=========================================")
x_train, x_test, y_train, y_test = train_test_split(feature, target, test_size=0.1, random_state=2020)
linner = LinearRegression()
linner.fit(x_train, y_train)
coef = linner.coef_ # 返回w系数
intercept = linner.intercept_ # 截距
print("返回的八个系数:\n", coef)
print("返回的截距:\n", intercept)
(20640, 8)
(20640,)
=========================================
返回的八个系数:
[ 4.38210243e-01 9.66229111e-03 -1.08329143e-01 6.52754576e-01
-4.79939625e-06 -3.60231961e-03 -4.21938498e-01 -4.34993681e-01]
返回的截距:
-36.99211044654091
Process finished with exit code 0
回归模型评价指标
方法一
-
使用sklearn专用的模型评估模块metrics里的类mean_squared_error
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.datasets import fetch_california_housing as fch
from sklearn.metrics import mean_squared_error # 模型评估模块类
feature = fch().data
target = fch().target
x_train, x_test, y_train, y_test = train_test_split(feature, target, test_size=0.1, random_state=2020)
linner = LinearRegression()
linner.fit(x_train, y_train)
coef = linner.coef_ # 返回w系数
intercept = linner.intercept_ # 截距
y_true = y_test
y_pred = linner.predict(x_test)
# 平均误差
mean_squard = mean_squared_error(y_true, y_pred)
max_squard = y_true.max()
min_squard = y_true.min()
print(mean_squard)
print(max_squard)
print(min_squard)
0.5273022319571916
5.00001
0.325
方法二
-
调用交叉验证的类cross_val_score并使用里面的scoring参数来设置为:neg_mean_squared_error使用均方误差
-
在sklearn计算模型评估指标的时候,会考路指标本身的性质,均方误差本身是一种误差,所以被sklearn划分为模型的一种损失,在sklearn中,所有的损失都使用负数表示,所以均方误差也被显示为附属。真正的均方误差MSE的数值,就是去掉负号的值
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.datasets import fetch_california_housing as fch
from sklearn.model_selection import cross_val_score # 交叉验证的MSE
feature = fch().data
target = fch().target
x_train, x_test, y_train, y_test = train_test_split(feature, target, test_size=0.1, random_state=2020)
linner = LinearRegression()
linner.fit(x_train, y_train)
coef = linner.coef_ # 返回w系数
intercept = linner.intercept_ # 截距
y_true = y_test
y_pred = linner.predict(x_test)
# 交叉验证
JcYz = cross_val_score(linner, x_train, y_train, cv=5, scoring='neg_mean_squared_error').mean()
print(JcYz)
-0.5312602263524762
是否拟合到足够信息
浙公网安备 33010602011771号