Overfitting문제를 해결하기 위해서는 Feature의 수를 줄여야 한다. 하지만 Feature의 수를 줄일 수 없는 경우에는 Feature의 값(영향)을 줄여야 한다. 이것이 Regularization이다.

위와 같이 높은 차수의 모델을 사용할 경우 Overfitting된다. 따라서 Cost Functino에서 높은 차수의 parameter에 Penalty를 둔다.
$$
J(\theta)=\frac{1}{2 m} \sum_{i=1}^{m}\left(h_{\theta}\left(x^{(i)}\right)-y^{(i)}\right)^{2}+\textcolor{gold}{1000 \theta_{3}^{2}+1000 \theta_{4}^{2}} $$
Cost Function은 최소가 되도록 업데이트한다. 이때 $\theta_3, \theta_4$와 같은 높은 차수의 parameter가 커지게 되면 앞에 붙은 계수로 인해 오차가 매우 커진다. 이러한 방법으로 높은 차수의 parameter의 영향을 줄이는 것이다.
위를 일반화 시켜보자.
overfitting을 줄이는 간단한 Hypothesis를 만들기 위해서 parameter가 작아야 한다.
100개의 features $x_1, x_2, \dots, x_{100}$과 Parameter $\theta_1, \theta_2, \dots, \theta_{100}$가 있다고 할 떄 Cost Fucntion을 아래와 같이 작성한다.
$$
J(\theta)=\frac{1}{2 m}\left[ \sum_{i=1}^{m}\left(h_{\theta}\left(x^{(i)}\right)-y^{(i)}\right)^{2}+\large\textcolor{gold}{\lambda\sum_{j=1}^{100} \theta_{j}^{2}}\right] $$
여기서 $\lambda$가 penalty이다. 만약 $\lambda$가 너무 클 경우 모든 parameter가 0으로 수렴할 가능성이 있다. 이런 경우를 underfit이라고 부른다.
Linear regression에서 Parameter를 정하는 두 가지 방법이 있었다. 새로 정의한 Cost Function으로 풀어보자.
$$ \theta_j = \theta_j -\alpha\frac{\partial}{\partial\theta_j}J(\theta)\\
\theta_j = \theta_j - \alpha\left[ \frac{1}{m}\sum_{i=1}^{m}(h_\theta(x^{(i)})-y^{(i)}){x_j}^{(i)} + \frac{\lambda}{m}\theta_j \right]\\
= \theta_j\textcolor{gold}{\large\left( 1 - \alpha\frac{\lambda}{m}\right)} - \alpha{1\over m}\sum_{i=1}^m(h_\theta(x^{(i)}) - y^{(i)}){x_j}^{(i)} $$