다음에 해당하는 Dataset을 생성하고,
이를 Linear Regression 모델을 통해 학습 후 성능을 보이는 코드 작성해보기.
1. Dataset Description
다음 조건을 만족하는 synthetic linear regression dataset 을 생성할 것.
numpy의 random 모듈을 활용해볼 것:
2024.03.29 - [Python] - [DL] Tensor: Random Tensor 만들기 (NumPy, PyTorch)
[DL] Tensor: Random Tensor 만들기 (NumPy, PyTorch)
Tensor: Random Tensor 만들기 (NumPy, PyTorch)Random value를 얻는데 이용되는 Probability Distribution에 따라 크게 2가지로 나눌 수 있음.Uniform DistributionNormal Distribution 0. Uniform DistributionRandom Variable이 가질 수 있
ds31x.tistory.com
1-1. 샘플 수 (Number of samples)
- 전체 샘플 수는 $n = 1000$ 으로 설정.
1-2. 입력 특성 (Input features)
- 두 개의 연속형 특성 $x_0$, $x_1$ 을 독립적으로 생성함.
$$x_0 \sim \mathcal{N}(\mu=5,\ \sigma=2)$$
$$x_1 \sim \mathcal{N}(\mu=10,\ \sigma=3)$$
1-3. 목표값 (Target value)
Ideal Target $y_{\text{true}}$ 는 다음의 선형 관계를 input features와 가짐:
$$y_{\text{true}} = x_0 + 2x_1 + 5$$
1-4. 노이즈 (Noise)
실제 관측값 $y$ 는 $y_{\text{true}}$ 에 다음의 Gaussian noise 를 추가하여 생성함.
$$\epsilon \sim \mathcal{N}(\mu=0,\ \sigma=2.0)$$
$$y = y_{\text{true}} + \epsilon$$
1-5.최종 데이터 형태
- input matrix $X$ 는 $x_0$ 과 $x_1$ 을 열 방향으로 결합한 $(1000,\ 2)$ 형태의 2차원 배열임.
- target vector $y$ 는 $(1000,)$ 형태의 1차원 배열임.
2. Linear Regression
- OLS 모델로 위의 Dataset을 학습하고 성능 및 parameters를 확인해 볼 것.
- Hold-out 방식으로 Train/Test 를 위한 Dataset 을 나눌 것.
- Feature Scaling을 수행할 것: Standardization
반드시 모델에 Regularization을 추가해서 해당 학습 결과도 확인하는 코드를 작성할 것.
Polynomial Regression 을 사용하여 Over-fitting 이 되는 경우도 작성해볼 것
- Train에서 성능과
- Test에서의 성능을 비교하여 Over-fitting 임을 확인시키는 code cell과 설명이 있어야 함.
Option
Normal Equation 의 경우 Feature Scaling이 필요하지 않음에도 LinearRegression에서 Feature Scaling을 해주는 것을 권하는 이유는 뭘까?
(Hint: Gradient Descent 를 사용하여 OLS를 푸는 경우!)
참고로 Ridge는 Closed Form Solution이 있지만, LASSO는 없음.
이를 기반으로 scikit-learn에서 argument가 어떤 차이를 보이는지 한번 비교해 볼 것.
참고 자료:
다음 URL에서 Linear Regression 관련 모델들을 참고할 것.
(해당 페이지의 Git의 gist로 공개된 ipynb파일도 참고할 것)
https://dsaint31.me/mkdocs_site/ML/ch01/ch01_41/
BME
bagging boosting ensemble machine learning random forest regression scikit-learn support vector machine [ML] Classic Regressor (Summary) DeepLearning 계열을 제외한 Regressor 모델들을 간단하게 정리함. 분류 Instance Based Algorithm Mod
dsaint31.me
같이 보면 좋은 자료들
https://dsaint31.tistory.com/274
[Fitting] Ordinary Least Squares : OLS, 최소자승법
Ordinary Least Squares : OLS, 최소자승법Solution을 구할 수 없는 Over-determined system에서 solution의 approximation을 구하는 가장 기본적인 방법임.Machine Learning에서 Supervised Learning의 대표적인 task인 Regression을
dsaint31.tistory.com
https://dsaint31.me/mkdocs_site/ML/ch00/ch00_20_supervised/
BME
Supervised Learning 가장 전형적인 ML을 가리킨다. input data 와 label (or target)이라고 불리는 원하는 output 에 해당하는 데이터로 구성된 pair들을 가지는 training set과 test set을 가지고 있고 이를 통해 학습
dsaint31.me
2024.05.04 - [분류 전체보기] - [ML] sklearn.preprocessing.StandardScaler
[ML] sklearn.preprocessing.StandardScaler
sklearn.preprocessing.StandardScalerStandardization을 수행하는 클래스.관련 libraryfrom sklearn.preprocessing import StandardScaler Scikit-Learn이 NumPy의 ndarray array의 객체와 같이 사용되기 때문에 numpy를 같이 import 하는 것
ds31x.tistory.com
https://gist.github.com/ds31x/67cd7c5b80893d082c313bf71d146307
ml_regressor_summary.ipynb
ml_regressor_summary.ipynb. GitHub Gist: instantly share code, notes, and snippets.
gist.github.com
'ML' 카테고리의 다른 글
| [ML] linear_model.SGDRegressor (0) | 2026.04.28 |
|---|---|
| Multi-Head "Masked/Cross" Attention - Transformer Decoder (1) | 2026.04.18 |
| 튜토리얼: Gemini CLI + MCP + Context7 + VS Code Companion (작성중) (0) | 2026.04.11 |
| torch.nn.Module의 상태(state)-Parameter and Buffer (0) | 2026.04.09 |
| pytorch-torchinfo 란 (0) | 2026.04.09 |
