필요한 라이브러리를 불러온다
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsRegressor
import numpy as np
import matplotlib.pyplot as plt
농어의 길이, 무게에 대한 데이터셋을 불러온다
데이터의 형태를 확인하기 위한 산점도를 그린다
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]
)
plt.scatter(perch_length, perch_weight)
plt.xlabel('length')
plt.ylabel('weight')
plt.show()
결과는 다음과 같다
길이가 길어지니 농어가 통통해졌다
훈련용 데이터셋과 테스트 데이터셋으로 나눈다.
train_input, test_input, train_target, test_target = train_test_split(
perch_length, perch_weight, random_state=42)
print(train_input.shape, test_input.shape)
#결과 (42,)(14,)
결과가 1차원 배열로 나온다.
2차원 배열로 바꾸기 위한 작업을 해준다.
test_array = np.array([1,2,3,4])
print(test_array.shape)
#결과 (4,)
test_array = test_array.reshape(2, 2)
print(test_array.shape)
#결과 (2,2)
train_input = train_input.reshape(-1, 1)
test_input = test_input.reshape(-1, 1)
print(train_input.shape, test_input.shape)
#결과 (42,1)(14,1)
3-2 문제 풀이
# k-최근접 이웃 회귀 객체를 만듭니다
knr = KNeighborsRegressor()
# 5에서 45까지 x 좌표를 만듭니다
x = np.arange(5, 45).reshape(-1, 1)
# n = 1, 5, 10일 때 예측 결과를 그래프로 그립니다.
for n in [1, 5, 10]:
# 모델 훈련
knr.n_neighbors = n
knr.fit(train_input, train_target)
# 지정한 범위 x에 대한 예측 구하기
prediction = knr.predict(x)
# 훈련 세트와 예측 결과 그래프 그리기
plt.scatter(train_input, train_target)
plt.plot(x, prediction)
plt.title('n_neighbors = {}'.format(n))
plt.xlabel('length')
plt.ylabel('weight')
plt.show()
'혼공족 ML&DL' 카테고리의 다른 글
[혼공족 12기] 5주차(8/5-8/11) (0) | 2024.08.12 |
---|---|
[혼공족 12기] 4주차(7/22-7/28) (0) | 2024.07.29 |
[혼공족 12기] 3주차(7/15-7/21) (0) | 2024.07.22 |
[혼공족 12기] 1주차(7/1-7/7) (0) | 2024.07.07 |