알파카징징이 알파카징징이 코딩하는 알파카

혼공머_3_1 k-최근접 이웃 회귀

» writing

혼자공부하는머신러닝+딥러닝 수업을 참고하여 작성하였습니다

3_1_k-최근접 이웃 회귀

정의


k-최근접 이웃 회귀

1. 농어의 무게를 예측하라
perch_length = [23,34,23,27,22,33,42,28,45,34,23,38]
perch_weight = [242,323,265,284,226,338,466,284,477,385,238,409]

import matplotlib.pyplot as plt

plt.scatter(perch_length, perch_weight)
plt.xlabel('length')
plt.ylabel('weight')
plt.show()

농어 길이, 무게

2. 훈련 세트 준비
import numpy as np
perch_length = np.array(perch_length)
perch_weight = np.array(perch_weight)

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 = train_input.reshape(-1,1)
test_input = test_input.reshape(-1,1)
3. 회귀 모델 훈련
- 과소적합
- 과대적합
from sklearn.neighbors import KNeighborsRegressor

knr = KNeighborsRegressor()
knr.fit(train_input, train_target)
knr.score(test_input, test_target)

from sklearn.metrics import mean_absolute_error

test_prediction = knr.predict(test_input)
mae = mean_absolute_error(test_target, test_prediction)
print(mae)
4. 이웃 개수 줄이기
- k 높을수록 과소적합
- k 낮을수록 과대적합
knr.n_neighbors = 3
knr.fit(train_input, train_target)

print(knr.score(train_input, train_target))
# 0.938709599713688
print(knr.score(test_input, test_target))
# 0.8942577363692543