线性回归 python实现 不用现成库

说实话,觉得写错了。。。只是应付一下课堂作业

用的数据集

import numpy as np
import pandas as pd
import numpy.random
import time
import matplotlib.pyplot as plt
%matplotlib inline
data=pd.read_csv('Folds5x2_pp.CSV')

就是开始梯度大大大大大大,当时就觉得不太对,但是懒得重新看了,给数据处理下凑合用吧

a1=data.loc[:,"AT"].max()
a2=data.loc[:,"V"].max()
a3=data.loc[:,"AP"].max()
a4=data.loc[:,"RH"].max()
a5=data.loc[:,"PE"].max()
data=pd.DataFrame({'a1':data['AT']/a1,
                  'a2':data['V']/a2,
                  'a3':data['AP']/a3,
                  'a4':data['RH']/a4,
                  'a5':data['PE']/a5})
data.insert(0,'f',1)
Udata=data.values
x=Udata[:,0:5]
y=Udata[:,5:6]
theta=np.zeros([1,5])
def hx(x,theta):
    return np.dot(x,theta.T)
def loss(x,y,theta):
    a=np.square(hx(x,theta)-y)
    return a.sum()/(len(y))
def pd(x,y,theta):
    grad=np.zeros(theta.shape)#梯度结果,占位
    error=(y-hx(x,theta)).ravel()# ravel 多维数组拉成一维数组
    for i in range(len(theta.ravel())):# 所有参数的偏导
        grad[0,i]=(np.sum(np.multiply(error,x[:,i])))
    return grad
def DM2(x,y,theta,YuZhi,learnning_Rate):
    start_time=time.time()
    i=0
    k=0
    grad=np.zeros(theta.shape)
    Loss=[loss(x,y,theta)]
    while True:
        grad=pd(x[k:k+1],y[k:k+1],theta)
        k+=1
        if k>=len(x):
            k=0
        theta=theta+(learnning_Rate*grad)
        Loss.append(loss(x,y,theta))
        i+=1
        if i>YuZhi:
            break
    return theta,i-1,Loss,time.time()-start_time
ENDtheta,times,ENDcosts,spend=DM2(x,y,theta,15000,0.0001)
print('耗时:%f'%(spend))
fig,ax=plt.subplots(figsize=(12,4))
ax.plot(np.arange(len(ENDcosts)),ENDcosts,'r')
ax.set_xlabel("Iter")
ax.set_ylabel("Loss")
耗时:2.377032

在这里插入图片描述

ENDcosts[-1]
0.006015465001446885
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值