선형회귀는 아래와 같은 순서로 진행된다. 나름의 정리니 참고만 하시기 바란다.

유명한 파이썬 머신 러닝 라이브러리 싸이킷런( scikit-learn )을 이용한 코드를 살펴 보자.
또한, 데이터를 불러올 때 필요한 pandas, 배열을 바꿀 때 필요한 numpy, 시각화를 위한 matplotlib도 함께 사용했으나 이 장에서 설명은 하지 않겠다.
from sklearn.linear_model import LinearRegression
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# read data
df = pd.read_csv("heights.csv")
df.head()
# check data
X = df["height"]
y = df["weight"]
plt.plot(X, y, 'o')
plt.xlabel("height(inch)")
plt.ylabel("weight(lb)")
plt.title("LINEAR REGRESSION")
plt.show()
# fitting data
line_fitter = LinearRegression()
line_fitter.fit(X.values.reshape(-1,1), y)
# print coef_, intercept_
print("line_fitter.coef_", line_fitter.coef_)
print("line_fitter.intercept_", line_fitter.intercept_)
# draw graph
plt.plot(X, y, 'o')
plt.plot(X,line_fitter.predict(X.values.reshape(-1,1)))
plt.xlabel("height(inch)")
plt.ylabel("weight(lb)")
plt.title("LINEAR REGRESSION")
plt.show()
아래는 코드 실행 결과다. 기울기는 [5.84898707] 절편이 [-216.3688677158655]인 주황색 선으로 모든 데이터가 회귀될거라 예측됐다.
'머신러닝(ML)' 카테고리의 다른 글
| K-최근접 이웃( K-Nearest Neighbor )란... (0) | 2020.07.09 |
|---|---|
| 다중선형회귀란( Multiple Linear Regression )란... (0) | 2020.07.09 |
| 선형회귀란?( 손실, 경사하강법, 학습률 ) (0) | 2020.07.09 |
| 선형회귀란( Linear Regression )란... (0) | 2020.07.09 |
| 머신 러닝( Machine Learning )이란... (0) | 2020.07.09 |
