ML_study_task02

线性回归的概念

1.线性回归的原理

原理

极大似然估计(概率角度的诠释)

极大似然估计诠释线性回归原理

2.线性回归损失函数、代价函数、目标函数

  • 损失函数(Loss Function): 度量单样本预测的错误程度,损失函数值越小,模型越好。
  • 代价函数(Cost Function):度量全部样本集的平均误差。
  • 目标函数(Object Function):代价函数和正则化函数,最终要优化的函数
    思考题: 目标函数的设定是使结构化风险达到最小值,防止模型过拟合。

3.线性回归的优化方法

  1. 梯度下降法
    梯度下降法
  2. 最小二乘法矩阵求解
    在这里插入图片描述
  3. 牛顿法
    在这里插入图片描述
  4. 拟牛顿法

原理

4. 线性回归的评价指标

在这里插入图片描述

5.sklearn.linear_model参数详解

API详细解释

练习题

  1. 生成数据

```python
#生成数据
import numpy as np
#生成随机数
np.random.seed(1234)
x = np.random.rand(500,3)
#构建映射关系,模拟真实的数据待预测值,映射关系为y = 4.2 + 5.7*x1 + 10.8*x2,可自行设置值进行尝试
y = x.dot(np.array([4.2,5.7,10.8]))

 2. **先尝试调用sklearn的线性回归模型训练数据**
 

```python
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt

#调用模型
lr = LinearRegression(fit_intercept = True)
#训练模型
lr.fit(x,y)
print("估计的参数值为: %s" %(lr.coef_))
#计算R平方
print('R^2:%s' %(lr.score(x,y)))
#任意设定变量,预测目标值
x_test = np.array([2,4,5]).reshape(1,-1)
y_hat = lr.predict(x_test)
print("预测值为:%s" %(y_hat))
  1. 最小二乘法的矩阵求解
class LR_LS():
    def __init__(self):
        self.w = None      
    def fit(self, X, y):
        # 最小二乘法矩阵求解
        #============================= show me your code =======================
        self.w = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)
        #============================= show me your code =======================
    def predict(self, X):
        # 用已经拟合的参数值预测新自变量
        #============================= show me your code =======================
        y_pred = X.dot(self.w)
        #============================= show me your code =======================
        return y_pred

if __name__ == "__main__":
    lr_ls = LR_LS()
    lr_ls.fit(x,y)
    print("估计的参数值:%s" %(lr_ls.w))
    x_test = np.array([2,4,5]).reshape(1,-1)
    print(x_test)
    print("预测值为: %s" %(lr_ls.predict(x_test)))

4.梯度下降法

class LR_GD():
    def __init__(self):
        self.w = None     
    def fit(self,X,y,alpha=0.02,loss = 1e-10): # 设定步长为0.002,判断是否收敛的条件为1e-10
        y = y.reshape(-1,1) #重塑y值的维度以便矩阵运算
        [m,d] = np.shape(X) #自变量的维度
        self.w = np.zeros((d)) #将参数的初始值定为0
        tol = 1e5
        #============================= show me your code =======================
        while tol > loss:
            h_f = X.dot(self.w).reshape(-1,1) 
            theta = self.w + alpha*np.mean(X*(y - h_f),axis=0) #计算迭代的参数值
            tol = np.sum(np.abs(theta - self.w))
            self.w = theta
        #============================= show me your code =======================
    def predict(self, X):
        # 用已经拟合的参数值预测新自变量
        y_pred = X.dot(self.w)
        return y_pred  

if __name__ == "__main__":
    lr_gd = LR_GD()
    lr_gd.fit(x,y)
    print("估计的参数值为:%s" %(lr_gd.w))
    x_test = np.array([2,4,5]).reshape(1,-1)
    print("预测值为:%s" %(lr_gd.predict(x_test)))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值