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

2020-07-14  本文已影响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的尺寸没调好暂时这样吧


上一篇 下一篇

猜你喜欢

热点阅读