회귀 알고리즘과 모델 규제

1. k-최근접 이웃 회귀

지도 학습(supervised learning) 알고리즘 : 분류 + 회귀(regression)

k-최근접 이웃 알고리즘

데이터 준비

# 훈련 세트와 테스트 세트로 나누기
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)
  ### random_state는 책과 결과를 동일하게 유지하기 위함

# 사이킷런에 사용할 훈련 세트는 2차원 배열이어야 함!
print(train_input.shape, test_input.shape) # shape : 배열의 크기 출력
train_input=train_input.reshape(-1,1) # reshape : 배열의 크기 원하는 형태로 바꾸기
test_input=test_input.reshape(-1,1) # 크기에 -1 : 나머지 원소 개수로 모두 채움
print(train_input.shape, test_input.shape)

※ reshape() : 바꾸려는 배열의 크기를 지정

결정계수($R^2$)