[Python] 지도학습-LinearRegression 실습1

정현명·2021년 7월 21일
0

ML

목록 보기
2/12
post-thumbnail

최소 제곱법을 이용한 예측

최소 제곱법


  • 파이썬으로 최소제곱법 함수 구현
def compute_a(x, y, mean_x, mean_y):
    dc = sum([(x[i]-mean_x)*(y[i]-mean_y) for i in range(len(x))]) # 분자 부분
    d = sum([(x[i]-mean_x)**2 for i in range(len(x))]) # 분모 부분  
    return dc / d



최소제곱법을 활용한 배달시간 예측

  • 데이터
import numpy as np 
from matplotlib import pyplot as plt

# 배달거리와 배달시간 데이터
data = np.array([
    	[100, 20], 
	[150, 24], 
	[300, 36], 
	[400, 47], 
	[130, 22], 
	[240, 32],
	[350, 47], 
	[200, 42], 
	[100, 21], 
	[110, 21], 
	[190, 30], 
	[120, 25], 
	[130, 18], 
	[270, 38], 
	[255, 28]
    ])

x = data[:, 0]
y = data[:, 1]

plt.xlim(0, 450)
plt.ylim(0, 50)
plt.grid()
plt.scatter(x, y)


  • 최소제곱법을 활용하여 선 그리기
mx = np.mean(x)
my = np.mean(y)
a = compute_a(x, y, mx, my)  # 기울기
b = my - (mx * a)            # 절편

y_pred = [a * x1 + b for x1 in x]
plt.plot(x, y_pred, 'r-o')
plt.plot(x, y, 'bo')
plt.grid()
plt.show()

profile
꾸준함, 책임감

0개의 댓글