yufeng0528 3 years ago
parent
commit
f167426995
8 changed files with 847 additions and 0 deletions
  1. 2 0
      .gitignore
  2. 0 0
      draw/__init__.py
  3. 20 0
      draw/draw_util.py
  4. 100 0
      linear/test_data
  5. 100 0
      linear/test_paracurve_data
  6. 81 0
      linear/train.py
  7. 500 0
      linear/train_data
  8. 44 0
      linear/train_xsquare.py

+ 2 - 0
.gitignore

@@ -58,3 +58,5 @@ docs/_build/
58 58
 # PyBuilder
59 59
 target/
60 60
 
61
+.idea/
62
+venv/

+ 0 - 0
draw/__init__.py


+ 20 - 0
draw/draw_util.py

@@ -0,0 +1,20 @@
1
+#!/usr/bin/python
2
+# -*- coding: UTF-8 -*-
3
+import matplotlib.pyplot as plt
4
+
5
+#绘制散点图
6
+def drawScatter(heights,weights):
7
+    #创建散点图
8
+    #第一个参数为点的横坐标
9
+    #第二个参数为点的纵坐标
10
+    plt.scatter(heights,weights)
11
+    plt.xlabel('Heights')
12
+    plt.ylabel('Weight')
13
+    plt.title('Heights & Weight of Students')
14
+    plt.show()
15
+
16
+
17
+if __name__ == '__main__':
18
+    heights = [1.5, 1.7]
19
+    weights = [43, 61]
20
+    drawScatter(heights,weights)

+ 100 - 0
linear/test_data

@@ -0,0 +1,100 @@
1
+[[5.018022234478728], [-1.9875721873066619]]
2
+[[0.6333422890911256], [2.4001502841357367]]
3
+[[9.636101088393259], [67.7586073478962]]
4
+[[6.705362276318382], [49.58080836221389]]
5
+[[4.62439666316118], [23.44371764421596]]
6
+[[1.0174713302472371], [-6.3656771545852875]]
7
+[[3.807863027573867], [19.754050981747437]]
8
+[[5.0265196379026245], [68.05140991382294]]
9
+[[8.870264446450545], [35.76508219178668]]
10
+[[9.411127555555641], [31.935676644211387]]
11
+[[3.0255262337250475], [-3.530846709071504]]
12
+[[2.3198505767599054], [29.25850242783631]]
13
+[[3.303102903236852], [32.90680367525282]]
14
+[[7.240664398497863], [22.215131172066744]]
15
+[[4.154767538033951], [18.543781412908213]]
16
+[[1.291147840130965], [-5.06466109355477]]
17
+[[7.946629044368601], [52.135952429038696]]
18
+[[0.9850324331949245], [29.700007176421067]]
19
+[[1.1535890494024925], [46.90189904112563]]
20
+[[2.5688866260949816], [0.6672812302727922]]
21
+[[7.74535958886379], [21.303820467970347]]
22
+[[3.797180066747826], [17.08166444719319]]
23
+[[4.294575859504661], [14.11512447352298]]
24
+[[0.3638578702490969], [-20.30318751400874]]
25
+[[3.2440563409445975], [29.96155975090097]]
26
+[[2.8886864687654255], [51.22846735819252]]
27
+[[9.785585365914013], [64.47980407491842]]
28
+[[9.986056516136532], [71.33256904910697]]
29
+[[0.6774634537453694], [26.235854980372768]]
30
+[[2.3784583988376085], [30.966242710536036]]
31
+[[3.3484451310609153], [-17.01553911085646]]
32
+[[7.061341895292893], [47.15212802114509]]
33
+[[7.1324307332507395], [47.81119849359591]]
34
+[[7.608540920864798], [4.595420496078534]]
35
+[[2.291532504095515], [8.168432914364416]]
36
+[[9.190647106990399], [49.09673606149609]]
37
+[[4.617574755246943], [24.957764395374877]]
38
+[[2.590008665089968], [13.10266483536963]]
39
+[[0.5690164689764765], [-50.55834442851013]]
40
+[[6.498686813853585], [41.65742821119823]]
41
+[[5.25897316613926], [5.901121630210308]]
42
+[[7.484238179334628], [28.269110481069138]]
43
+[[9.106164078739791], [50.73688676316995]]
44
+[[2.00449772063104], [-5.636081891001624]]
45
+[[8.86339611336273], [68.23915769673559]]
46
+[[3.0036124122169627], [17.64090735437589]]
47
+[[6.261414954408084], [-20.9846513791243]]
48
+[[2.2659643665498708], [18.624124927200768]]
49
+[[0.5280825357625607], [-2.858411078097871]]
50
+[[4.214544443108563], [27.551136918233734]]
51
+[[7.6908703719559535], [15.025694081435034]]
52
+[[0.9320949757847841], [-0.930841922502191]]
53
+[[8.398035325254071], [20.500269449289334]]
54
+[[4.283872880372813], [26.923302754067343]]
55
+[[0.6683424129044568], [24.868640965913002]]
56
+[[8.253186146341413], [13.266943188288423]]
57
+[[0.29852602901986436], [17.89762421024574]]
58
+[[3.456419458988581], [19.887459079417336]]
59
+[[5.316081905444733], [17.847689122951817]]
60
+[[1.508354652533852], [23.89726310893176]]
61
+[[4.161971728090432], [62.88433149591566]]
62
+[[6.775749997407975], [22.198616935825584]]
63
+[[3.15859611883357], [19.4247099157934]]
64
+[[3.220152283333013], [-15.423983517832175]]
65
+[[4.1592268014878915], [35.487880196776906]]
66
+[[8.50027804540691], [3.7595548242505004]]
67
+[[2.4161633490757604], [28.142935412461853]]
68
+[[6.541386222236096], [46.684279356872096]]
69
+[[8.134491082023978], [55.766729091905596]]
70
+[[0.060651807836825666], [16.12890269132393]]
71
+[[5.792871375835383], [29.673529821883815]]
72
+[[6.513515096856553], [9.918869792991547]]
73
+[[7.88346145730042], [100.58909126716065]]
74
+[[7.3193321568596215], [41.69018166222348]]
75
+[[7.691291404847433], [61.05652220798617]]
76
+[[8.70355906246655], [18.22722517218979]]
77
+[[5.005154475218218], [26.347957741030456]]
78
+[[0.09299937363926425], [-3.1312871569301115]]
79
+[[6.184658615366805], [-10.824657693491563]]
80
+[[4.877050515359191], [47.99598774815966]]
81
+[[1.6031321229692852], [10.054882857268185]]
82
+[[0.8153803678877702], [-13.364816226169655]]
83
+[[3.376369355648637], [19.375557430702784]]
84
+[[8.768149769510941], [15.625792555583613]]
85
+[[1.8566332361376614], [40.90722751050002]]
86
+[[4.269230964351863], [44.04999297805235]]
87
+[[1.340840932581574], [-10.42488944350814]]
88
+[[5.597575580595589], [51.77181829502289]]
89
+[[4.712855421183408], [9.048347005660974]]
90
+[[6.016918641406934], [30.650521783308466]]
91
+[[6.875773733496593], [22.48069934189425]]
92
+[[0.28673576568612025], [3.6200624634079643]]
93
+[[9.71879611008971], [59.41624559083895]]
94
+[[9.83406180339071], [39.21006693284049]]
95
+[[7.832440717103303], [22.991793193861817]]
96
+[[5.164220672199139], [51.22857429504614]]
97
+[[5.328897254716029], [49.23761953109918]]
98
+[[5.870837824039297], [14.800551900604873]]
99
+[[9.114805095474717], [59.603827595535414]]
100
+[[6.957797744133879], [39.23251298433744]]

+ 100 - 0
linear/test_paracurve_data

@@ -0,0 +1,100 @@
1
+[[1.3100797163403826], [-27.696561125320525]]
2
+[[4.523382080736679], [21.631870943702907]]
3
+[[5.6436049604497125], [12.333879011930172]]
4
+[[-1.5872926751040453], [17.110666868837328]]
5
+[[8.142147276576058], [64.8360838821661]]
6
+[[0.6685659671798518], [12.517033834425833]]
7
+[[4.413559332289342], [32.92755049553112]]
8
+[[3.8868426006272436], [23.5089836361273]]
9
+[[-8.61857986165099], [90.33727244802301]]
10
+[[-1.5233820718518274], [-3.2530556712555736]]
11
+[[-3.7287768646968855], [3.942957977498752]]
12
+[[5.431015905700093], [6.409577412184314]]
13
+[[-0.4851976256379853], [20.476590194984738]]
14
+[[3.482900894524203], [46.1099292065206]]
15
+[[8.92656254501848], [86.52048248748359]]
16
+[[7.730177200459526], [66.76817575442749]]
17
+[[-0.2798155208496045], [5.333590490950443]]
18
+[[9.776081966892722], [106.22176626528527]]
19
+[[-2.9290137290173774], [-0.8265627410574661]]
20
+[[0.8092075593162544], [37.02850922531683]]
21
+[[-1.3318103804002224], [25.307598126181716]]
22
+[[1.277788866829093], [-11.822759130860703]]
23
+[[6.594791953188569], [64.34074820814988]]
24
+[[-9.259417611289711], [66.848400555609]]
25
+[[5.008238558179929], [63.54243386233956]]
26
+[[3.764852357217306], [24.388268795632488]]
27
+[[7.758446659758512], [99.46046255986357]]
28
+[[-1.0989170990067656], [12.435364595847016]]
29
+[[-3.4698284049336543], [23.984232900574273]]
30
+[[-9.36503253045962], [52.00275351800425]]
31
+[[-4.445824068828161], [24.867127077692643]]
32
+[[-0.9998474971781839], [7.4916712682416575]]
33
+[[-7.708061562462294], [53.08648194096224]]
34
+[[8.857293663548099], [88.14031596736535]]
35
+[[0.1230375002873565], [-1.1367313097564122]]
36
+[[8.847967849655031], [80.1192786344477]]
37
+[[6.787245675898333], [50.36090460936821]]
38
+[[6.178512323387736], [23.306067614680774]]
39
+[[6.978819008621535], [23.010197772202094]]
40
+[[2.8398440919072456], [8.786149420220555]]
41
+[[-5.671887503629385], [53.1938846922883]]
42
+[[9.36797365408864], [107.48231708829965]]
43
+[[-9.820753850006989], [81.8870141627401]]
44
+[[7.091414811503995], [63.3538228987869]]
45
+[[-5.859603267826962], [23.349246264534415]]
46
+[[8.210474489798663], [87.45847597511599]]
47
+[[1.5589001150902142], [13.366417218261823]]
48
+[[3.375371243630914], [-22.480581450720397]]
49
+[[-2.6088313302801875], [13.905501454081861]]
50
+[[-7.384866295270753], [66.63740713771406]]
51
+[[6.213882687502178], [48.3498847244585]]
52
+[[-8.14630524252751], [67.47394588639565]]
53
+[[-5.027878587405212], [28.49020420283358]]
54
+[[-9.93800499737091], [68.92554644158973]]
55
+[[-4.684305925545376], [32.24645248871957]]
56
+[[2.771122610840372], [3.8595553205642976]]
57
+[[-5.630410968933816], [24.09679577591013]]
58
+[[6.4049046543972885], [62.16152616666011]]
59
+[[3.9960685252384636], [21.891342962326778]]
60
+[[1.303112101193138], [19.645546477292815]]
61
+[[7.149455932965317], [71.82065945904844]]
62
+[[-0.3774600534946302], [24.440414907252624]]
63
+[[2.30584738436791], [47.12720314481584]]
64
+[[-0.17710216916881905], [25.812124805102197]]
65
+[[5.286634493031777], [42.92225609978689]]
66
+[[-4.867599886787632], [62.16546679335654]]
67
+[[-9.627381857142424], [86.7303767839031]]
68
+[[4.363159018182397], [-6.429113200787111]]
69
+[[0.2927983873839892], [34.79810439752676]]
70
+[[-9.25456312567909], [90.96304925903182]]
71
+[[5.156302801651265], [12.987500919082448]]
72
+[[7.302706640084956], [28.726772280470318]]
73
+[[0.6418967621998384], [-13.102101484369669]]
74
+[[1.8172087812645827], [15.430675847979963]]
75
+[[-0.11892188306192253], [-2.309501792589623]]
76
+[[-4.331293170591155], [20.724658302561448]]
77
+[[5.051094760389889], [59.193556928920124]]
78
+[[-4.317402077576231], [3.6782973464658877]]
79
+[[7.247446214562849], [48.472132250438754]]
80
+[[-4.004013187800581], [42.968799389765714]]
81
+[[6.596118032860101], [39.95228246681792]]
82
+[[3.8612149360165624], [27.227437722331903]]
83
+[[0.10714491442691099], [-17.80048424974983]]
84
+[[-6.1188815116623685], [34.49936713054759]]
85
+[[3.431790176310379], [-19.34364242625047]]
86
+[[3.2477475117471233], [7.730717234273948]]
87
+[[2.3229764816153686], [-18.260187753253003]]
88
+[[8.00624720028155], [71.21154675695986]]
89
+[[-6.332596097008361], [22.52563060138708]]
90
+[[6.986081826571272], [103.26846371939597]]
91
+[[8.628657867421957], [74.17735360349391]]
92
+[[-6.977792745656433], [35.6653180109459]]
93
+[[1.2640029171074048], [5.274240742031664]]
94
+[[-2.8902100901196537], [-0.44959830482239593]]
95
+[[0.6071818932826307], [7.793664775742688]]
96
+[[8.084142708218653], [63.105082577751084]]
97
+[[1.7614013738802292], [28.048025224353793]]
98
+[[3.87440028540534], [37.44181846415013]]
99
+[[6.339670261610959], [37.04986653142186]]
100
+[[-7.012775499072537], [70.64603592163243]]

+ 81 - 0
linear/train.py

@@ -0,0 +1,81 @@
1
+#!/usr/bin/python
2
+# -*- coding: UTF-8 -*-
3
+import sys
4
+reload(sys)
5
+sys.setdefaultencoding('utf-8')
6
+
7
+import numpy as np
8
+from sklearn.linear_model import LinearRegression
9
+from sklearn import metrics
10
+from draw import draw_util
11
+
12
+
13
+def curce_data(x,y,y_pred):
14
+	x=x.tolist()
15
+	y=y.tolist()
16
+	y_pred=y_pred.tolist()
17
+	results=zip(x,y,y_pred)
18
+	results=["{},{},{}".format(s[0][0],s[1][0],s[2][0]) for s in results ]
19
+	return results
20
+
21
+
22
+def read_data(path):
23
+	with open(path) as f :
24
+		lines=f.readlines()
25
+	lines=[eval(line.strip()) for line in lines]
26
+	X,y=zip(*lines)
27
+	X=np.array(X)
28
+	y=np.array(y)
29
+	return X,y
30
+
31
+
32
+def test():
33
+	X_train,y_train=read_data("train_data")
34
+	X_test,y_test=read_data("test_data")
35
+
36
+	#一个对象,它代表的线性回归模型,它的成员变量,就已经有了w,b. 刚生成w和b的时候 是随机的
37
+	model = LinearRegression()
38
+	#一调用这个函数,就会不停地找合适的w和b 直到误差最小
39
+	model.fit(X_train, y_train)
40
+	#打印W
41
+	print model.coef_
42
+	#打印b
43
+	print model.intercept_
44
+	#模型已经训练完毕,用模型看下在训练集的表现
45
+	y_pred_train = model.predict(X_train)
46
+	#sklearn 求解训练集的mse
47
+	# y_train 在训练集上 真实的y值
48
+	# y_pred_train 通过模型预测出来的y值
49
+	#计算  (y_train-y_pred_train)^2/n
50
+	train_mse = metrics.mean_squared_error(y_train, y_pred_train)
51
+	print "训练集MSE:".decode('utf-8'), train_mse
52
+
53
+	#看下在测试集上的效果
54
+	y_pred_test = model.predict(X_test)
55
+	test_mse = metrics.mean_squared_error(y_test, y_pred_test)
56
+	print "测试集MSE:".decode('utf-8'),test_mse
57
+
58
+	train_curve = curce_data(X_train,y_train,y_pred_train)
59
+	test_curve = curce_data(X_test,y_test,y_pred_test)
60
+	print "推广mse差".decode('utf-8'), test_mse-train_mse
61
+
62
+	'''
63
+	with open("train_curve.csv","w") as f :
64
+		f.writelines("\n".join(train_curve))
65
+	with open("test_curve.csv","w") as f :
66
+		f.writelines("\n".join(test_curve))
67
+	'''
68
+
69
+
70
+def draw_line():
71
+	x_train, y_train = read_data("train_data")
72
+	print(x_train.tolist())
73
+	print(y_train.tolist())
74
+	draw_util.drawScatter(x_train.tolist(), y_train.tolist())
75
+
76
+
77
+if __name__ == '__main__':
78
+	draw_line()
79
+	test()
80
+
81
+

+ 500 - 0
linear/train_data

@@ -0,0 +1,500 @@
1
+[[5.344187740028914], [30.91441332291272]]
2
+[[4.690797837330457], [17.989132245249227]]
3
+[[3.06514407164054], [32.67390058378043]]
4
+[[0.29136844635404446], [-15.046942990405128]]
5
+[[2.7042454045721764], [-4.198779971237319]]
6
+[[9.15496044375243], [54.50659423843143]]
7
+[[4.323588254945952], [76.06219903136115]]
8
+[[7.176051758221099], [56.32999942086154]]
9
+[[4.085369801211629], [24.249871483311825]]
10
+[[9.009164232156868], [86.55457821995637]]
11
+[[8.22931838623372], [69.31442713183536]]
12
+[[5.347990632373546], [29.231394093999132]]
13
+[[1.560025410761663], [3.3207165250820188]]
14
+[[4.4059621993455185], [-9.743791273899049]]
15
+[[7.533939545416025], [39.91068385246699]]
16
+[[3.9458478690202647], [27.915915653815176]]
17
+[[5.809863423970326], [54.36568073847534]]
18
+[[4.382427333615518], [50.68964392520528]]
19
+[[4.359049004538393], [14.274874817037169]]
20
+[[9.98584755449813], [87.51971064267029]]
21
+[[3.968983429955034], [36.65469666040323]]
22
+[[1.260697221055157], [11.852326370780558]]
23
+[[4.4280355601008194], [2.7038338688062753]]
24
+[[9.697421805354825], [46.58457810694824]]
25
+[[1.2566572127813658], [16.88434972674578]]
26
+[[5.5631549011725205], [26.30846691524932]]
27
+[[6.160673836575269], [44.08273490472635]]
28
+[[4.551876354358495], [8.621377815651693]]
29
+[[8.658091391595375], [14.655460983901259]]
30
+[[9.215808293425168], [27.352168971004836]]
31
+[[4.051136473706592], [11.76954803828216]]
32
+[[6.665674229608799], [46.0899387471286]]
33
+[[0.6887031931016663], [20.6990698021868]]
34
+[[3.618890420919011], [24.832823021460293]]
35
+[[8.701678593623612], [45.88622449848604]]
36
+[[4.19830490184753], [48.16973398999027]]
37
+[[0.6824276913336991], [34.20903793980796]]
38
+[[7.156343598751166], [29.35921237294503]]
39
+[[3.9860565277577784], [54.10225906491868]]
40
+[[6.385636601882902], [28.92549840321469]]
41
+[[0.2285826851289774], [-19.089479740640446]]
42
+[[7.782227569544897], [24.878193131187942]]
43
+[[8.19075623774227], [26.359666691361834]]
44
+[[4.859003901711594], [9.677981550554135]]
45
+[[3.798285354517156], [35.295346369519265]]
46
+[[7.154587539202738], [10.679533981928657]]
47
+[[6.016991104575534], [44.61911893872275]]
48
+[[9.18788980376523], [53.07662289163775]]
49
+[[8.15392445151059], [81.92426662930397]]
50
+[[0.8988454235024246], [-2.436217753217399]]
51
+[[3.666058049176222], [7.585687138194792]]
52
+[[8.531033550463556], [11.584441597840007]]
53
+[[3.1410838247053876], [27.12813639560672]]
54
+[[6.062390034144035], [54.88295563833771]]
55
+[[2.4822327087332074], [8.365386481418238]]
56
+[[8.793991960642835], [4.706769474615514]]
57
+[[2.0199065970833074], [12.50947892825598]]
58
+[[0.7779917366327294], [34.387578806957634]]
59
+[[4.646418882859676], [14.867341096337523]]
60
+[[8.346215763151335], [42.38240390574568]]
61
+[[1.0754837297212916], [-26.468498329380246]]
62
+[[2.7105797700123646], [-11.91150094939647]]
63
+[[1.4415323471630603], [3.506900089178033]]
64
+[[8.933362897912234], [46.88357360075013]]
65
+[[4.157119939104249], [39.68565789470964]]
66
+[[1.5631862387028472], [17.518031678520995]]
67
+[[7.2787241944984595], [54.01762650173925]]
68
+[[2.365113391591577], [5.273903174484838]]
69
+[[7.354404684601149], [19.960541641475515]]
70
+[[8.906369903371422], [43.298139007410825]]
71
+[[4.846448584988466], [45.766062874680614]]
72
+[[3.43004531354988], [9.090032402346909]]
73
+[[1.5109702583548668], [18.361830459240483]]
74
+[[4.528297298094699], [5.664039005065425]]
75
+[[8.187830202368321], [67.96585395107208]]
76
+[[7.029155690418619], [18.733992001374588]]
77
+[[9.260383893301183], [37.79728765142461]]
78
+[[9.638432674301413], [33.92454293783924]]
79
+[[8.834270126022187], [82.80892441830636]]
80
+[[1.9047395979072623], [2.7023996063975266]]
81
+[[2.129887185148723], [25.101052789709883]]
82
+[[2.872065645671702], [49.10434938181275]]
83
+[[7.429291641489718], [30.869477670675785]]
84
+[[1.3660705261191408], [-36.84204320564918]]
85
+[[4.257956096153858], [-17.39345082265206]]
86
+[[6.8783308257502895], [42.75299482725825]]
87
+[[1.7752376774957812], [-13.969448418876052]]
88
+[[2.3406622770731564], [-24.922355622817477]]
89
+[[0.5256521044176843], [-0.5164477782985637]]
90
+[[3.2549622936053204], [21.01884045374395]]
91
+[[3.4708245486690146], [17.157030170584196]]
92
+[[3.8225761372651412], [36.034324981212016]]
93
+[[1.8743990060577231], [19.665738272057418]]
94
+[[1.805139356394726], [0.3433327052800068]]
95
+[[4.847539526757658], [59.90754344693898]]
96
+[[3.8243066937935377], [2.7438201659287587]]
97
+[[7.50778782285185], [64.07850290953687]]
98
+[[8.075307298317288], [36.59525634384048]]
99
+[[0.33806031006052284], [-10.202504384201335]]
100
+[[8.569773510833757], [42.431275554811414]]
101
+[[9.648611902715844], [79.94416470773571]]
102
+[[1.0338191673097463], [-16.07780371439588]]
103
+[[5.274922216431577], [40.41997142761674]]
104
+[[0.14549406903564988], [-3.7314844602651687]]
105
+[[2.3270492683804855], [6.584121931529835]]
106
+[[0.7575362859519197], [23.59084921029622]]
107
+[[4.409899435154646], [7.6601332552103925]]
108
+[[3.827877121730391], [10.603799865622268]]
109
+[[5.435340425039229], [52.02417438179629]]
110
+[[8.623078249487424], [39.68651963492927]]
111
+[[7.290823031277337], [72.49923370083643]]
112
+[[2.7294537296230734], [0.38640135901206385]]
113
+[[4.699896387939331], [14.370757827110118]]
114
+[[9.207769733484037], [54.94675940088927]]
115
+[[3.668721686662224], [36.925843473865065]]
116
+[[4.718714522324703], [32.210615806268734]]
117
+[[6.225866896957806], [50.08056211854668]]
118
+[[9.503232664369975], [29.586576762158256]]
119
+[[0.9005319828092517], [30.58937260369009]]
120
+[[1.975920334907989], [26.571857675792412]]
121
+[[5.498245555117003], [23.730959753251945]]
122
+[[0.7343005345339304], [-15.540644180047657]]
123
+[[8.955999310971434], [55.439647242513594]]
124
+[[0.1103536552338269], [7.210977125087898]]
125
+[[3.438195551504003], [0.7822392032279424]]
126
+[[8.50242011196903], [14.030839791770688]]
127
+[[9.826330845322493], [22.306280853736606]]
128
+[[1.0889691781573185], [-6.989877374171701]]
129
+[[9.597296938018262], [27.793858830033724]]
130
+[[9.443737223325483], [66.37554901210098]]
131
+[[8.986034928029799], [52.45274518301188]]
132
+[[8.337323807703617], [51.08741973385099]]
133
+[[2.6373106396280175], [-2.030616820079193]]
134
+[[8.055737117784771], [58.62683629792019]]
135
+[[8.969489945405149], [39.159505005000675]]
136
+[[3.6496734377971474], [12.447693601501015]]
137
+[[6.694875891870291], [65.8376944482876]]
138
+[[8.6517002079403], [65.2645348952988]]
139
+[[9.730866971135137], [46.642307307527474]]
140
+[[0.20868089437102277], [-19.25547006486181]]
141
+[[4.466323140698504], [30.929624743792722]]
142
+[[2.493477707427431], [18.884678388712413]]
143
+[[9.688975128860958], [36.96283296712094]]
144
+[[6.972495242913156], [34.426473862812095]]
145
+[[1.874811643863058], [31.822992889268097]]
146
+[[8.46886829112106], [70.80238197077283]]
147
+[[4.0544019892047345], [13.85563423728646]]
148
+[[3.729078536262623], [-5.7873697617021955]]
149
+[[9.417111176167579], [51.1963202404604]]
150
+[[4.848747664533429], [-14.274706064752571]]
151
+[[9.788909368744585], [62.53162412516888]]
152
+[[7.621726464997605], [43.16820841970178]]
153
+[[2.006701266785708], [36.52203787145277]]
154
+[[7.146233788408839], [39.221335573856784]]
155
+[[6.081779973017236], [16.897985016506937]]
156
+[[1.8103557073034449], [-35.871589959799614]]
157
+[[9.685675731344777], [11.848670243998185]]
158
+[[1.787786555343186], [32.917257460941975]]
159
+[[5.617326777258253], [15.733067346888747]]
160
+[[9.76530938766783], [48.57859264261001]]
161
+[[2.9709549882102504], [33.11158534741057]]
162
+[[8.909351276210131], [63.72764109318631]]
163
+[[4.927995974738306], [30.2032873914942]]
164
+[[8.591172344617979], [26.448923985470223]]
165
+[[9.81408852812206], [36.581836169129744]]
166
+[[9.725745187950295], [23.377521571792006]]
167
+[[9.565620883485689], [34.973866623574196]]
168
+[[5.308812293744737], [61.00055796812363]]
169
+[[6.657650600232133], [18.27795084329081]]
170
+[[9.855484302300447], [43.4596286661527]]
171
+[[1.7045445519357572], [14.722271224847647]]
172
+[[2.596977213551468], [-0.06892039004274167]]
173
+[[8.956631776164999], [18.91798127349243]]
174
+[[1.0586337704421533], [4.335595869051656]]
175
+[[0.05730559413504088], [-24.11819399166289]]
176
+[[8.87028704319871], [19.175593062617512]]
177
+[[0.11104264600017899], [35.01411090508279]]
178
+[[8.97983570699938], [60.71426982685173]]
179
+[[7.591982154159903], [59.82253577499979]]
180
+[[8.61555138209027], [61.25241021167989]]
181
+[[8.927421712059214], [84.26577071731033]]
182
+[[2.9209705108273143], [49.73787972608022]]
183
+[[9.60893301177816], [85.07736194805604]]
184
+[[2.0274156332174096], [26.395930244769833]]
185
+[[9.212567327358007], [75.88275411485044]]
186
+[[8.497690996724042], [56.76297683771713]]
187
+[[7.908809831840504], [17.755050454363072]]
188
+[[4.8273707811929185], [28.988158782437722]]
189
+[[1.9824818675327083], [11.996023758655856]]
190
+[[2.591103560204883], [2.537564680679963]]
191
+[[2.1228356561963144], [5.211661673433073]]
192
+[[1.160675901396998], [3.623122256827501]]
193
+[[7.046616927428481], [13.57491799973252]]
194
+[[8.512881492262899], [68.15833127760115]]
195
+[[7.948733905965586], [47.376337976495854]]
196
+[[2.5163673621916995], [16.51677144467182]]
197
+[[8.492714262837533], [38.22066519925027]]
198
+[[4.45344342628781], [23.299748526917526]]
199
+[[6.645195573351105], [14.31498721484587]]
200
+[[4.583715369001152], [-4.379074440369343]]
201
+[[1.1644713517910232], [-0.6956792991282521]]
202
+[[3.4974665300808616], [75.53543125254218]]
203
+[[8.460944063745186], [35.04834925106921]]
204
+[[1.272008158436948], [-23.06263640794283]]
205
+[[0.20843943089938088], [-33.41209078333146]]
206
+[[9.797846161568614], [19.13989253095052]]
207
+[[7.167308469511609], [36.04815689816415]]
208
+[[9.006371133970596], [25.909486667702684]]
209
+[[2.883423033065929], [-1.9055907247946269]]
210
+[[2.1933012668993723], [6.50212330041901]]
211
+[[4.461143305304013], [22.51764386986535]]
212
+[[0.7791941427926585], [-23.486465802048627]]
213
+[[2.630462382728683], [11.571690604371613]]
214
+[[6.532156234312202], [7.9902598374348335]]
215
+[[6.943000782941114], [22.287086266747167]]
216
+[[5.961438222715665], [40.150330687598576]]
217
+[[9.74100091630721], [68.88366177075308]]
218
+[[2.029400265053077], [50.776180625208724]]
219
+[[7.761170164187173], [38.25246471862199]]
220
+[[4.529598287800494], [46.77945198231833]]
221
+[[6.037563989697743], [3.414008603476503]]
222
+[[5.375663407946059], [38.533116224538674]]
223
+[[0.21562809871277344], [63.85305855102641]]
224
+[[0.40737174988153435], [8.823306749182407]]
225
+[[7.8538183658101115], [59.25691901545788]]
226
+[[7.3190174167345265], [8.562223216853946]]
227
+[[7.9948471594206705], [45.52649922320381]]
228
+[[0.7363626491588582], [-25.374094951609532]]
229
+[[6.9296459191491], [-2.261651867848082]]
230
+[[8.432665248436175], [26.45518273721477]]
231
+[[1.2497975001353634], [-17.397320112513025]]
232
+[[6.745961510469497], [41.18620376407758]]
233
+[[4.409866630786576], [1.5549267586271007]]
234
+[[6.6725105505317694], [16.725994943609]]
235
+[[7.447509593830034], [60.962179990097624]]
236
+[[9.916437188501494], [57.4277195426611]]
237
+[[8.006979169369892], [38.54971661519791]]
238
+[[3.6437936351469946], [25.397750650731062]]
239
+[[4.2418705382290085], [37.5121943190465]]
240
+[[8.5501915978585], [40.71959534767296]]
241
+[[5.020145237855653], [17.273170904106088]]
242
+[[6.748404810451845], [32.023980600332095]]
243
+[[5.931575079329088], [51.995622408072656]]
244
+[[9.100799963215948], [37.69443158772331]]
245
+[[6.383274364225966], [38.47595060190242]]
246
+[[2.81259327943269], [15.767526379924638]]
247
+[[5.8586776562821985], [19.291127581866142]]
248
+[[3.307088799595872], [22.280606583137455]]
249
+[[7.399757590460792], [18.475753241835744]]
250
+[[3.69370429450803], [28.10535854831817]]
251
+[[7.7305964527548445], [47.57901391485482]]
252
+[[1.6988911001957552], [0.07241158206796672]]
253
+[[8.319767659681464], [32.835554933062014]]
254
+[[6.190313631817884], [87.00594563966747]]
255
+[[3.026323296164067], [17.266506643746844]]
256
+[[5.484744870353307], [36.03593374524982]]
257
+[[1.2806652744698421], [-9.09137317124875]]
258
+[[6.013177250320329], [48.52196007018655]]
259
+[[7.1317838842524655], [36.31005005707061]]
260
+[[8.051229547389152], [57.78081878262149]]
261
+[[7.383816382624618], [37.78817037869924]]
262
+[[4.214991041150398], [37.31608861135604]]
263
+[[5.062175435306236], [81.66561776398368]]
264
+[[4.59148283494518], [83.63394442325948]]
265
+[[8.672548510960207], [78.32853797992539]]
266
+[[4.421081995841095], [55.733312635728396]]
267
+[[1.2481754207797413], [23.547865089438428]]
268
+[[9.360885950311417], [61.63502189095658]]
269
+[[4.21228598950997], [3.16204650751637]]
270
+[[3.490609120229735], [22.962605216566217]]
271
+[[3.385536156282014], [38.47324279630292]]
272
+[[8.291235149019894], [33.889035169044135]]
273
+[[8.443673266960998], [39.785077689973676]]
274
+[[0.1419246306024169], [58.81480808707774]]
275
+[[6.010090080574151], [9.15204672182503]]
276
+[[8.245110134458466], [0.9436714646878361]]
277
+[[9.371416034812924], [20.640064318577608]]
278
+[[8.928105200277393], [70.73053979374096]]
279
+[[2.6933852646740153], [14.85703691519636]]
280
+[[6.624969214895568], [49.12585639398856]]
281
+[[2.3119136561804545], [5.854987477971048]]
282
+[[1.625739818000148], [0.42576431161443806]]
283
+[[4.599998105061883], [23.3693509576483]]
284
+[[8.85953003163596], [11.90466775595498]]
285
+[[8.849648097487485], [38.044149805953396]]
286
+[[7.777298118361248], [26.635743260957607]]
287
+[[7.836127978997673], [23.160799479422096]]
288
+[[5.1530455535611175], [31.819284235871624]]
289
+[[9.4300306240366], [53.41007463783529]]
290
+[[9.792149820050774], [67.00385683484585]]
291
+[[3.625188535992997], [2.954409426778671]]
292
+[[6.110569958966642], [41.8054744767267]]
293
+[[2.6913887817585493], [8.429141197093278]]
294
+[[3.0456714130345963], [25.71483525624232]]
295
+[[5.926597545978654], [47.93558573608567]]
296
+[[3.815407547664016], [20.27429600134248]]
297
+[[6.703085069599275], [31.63640461792912]]
298
+[[8.355591232097122], [50.56567711668815]]
299
+[[8.756938671999627], [85.05682489609329]]
300
+[[2.375345891377749], [-17.81670610301164]]
301
+[[2.493346745653745], [17.017088000100514]]
302
+[[8.615659425986447], [43.312269969378576]]
303
+[[5.001646635648505], [46.58652824891815]]
304
+[[0.5606901965088307], [15.403115516850974]]
305
+[[8.974133912959939], [36.27251347195046]]
306
+[[7.582053201005138], [23.954479676006493]]
307
+[[8.599351524660946], [53.52929494333892]]
308
+[[6.592481126941273], [24.74121338974936]]
309
+[[6.040559567751366], [-3.4082446576619745]]
310
+[[6.851543918172247], [1.9948790614063685]]
311
+[[2.4355316698894383], [24.049106649289673]]
312
+[[7.997734725241278], [49.155032110492925]]
313
+[[8.43373366349954], [57.7016159133016]]
314
+[[3.4032031676435404], [48.209249069582974]]
315
+[[2.0090847947749024], [12.327119003913202]]
316
+[[7.530069097700894], [38.149183080243894]]
317
+[[9.619627542649607], [54.82395422222435]]
318
+[[4.927270578782526], [46.53395237794213]]
319
+[[0.7605159827705488], [24.588781235941457]]
320
+[[9.748594779918683], [72.08595812506977]]
321
+[[2.7845365073754467], [22.934614225165426]]
322
+[[2.4603755167181007], [12.00825667357023]]
323
+[[6.94604655249541], [1.8211442905932298]]
324
+[[9.602806047717765], [73.65328837900631]]
325
+[[7.5007227628096], [51.37321705833652]]
326
+[[0.1291279950616253], [-7.396950808763803]]
327
+[[6.774660894131122], [51.83640012054808]]
328
+[[6.787387855408057], [31.81115041551974]]
329
+[[2.103983727341917], [35.628497377042734]]
330
+[[9.42850257395519], [72.74691053544716]]
331
+[[4.717403335643585], [21.966277956009]]
332
+[[7.015709816550166], [34.336437714640084]]
333
+[[3.4333000468864485], [11.34515930964374]]
334
+[[6.410671359384594], [16.720355111096683]]
335
+[[9.332137148176294], [10.20274229292766]]
336
+[[4.803403616111717], [30.434116411151603]]
337
+[[4.809133029997353], [25.670428544022833]]
338
+[[9.508020819296522], [5.2320688995912485]]
339
+[[5.408351334521755], [49.56707712680438]]
340
+[[2.597824450169963], [12.313555146522091]]
341
+[[9.219959888677717], [59.948218277160485]]
342
+[[8.933999847242298], [48.156932901211476]]
343
+[[8.183539117662319], [50.00768480336893]]
344
+[[2.4610364542755816], [40.99672876763748]]
345
+[[4.78388836505858], [17.523060508948326]]
346
+[[4.290367676446315], [-5.671539336170729]]
347
+[[9.823442547567357], [66.6281540117045]]
348
+[[2.043102847624798], [24.838801347294364]]
349
+[[3.1571283345392964], [1.1145930485378306]]
350
+[[4.706977828412089], [11.613476234731973]]
351
+[[4.432162190955483], [16.903654039328462]]
352
+[[8.441161355867239], [26.145267751014035]]
353
+[[1.37031480007473], [20.613661356311525]]
354
+[[2.8171504139089976], [39.548184817410636]]
355
+[[4.3181390785815275], [54.68929630660155]]
356
+[[6.82192450036821], [53.120512377135746]]
357
+[[1.6564525018015674], [9.339941418939766]]
358
+[[3.1488212288741635], [41.828229676802536]]
359
+[[4.460617918199171], [10.504900397777412]]
360
+[[9.17661757776678], [65.56728288835438]]
361
+[[0.10580699697470886], [-2.229941178282401]]
362
+[[9.431474712824043], [69.2840884477175]]
363
+[[5.527504958528636], [2.750661527618072]]
364
+[[1.9347715531597853], [-13.085164292489438]]
365
+[[9.703942180524995], [63.102817785423724]]
366
+[[1.0413630975026467], [2.9189395967611924]]
367
+[[5.5025355029154674], [1.3571193570721616]]
368
+[[8.519407435144258], [38.602176789125814]]
369
+[[9.552936484563736], [82.45167233849226]]
370
+[[3.6930057011786053], [-7.511431918891932]]
371
+[[3.0604531324931195], [11.932413319167042]]
372
+[[3.2807142439947015], [36.13303096528594]]
373
+[[6.123130361880634], [9.049044760611634]]
374
+[[1.6501522698417137], [6.16153312699578]]
375
+[[1.900977993890981], [51.49550903885193]]
376
+[[0.5274559234773335], [14.41997354699642]]
377
+[[7.746271557742695], [75.32086861265518]]
378
+[[8.796525877303765], [50.22302084181331]]
379
+[[0.6603643756119482], [14.696612173059743]]
380
+[[1.3123352121032117], [10.83064394189366]]
381
+[[0.04979237383854751], [-1.27720715982875]]
382
+[[7.7312236184399215], [44.94244129100787]]
383
+[[7.192110931879294], [61.234979700963834]]
384
+[[6.9944565892025885], [44.043819178431676]]
385
+[[9.143968344025977], [21.241572823459613]]
386
+[[2.390232274402692], [4.722993683318545]]
387
+[[3.442492166615294], [16.708259490363126]]
388
+[[9.537824929513498], [106.90936712533906]]
389
+[[4.455881373396956], [6.809873107452473]]
390
+[[7.894185232962274], [28.808388882630734]]
391
+[[1.5802569487326268], [32.114943078383725]]
392
+[[4.447801606528118], [49.15653731373572]]
393
+[[5.126701520877336], [13.239702972623878]]
394
+[[1.1671796664478606], [44.72050540673414]]
395
+[[9.295462589448773], [24.252257494435632]]
396
+[[2.7875770251154774], [35.61156331119557]]
397
+[[9.284441486336917], [64.7099248593629]]
398
+[[3.9112202243845284], [8.404591463662667]]
399
+[[8.50518622986826], [41.714943395789675]]
400
+[[1.2194226907908146], [-9.447182673379489]]
401
+[[2.146907750166931], [6.399267066580958]]
402
+[[3.2608714076179046], [22.986409337894237]]
403
+[[1.6198954224249584], [40.422404644574456]]
404
+[[7.321210252117899], [66.33153499291461]]
405
+[[9.928608955305238], [62.455859430624585]]
406
+[[5.531014785352105], [34.783876454721394]]
407
+[[4.188917344758187], [4.753209770158497]]
408
+[[1.2641016683481898], [-34.50028672332751]]
409
+[[1.4970159220618129], [-16.466972590161735]]
410
+[[8.184146800498839], [49.01638174575901]]
411
+[[8.61849337249395], [50.33625264498656]]
412
+[[7.4630413629138275], [62.83683134104041]]
413
+[[7.11071098041495], [22.496621204830543]]
414
+[[9.144426892723068], [10.118940733087387]]
415
+[[1.4697738885015343], [20.596166066752286]]
416
+[[5.307993926916565], [30.963232111451795]]
417
+[[1.9469737687838307], [34.37270743744643]]
418
+[[5.156044780087445], [43.89182104313191]]
419
+[[8.614330871298728], [55.66590617761084]]
420
+[[0.27860302344652865], [-5.472054586811887]]
421
+[[4.910235640136646], [12.755759028135701]]
422
+[[8.558609250494447], [46.58551116454578]]
423
+[[2.9922563137661404], [13.455544174165949]]
424
+[[8.690867342780821], [92.12184975123299]]
425
+[[6.8766085805822605], [54.722292713502945]]
426
+[[9.798965309332441], [73.31220348258981]]
427
+[[1.5927349663933188], [48.42948530559911]]
428
+[[3.938182028104742], [44.82027340638032]]
429
+[[0.4488618243459874], [-33.85571127341106]]
430
+[[4.243260986588449], [29.425735401879557]]
431
+[[0.36557455297250296], [7.60528868940977]]
432
+[[4.627119365561506], [12.068988094197241]]
433
+[[3.010843562785639], [10.471294903795362]]
434
+[[1.9743629764664727], [2.563522787480701]]
435
+[[6.6210325519668745], [58.13424528679076]]
436
+[[9.363389825132773], [61.655832284378164]]
437
+[[4.0048926781206164], [-2.641574551503109]]
438
+[[5.3526069847185385], [11.738000067808757]]
439
+[[8.473943781431597], [16.186325677186158]]
440
+[[0.6850803050816134], [-16.524398015734935]]
441
+[[5.81153595499293], [74.82923130738158]]
442
+[[4.336316477001461], [33.282063024012515]]
443
+[[4.6607781750488], [38.42594166314506]]
444
+[[6.789497177588542], [65.74954342580114]]
445
+[[2.1837579637317592], [-21.452034405672393]]
446
+[[8.958226785897438], [64.78578340522085]]
447
+[[2.2544388807682], [-2.437319548936268]]
448
+[[6.114974090112639], [77.72321655297503]]
449
+[[7.046875609673853], [42.36226864747536]]
450
+[[4.909396778485878], [54.38061616567643]]
451
+[[6.433656425074917], [54.48704800613696]]
452
+[[7.656828106783953], [22.197743958451937]]
453
+[[5.0731785307480095], [9.579569113157774]]
454
+[[1.2543198061408167], [41.81221815217522]]
455
+[[5.379276640601088], [21.875316211164858]]
456
+[[7.3508002274377215], [44.716719130789755]]
457
+[[4.93436178387173], [12.637391091308949]]
458
+[[2.8839385406275575], [17.82142533683158]]
459
+[[5.442906950096687], [15.927031089151107]]
460
+[[7.325560954739779], [-10.296495122081014]]
461
+[[0.4999100380622057], [35.629816760922076]]
462
+[[6.76282738554698], [37.76473618119009]]
463
+[[2.6420423655625394], [-14.783852716374618]]
464
+[[1.8181630033594298], [3.422668314320039]]
465
+[[5.740144525117367], [24.804796902935184]]
466
+[[8.383274892940424], [52.84398842992908]]
467
+[[7.914477038207862], [47.524413225023686]]
468
+[[0.09571378052649959], [-12.967817190867976]]
469
+[[7.897956377879581], [31.898524534304467]]
470
+[[7.771514446618216], [33.42396455188919]]
471
+[[8.068251955408105], [34.060304865635246]]
472
+[[5.507114712682457], [29.067749928273294]]
473
+[[7.185257036451301], [66.94174553898847]]
474
+[[8.069475811171849], [18.388603904090715]]
475
+[[5.948945048866956], [46.77407162979844]]
476
+[[5.94586268535166], [33.8270139810513]]
477
+[[4.039085625237028], [-6.9537357888583236]]
478
+[[2.1110884441290545], [41.777778481103084]]
479
+[[0.5637203810784464], [7.820888319144953]]
480
+[[2.981561598749755], [36.59933521473512]]
481
+[[2.3051633456394516], [9.688660837052936]]
482
+[[9.432156496152137], [62.20450179200939]]
483
+[[5.120497083962297], [67.58120862172012]]
484
+[[0.397133005866922], [-23.275177498714925]]
485
+[[6.77348839900776], [25.743640635393543]]
486
+[[5.63759875419156], [36.10549931547563]]
487
+[[7.412697484464735], [40.46315753335505]]
488
+[[7.725643288479405], [5.070528267459375]]
489
+[[7.962260006039173], [38.71198509433739]]
490
+[[7.166300307934296], [47.21893989425605]]
491
+[[0.22907656218386974], [-12.020362195485252]]
492
+[[2.0079884491748445], [11.067261892470324]]
493
+[[8.226381408246816], [55.034651195845676]]
494
+[[2.697160972439603], [12.9054549710716]]
495
+[[4.201435073835789], [-3.8566284457268867]]
496
+[[0.7191090383725585], [33.197759583146414]]
497
+[[6.852308016632246], [35.60728761526811]]
498
+[[8.508094719275764], [54.92983106932969]]
499
+[[2.690643264118905], [23.854984486074112]]
500
+[[8.99188960377921], [41.60952797220094]]

+ 44 - 0
linear/train_xsquare.py

@@ -0,0 +1,44 @@
1
+#!/usr/bin/python
2
+# -*- coding: UTF-8 -*-
3
+import sys
4
+
5
+reload(sys)
6
+sys.setdefaultencoding('utf-8')
7
+import numpy as np
8
+from sklearn.linear_model import LinearRegression
9
+from sklearn import metrics
10
+
11
+
12
+def extend_feature(x):
13
+    # return [x[0]]
14
+    return [x[0], x[0] * x[0]]
15
+
16
+
17
+def read_data(path):
18
+    with open(path) as f:
19
+        lines = f.readlines()
20
+    lines = [eval(line.strip()) for line in lines]
21
+    X, y = zip(*lines)
22
+    X = [extend_feature(x) for x in X]
23
+    X = np.array(X)
24
+    y = np.array(y)
25
+    return X, y
26
+
27
+
28
+if __name__ == '__main__':
29
+    X_train, y_train = read_data("train_paracurve_data")
30
+    X_test, y_test = read_data("test_paracurve_data")
31
+    model = LinearRegression()
32
+    model.fit(X_train, y_train)
33
+    print model.coef_
34
+    print model.intercept_
35
+
36
+    y_pred_train = model.predict(X_train)
37
+    train_mse = metrics.mean_squared_error(y_train, y_pred_train)
38
+    print "特征+平方非线性"
39
+    print "MSE:", train_mse
40
+    y_pred_test = model.predict(X_test)
41
+    test_mse = metrics.mean_squared_error(y_test, y_pred_test)
42
+    print "MSE:", test_mse
43
+    print "推广mse差", test_mse - train_mse
44
+