Section2. N211단순선형회귀

포동동·2022년 5월 26일
0

[ML]

목록 보기
1/4

단순선형회귀

  • 피쳐가 하나인 예측 모델의 경우 사용할 수 있다.
  • 주로, 예측값과 실제값의 잔차의 제곱합을 최소화하는 OLS로 도출된다.
  • 선형회귀는 보간(interpolate)과 외삽(extrapolate)을 예측하는 데도 도움이 된다.
  • 이 때, 종속변수(y)는 반응변수, 레이블, 타겟 등으로 불리며, 독립변수(x)는 예측변수, 설명, 특성 등으로 불린다.
  • 기준모델
    • 회귀문제 : 평균값
    • 분류문제 : 최빈값
    • 시계열 : 그 직전 값

from sklearn.linear_model import LinearRegression

#모델 설정
model = LinearRegression()
feature = ['궁금한 피쳐']
target = ['예측할 타겟']
X_train = df[feature]
y_train = df[target]

#모델 학습
model.fit(X_train, y_train)

#테스트 및 성능
y_pred = model.predict(X_test)

mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)

#회귀 계수 확인
model.coef_
model.intercept_

#시각화
plt.scatter(X_train, y_train, color='black', linewidth=1)
plt.scatter(X_test, y_pred, color='blue', linewidth=1)



실습

주택 가격 예측 : https://www.kaggle.com/c/house-prices-advanced-regression-techniques/overview

관심있는 피쳐는 완공연도(builtyear), 타겟은 가격(saleprice)으로 설정하고 단순선형회귀모델을 설정해보았다.
그 결과 mae는 49200, mse는 5037617329(...?), rmse는 70976이 나왔다.
성능 쓰레기다😁

profile
완료주의

0개의 댓글