KakaoTalk_20240105_064612445.png

Chapter 03 회귀 알고리즘과 모델 규제

1. k-최근접 이웃 회귀

지도 학습 알고리즘 : 분류와 회귀

k-최근접 이웃 분류처럼 예측하려는 샘플에 가장 가까운 샘플 k개를 선택한다.

-> 이웃한 샘플의 타킷은 클래스가 아닌 임의의 수치로 평균을 구한다.

a. 데이터 준비

# 농어 데이터를 머신러닝에 사용하기 전에 훈련 세트와 테스트 세트로 나누기
from sklearn.model_selection import train_test_split
train_input, test_input, train_target, test_target = train_test_split(perch_length, perch_weight, random_state=42)
# train_input의 크기는 (42,) 이를 2차원 배열인 (42,1)로 바꾸기
train_input = train_input.reshape(-1,1)
test_input = test_input.reshape(-1,1) # -1은 나머지 원소 개수로 모두를 채우라는 의미
print(train_input.shape, test_input.shape)

b. 결정계수(R^2)