Coursera ML(4)-Logistic Regression

本节笔记对应第三周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}

以上

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Coursera-ml-andrewng-notes-master.zip是一个包含Andrew Ng的机器学习课程笔记和代码的压缩包。这门课程是由斯坦福大学提供的计算机科学和人工智能实验室(CSAIL)的教授Andrew Ng教授开设的,旨在通过深入浅出的方式介绍机器学习的基础概念,包括监督学习、无监督学习、逻辑回归、神经网络等等。 这个压缩包中的笔记和代码可以帮助机器学习初学者更好地理解和应用所学的知识。笔记中包含了课程中涉及到的各种公式、算法和概念的详细解释,同时也包括了编程作业的指导和解答。而代码部分包含了课程中使用的MATLAB代码,以及Python代码的实现。 这个压缩包对机器学习爱好者和学生来说是一个非常有用的资源,能够让他们深入了解机器学习的基础,并掌握如何运用这些知识去解决实际问题。此外,这个压缩包还可以作为教师和讲师的教学资源,帮助他们更好地传授机器学习的知识和技能。 ### 回答2: coursera-ml-andrewng-notes-master.zip 是一个 Coursera Machine Learning 课程笔记和教材的压缩包,由学生或者讲师编写。这个压缩包中包括了 Andrew Ng 教授在 Coursera 上发布的 Machine Learning 课程的全部讲义、练习题和答案等相关学习材料。 Machine Learning 课程是一个介绍机器学习课程,它包括了许多重要的机器学习算法和理论,例如线性回归、神经网络、决策树、支持向量机等。这个课程的目标是让学生了解机器学习的方法,学习如何使用机器学习来解决实际问题,并最终构建自己的机器学习系统。 这个压缩包中包含的所有学习材料都是免费的,每个人都可以从 Coursera 的网站上免费获取。通过学习这个课程,你将学习到机器学习的基础知识和核心算法,掌握机器学习的实际应用技巧,以及学会如何处理不同种类的数据和问题。 总之,coursera-ml-andrewng-notes-master.zip 是一个非常有用的学习资源,它可以帮助人们更好地学习、理解和掌握机器学习的知识和技能。无论你是机器学习初学者还是资深的机器学习专家,它都将是一个重要的参考工具。 ### 回答3: coursera-ml-andrewng-notes-master.zip是一份具有高价值的文件,其中包含了Andrew Ng在Coursera上开授的机器学习课程笔记。这份课程笔记可以帮助学习者更好地理解掌握机器学习技术和方法,提高在机器学习领域的实践能力。通过这份文件,学习者可以学习到机器学习的算法、原理和应用,其中包括线性回归、逻辑回归、神经网络、支持向量机、聚类、降维等多个内容。同时,这份笔记还提供了很多代码实现和模板,学习者可以通过这些实例来理解、运用和进一步深入研究机器学习技术。 总的来说,coursera-ml-andrewng-notes-master.zip对于想要深入学习和掌握机器学习技术和方法的学习者来说是一份不可多得的资料,对于企业中从事机器学习相关工作的从业人员来说也是进行技能提升或者知识更新的重要资料。因此,对于机器学习领域的学习者和从业人员来说,学习并掌握coursera-ml-andrewng-notes-master.zip所提供的知识和技能是非常有价值的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值