{"componentChunkName":"component---src-templates-post-template-js","path":"/211024-ml-regression/","result":{"data":{"allMarkdownRemark":{"edges":[{"node":{"id":"12c409fd-457c-5119-b60d-ef7d9d99e997","html":"<h1>Linear Regression (선형 회귀)</h1>\n<hr />\n<ul>\n<li>\n<p><strong>독립 변수</strong> : ‘x값이 변함에 따라 y값도 변한다’는 이 정의 안에서, 독립적으로 변할 수 있는 x값</p>\n</li>\n<li>\n<p><strong>종속 변수</strong> : 독립변수에 따라 종속적으로 변하는 값</p>\n</li>\n</ul>\n<blockquote>\n<p>선형회귀란 독립 변수 x를 사용해 종속 변수 y의 움직임을 예측하고 설명하는 작업을 말한다.</p>\n</blockquote>\n<ul>\n<li>\n<p><strong>단순 선형 회귀(simple linear regression)</strong> : 하나의 x값 만으로도 y값을 설명할 수 있을 때</p>\n</li>\n<li>\n<p><strong>다중 선형 회귀(multiple linear regression)</strong> : x값이 여러 개 필요할 때</p>\n</li>\n</ul>\n<h2>단순선형회귀 예시</h2>\n<br />\n<table>\n <thead>\n    <tr align=\"center\">\n        <th style=\"background-color: lightgray;\">공부한 시간\u001f</th>\n        <th style=\"background-color: lightgray;\">2시간</th>\n        <th style=\"background-color: lightgray;\">4시간</th>\n        <th style=\"background-color: lightgray;\">6시간</th>\n        <th style=\"background-color: lightgray;\">8시간</th>\n    </tr>\n </thead>\n <tbody>\n    <tr align=\"center\">\n        <td>성적</td>\n        <td>81점</td>\n        <td>93점</td>\n        <td>91점</td>\n        <td>97점</td>\n    </tr>\n </tbody>\n</table>\n<ul>\n<li>\n<p>여기서 공부한 시간을 x라 하고 성적을 y라 할 때 집합 X와 집합 Y를 다음과 같이 표현할 수 있다.</p>\n<p><strong>X = {2, 4, 6, 8}</strong></p>\n<p><strong>Y = {81, 93, 91, 97}</strong></p>\n</li>\n</ul>\n<br />\n<ul>\n<li>\n<p><strong>최소 제곱법</strong> (method of least squares)</p>\n<p><strong>a = (x - x평균\u001d)(y - y평균\u001d)의 합 / (x - x의 평균\u001d)의 제곱의 합</strong></p>\n<p>➡️ 46 / 20 = <strong>2.3</strong></p>\n</li>\n</ul>\n<br />\n<ul>\n<li>\n<p><strong>y절편인 b를 구하는 공식</strong></p>\n<p>b = y의 평균 - (x의 평균 X 기울기 a)</p>\n<p>➡️ b = 90.5 - (5 X 2.3) = 79</p>\n<p>➡️ y = 2.3x + 79</p>\n</li>\n</ul>\n<br />\n<table>\n <thead>\n    <tr align=\"center\">\n        <th style=\"background-color: lightgray;\">공부한 시간\u001f</th>\n        <th style=\"background-color: lightgray;\">2시간</th>\n        <th style=\"background-color: lightgray;\">4시간</th>\n        <th style=\"background-color: lightgray;\">6시간</th>\n        <th style=\"background-color: lightgray;\">8시간</th>\n    </tr>\n </thead>\n <tbody>\n    <tr align=\"center\">\n        <td>성적</td>\n        <td>81점</td>\n        <td>93점</td>\n        <td>91점</td>\n        <td>97점</td>\n    </tr>\n    <tr align=\"center\">\n        <td>예측 값</td>\n        <td>83.6</td>\n        <td>88.2</td>\n        <td>92.8</td>\n        <td>97.4</td>\n    </tr>\n </tbody>\n</table>\n<br />\n<div style=\"text-align:center\">\n<img src=\"https://i.ibb.co/SmR9ky8/2021-10-25-3-16-11.png\" width=\"500\">\n</div>\n<br />\n<h1>오차수정 (잘못 그은 선 바로잡기)</h1>\n<hr />\n<ul>\n<li>\n<p>일단 선을 그리고 ➡️ 조금씩 수정해 나가기</p>\n</li>\n<li>\n<p>언제까지? <strong>오차가 최소가 될 때까지</strong></p>\n</li>\n<li>\n<p>기울기를 너무 작게 잡았을 때의 오차 예시</p>\n<div>\n  <img src=\"https://i.ibb.co/Kq8PH4p/2021-10-25-3-26-55.png\" width=\"500\">\n  </div>\n<p><strong>오차 = 예측 값 – 실제 값</strong></p>\n</li>\n</ul>\n<blockquote>\n<p>적절한 기울기를 찾았을 때 오차가 최소화된다.</p>\n</blockquote>\n<h2><strong>학습률(learning rate)</strong></h2>\n<blockquote>\n<p>선을 어느 만큼 이동시킬지를 신중히 결정해야 하는데, 이때 이동 거리를 정해주는 것을 학습률(learning rate)이라 한다.</p>\n</blockquote>\n<div style=\"text-align: center\">\n  <img src=\"https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FlPCKi%2FbtqE9KemmiM%2FFnnzdLjFMtfU1s5r5vdrc1%2Fimg.jpg\" width=\"350\">\n</div>\n<ul>\n<li>학습률을 크게 설정하면 최적의 값을 제대로 찾지 못한다.</li>\n</ul>\n<br />\n<div style=\"text-align: center\">\n  <img src=\"https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FJRSVw%2FbtqFa7TIFhh%2Fxs8LZb6AtHKmE1TNdjUCI1%2Fimg.jpg\" width=\"350\">\n</div>\n<ul>\n<li>\n<p>그렇다고 학습률을 작게 설정하면 최적의 값으로 수렴할 때까지 시간이 오래 걸린다.</p>\n</li>\n<li>\n<p>모델을 학습시킬 때는 효율적으로 파라미터를 조정하면서도 결국 최적의 값을 찾아 수렴할 수 있을 수준으로 최적의 학습률을 찾는 게 중요하다.</p>\n</li>\n</ul>\n<h2><strong>경사하강법(Gradient Descent)</strong></h2>\n<blockquote>\n<p>오차의 변화에 따라 이차 함수 그래프를 만들고 적절한 학습률을 설정해 미분 값이 0인 지점을 구하는 것을 경사하강법(Gradient Descent)이라 한다.</p>\n</blockquote>\n<div style=\"text-align: center\">\n  <img src=\"https://thebook.io/img/080228/076_2.jpg\" width=\"350\">\n</div>\n<ol>\n<li>\n<p>임의의 점 a1에서 미분하여 기울기를 구한다.</p>\n</li>\n<li>\n<p>구해진 기울기의 반대 방향(기울기가 +면 음의 방향, -면 양의 방향)으로 얼마간 이동시킨 a2에서 미분하여 기울기 구한다.</p>\n</li>\n<li>\n<p>미분 값이 0이 될 때 까지 왔다갔다하며 반복한다.</p>\n</li>\n</ol>\n<p><strong>➡️ 파라미터를 임의로 정한 다음에 조금씩 변화시켜가며 손실을 점점 줄여가는 방법으로 최적의 파라미터를 찾아간다.</strong></p>\n<br />\n<h3>평균 제곱 오차(Mean Squared Error, MSE)</h3>\n<p><strong>평균 제곱 오차(MSE)는 “비용함수”라고도 한다.</strong></p>\n<br />\n<div style=\"text-align: center\">\n  <img src=\"https://media.vlpt.us/images/yuns_u/post/818c1992-3a20-4453-8649-c8960dfb9648/image.png\" width=\"350\">\n</div>\n<p><strong>오차를 제곱</strong>하면 위 이미지와 같이 해당 거리의 길이가 한 변인 정사각형의 넓이가 된다.</p>\n<ul>\n<li>\n<p>이 때, <strong>왜 제곱을 하는 것일까?</strong></p>\n<ul>\n<li>\n<p>거리를 제곱함으로서 <strong>항상 양수인 거리값</strong>을 얻을 수 있다.</p>\n</li>\n<li>\n<p>두 개의 회귀를 비교했을 때 그 차이가 커지기 때문에 <strong>더 쉽게 비교</strong>할 수 있다.</p>\n</li>\n</ul>\n</li>\n</ul>\n<br />\n<div style=\"text-align: center\">\n  <img src=\"https://blog.kakaocdn.net/dn/qJowI/btqBBUCMNDv/L7bfq2lu0hfsZzCSDx0E5k/img.png\" width=\"350\">\n</div>\n<br />\n<p>수식에 대해 간단히 설명하면 <strong>거리를 제곱한 값을 모두 더한 후 평균을 낸 것</strong>이다.</p>\n<ul>\n<li>\n<p>이 평균 제곱 오차가 바로 위의 <strong>경사하강법</strong>에서 봤던 <strong>오차-x 그래프</strong>를 나타낸 것이다.</p>\n</li>\n<li>\n<p>이는 y</p>\n</li>\n</ul>\n<br />\n<h1>Polynomial Regression (다항 회귀)</h1>\n<hr />\n<blockquote>\n<p>항이 여러 개인 가설 함수로 결과를 예측하는 회귀 분석 방법이다.</p>\n</blockquote>\n<p><strong>항은 제곱근이나 2차항, 3차항 등 다양하게 있으며 함수의 형태가 nonlinear인 특징이 있다.</strong></p>\n<ul>\n<li>\n<p>해당 data 분포 함수의 degree보다 크게 높은 degree는 <strong>overfitting</strong>이 발생하고, 낮은 degree는 <strong>underfitting</strong>이 발생할 수 있다.</p>\n<ul>\n<li>그러한 결과를 확인한 경우 <strong>더 복잡한 모델</strong>을 사용하거나 <strong>더 나은 features</strong>를 제공해야 한다. (<strong>underfitting</strong>의 경우 <strong>training examples</strong>을 더 추가해도 도움이 되지 않는다.)</li>\n</ul>\n</li>\n</ul>\n<br />\n<h2>reduce overfitting - Regularization (규제, 정규화)</h2>\n<ul>\n<li>\n<p><strong>overfitting</strong>을 줄이는 방법은 모델을 <strong>정규화</strong>하는 것이다.</p>\n</li>\n<li>\n<p>더 적은 자유도 (Degrees of freedom)는 데이터를 과적합하기 더 어려워진다.</p>\n</li>\n</ul>\n<blockquote>\n<p><strong>다항식 모델을 정규화하는 간단한 방법은 다항식 차수의 수를 줄이는 것이다.</strong></p>\n</blockquote>\n<br />\n<h3><strong>Ridge Regression</strong></h3>\n<ul>\n<li>\n<p>선형 회귀의 정규화된 버전이다.</p>\n</li>\n<li>\n<p>비용 함수에 정규화 항  <span><img src=\"https://yganalyst.github.io/assets/images/ML/chap3/ridge1.png\"></span>  이 추가된다.</p>\n</li>\n</ul>\n<br />\n<div style=\"text-align: center\">\n    <img src=\"https://yganalyst.github.io/assets/images/ML/chap3/ridge2.png\" width=\"300\">\n</div>\n<br />\n<h3>Lasso Regression</h3>\n<ul>\n<li>릿지 회귀처럼 비용함수에 규제항을 더하지만 <strong>ℓ2norm의 제곱을 2로 나눈 것 대신 가중치 벡터의 ℓ1norm을 사용한다.</strong></li>\n</ul>\n<br />\n<div style=\"text-align: center\">\n    <img src=\"https://yganalyst.github.io/assets/images/ML/chap3/lasso1.png\" width=\"300\">\n</div>\n<ul>\n<li><strong>Lasso Regression</strong>의 중요한 특징은 덜 중요한 변수의 가중치를 완전히 제거하려고 한다는 점이다.</li>\n</ul>\n<br />\n<h2>reduce overfitting - Early Stopping</h2>\n<blockquote>\n<p>반복 학습 알고리즘(예: Gradient Descent)을 정규화하는 매우 다른 방법은 유효성 검사 오류가 최소값에 도달하는 즉시 학습을 중지하는 것이다.</p>\n</blockquote>\n<div style=\"text-align: center\">\n    <img src=\"https://www.oreilly.com/library/view/hands-on-machine-learning/9781491962282/assets/mlst_0415.png\" width=\"500\">\n</div>\n<br />\n<h1>Logistic Regression (로지스틱 회귀)</h1>\n<hr />\n<blockquote>\n<p>로지스틱 회귀는 회귀를 사용하여 데이터가 어떤 범주에 속할 확률을 0에서 1 사이의 값으로 예측하고 그 확률에 따라 가능성이 더 높은 범주에 속하는 것으로 분류해주는 지도 학습 알고리즘이다.</p>\n</blockquote>\n<p>어떤 메일을 받았을 때 그것이 스팸일 확률이 0.5 이상이면 스팸으로 분류하고, 확률이 0.5보다 작은 경우 일반 메일로 분류하는 것이다. 이렇게 <strong>데이터가 2개의 범주 중 하나에 속하도록 결정하는 것</strong>을 <strong>이진 분류(binary classification)</strong> 라고 한다.</p>\n<ul>\n<li>\n<p><strong>ex)</strong> “공부한 시간이 적으면 시험에 통과 못하고, 공부한 시간이 많으면 시험에 통과한다”</p>\n<div style=\"text-align: center\">\n  <img src=\"https://hleecaster.com/wp-content/uploads/2019/12/logreg02.png\" width=\"700\">\n  </div>\n</li>\n</ul>\n<h2>Decision Boundary (결정 경계)</h2>\n<blockquote>\n<p>Decision Boundary란 결과값 y를 클래스에 대하여 두 개의 집합으로 나누는 기준선이다.</p>\n</blockquote>\n<div style=\"text-align: center\">\n    <img src=\"https://www.oreilly.com/library/view/hands-on-machine-learning/9781491962282/assets/mlst_04in07.png\" width=\"700\">\n</div>\n<br />\n<h2>Softmax Regression</h2>\n<blockquote>\n<p>Softmax Regression은 두 가지 이상의 클래스를 분류하는데 사용하는 Logistic Regression이다.</p>\n</blockquote>\n<ul>\n<li>\n<p>데이터가 주어지면 Softmax Regression 모델은 해당 데이터가 각 클래스에 대한 점수를 계산한다.</p>\n</li>\n<li>\n<p>softmax 함수를 이용하여 이 점수를 확률값으로 바꾸게 되고, 가장 높은 확률값을 가진 클래스로 데이터 x를 분류하게 된다.</p>\n</li>\n</ul>\n<div style=\"text-align: center\">\n    <img src=\"https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2F0cqKW%2FbtqJZmxTeIG%2FmqXqrqFjAXbvmpvk35wRCk%2Fimg.png\" width=\"700\">\n</div>\n<br />\n<br />\n<p><strong>Source:</strong></p>\n<p>📖 핸즈온머신러닝, 2/E, 2020 (번역)</p>\n<p>📖 모두의 딥러닝, 2/E, 2020</p>","frontmatter":{"title":"[Machine Learning] Regression","date":"2021.10.24.","category":"Data Science","categoryColor":"seagreen","tags":["AI","ML"]}}}]}},"pageContext":{"slug":"/211024-ml-regression/","next":{"frontmatter":{"title":"[Machine Learning] 지도 방식에 따른 머신러닝 유형"},"fields":{"slug":"/211023-ml-types/"}},"previous":{"frontmatter":{"title":"[Machine Learning] Decision Tree"},"fields":{"slug":"/211024-ml-decision-tree/"}}}},"staticQueryHashes":[]}