2차원 배열로 바꿀 때 reshape 메소드가 유용함
object.reshape()를 통해 사용
ex) test.reshape(1,4), test.reshape(-1,1)
-1을 지정하면 나머지 원소의 개수로 모두 채움
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)
from sklearn.neighbors import KNeighborsRegressor
knr = KNeighborsRegressor()
knr.fit(train_input, train_target)
knr.score(test_input, test_target)
회귀의 경우 점수를 결정계수로 평가함.
$$ R^2 = 1 - \frac{\sum(target - predict)^2}{\sum(target - mean)^2} $$
타깃과 예측의 오차를 보여주는 메소드도 존재함.
from sklearn.metrics import mean_absolute_error
test_prediction = knr.predict(test_input)
mae = mean_absolute_error(test_target, test_prediction)
print(mae)