Coursera ML(4)-Logistic Regression

本文详细介绍了逻辑回归模型,包括假设函数的表示方式、Sigmoid函数的应用、代价函数的定义及其简化形式,以及梯度下降法的具体步骤。此外还讨论了如何解决过拟合问题,包括正则化的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本节笔记对应第三周Coursera课程 binary classification problem


Classification is not actually a linear function.

Classification and Representation

Hypothesis Representation

  • Sigmoid Function(or we called Logistic Function)

    hθ(x)=g(θTx)z=θTxg(z)=11+ez

    Sigmoid Function 可以使输出值范围在 (0,1) 之间。 g(z) 对应的图为:

  • hθ(x) will give us the probability that our output is 1.

  • Some basic knowledge of discrete
    hθ(x)=P(y=1|x;θ)=1P(y=0|x;θ)P(y=0|x;θ)+P(y=1|x;θ)=1

Decision Boundary

  • translate the output of the hypothesis function as follows:
    hθ(x)0.5y=1hθ(x)<0.5y=0
  • From these statements we can now say:
    θTx0y=1θTx<0y=0

Logistic Regression Model

Cost function for one variable hypothesis

  • To let the cost function be convex for gradient descent, it should be like this:
    J(θ)=1mi=1mCost(hθ(x(i)),y(i))

Cost(hθ(x),y)={log(hθ(x)),(y=1)log(1hθ(x)),(y=0)

  • example
    Cost(hθ(x),y)=0 if hθ(x)=yCost(hθ(x),y) if y=0andhθ(x)1Cost(hθ(x),y) if y=1andhθ(x)0

Simplified Cost Function and Gradient Descent

  • compress our cost function’s two conditional cases into one case:

    Cost(hθ(x),y)=ylog(hθ(x))(1y)log(1hθ(x))

  • entire cost function

    J(θ)=1mi=1m[y(i)log(hθ(x(i)))+(1y(i))log(1hθ(x(i)))]

Gradient Descent

  • the general form of gradient descent ,求偏导的得到 J(θ) 的极值

    Repeat{θj:=θjαθjJ(θ)}

  • using calculus

    θjJ(θ)=1mi=1m[(hθ(x(i))y(i))x(i)j]

  • get

    Repeat{θj:=θjαmi=1m(hθ(x(i))y(i))x(i)j}

Multiclass Classification: One-vs-all

  • For more than 2 features of y, do logisitc regression for each feature separately
  • Train a logistic regression classifier hθ(x) for each class to predict the probability that  y = i .
  • To make a prediction on a new x, pick the class that maximizes hθ(x)

Solving the Problem of Overfitting

The Problem of Overfitting

mark

address the issue of overfitting

  • Reduce the number of features:
    • Manually select which features to keep.
    • Use a model selection algorithm (studied later in the course).
  • Regularization:
    • Keep all the features, but reduce the magnitude of parameters θj .
    • Regularization works well when we have a lot of slightly useful features.

Cost Function


- in a single summation

minθ 12m i=1m(hθ(x(i))y(i))2+λ j=1nθ2j

The λ, or lambda, is the regularization parameter. It determines how much the costs of our theta parameters are inflated.

Regularized Linear Regression

  • Gradient Descent

    Repeat {    θ0:=θ0α 1m i=1m(hθ(x(i))y(i))x(i)0    θj:=θjα [(1m i=1m(hθ(x(i))y(i))x(i)j)+λmθj]}          j{1,2...n}

  • Normal Equation

    θ=(XTX+λL)1XTywhere  L=0111

    • L is a matrix with 0 at the top left and 1’s down the diagonal, with 0’s everywhere else. It should have dimension (n+1)×(n+1)
    • Recall that if m ≤ n, then XTX is non-invertible. However, when we add the term λ⋅L, then XTX+λL becomes invertible.

Summary

我在这里整理一下上述两个方法,补全课程上的相关推导。

Logistic Regression Model

hθ(x) 是假设函数

hθ(x)=g(θTx)=11+eθTx

注意假设函数和真实数据之间的区别

Cost Function

J(θ)=1mi=1m[y(i) log(hθ(x(i)))+(1y(i)) log(1hθ(x(i)))]

回头看看上边的那个 hθ(x) ,cost function定义了训练集给出的结果 和 当前计算结果之间的差距。当然,该差距越小越好,那么需要求导一下。

Gradient Descent

  • 原始公式
    θj:=θjαθjJ(θ)
  • 求导计算
    θjJ(θ)=1mi=1m[(hθ(x(i))y(i))x(i)j]
  • 计算结果
    θj:=θjαmi=1m(hθ(x(i))y(i))x(i)j

这里推导一下 θjJ(θ)

  • 计算 hθ(x) 导数

    hθ(x)=(11+eθx)        =eθxx1+eθx        =1+eθx1(1+eθx)2x        =[11+eθx1(1+eθx)2]x        =hθ(x)(1hθ(x))x

  • 推导 θjJ(θ)

θjJ(θ)=θj1mi=1m[y(i) log(hθ(x(i)))(1y(i)) log(1hθ(x(i)))]              =1mi=1m[y(i) 1hθ(x(i))hθ(x(i))(1y(i))11hθ(x(i))hθ(x(i))]              =1mi=1m[y(i) 1hθ(x(i))hθ(x(i))(1hθ(x(i)))x(i)                                                   (1y(i))11hθ(x(i))hθ(x(i))(1hθ(x(i)))x(i)]              =1mi=1m[y(i)(1hθ(x(i))x(i))+(1y)hθ(x(i))x(i))]              =1mi=1m[x(i)y(i)+x(i)y(i)hθ(x(i))                                                                                    +x(i)hθ(x(i))x(i)y(i)hθ(x(i))]              =1mi=1m[(hθ(x(i))y(i))x(i)j]

即:

θjJ(θ)=1mi=1m[(hθ(x(i))y(i))x(i)j]

Solving the Problem of Overfitting

其他地方都一样,稍作修改
- Cost Function

J(θ)=1mi=1m[y(i) log(hθ(x(i)))+(1y(i)) log(1hθ(x(i)))]+λ2mj=1nθ2j

  • Gradient Descent
    Repeat {    θ0:=θ0α 1m i=1m(hθ(x(i))y(i))x(i)0    θj:=θjα [(1m i=1m(hθ(x(i))y(i))x(i)j)+λmθj]}          j{1,2...n}

以上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值