사다리꼴 하강법을 이용하여 일원 선형 회귀를 실현하다

1519 단어 기계 학습
import numpy as np
import matplotlib.pyplot as plt

# 
data = np.genfromtxt("data.csv", delimiter=",")
x_data = data[:,0]
y_data = data[:,1]


# learning rate
lr = 0.0001
# 
b = 0
# 
k = 0
# 
epochs = 100
# 


def compute_error(b, k, x_data, y_data):
    totalError = 0
    for i in range(0, len(x_data)):
        totalError += (y_data[i] - (k * x_data[i] + b)) ** 2
    return totalError / float(len(y_data))

# 
def gradient_descent_runner(x_data, y_data, b, k, lr, epochs):
    m = float(len(x_data))
    for i in range(epochs):
        b_grad = 0
        k_grad = 0
        for j in range(0, int(m)):
            b_grad += (k * x_data[i] + b - y_data[i])
            k_grad += ((k * x_data[i] + b - y_data[i]) * x_data[i])

        b_grad /= m
        k_grad /= m

        b = b - lr * b_grad
        k = k - lr * k_grad

        if i % 5 == 0:
            print("epochs:", i)
            plt.plot(x_data, y_data, 'b.')
            plt.plot(x_data, x_data * k + b, 'r')
            plt.show()
    return b, k


print("Starting b = {0}, k = {1}, error = {2}".format(b, k, compute_error(b, k, x_data, y_data)))

print("Running......")

b, k = gradient_descent_runner(x_data, y_data, b, k, lr, epochs)

print("After {0} iterations b = {1}, k = {2}, error = {3}".format(epochs, b, k, compute_error(b, k, x_data, y_data)))

# plt.plot(x_data, y_data, 'b.')
# plt.plot(x_data, x_data * k + b, 'r')
# plt.show()

좋은 웹페이지 즐겨찾기