做网站需要什么代码,免费咨询群,wordpress主题文章页面不显示,在郑州网站建设1、线性回归
例如#xff1a;对于一个房子的价格#xff0c;其影响因素有很多#xff0c;例如房子的面积、房子的卧室数量、房子的卫生间数量等等都会影响房子的价格。这些影响因子不妨用 x i x_{i} xi表示#xff0c;那么房价 y y y可以用如下公式表示#xff1a; y …1、线性回归
例如对于一个房子的价格其影响因素有很多例如房子的面积、房子的卧室数量、房子的卫生间数量等等都会影响房子的价格。这些影响因子不妨用 x i x_{i} xi表示那么房价 y y y可以用如下公式表示 y w 1 x 1 w 2 x 2 w 3 x 3 b yw_1x_1w_2x_2w_3x_3b yw1x1w2x2w3x3b 其中 w i w_{i} wi表示特征 i i i的权重 b b b表示偏置也称作截距当然在实际问题中 x i x_i xi为 0 0 0时 y y y肯定为 0 0 0而不可能为 b b b但是加上偏置后可以是模型的拟合效果更好。
2、损失的衡量
在分类问题时我们可以用准确率(预测正确的数量/测试集总样本数量)而在回归任务时衡量误差的损失函数通常使用均方误差设 y ′ y y′是预测值 y y y是真实值则损失函数为: l o s s 1 n ∑ i 1 n 1 2 ( y i ′ − y ) 2 loss \frac{1}{n}∑_{i1}^{n} \frac{1}{2}(y_{i}-y)² lossn1i1∑n21(yi′−y)2 这个又称作均方误差前面的系数 1 2 \frac{1}{2} 21是为了求导后与平方项的 2 2 2相乘时得到 1 1 1 可知均方误差越小拟合效果越好。反之拟合效果越差。 另外一个重要的衡量指标为 R 2 R^2 R2系数当 R 2 0.3 R^20.3 R20.3时拟合能力
3、优化损失
对于损失较大的时候如何优化权重 w w w和 b b b使其让我们的均方误差尽可能的小。这里提供两种方法。 a)使用正规方程进行优化 b)使用梯度下降进行优化。 正规方程依次即可求得最优解而梯度下降法需要逐次迭代寻找出最优解。但是对于大规模的数据集通常是采用梯度下降进行优化而正规方程在小规模数据集的优化上表现略由于梯度下降。
4、线性回归API及其调用
在sklearn中提供了线性回归的API根据优化方法不同分为以下两种
sklearn.linear_model.LinearRegression(fit_interceptTrue)
通过正规方程进行优化
fit_intercept:是否计算偏置默认为True,不计算偏置则模型一定过原点
LinearRegression.coef_:回归系数
LinearRegression。intercept_:偏置sklearn.linear_model.SGDRegressor(losssquared_loss, fit_interceptTrue, learning_rate invscaling, eta00.01)
loss:损失类型losssquared_loss 普通最小二乘法
fit_intercept:是否计算偏置
learning_rate学习率5、线性回归实例–波士顿房价预测(数据集点我
RM: 每个住宅的平均房间数 LSTAT: 区域内房东的地位表示低收入人群的百分比 PTRATIO: 区域内学生和教师的比例 MEDV: 自住房的中位数价值以千美元为单位
import pandas as pd
data pd.read_csv(housing.csv,sep,)# 检查是否具有缺失值全部为False,说明没有缺失值
pd.isnull(data).any()# 数据集的切分
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test train_test_split(data[[RM,LSTAT,PTRATIO]],data.MEDV,train_size0.8)
# 数据归一化
from sklearn.preprocessing import StandardScaler
transfer StandardScaler()
x_traintransfer.fit_transform(x_train)
x_testtransfer.transform(x_test)
import matplotlib.pyplot as plt
plt.rcParams[font.family]STFangsong
# 创建一个画布分成三个绘图区查看每个变量和目标值的关系
figure, axes plt.subplots(nrows1, ncols3, figsize(20, 8), dpi80)
axes[0].scatter(data.RM,data.MEDV)
axes[1].scatter(data.LSTAT,data.MEDV)
axes[2].scatter(data.PTRATIO,data.MEDV)
# 加网格透明度为0.5
axes[0].grid(linestyle--,alpha0.5)
axes[1].grid(linestyle--,alpha0.5)
axes[2].grid(linestyle--,alpha0.5)
plt.show()# 采用回归算法进行预测
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error,r2_score
# 使用正则化进行优化
estimator LinearRegression(fit_interceptTrue)
estimator.fit(x_train, y_train)
y_predict1 estimator.predict(x_test)
print(fr方系数为{r2_score(y_predict1,y_test)})
print(f方差为{mean_squared_error(y_predict1,y_test)})
print(f优化后的权重参数为:{estimator.coef_},偏置为:{estimator.intercept_})from sklearn.linear_model import SGDRegressor
estimator SGDRegressor(fit_interceptTrue)
estimator.fit(x_train, y_train)
y_predict1 estimator.predict(x_test)
print(fr方系数为{r2_score(y_predict1,y_test)})
print(f方差为{mean_squared_error(y_predict1,y_test)})
print(f优化后的权重参数为:{estimator.coef_},偏置为:{estimator.intercept_})观察可发现使用梯度下降优化的方差较小可认为其预测的拟合效果更好。