美文网首页
梯度下降法线性规划python代码

梯度下降法线性规划python代码

作者: 路过的飞碟 | 来源:发表于2020-07-14 09:38 被阅读0次

批量梯度下降


```

import math

import numpy as np

from matplotlib import pyplot

a=[150,200,250,300,350,400,600]#面积

p=[6450,7450,8450,9450,11450,15450,18450]#价格

ste=0.000000001#步长

theta0=300

theta1=1

m=7

temp1=0

temp2=0

k=0

b1=1

b2=0

while math.fabs(theta0-temp1)>0.0000001 and math.fabs(theta1-temp2)>0.0000001:#当参数ans1与真实值相差小于0.0000001

# while math.fabs(b1-b2)>0.0001:

    sum = 0                                                              #并且参数ans2与真实值相差也小于0.0000001时结束迭代

    k+=1                        #次数统计

    for i in range(0, 7):

        sum +=theta0 + theta1 * a[i] - p[i]

    sum /= m

    sum1=0

    for i in range(0, 7):

        sum1 += (theta0+ theta1 * a[i] - p[i])*a[i]

    sum1 /= m

    temp2 = theta1

    theta0 = theta0 - sum * ste  # 生成新的theta0

    theta1 = theta1 - sum1* ste  # 生成新的theta1

    b2 = b1

    for i in range(0,7):

        b1=( theta0 +theta1* a[i] -p[i])**2

    b1/=(2*m)

    print(k,theta0,theta1,k)    #表示正在运行,输出点东西要好些吧,为了和结果区分,对称一下,绘图的第三方库安装出现问题了,输出数据来小替代一下吧

print(k,theta0,theta1);#输出参数theta0与参数theta1

#打印图形

pyplot.scatter(a, p)

x = np.arange(100, 700, 100)

y = theta1 * x + theta0

pyplot.plot(x, y)

pyplot.xlabel('area')

pyplot.ylabel('price')

pyplot.show()

```


随机梯度下降

```

import math

import random

import numpy as np

from matplotlib import pyplot

a=[150,200,250,300,350,400,600]#面积

p=[6450,7450,8450,9450,11450,15450,18450]#价格

ste=0.000000001#步长

theta0=300

theta1=1

m=7

temp1=0

temp2=0

k=0

b1=1

b2=0

while math.fabs(theta0-temp1)>0.0000001 and math.fabs(theta1-temp2)>0.0000001:#当参数ans1与真实值相差小于0.0000001

# while math.fabs(b1-b2)>0.0001:

                                                    #并且参数ans2与真实值相差也小于0.0000001时结束迭代

    k+=1                        #次数统计

    temp2 = theta1

    i=random.randint(0,6)

    theta0 = theta0 - (theta0 + theta1 * a[i] - p[i]) * ste  # 生成新的theta0

    theta1 = theta1 - (theta0+ theta1 * a[i] - p[i])*a[i]* ste  # 生成新的theta1

    b2 = b1

    for i in range(0,7):

        b1=( theta0 +theta1* a[i] -p[i])**2

    b1/=m

    print(k,theta0,theta1,k)    #表示正在运行,输出点东西要好些吧,为了和结果区分,对称一下,绘图的第三方库安装出现问题了,输出数据来小替代一下吧

print(k,theta0,theta1);#输出参数theta0与参数theta1

#打印图形

pyplot.scatter(a, p)

x = np.arange(100, 700, 100)

y = theta1 * x + theta0

pyplot.plot(x, y)

pyplot.xlabel('area')

pyplot.ylabel('price')

pyplot.show()

```


批量梯度等了几小时,还没跑完,先来一个随机梯度的结果吧,

又过了几个小时emmm,我改了一下步长再交一下粗略结果吧


批量梯度下


```

import math

import numpy as np

#from matplotlib import pyplot

a=[1.1,1.3,1.5,2,2.2,2.9,3,3.2,3.2,3.7,3.9,4,4,4.1,4.5,4.9,5.1,5.3,5.9,6,6.8,7.1,7.9,8.2,8.7,9,9.5,9.6,10.3,10.5]#工作经验

p=[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]#薪水

ste=0.00001#步长

theta0=30000

theta1=1

m=30

temp1=0

temp2=0

k=0

b1=1

b2=0

while math.fabs(theta0-temp1)>0.0000001 and math.fabs(theta1-temp2)>0.0000001:#当参数ans1与真实值相差小于0.0000001

# while math.fabs(b1-b2)>0.0001:

    sum = 0                                                              #并且参数ans2与真实值相差也小于0.0000001时结束迭代

    k+=1                        #次数统计

    for i in range(0, 30):

        sum +=theta0 + theta1 * a[i] - p[i]

    sum /= m

    sum1=0

    for i in range(0, 30):

        sum1 += (theta0+ theta1 * a[i] - p[i])*a[i]

    sum1 /= m

    temp2 = theta1

    theta0 = theta0 - sum * ste  # 生成新的theta0

    theta1 = theta1 - sum1* ste  # 生成新的theta1

    b2 = b1

    for i in range(0,30):

        b1=( theta0 +theta1* a[i] -p[i])**2

    b1/=(2*m)

    print(k,theta0,theta1,k)    #表示正在运行,输出点东西要好些吧,为了和结果区分,对称一下,绘图的第三方库安装出现问题了,输出数据来小替代一下吧

print(k,theta0,theta1);#输出参数theta0与参数theta1

#打印图形

pyplot.scatter(a, p)

x = np.arange(10000, 70000, 10000)

y = theta1 * x + theta0

pyplot.plot(x, y)

pyplot.xlabel('area')

pyplot.ylabel('price')

pyplot.show()

```


随机梯度下降

```


import math

import random

import numpy as np

from matplotlib import pyplot

a=[1.1,1.3,1.5,2,2.2,2.9,3,3.2,3.2,3.7,3.9,4,4,4.1,4.5,4.9,5.1,5.3,5.9,6,6.8,7.1,7.9,8.2,8.7,9,9.5,9.6,10.3,10.5]#工作经验

p=[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]#薪水

ste=0.000001#步长

theta0=30000

theta1=1

m=30

temp1=0

temp2=0

k=0

b1=1

b2=0

while math.fabs(theta0-temp1)>0.0000001 and math.fabs(theta1-temp2)>0.0000001:#当参数ans1与真实值相差小于0.0000001

# while math.fabs(b1-b2)>0.0001:

                                                    #并且参数ans2与真实值相差也小于0.0000001时结束迭代

    k+=1                        #次数统计

    temp2 = theta1

    i=random.randint(0,29)

    theta0 = theta0 - (theta0 + theta1 * a[i] - p[i]) * ste  # 生成新的theta0

    theta1 = theta1 - (theta0+ theta1 * a[i] - p[i])*a[i]* ste  # 生成新的theta1

    b2 = b1

    for i in range(0,29):

        b1=( theta0 +theta1* a[i] -p[i])**2

    b1/=m

    print(k,theta0,theta1,k)    #表示正在运行,输出点东西要好些吧,为了和结果区分,对称一下,绘图的第三方库安装出现问题了,输出数据来小替代一下吧

print(k,theta0,theta1);#输出参数theta0与参数theta1

#打印图形

pyplot.scatter(a, p)

x = np.arange(100, 700, 100)

y = theta1 * x + theta0

pyplot.plot(x, y)

pyplot.xlabel('area')

pyplot.ylabel('price')

pyplot.show()

```


多元线性回归


批量梯度下降

```

import numpy as np

from matplotlib import pyplot

a=[[1,2],[2,1],[2,3],[3,5],[1,3],[4,2],[7,3],[4,5],[11,3],[8,7]]#数据集一

p=[7,8,10,14,8,13,20,16,28,26]#数据二

ste=0.00001#步长

theta0=5

theta1=1

theta2=1

m=10

temp1=0

temp2=0

temp3=0

k=0

b1=1

b2=0

while math.fabs(theta0-temp1)>0.0000001 and math.fabs(theta1-temp2)>0.0000001 and math.fabs(theta2-temp3)>0.0000001:#当参数ans1与真实值相差小于0.0000001

# while math.fabs(b1-b2)>0.0001:

    sum = 0                                                              #并且参数ans2与真实值相差也小于0.0000001时结束迭代

    k+=1                        #次数统计

    for i in range(0, 10):

        sum +=theta0 + theta1 * a[i][0]+theta2*a[i][1] - p[i]

    sum /= m

    sum1=0

    sum2=0

    for i in range(0, 10):

        sum1 += (theta0+ theta1 * a[i][0] +theta2*a[i][1]- p[i])*a[i][0]

        sum2 += (theta0+ theta1 * a[i][0] +theta2*a[i][1]- p[i])*a[i][1]

    sum1 /= m

    sum2 /= m

    temp2 = theta1

    temp3 = theta2

    theta0 = theta0 - sum * ste  # 生成新的theta0

    theta1 = theta1 - sum1* ste  # 生成新的theta1

    theta2 = theta2 - sum2* ste  # 生成新的theta2

    b2 = b1

    for i in range(0,10):

        b1=( theta0 +theta1* a[i][0] +theta2*a[i][1] -p[i])**2

    b1/=(2*m)

    print(k,theta0,theta1,theta2,k)    #表示正在运行,输出点东西要好些吧,为了和结果区分,对称一下,绘图的第三方库安装出现问题了,输出数据来小替代一下吧

print(k,theta0,theta1,theta2);#输出参数theta0与参数theta1

#打印图形

pyplot.scatter(a, p)

x = np.arange(100, 700, 100)

y = theta1 * x + theta0

pyplot.plot(x, y)

pyplot.xlabel('area')

pyplot.ylabel('price')

pyplot.show()

```


随机梯度下降

```

import math

import random

import numpy as np

from matplotlib import pyplot

a=[[1,2],[2,1],[2,3],[3,5],[1,3],[4,2],[7,3],[4,5],[11,3],[8,7]]#数据集一

p=[7,8,10,14,8,13,20,16,28,26]#数据二

ste=0.00000001#步长

theta0=5

theta1=1

theta2=1

m=10

temp1=0

temp2=0

temp3=0

k=0

b1=1

b2=0

while math.fabs(theta0-temp1)>0.0000001 and math.fabs(theta1-temp2)>0.0000001 and math.fabs(theta2-temp3)>0.0000001:#当参数ans1与真实值相差小于0.0000001# while math.fabs(b1-b2)>0.0001:

                                                    #并且参数ans2与真实值相差也小于0.0000001时结束迭代

    k+=1                        #次数统计

    temp2 = theta1

    temp3 = theta2

    i=random.randint(0,9)

    theta0 = theta0 - (theta0 + theta1 * a[i][0]+theta2*a[i][1] - p[i]) * ste  # 生成新的theta0

    theta1 = theta1 - (theta0+ theta1 * a[i][0] +theta2*a[i][1]- p[i])*a[i][0]* ste  # 生成新的theta1

    theta2 = theta2 - (theta0+ theta1 * a[i][0] +theta2*a[i][1]- p[i])*a[i][1]* ste  # 生成新的theta2

    b2 = b1

    for i in range(0,10):

        b1=( theta0 +theta1* a[i][0]+theta2*a[i][1] -p[i])**2

    b1/=m

    print(k,theta0,theta1,theta2,k)    #表示正在运行,输出点东西要好些吧,为了和结果区分,对称一下,绘图的第三方库安装出现问题了,输出数据来小替代一下吧

print(k,theta0,theta1,theta2);#输出参数theta0与参数theta1

#打印图形

pyplot.scatter(a, p)

x = np.arange(100, 700, 100)

y = theta1 * x + theta0

pyplot.plot(x, y)

pyplot.xlabel('area')

pyplot.ylabel('price')

pyplot.show()

```

x和y的尺寸没调好暂时这样吧


相关文章

  • 梯度下降法线性规划python代码

    批量梯度下降 ``` import math import numpy as np from matplotlib...

  • 梯度寻优

    参考:梯度下降算法的Python实现 批量梯度下降: 在上述代码中,nb_epochs为迭代次数;data是所有的...

  • (三)线性回归--梯度下降

    一、梯度下降 二、代码的实现 (一.梯度下降) 导包 构建数据 梯度下降 使用梯度下降,可视化 (二。梯度下降矩阵...

  • 机器学习-常用优化方法

    一阶方法:梯度下降、随机梯度下降、mini 随机梯度下降降法。 随机梯度下降不但速度上比原始梯度下降要快,局部最优...

  • ML-梯度下降代码-线性回归为例

    梯度下降代码线性回归为例 bgd 批量梯度下降 sbd 随机梯度下降 mbfd 小批量随机梯度下降

  • 梯度优化算法

    梯度下降,共轭梯度法;牛顿法,拟牛顿法;信赖域方法,罚函数法。

  • 优化算法

    动量法、AdaGrad、RMSProp、AdaDelta、Adam 1.动量法 在7.2节(梯度下降和随机梯度下降...

  • ML-梯度下降法求解线性回归

    梯度法 是基于搜索来最优化一个目标函数的方法。分为梯度下降法 和 梯度上升法 :梯度下降法 用来最小化一个损失函数...

  • 最优化方法

    常见最优化方法 1.梯度下降法 2.牛顿法 3.拟牛顿法 4.共轭梯度法

  • 梯度下降(代码示例)

    梯度下降可以说是当代机器学习的基石。一切机器学习问题,归根到底都是求最优的问题,而求最优都会被转换成求最小值。梯度...

网友评论

      本文标题:梯度下降法线性规划python代码

      本文链接:https://www.haomeiwen.com/subject/bdzthktx.html