[ML] 기본 지도 학습 알고리즘들 - 1. 선형 회귀(Linear Regression)
20. 학습률 알파
학습률 알파가 너무 큰 경우
경사하강 할 때마다 세타 값이 많이 바뀐다. 왼쪽, 오른쪽 성큼성큼 왔다갔다 하면서 진행
너무 크면 손실함수 J의 최소점에서 멀어질 수도 있다.
학습률 알파가 너무 작은 경우
세타가 계속 찔끔찔끔씩 움직여 너무 최소지점을 찾는데 너무 오래걸린다(iteration 커짐)
일반적으로 1.0~0.0 사이의 숫자로 정하고
1, 0.1, 0.01, 0.001 또는 0.5, 0.05, 0.005 이런 식으로, 여러 개를 실험해보면서
경사하강을 가장 적게 하면서(iteration 적게), 손실이 잘 줄어드는 학습률 선택
21. 모델 평가하기
선형 회귀 '모델'.
모델 개선 = 모델 학습시킨다.
모델이 얼마나 좋은지 평가를 해야.. 결과를 얼마나 정확하게 예측하는지 평가
평균 제곱근 오차 (root mean square error - RMSE)
기존 MSE는 단위가 제곱이 되어, 익숙한 형태 단위로 확인하기 위해 루트 씌운 것.
데이터에 맞게 학습시켰기 때문에 평균 제곱 오차가 낮을 수 밖에 없다.
어떻게 해야 신빙성 있게 모델 평가 가능??
학습과 평가를 위한 데이터를 나눈다.
ex) 데이터 30개
학습 : 24개 / 평가 : 6개
학습 데이터 : training set
평가 데이터 : test set
23. scikit-learn 소개 및 데이터 준비
boston_dataset.DESC
boston_dataset.feature_names
boston_dataset.data
boston_dataset.data.shape
boston_dataset.target
boston_dataset.target.shape
numpy 형태 -> pandas 데이터프레임 사용
x = pd.DataFrame(boston_dataset.data , columns = boston_dataset.feature_names)
x = x[['AGE']]
x는 입력변수
y = pd.DataFrame(boston_dataset.target, columns=['MEDV'])
y는 목표변수
24. scikit-learn 데이터 셋 나누기
from sklearn.model_selection import train_set_split
x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.2,random_state=5) // 특정 정수값을 넘겨주면 매번 똑같은 데이터 선택된다.
25. scikit-learn으로 선형 회귀 쉽게 하기
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(x_train, y_train)
model.coef_ : 세타 1 값
model.intercept_ : 세타 0의 값
# f(x) = intercept 값 + coef 값 * x
// 모델로 계산한 예측 값
y_test_prediction = model.predict(x_test)
// 모델 오차 계산하기
from sklearn.metrics import mean_squared_error
mean_squared_error(y_test, y_test_prediction) ** 0.5
8.2... => 약 8000달러의 오차 예