欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

机器学习--线性回归

程序员文章站 2022-05-02 14:03:45
...

理论依据是吴恩达的视频:
机器学习--线性回归

import matplotlib.pyplot as plt
import numpy as np
import random

def linear_reg(x, y):

    theta_0, theta_1, alpha = 0, 0, 0.01
    J_pre = (1/len(x)) * sum(y)
    x_ = np.linspace(1,200)/10
    plt.scatter(x, y)
    while True:
        h = theta_0 + theta_1 * x
        J_cur = (1 / (2 * len(x))) * sum(pow(h - y,2))
        if abs(J_cur - J_pre) >= 1e-7 :
            temp0 = theta_0 - alpha * (1/len(x)) * sum(h-y)
            temp1 = theta_1 - alpha * (1/len(x)) * sum((h-y) * x)
            theta_0 = temp0
            theta_1 = temp1
            J_pre = J_cur
        else:
            break


    print('Theta_0:{}'.format(theta_0))
    print('Theta_1:{}'.format(theta_1))
    plt.plot(x_, theta_0 + theta_1 * x_)
    plt.show()
    return theta_0, theta_1

def generate_data():
    """取一个直线上的一些点,并且加上随机数来作为噪声,原方程y=8-2x"""
    data_list = [ ]
    x = [ ]
    num = 1
    test = []
    for i in range(200):
        if i < 170 and i > 40 and random.randint(1,10) > 3:
            if num < 80:
                x.append(i+random.randint(2,5)/10)
                data_list.append((80 - 2 * i+random.randint(2,5)/10)+random.randint(-45,45))
                num += 1
            else:
                break
        else:
            pass

    x = np.array(x)/10
    y = np.array(data_list)/10

    return x,y, test
if __name__ == '__main__':
    x, y, test= generate_data()
    the0, the1 = linear_reg(x,y)

机器学习--线性回归
机器学习--线性回归