• 二元线性回归


    import numpy as np
    import  matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    data=np.genfromtxt("Delivery.csv",delimiter=',')
    x_data = data[:,[1,2]]
    y_data = data[:,[-1]]
    
    lr=0.000001
    theta0=0
    theta1=0
    theta2=0
    epochs=50
    print (x_data[0])
    def gredient_desent_runner(x_data,y_data,lr,theta0,theta1,theta2,epochs):
        m = len(x_data)
        for i in range(epochs):
            tmp_theta0=0
            tmp_theta1=0
            tmp_theta2=0
            for j in range(m):
                # print ()
                tmp_theta0+=theta0+theta1*x_data[j][0]+theta2*x_data[j][1]-y_data[j][0]
                tmp_theta1+=(theta0+theta1*x_data[j][0]+theta2*x_data[j][1]-y_data[j][0])*x_data[j][0]
                tmp_theta2+=(theta0+theta1*x_data[j][0]+theta2*x_data[j][1]-y_data[j][0])*x_data[j][1]
            theta0-=lr*tmp_theta0/m
            theta1-=lr*tmp_theta1/m
            theta2-=lr*tmp_theta2/m
        return theta0,theta1,theta2
    theta0,theta1,theta2=gredient_desent_runner(x_data,y_data,lr,theta0,theta1,theta2,epochs)
    ax=plt.figure().add_subplot(111,projection = '3d')
    x0=x_data[:,0]
    x1=x_data[:,1]
    x0,x1 = np.meshgrid(x0,x1)
    z=theta0+theta1*x0+theta2*x1
    ax.plot_surface(x0,x1,z)
    plt.show()

    梯度下降法实现线性回归

  • 相关阅读:
    RAID技术
    Mysql的用户基本操作
    LNMP之Php的安装配置
    java 实现图片拼接
    java 实现Serv-U FTP 和 SFTP 上传 下载
    Image合并添加文字内容
    AOP切面用于系统日志
    网页评论实现
    java web 实体类生成
    java接口调试思想
  • 原文地址:https://www.cnblogs.com/students/p/10658041.html
Copyright © 2020-2023  润新知