python使用梯度下降算法实现一个多线性回归

 更新时间:2020-03-25 02:16:20   作者:佚名   我要评论(0)

python使用梯度下降算法实现一个多线性回归,供大家参考,具体内容如下
图示:




import pandas as pd
import matplotlib.pylab as plt
import numpy as np
# Rea

python使用梯度下降算法实现一个多线性回归,供大家参考,具体内容如下

图示:

import pandas as pd
import matplotlib.pylab as plt
import numpy as np
# Read data from csv
pga = pd.read_csv("D:\python3\data\Test.csv")
# Normalize the data 归一化值 (x - mean) / (std)
pga.AT = (pga.AT - pga.AT.mean()) / pga.AT.std()
pga.V = (pga.V - pga.V.mean()) / pga.V.std()
pga.AP = (pga.AP - pga.AP.mean()) / pga.AP.std()
pga.RH = (pga.RH - pga.RH.mean()) / pga.RH.std()
pga.PE = (pga.PE - pga.PE.mean()) / pga.PE.std()


def cost(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y):
 # Initialize cost
 J = 0
 # The number of observations
 m = len(x1)
 # Loop through each observation
 # 通过每次观察进行循环
 for i in range(m):
 # Compute the hypothesis
 # 计算假设
 h=theta0+x1[i]*theta1+x2[i]*theta2+x3[i]*theta3+x4[i]*theta4
 # Add to cost
 J += (h - y[i])**2
 # Average and normalize cost
 J /= (2*m)
 return J
# The cost for theta0=0 and theta1=1


def partial_cost_theta4(theta0,theta1,theta2,theta3,theta4,x1,x2,x3,x4,y):
 h = theta0 + x1 * theta1 + x2 * theta2 + x3 * theta3 + x4 * theta4
 diff = (h - y) * x4
 partial = diff.sum() / (x2.shape[0])
 return partial


def partial_cost_theta3(theta0,theta1,theta2,theta3,theta4,x1,x2,x3,x4,y):
 h = theta0 + x1 * theta1 + x2 * theta2 + x3 * theta3 + x4 * theta4
 diff = (h - y) * x3
 partial = diff.sum() / (x2.shape[0])
 return partial


def partial_cost_theta2(theta0,theta1,theta2,theta3,theta4,x1,x2,x3,x4,y):
 h = theta0 + x1 * theta1 + x2 * theta2 + x3 * theta3 + x4 * theta4
 diff = (h - y) * x2
 partial = diff.sum() / (x2.shape[0])
 return partial


def partial_cost_theta1(theta0,theta1,theta2,theta3,theta4,x1,x2,x3,x4,y):
 h = theta0 + x1 * theta1 + x2 * theta2 + x3 * theta3 + x4 * theta4
 diff = (h - y) * x1
 partial = diff.sum() / (x2.shape[0])
 return partial

# 对theta0 进行求导
# Partial derivative of cost in terms of theta0


def partial_cost_theta0(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y):
 h = theta0 + x1 * theta1 + x2 * theta2 + x3 * theta3 + x4 * theta4
 diff = (h - y)
 partial = diff.sum() / (x2.shape[0])
 return partial


def gradient_descent(x1,x2,x3,x4,y, alpha=0.1, theta0=0, theta1=0,theta2=0,theta3=0,theta4=0):
 max_epochs = 1000 # Maximum number of iterations 最大迭代次数
 counter = 0 # Intialize a counter 当前第几次
 c = cost(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y) ## Initial cost 当前代价函数
 costs = [c] # Lets store each update 每次损失值都记录下来
 # Set a convergence threshold to find where the cost function in minimized
 # When the difference between the previous cost and current cost
 # is less than this value we will say the parameters converged
 # 设置一个收敛的阈值 (两次迭代目标函数值相差没有相差多少,就可以停止了)
 convergence_thres = 0.000001
 cprev = c + 10
 theta0s = [theta0]
 theta1s = [theta1]
 theta2s = [theta2]
 theta3s = [theta3]
 theta4s = [theta4]
 # When the costs converge or we hit a large number of iterations will we stop updating
 # 两次间隔迭代目标函数值相差没有相差多少(说明可以停止了)
 while (np.abs(cprev - c) > convergence_thres) and (counter < max_epochs):
 cprev = c
 # Alpha times the partial deriviative is our updated
 # 先求导, 导数相当于步长
 update0 = alpha * partial_cost_theta0(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y)
 update1 = alpha * partial_cost_theta1(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y)
 update2 = alpha * partial_cost_theta2(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y)
 update3 = alpha * partial_cost_theta3(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y)
 update4 = alpha * partial_cost_theta4(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y)
 # Update theta0 and theta1 at the same time
 # We want to compute the slopes at the same set of hypothesised parameters
 #  so we update after finding the partial derivatives
 # -= 梯度下降,+=梯度上升
 theta0 -= update0
 theta1 -= update1
 theta2 -= update2
 theta3 -= update3
 theta4 -= update4

 # Store thetas
 theta0s.append(theta0)
 theta1s.append(theta1)
 theta2s.append(theta2)
 theta3s.append(theta3)
 theta4s.append(theta4)

 # Compute the new cost
 # 当前迭代之后,参数发生更新
 c = cost(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y)

 # Store updates,可以进行保存当前代价值
 costs.append(c)
 counter += 1 # Count
 # 将当前的theta0, theta1, costs值都返回去
 #return {'theta0': theta0, 'theta1': theta1, 'theta2': theta2, 'theta3': theta3, 'theta4': theta4, "costs": costs}
 return {'costs':costs}

print("costs =", gradient_descent(pga.AT, pga.V,pga.AP,pga.RH,pga.PE)['costs'])
descend = gradient_descent(pga.AT, pga.V,pga.AP,pga.RH,pga.PE, alpha=.01)
plt.scatter(range(len(descend["costs"])), descend["costs"])
plt.show()

损失函数随迭代次数变换图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

  • python实现梯度下降法
  • python梯度下降算法的实现
  • python梯度下降法的简单示例
  • 基于随机梯度下降的矩阵分解推荐算法(python)
  • python实现梯度下降算法
  • python+numpy+matplotalib实现梯度下降法
  • Python利用逻辑回归分类实现模板
  • python代码实现逻辑回归logistic原理
  • python编写Logistic逻辑回归
  • python实现梯度下降和逻辑回归

相关文章

  • python使用梯度下降算法实现一个多线性回归

    python使用梯度下降算法实现一个多线性回归

    python使用梯度下降算法实现一个多线性回归,供大家参考,具体内容如下 图示: import pandas as pd import matplotlib.pylab as plt import numpy as np # Rea
    2020-03-25
  • 详解nginx请求头数据读取流程

    详解nginx请求头数据读取流程

    在上一篇文章中,我们讲解了nginx是如何读取请求行的数据,并且解析请求行的。本文我们则主要讲解nginx是如何读取客户端发送来的请求头的数据,并且解析这些数据的。
    2020-03-25
  • PyQt5+python3+pycharm开发环境配置教程

    PyQt5+python3+pycharm开发环境配置教程

    1.下载PyQt 官方网站:http://www.riverbankcomputing.com/software/pyqt/download5 我的操作系统是64位的,安装的是Python3.4.3,所以我选择下载:PyQt5-5.4.1-gpl
    2020-03-25
  • Java 5亿整数大文件怎么排序

    Java 5亿整数大文件怎么排序

    问题 给你1个文件bigdata,大小4663M,5亿个数,文件中的数据随机,如下一行一个整数: 6196302 3557681 6121580 2039345 2095006 1746773 7934312 2
    2020-03-25
  • Golang通过小程序获取微信openid的方法示例

    Golang通过小程序获取微信openid的方法示例

    为什么要获取小程序的 openid 在开发微信小程序的过程中,小程序可以通过微信官方提供的登录能力方便地获取微信提供的用户身份标识,快速建立小程序内的用户体系。那
    2020-03-25
  • 详解Python 实现 ZeroMQ 的三种基本工作模式

    详解Python 实现 ZeroMQ 的三种基本工作模式

    简介 引用官方说法:ZMQ(以下 ZeroMQ 简称 ZMQ)是一个简单好用的传输层,像框架一样的一个 socket library,他使得 Socket 编程更加简单、简洁和性能更高。 是
    2020-03-25
  • 微信小程序 flexbox layout快速实现基本布局的解决方案

    微信小程序 flexbox layout快速实现基本布局的解决方案

    问题描述 flexbox layout ——弹性盒子布局。弹性盒子可以快速的对小程序进行布局。一般传统的小程序布局方法对内容量少的页面而言很方便,但对页面比较复杂的来讲
    2020-03-25
  • 解决Android Studio Gradle Metadata特别慢的问题

    解决Android Studio Gradle Metadata特别慢的问题

    如下所示: 更改build.gradle buildscript { repositories { // jcenter() // jcenter(){ url 'http://jcenter.bintray.com/'} maven{url 'http://m
    2020-03-25
  • Java线程优先级和守护线程原理解析

    Java线程优先级和守护线程原理解析

    一、线程优先级的介绍 java 中的线程优先级的范围是1~10,默认的优先级是5。“高优先级线程”会优先于“低优先级线程”执行。 java 中有两种线程:用户线程和守护线
    2020-03-25
  • Android实现获取meta-data和build.gradle的值

    Android实现获取meta-data和build.gradle的值

    有时候会用到meta-data中的参数,比如定义的渠道号,类似友盟统计。也会用到定义在build.gradle文件中android标签下的defaultConfig标签下,添加manifestPlaceholde
    2020-03-25

最新评论