多项式形式
import numpy as np
import random
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
import matplotlib.pyplot as plt
def create_vertix(n):
vertix_x = []
vertix_y = []
for i in range(n):
x1= i*4-2.5
y = (x1**2.5+3*x1+5 +random.random()).real
vertix_x .append(x1)
vertix_y .append(y)
return vertix_x,vertix_y
def line_model(x,y):
x_data =np.array(x)
y_data =np.array(y)
x = x_data[:,np.newaxis]
y = y_data[:,np.newaxis]
model = LinearRegression()
model.fit(x,y)
y_true = y
y_pred = model.predict(x)
print ("cost error={}".format(cost_error(y_true,y_pred)))
plt.plot(x,y,"r.")
plt.plot(x,model.predict(x),"b-")
plt.show()
def cost_error(y_true,y_pred):
m,n = (y_true.shape)
error = 0
for i in range(m):
error = np.abs(y_true[i][0] -y_pred[i][0])
return error
def ploy_model(x,y):
x_data =np.array(x)
y_data =np.array(y)
x = x_data[:,np.newaxis]
y = y_data[:,np.newaxis]
poly_reg = PolynomialFeatures(degree = 3)
x_ploy = poly_reg.fit_transform(x)
model = LinearRegression()
model.fit(x_ploy,y)
y_true = y
y_pred = model.predict(poly_reg.fit_transform(x))
print ("cost error={}".format(cost_error(y_true,y_pred)))
plt.plot(x_data ,y_data ,"r.")
plt.plot(x,y_pred,"b-")
plt.xlabel("x")
plt.ylabel("y")
plt.title("graph")
plt.show()
def main():
x,y= create_vertix(25)
print ("start".center(25,"*"))
line_model(x,y)
ploy_model(x,y)
print ("end".center(25,"*"))
main()
cost error=19933.425934265753
cost error=136.99519175218302
1,error = (y_true[i][0] -y_pred[i][0])**2
代价函数计算结果:
degree=1,cost error=397374110.68102705
degree=2,cost error=3222004.262112837
degree=3,cost error=18674.201406141176
degree=4,cost error=736.6738281166301
degree=5,cost error=59.8888489843362
degree=6,cost error=7.254368883840344
degree=7,cost error=1.0429169629052022
degree=8,cost error=0.18823767945918912
degree=9,cost error=6.930518441533647
degree=10,cost error=32.59044688556865
degree=11,cost error=5827.685647543228
degree=12,cost error=9138.200209790424
degree=13,cost error=11436.694525020814
degree=14,cost error=12283.801352224662
degree=15,cost error=12304.546888130342
degree=16,cost error=8801.843441774437
degree=17,cost error=6135.41746705274
degree=18,cost error=850.3205485020965
degree=19,cost error=207.99105764266483
degree=20,cost error=7024.632562533782
degree=21,cost error=26900.48111385503
degree=22,cost error=19775.284127129136
degree=23,cost error=14304.31718213598
degree=24,cost error=10202.18405126948
degree=25,cost error=7189.340789771009
degree=26,cost error=5010.893172748555
degree=27,cost error=3459.2398151649086
degree=28,cost error=2367.514531857139
degree=29,cost error=1606.6299654682036
degree=30,cost error=1092.2391728585922
degree=31,cost error=726.4110671157522
degree=32,cost error=474.790536898492
degree=33,cost error=325.6120361376906
degree=34,cost error=213.0397611295138
degree=35,cost error=138.79875482035803
degree=36,cost error=74.79174647890947
degree=37,cost error=64.58573343527506
degree=38,cost error=87.91046010020833
degree=39,cost error=115.26192658724946
degree=40,cost error=96.08538964105223
degree=41,cost error=10.398764939728828
degree=42,cost error=4.404816878973304
degree=43,cost error=2.4116806493025953
degree=44,cost error=210.09882335990454
degree=45,cost error=68.0606620381541
degree=46,cost error=80.41376288434076
degree=47,cost error=3.4819004091074484
degree=48,cost error=1077.0425088979925
degree=49,cost error=64092.79814366737
degree=50,cost error=2031.4461310342447
degree=51,cost error=15.70104652300768
degree=52,cost error=10.959921430677086
degree=53,cost error=7.642343991013857
degree=54,cost error=5.324770517766025
degree=55,cost error=3.707391284600096
degree=56,cost error=2.5791739624943273
degree=57,cost error=1.7937959316640717
degree=58,cost error=1.2461350894133718
degree=59,cost error=0.8652496995310527
degree=60,cost error=0.6003240821484956
degree=61,cost error=0.4165409790121324
degree=62,cost error=0.288716827579658
degree=63,cost error=0.19919844632517827
degree=64,cost error=0.1384879884757483
degree=65,cost error=0.09637587719452437
degree=66,cost error=0.06615922631509066
degree=67,cost error=0.04591425828745934
degree=68,cost error=0.03170107119379697
degree=69,cost error=0.022082643012714922
degree=70,cost error=0.015055413609991988
degree=71,cost error=0.01160281697948409
degree=72,cost error=0.007899547344426463
degree=73,cost error=0.005622940734813532
degree=74,cost error=0.003522854689771339
degree=75,cost error=0.001814716592867828
degree=76,cost error=0.0012314601359652247
degree=77,cost error=1.3321763621700158e-05
degree=78,cost error=2.112270168077727e-05
degree=79,cost error=0.0006687705766049948
degree=80,cost error=0.01562805271162362
degree=81,cost error=0.0004779818571420084
degree=82,cost error=0.048947239882011793
degree=83,cost error=0.00018145335196941985
degree=84,cost error=0.018140394697914754
degree=85,cost error=0.0883051760626318
degree=86,cost error=0.012745672036333613
degree=87,cost error=7.692614628731646e-05
degree=88,cost error=0.0017190293161194706
degree=89,cost error=0.05846868197569912
degree=90,cost error=0.02223892000425445
degree=91,cost error=0.9464520320623381
degree=92,cost error=0.13185355761084475
degree=93,cost error=17.31842974752808
degree=94,cost error=71.31845791748144
degree=95,cost error=0.10356815847063552
degree=96,cost error=3.495352478545611
degree=97,cost error=53.77721087769736
degree=98,cost error=0.31755399057206424
degree=99,cost error=9.41906218821667
degree=100,cost error=35.959508501190385
2,error = np.abs(y_true[i][0] -y_pred[i][0])
代价函数计算结果:
degree=1,cost error=19933.87325310307
degree=2,cost error=1794.7235066535359
degree=3,cost error=136.52694078435889
degree=4,cost error=27.253666089702165
degree=5,cost error=7.7971316622861195
degree=6,cost error=2.6681491245981306
degree=7,cost error=0.9413078982324805
degree=8,cost error=1.0276916620496195
degree=9,cost error=2.6628138990781736
degree=10,cost error=14.558545207284624
degree=11,cost error=76.26971171129844
degree=12,cost error=95.54841162715456
degree=13,cost error=107.38353793029091
degree=14,cost error=106.39139230255387
degree=15,cost error=109.63265431378386
degree=16,cost error=103.29140785787604
degree=17,cost error=59.007412974577164
degree=18,cost error=5.628399615030503
degree=19,cost error=135.70975292180083
degree=20,cost error=141.38566660429933
degree=21,cost error=164.0238521352003
degree=22,cost error=140.6331896140764
degree=23,cost error=119.60676204707124
degree=24,cost error=101.00986909415224
degree=25,cost error=84.80249511744478
degree=26,cost error=70.79208302046754
degree=27,cost error=58.8387395098398
degree=28,cost error=48.67847287203767
degree=29,cost error=40.08887230898836
degree=30,cost error=33.04033553149202
degree=31,cost error=26.952035303373123
degree=32,cost error=21.930476541776443
degree=33,cost error=18.01058756854036
degree=34,cost error=14.71559369112947
degree=35,cost error=11.502797479886794
degree=36,cost error=11.821737642545486
degree=37,cost error=8.250008936185623
degree=38,cost error=6.1427816107461695
degree=39,cost error=5.301852579374099
degree=40,cost error=8.38747918154695
degree=41,cost error=3.153111810941482
degree=42,cost error=0.7007955267617945
degree=43,cost error=2.1043103888223413
degree=44,cost error=9.052153234224534
degree=45,cost error=9.750016565580154
degree=46,cost error=3.21906149838469
degree=47,cost error=2.3003858282754663
degree=48,cost error=62.280424718599534
degree=49,cost error=84.18282950375578
degree=50,cost error=310.86265694643953
degree=51,cost error=3.962617997865891
degree=52,cost error=3.310543660860276
degree=53,cost error=2.7644478127767798
degree=54,cost error=2.3075901315023657
degree=55,cost error=1.9253574654867407
degree=56,cost error=1.6062227532675024
degree=57,cost error=1.3392578408529516
degree=58,cost error=1.1160713479330298
degree=59,cost error=0.9303900048544165
degree=60,cost error=0.7749544427206274
degree=61,cost error=0.6447531029989477
degree=62,cost error=0.537155751924729
degree=63,cost error=0.4460359856893774
degree=64,cost error=0.37489188168547116
degree=65,cost error=0.3101526543905493
degree=66,cost error=0.2582002923300024
degree=67,cost error=0.21406424496672116
degree=68,cost error=0.17785513852140866
degree=69,cost error=0.14753210995695554
degree=70,cost error=0.12342703793547116
degree=71,cost error=0.10347617123625241
degree=72,cost error=0.08821738217375241
degree=73,cost error=0.06764853451750241
degree=74,cost error=0.06056845639250241
degree=75,cost error=0.03640616391203366
degree=76,cost error=0.05773795102140866
degree=77,cost error=0.007325525541091338
degree=78,cost error=0.04032767270109616
degree=79,cost error=0.025725011568283662
degree=80,cost error=0.06530631039640866
degree=81,cost error=0.0008837029745336622
degree=82,cost error=0.11590445492765866
degree=83,cost error=0.014891271333908662
degree=84,cost error=0.12405264828703366
degree=85,cost error=0.028410558443283662
degree=86,cost error=0.07248055483796634
degree=87,cost error=0.43588387515046634
degree=88,cost error=0.11923348452546634
degree=89,cost error=0.19021475766203366
degree=90,cost error=0.016816492337966338
degree=91,cost error=0.36550772641203366
degree=92,cost error=1.6365674689004663
degree=93,cost error=4.900541906099534
degree=94,cost error=0.40573250796296634
degree=95,cost error=3.0461133673379663
degree=96,cost error=0.27951180483796634
degree=97,cost error=10.711640711087966
degree=98,cost error=5.635283117037034
degree=99,cost error=2.9309374139120337
degree=100,cost error=10.934355382662034