优化方法:
正规方程“天才”
直接求解W
梯度下降“努力”
试错,改进(更为通用)
其中系数阿尔法可以理解为步长,后边偏导数代表方向,然后不断进行迭代至偏导为0
可能会产生局部最优解,这里暂且不提
线性回归API:
案例:
波士顿房价预测:
流程:
获取数据集(已处理)
划分数据集
特征工程: 无量纲化(标准化)
预估器流程:fit() ——> 模型:coef_intercept_
模型评估:学习率可调
回归性能评估及API:
from sklearn.datasets import load_boston from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LinearRegression, SGDRegressor from sklearn.metrics import mean_squared_error def liner1(): """ 正规方程的优化方法对波士顿房价进行预测 :return: """ # 1、获取数据 boston = load_boston() print("特征数量:\n", boston.data.shape) # 2、划分数据集 x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=22) # 3、标准化 transfer = StandardScaler() x_train = transfer.fit_transform(x_train) x_test = transfer.fit_transform(x_test) # 4、预估器 estimator = LinearRegression() estimator.fit(x_train, y_train) # 5、得出模型 print("正规——权重系数为:\n", estimator.coef_) print("正规——偏置为:\n", estimator.intercept_) # 6、模型评估 y_predict = estimator.predict(x_test) # print("预测房价:\n", y_predict) error = mean_squared_error(y_test, y_predict) print("正规——均方误差:\n", error) return None def liner2(): """ 梯度下降的优化方法对波士顿房价进行预测 :return: """ # 1、获取数据 boston = load_boston() # 2、划分数据集 x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=22) # 3、标准化 transfer = StandardScaler() x_train = transfer.fit_transform(x_train) x_test = transfer.fit_transform(x_test) # 4、预估器 estimator = SGDRegressor() estimator.fit(x_train, y_train) # 5、得出模型 print("梯度——权重系数为:\n", estimator.coef_) print("梯度——偏置为:\n", estimator.intercept_) # 6、模型评估 y_predict = estimator.predict(x_test) # print("预测房价:\n", y_predict) error = mean_squared_error(y_test, y_predict) print("梯度——均方误差:\n", error) return None if __name__ == "__main__": # 正规方程预测 liner1() # 梯度下降预测 liner2()
梯度下降优化器:
GD:梯度下降
SGD:随机梯度下降
SAG:随机平均梯度下降
欠拟合与过拟合
由于正规化方法很难解决过拟合问题所以比较少用
![]()
过拟合:在训练集上表现良好,但是在测试集上表现不好
解决:正则化
损失函数 + 惩罚项:Rige - 岭回归:L2
LASSO:L1
欠拟合:在训练集和测试集上都不能很好的拟合
解决:增加数据的特征量
线性回归的改进 -- 岭回归
岭回归也是一种线性回归,只不过在算法建立回归方程的时候,加上正则化限制,从而达到解决过拟合的问题。
API:
alpha就是惩罚项的系数
![]()
![]()
案例:
依然是波士顿房价预测的案例:
from sklearn.datasets import load_boston from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge from sklearn.metrics import mean_squared_error def liner1(): """ 正规方程的优化方法对波士顿房价进行预测 :return: """ # 1、获取数据 boston = load_boston() print("特征数量:\n", boston.data.shape) # 2、划分数据集 x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=22) # 3、标准化 transfer = StandardScaler() x_train = transfer.fit_transform(x_train) x_test = transfer.fit_transform(x_test) # 4、预估器 estimator = LinearRegression() estimator.fit(x_train, y_train) # 5、得出模型 print("正规——权重系数为:\n", estimator.coef_) print("正规——偏置为:\n", estimator.intercept_) # 6、模型评估 y_predict = estimator.predict(x_test) # print("预测房价:\n", y_predict) error = mean_squared_error(y_test, y_predict) print("正规——均方误差:\n", error) return None def liner2(): """ 梯度下降的优化方法对波士顿房价进行预测 :return: """ # 1、获取数据 boston = load_boston() # 2、划分数据集 x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=22) # 3、标准化 transfer = StandardScaler() x_train = transfer.fit_transform(x_train) x_test = transfer.fit_transform(x_test) # 4、预估器 estimator = SGDRegressor() estimator.fit(x_train, y_train) # 5、得出模型 print("梯度——权重系数为:\n", estimator.coef_) print("梯度——偏置为:\n", estimator.intercept_) # 6、模型评估 y_predict = estimator.predict(x_test) # print("预测房价:\n", y_predict) error = mean_squared_error(y_test, y_predict) print("梯度——均方误差:\n", error) return None def liner3(): """ 岭回归的优化方法对波士顿房价进行预测 :return: """ # 1、获取数据 boston = load_boston() # 2、划分数据集 x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=22) # 3、标准化 transfer = StandardScaler() x_train = transfer.fit_transform(x_train) x_test = transfer.fit_transform(x_test) # 4、预估器 estimator = Ridge() estimator.fit(x_train, y_train) # 5、得出模型 print("岭回归——权重系数为:\n", estimator.coef_) print("岭回归——偏置为:\n", estimator.intercept_) # 6、模型评估 y_predict = estimator.predict(x_test) # print("预测房价:\n", y_predict) error = mean_squared_error(y_test, y_predict) print("岭回归——均方误差:\n", error) return None if __name__ == "__main__": # 正规方程预测 liner1() # 梯度下降预测 liner2() # 岭回归预测 liner3()
逻辑回归与二分类
应用场景:
广告点击率,垃圾邮件,是否患病,金融诈骗,虚假账号,,,均属于二分类
原理:
输入:
线性回归的输出就是逻辑回归的输入
激活函数:
![]()
大于0.5认为是这个类别,小于0.5认为不是这个类别
![]()
假设函数/线性模型
损失函数:
这里均方和的损失已经不满足了,所以引入了对数似然函数:
这里纵轴就可以理解为损失函数的大小,h为预测值,y为真实值,当y=h=1时由上图可见损失函数接近于0,而当y=h=0时损失函数也接近于0,这正是我们想要的结果,反之损失函数趋向于无穷。
优化损失:
梯度下降
API:
![]()
案例:
评估: