공부

[혼공학습단11기] 혼공머신 2주차

Steboong 2024. 1. 11. 23:38

[기본미션]

Ch.03(03-1) 2번 문제 출력 그래프 인증하기

# -*- coding: utf-8 -*-
"""Untitled2.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1pLYExzbCOog_H1BXiVGtjgXBlYHNRYIz
"""

import numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import train_test_split

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]
     )

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)

knr = KNeighborsRegressor()
x = np.arange(5, 45).reshape(-1, 1)

for n in [1, 5, 10]:
  knr.n_neighbors = n
  knr.fit(train_input, train_target)
  prediction = knr.predict(x)
  plt.scatter(train_input, train_target)
  plt.plot(x, prediction)
  plt.show()

k=1, 5, 10

[선택 미션]

모델 파라미터에 대해 설명하기


머신러닝에서 파라미터란 모델이 데이터로부터 학습하는 값들을 말합니다. 예를들어, 선형회귀 모델에서의 가중치(weight)와 절편(intercept), 또는 인공신경망에서의 가중치와 편향(bias)이 파라미터에 해당합니다. 이러한 파라미터들은 학습 데이터를 통해 업데이트 되고, 모델의 예측을 결정하는 핵심 요소 입니다.

파라미터는 모델이 학습하는 동안 자동으로 조정되는 값 입니다. 예를 들어, 선형회귀 모델에서는 학습 데이터를 통해 가중치와 절편을 조정함으로써 예측 오차를 최소화 합니다.

반응형