지도 학습

{ 분류, 회귀 }

회귀

import numpy as np
perch_length = np.array([8.4, 13.7, 15.0, 16.2, 17.4, 18.0, 18.7, 19.0, 19.6, 20.0, 21.0,
       21.0, 21.0, 21.3, 22.0, 22.0, 22.0, 22.0, 22.0, 22.5, 22.5, 22.7,
       23.0, 23.5, 24.0, 24.0, 24.6, 25.0, 25.6, 26.5, 27.3, 27.5, 27.5,
       27.5, 28.0, 28.7, 30.0, 32.8, 34.5, 35.0, 36.5, 36.0, 37.0, 37.0,
       39.0, 39.0, 39.0, 40.0, 40.0, 40.0, 40.0, 42.0, 43.0, 43.0, 43.5,
       44.0])
perch_weight = np.array([5.9, 32.0, 40.0, 51.5, 70.0, 100.0, 78.0, 80.0, 85.0, 85.0, 110.0,
       115.0, 125.0, 130.0, 120.0, 120.0, 130.0, 135.0, 110.0, 130.0,
       150.0, 145.0, 150.0, 170.0, 225.0, 145.0, 188.0, 180.0, 197.0,
       218.0, 300.0, 260.0, 265.0, 250.0, 250.0, 300.0, 320.0, 514.0,
       556.0, 840.0, 685.0, 700.0, 700.0, 690.0, 900.0, 650.0, 820.0,
       850.0, 900.0, 1015.0, 820.0, 1100.0, 1000.0, 1100.0, 1000.0,
       1000.0])

# 그래프 그리기
import matplotlib.pyplot as plt
plt.scatter(perch_length, perch_weight);
plt.xlabel('length')
plt.ylabel('weight')
plt.show();

Figure_5.png

import numpy as np
perch_length = np.array([8.4, 13.7, 15.0, 16.2, 17.4, 18.0, 18.7, 19.0, 19.6, 20.0, 21.0,
       21.0, 21.0, 21.3, 22.0, 22.0, 22.0, 22.0, 22.0, 22.5, 22.5, 22.7,
       23.0, 23.5, 24.0, 24.0, 24.6, 25.0, 25.6, 26.5, 27.3, 27.5, 27.5,
       27.5, 28.0, 28.7, 30.0, 32.8, 34.5, 35.0, 36.5, 36.0, 37.0, 37.0,
       39.0, 39.0, 39.0, 40.0, 40.0, 40.0, 40.0, 42.0, 43.0, 43.0, 43.5,
       44.0])
perch_weight = np.array([5.9, 32.0, 40.0, 51.5, 70.0, 100.0, 78.0, 80.0, 85.0, 85.0, 110.0,
       115.0, 125.0, 130.0, 120.0, 120.0, 130.0, 135.0, 110.0, 130.0,
       150.0, 145.0, 150.0, 170.0, 225.0, 145.0, 188.0, 180.0, 197.0,
       218.0, 300.0, 260.0, 265.0, 250.0, 250.0, 300.0, 320.0, 514.0,
       556.0, 840.0, 685.0, 700.0, 700.0, 690.0, 900.0, 650.0, 820.0,
       850.0, 900.0, 1015.0, 820.0, 1100.0, 1000.0, 1100.0, 1000.0,
       1000.0])

# 그래프 그리기
# import matplotlib.pyplot as plt
# plt.scatter(perch_length, perch_weight);
# plt.xlabel('length')
# plt.ylabel('weight')
# plt.show();
# 농어의 길이가 커질 수록 무게도 늘어남을 그래프를 통해 확인

# 훈련 세트와 테스트 세트로 나누기
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)

# 사이킷런에서 사용할 훈련 세트는 이차원 배열이여야한다
# perch_length 가 1차원 배열이므로 train_input, test_input 또한 당연히 1차원 -> 2차원으로 바꿔줄 필요성
# numpy 배열은 크기를 바꿀 수 있는 reshape() 매서드
 
# 바꾸기 전 크기 확인
#test_array = np.array([1,2,3,4])
#print(test_array.shape)
# (4,) 이것을 (2,2) 로 바꿔줘야함

#test_array = test_array.reshape(2,2)
#print(test_array)

# 이차원 배열로 바꿔주기
train_input = train_input.reshape(-1,1)
test_input = test_input.reshape(-1,1)

# 훈련
from sklearn.neighbors import KNeighborsRegressor
knr = KNeighborsRegressor()
knr.fit(train_input, train_target) 

# 점수 확인
# print(knr.score(test_input, test_target))
# ->0.992809406101064

# 회귀의 경우 정확한 숫자를 맞추는 것은 불가능한 수준임 그렇다면 평가를 어떻게 할까?
# 결정 계수 R^2 = 1-(타깃 - 예측값)^2의 합/(타깃 - 평균)^2의 합

# 타깃과 예측한 값의 차이를 비교해서 어느 정도 예측이 벗어났는지 가늠해보기
# sklearn.metrics 의 mean_absolute_error 타깃과 예측의 절댓값 오차를 평균하여 반환함

from sklearn.metrics import mean_absolute_error
# test_input에 대한 예측을 만들고
test_prediction = knr.predict(test_input)
# 평균 절댓값 오차를 반환
mae = mean_absolute_error(test_target, test_prediction)
# print(mae)
# -> 19.157142857142862 즉 평균적으로 19g정도의 오차가 생김을 알 수 있다

과대 적합과 과소 적합

과대 적합(overfitting)