> ## Content Index
> Fetch the complete content index at: https://www.popit.kr/llms.txt
> Use this file to discover other available public pages before exploring further.

# Coursera Machine Learning으로 기계학습 배우기 : week3
- URL: https://www.popit.kr/coursera-machine-learning-week3/
- Published: 2017-07-30T23:16:26.000Z
- Updated: 2017-10-14T05:00:14.000Z
- Author: 광식 이
- Tags: #소프트웨어, ai, 기계학습, coursera, MachineLearning

# 개요

지난 시간에 이어 Coursera Machine Learning으로 기계학습 배우기 : week3 정리를 진행한다.

# 목차

해당 포스팅은 연재글로써 지난 연재는 아래의 링크를 참고한다.

- [Coursera Machine Learning으로 기계학습 배우기 : week1](http://www.kwangsiklee.com/ko/2017/07/corsera-machine-learning-week1-%EC%A0%95%EB%A6%AC/?ref=popit.kr)
- [Coursera Machine Learning으로 기계학습 배우기 : week2](http://www.kwangsiklee.com/ko/2017/07/corsera-machine-learning%EC%9C%BC%EB%A1%9C-%EA%B8%B0%EA%B3%84%ED%95%99%EC%8A%B5-%EB%B0%B0%EC%9A%B0%EA%B8%B0-week2/?ref=popit.kr)

# 글을 읽기에 앞서…

- 본글은 필자가 코세라 기계학습을 공부를 하는 과정에서 개념을 확고히 정리하기 위하는데 목적이 있다. (필자가 나중에 내용을 다시 찾아보기 위한 목적이 있다.)
- 코세라 강의 week 개수에 맞추어 포스팅을 진행할 예정이다.
- 코세라의 슬라이드에 한글 주석을 단것이 핵심으로 내용에서 글을 읽을 필요 없이 슬라이드 그림만으로 최대한 이해가 되게끔 하는데 목적이 있다.
- 수학은 한국의 고등수학을 베이스로 한다. 수학적 개념이 나올때 가급적 고등학교 수학을 베이스로 내용을 정리한다.
- 정리내용의 목차 구성을 코세라 강의와 동일하게 맞추고 또한 제목을 원문으로 둔다. (원본강의 내용과 정리 내용을 서로 서로 찾아보기 쉽게하기 위함이다.)

\====================== 3강 ============================================

# Classification and Representation

## Classification

보통 분류하기를 원하는 결과를 Positive로 두는 경향이 있다.

![](http://www.kwangsiklee.com/wp-content/uploads/2017/07/logisticregressionmodel0090.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/classificationandrepresentation0100-1.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/classificationandrepresentation0200-1.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/classificationandrepresentation0300.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/classificationandrepresentation0400.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/classificationandrepresentation0500.png)

## Hypothesis Representation

![](http://www.kwangsiklee.com/wp-content/uploads/2017/07/classificationandrepresentation0595.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/classificationandrepresentation0590.png)

로지스틱 함수(g함수) 중에 하나가 sigmoid 함수이다.

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/classificationandrepresentation0600.png)

로지스틱 회귀의 장점 중 하나는 출력값의 확률(가능성)을 가늠할 수 있다는 것이다.

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/classificationandrepresentation0700.png)

## Decision Boundary

![](http://www.kwangsiklee.com/wp-content/uploads/2017/07/classificationandrepresentation0800.png)

사실 선형의 결정경계를 구하는 경우의 수는 무한대에 가깝다.

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/classificationandrepresentation0900.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/classificationandrepresentation0990.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/classificationandrepresentation0995.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/classificationandrepresentation1000.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/classificationandrepresentation1100.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/classificationandrepresentation1200.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/classificationandrepresentation1500.png)

# Logistic Regression Model

## Cost Function

![](http://www.kwangsiklee.com/wp-content/uploads/2017/07/logisticregressionmodel0090.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel0100.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel0200.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel0300.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel0400.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel0500.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel0600.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel0800.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel0900.png)

## Simplified Cost Function and Gradient Descent

![](http://www.kwangsiklee.com/wp-content/uploads/2017/07/logisticregressionmodel0990.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel1000.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel1100.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel1200.png)

**선형 회귀의 경사감소법과 로지스틱 회귀의 경사감소법 수식은 완전히 똑같다. 다만 h(x) 정의가 다르다.** 그리고 수식의 의미는 가설함수의 값과 정답지의 값의 차이를 입력값의 크기에 비례해서 세타값을 줄이라는 의미이다.

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel1300.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel1400.png)

## Advanced Optimization

![](http://www.kwangsiklee.com/wp-content/uploads/2017/07/logisticregressionmodel1490.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel1500.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel1600.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel1700.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel1800.png)

이부분을 옥타브를 이용하여 직접 실습하여 보자.

아래와 같이 costFunction 함수를 만든다.

costFunction.m  

```matlab
function [jVal, gradient] = costFunction(theta)
jVal = (theta(1) - 5) ^ 2 + (theta(2) - 5) ^ 2

gradient = zeros(2, 1);
gradient(1) = 2 * (theta(1) - 5);
gradient(2) = 2 * (theta(2) - 5);
```

이제 옥타브 커맨드라인에서 costFunction.m이 있는 경로에서 아래의 3줄의 명령어를 입력해본다.  

```matlab
>> options = optimset('GradObj', 'on', 'MaxIter', '100');
>> initTheta = zeros(2, 1);
>> [optTheta, functionVal, exitFlag] = fminunc(@costFunction, initTheta, options);
```

그러면 알파값을 지정하지 않았음에도 \*\*fminunc()\*\*에 의해 비용함수가 최소화 된것을 확인 할 수 있다.

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel1910.png)

또한 쎄타값을 확인해보면 세타값이 기존의 0에서 바뀐것을 확인 할 수 있다.

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/logisticregressionmodel2000.png)

# Multiclass Classification

![](http://www.kwangsiklee.com/wp-content/uploads/2017/07/multiclassclassification0090-2.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/multiclassclassification0100.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/multiclassclassification0200.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/multiclassclassification0300.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/multiclassclassification0400.png)

# Solving Problem of Overfitting

## The Problem of Solving Overfitting

![](http://www.kwangsiklee.com/wp-content/uploads/2017/07/solvingtheproblemofoverfitting0090.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting0100.png)

bias와 variance의 딜레마다.

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting0200.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting0300.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting0400.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting0500.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting0600.png)

## Cost Function

![](http://www.kwangsiklee.com/wp-content/uploads/2017/07/solvingtheproblemofoverfitting0690.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting0700.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting0800.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting0900.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting1000.png)

## Regular Linear Regression

![](http://www.kwangsiklee.com/wp-content/uploads/2017/07/solvingtheproblemofoverfitting1090.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting1000.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting1100.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting1150.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting1160.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting1170.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting1200.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting1250.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting1300.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting1400.png)

## Regularized Logistic Regression

![](http://www.kwangsiklee.com/wp-content/uploads/2017/07/solvingtheproblemofoverfitting1490.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting1500.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting1600.png)

![](http://www.kwangsiklee.com/wp-content/uploads/2017/03/solvingtheproblemofoverfitting1700.png)