线性回归代码

纯手工实现线性回归,代码中数据为房价。

import numpy as np
import matplotlib.pyplot as plt


#线性回归,代价方程,含惩罚项
def hhh(x,y,xt,m,z,s):
    h=np.matmul(x,xt.T)
    w=h-y
    w1=np.matmul(z,np.square(w))+s
    J=np.multiply(0.5,np.divide(w1,m))
    return J,w


#梯度下降,含惩罚项
def fuck(w,alpha,xt,x,lmd):
    ab=[ ]
    xt_=list(xt)
    s=lmd/30
    for i in range(2):
        p=i
        xg=xt_[p]
        x_ = x[:, p]
        _=np.matmul(x_.T,w)
        m=(_+lmd*xg)/30
        a=alpha*m
        xg=xg-a
        ab.append(xg)
    return ab


#正则化
def zz(lmd,xt,x2):
    a=np.square(xt)
    b=np.matmul(a,x2.T)
    c=lmd*b
    return c


#线性回归,代价方程,不含惩罚项
#def hhh(x,y,xt,m,z):
#    h=np.matmul(x,xt.T)
#    w=h-y
#    w1=np.matmul(z,np.square(w))
#    J=np.multiply(0.5,np.divide(w1,m))
#    return J,w


#梯度下降,不含惩罚项
#def fuck(w,alpha,xt,x):
#    ab=[ ]
#    xt_=list(xt)
#    s=lmd/30
#    for i in range(2):
#        p=i
#        xg=xt_[p]
#        x_ = x[:, p]
#        _=np.matmul(x_.T,w)
#        m=_/30
#        a=alpha*m
#        xg=xg-a
#        ab.append(xg)
##    return ab



def main():
    x = [[1, 1.1],
         [1, 1.3],
         [1, 1.5],
         [1, 2.0],
         [1, 2.2],
         [1, 2.9],
         [1, 3.0],
         [1, 3.2],
         [1, 3.2],
         [1, 3.7],
         [1, 3.9],
         [1, 4.0],
         [1, 4.0],
         [1, 4.1],
         [1, 4.5],
         [1, 4.9],
         [1, 5.1],
         [1, 5.3],
         [1, 5.9],
         [1, 6.0],
         [1, 6.8],
         [1, 7.1],
         [1, 7.9],
         [1, 8.2],
         [1, 8.7],
         [1, 9.0],
         [1, 9.5],
         [1, 9.6],
         [1, 10.3],
         [1, 10.5]]
    y = [39343, 46205, 37731, 43525, 39891, 56642, 60150, 54445, 64445, 57189, 63218, 55794, 56957, 57081, 61111,
         67938, 66029, 83088, 81363, 93940, 91738, 98273, 101302, 113812, 109431, 105582, 116969, 112635,
         122391, 121872]
    z=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]

    dyn=[0,1]
    m=30
    lmd=1    #惩罚项
    alpha=0.0003    #学习率
    xt=[0,0]     #参数


#将list化为矩阵。
    x=np.array(x)
    y=np.array(y)/10000
    xt = np.array(xt)
    z=np.array(z)
    dyn=np.array(dyn)
    #print('x',x)


    sun=[ ]

#进行迭代次数
    for _ in range(500):
        xt = np.array(xt)
        zz1=zz(lmd,xt,dyn)

        J,w=hhh(x,y,xt,m,z,zz1)
        #J, w = hhh(x, y, xt, m, z)
        sun.append(J)
        xt=fuck(w,alpha,xt,x,lmd)
        #xt = fuck(w,alpha,xt,x)
        #if _%10==0:
        #   print(_,xt)
    sun=list(sun)
    print('sn',sun)
    x_axis = np.arange(0, 500, 1)
    print(xt)

    plt.plot(x_axis,sun)
    plt.show( )


if __name__ == '__main__':
   main( )
posted @ 2019-08-25 16:56  lcyok  阅读(377)  评论(0)    收藏  举报