Skip to content

Latest commit

 

History

History
813 lines (790 loc) · 8.11 KB

03-02 擬合.md

File metadata and controls

813 lines (790 loc) · 8.11 KB

03-02 擬合

執行結果:

GitHub Logo

(1)main.py

import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
import math

#----------------------------------------
# 讀取網路流量資料 (時間序列, 流量)
#----------------------------------------
data=np.genfromtxt('webTraffic.csv',  names=['time', 'amount'], delimiter=',')

# 找出趨近測試資料的n階線性方程式系數
fp1=sp.polyfit(data['time'], data['amount'], 1)
fp2=sp.polyfit(data['time'], data['amount'], 2)
fp3=sp.polyfit(data['time'], data['amount'], 3)

# 擬合方程式
f1=sp.poly1d(fp1)
f2=sp.poly1d(fp2)
f3=sp.poly1d(fp3)

# 印出誤差
print(f1.order, '階-', '誤差:', math.log(sp.sum((data['amount']-f1(data['time']))**2)))
print(f2.order, '階-', '誤差:', math.log(sp.sum((data['amount']-f2(data['time']))**2)))
print(f3.order, '階-', '誤差:', math.log(sp.sum((data['amount']-f3(data['time']))**2)))


#---------------------------
# 繪圖
#---------------------------
fig = plt.figure()

# 設定圖標題
plt.title('Web Traffic')

# 設定x軸及y軸標題
plt.xlabel('time')
plt.ylabel('amount')

#傳回1000個(從0到x元素個數), 間隔相同的數
fx=sp.linspace(0, data['time'][-1], 1000)

# 將測試資料畫在圖上
plt.axis([-100, 800, 0, 7500])
plt.plot(data['time'], data['amount'], 'y+')

# 將產生的數一個個代入線性方程式, 並畫在圖上
plt.plot(fx, f1(fx), 'r-', linewidth=4)
plt.plot(fx, f2(fx), 'g-', linewidth=4)
plt.plot(fx, f3(fx), 'b-', linewidth=4)

# 產生左上角的標籤說明
plt.legend(['data','Power of 1', 'Power of 2', 'Power of 3'], loc="upper left")

# 畫出格線
plt.grid()

#---------------------------
# 儲存圖檔
#---------------------------
fig.savefig('graph.png')

(2)webTraffic.csv

1,2272
3,1386
4,1365
5,1488
6,1337
7,1883
8,2283
9,1335
10,1025
11,1139
12,1477
13,1203
14,1311
15,1299
16,1494
17,1159
18,1365
19,1272
20,1246
21,1071
22,1876
24,1410
25,925
26,1533
27,2104
28,2113
29,1993
30,1045
31,2090
32,2227
33,1413
34,1718
35,1721
36,1291
37,1838
38,2540
39,1608
40,2455
41,1929
42,1767
43,1203
44,1761
45,1723
46,2160
47,808
49,1324
50,1809
51,1933
52,1351
53,2013
54,1207
55,2170
56,1700
57,1899
58,1757
59,1475
60,1921
61,1971
62,1809
63,1365
64,1775
65,1687
66,1706
67,1353
68,1316
69,1512
70,2430
71,1788
72,1380
73,1357
74,990
75,1586
76,2057
77,1690
78,1458
79,1201
80,1949
81,1493
82,1653
83,1217
84,1457
85,1179
86,1484
87,2730
88,1414
89,1060
90,1573
91,1260
92,1216
93,981
94,1345
96,1667
97,730
98,1034
99,1628
100,1155
101,1305
102,1444
103,2242
104,1842
105,1210
106,1384
107,1313
108,1508
109,1796
110,1265
111,1090
112,2159
113,1167
114,1391
115,1445
116,1196
117,1049
118,1999
119,472
120,1285
121,1737
122,1534
123,2636
124,1372
125,1325
126,833
127,1200
128,2431
129,1740
130,2121
131,1726
132,1344
133,1072
134,1386
135,1054
136,1051
137,1270
138,1857
139,1437
140,2016
141,1352
142,909
143,1761
144,1009
145,2035
146,1534
147,1708
148,733
149,1455
150,1332
151,1606
152,1065
153,1291
155,1495
156,1928
157,2249
158,987
159,1023
160,875
161,1569
162,1032
163,1079
164,1087
165,1152
166,961
167,1232
168,2188
169,1179
170,1475
171,1612
172,921
173,2432
174,1650
175,1077
176,823
177,1578
178,1872
179,1669
182,1407
183,1619
184,894
185,1948
186,2298
187,2163
188,1108
189,1731
190,1601
191,1684
192,2025
193,1688
194,1736
195,1474
196,1770
197,1348
198,1570
199,1861
200,1458
201,2282
202,1553
203,2323
204,1202
205,1768
206,2184
207,1329
208,1781
209,1242
211,1454
212,1501
213,875
214,1521
215,2611
216,1948
217,1707
218,1335
219,2211
220,1358
221,2501
222,1764
223,1527
224,1421
225,1949
226,2156
227,1503
228,1658
229,1032
230,1536
231,1345
232,2022
233,2035
234,2109
235,1587
236,1666
237,1064
238,1457
239,2399
240,1449
241,2406
242,1831
243,1423
244,1754
245,1641
246,1428
247,1928
248,1618
249,1361
250,1273
251,1300
252,997
253,1163
254,1480
255,2131
256,1833
257,1161
258,1168
259,1570
260,1675
261,966
262,1395
263,1638
264,1713
265,1799
266,1917
267,1894
268,1009
269,1003
270,1962
271,1730
272,731
273,2166
274,1059
275,1520
276,1708
277,1227
278,1085
279,1045
280,1720
281,1495
282,960
283,1420
284,1318
285,740
286,878
287,1357
288,2318
289,1544
290,1583
291,1693
292,1153
293,1469
294,2004
295,1114
296,1281
297,1500
298,1409
299,942
300,792
301,704
302,1584
303,1004
304,795
305,1000
306,2156
307,639
308,1391
309,1644
310,1398
311,967
312,1578
313,1068
314,1419
315,1784
316,1952
317,996
318,1485
319,1419
320,1534
321,1633
322,1013
323,2085
324,3102
325,1859
326,983
327,2169
328,2086
329,2204
330,1578
331,1526
332,1725
333,936
334,1678
335,1573
336,1187
337,1535
338,1333
339,1701
340,1925
341,1651
342,1491
343,1800
344,1976
345,1246
346,2141
347,1351
348,1505
349,1377
350,2386
351,1304
352,1424
353,1881
354,2393
355,1599
356,1444
357,1985
358,1158
359,2098
360,1540
361,1410
362,2115
363,1278
364,2039
365,2021
366,1901
367,1139
368,1903
369,2074
370,3661
371,1799
372,2431
373,1499
374,1040
375,1825
376,1733
377,1727
378,1076
379,1598
380,1146
381,1534
382,1514
383,1540
384,1445
385,1248
386,1710
387,2114
388,1816
389,1759
390,2173
391,1791
392,1710
393,1930
394,1803
395,1879
396,2289
397,1839
398,1641
399,1374
400,1524
401,1360
402,1303
403,1654
404,1928
405,1558
406,1736
407,1752
408,1042
409,1201
410,1498
411,2101
412,2389
413,1326
414,1285
415,1413
416,1970
417,1242
418,1920
419,1163
420,1651
421,1300
422,1850
423,1799
424,1703
425,1627
426,1522
427,1409
428,2631
429,1647
430,1536
431,1433
432,1749
433,1274
434,1658
435,1579
436,1607
437,1382
438,1322
439,1168
440,1067
441,1890
442,1659
443,1064
444,868
445,1288
446,2166
447,1382
448,1417
449,2018
450,1777
451,1596
452,1420
453,1324
454,1899
455,1513
456,1683
457,1369
458,1266
459,1034
460,2045
461,1498
462,1607
463,1331
464,1132
465,1238
466,2298
467,1241
468,2039
469,1177
470,1220
471,1746
472,1917
473,1165
474,860
475,1830
476,1170
477,1229
478,1274
479,1900
480,1867
481,1610
482,1963
483,1669
484,1291
485,1751
486,1335
487,1323
488,1652
489,2086
490,1437
491,1731
492,1950
493,2203
494,2260
495,1580
496,1562
497,1860
498,1793
499,1000
500,1912
501,2475
502,2105
503,1732
504,2309
505,1874
506,1816
507,1097
508,2015
509,2241
510,2772
511,1320
512,2738
513,1389
514,2251
515,2167
516,2028
517,1590
518,2341
519,2011
520,1613
521,1671
522,1999
523,2894
524,2637
525,1884
526,2404
527,2255
528,1960
529,1847
530,1558
531,1559
532,2040
533,1996
534,2051
535,1803
536,1969
537,1937
538,2082
539,1408
540,2731
541,2220
542,2330
543,2437
544,1915
545,1986
546,2145
547,2276
548,2157
549,2626
550,1536
551,1558
552,3044
553,2246
554,2383
555,2009
556,1972
557,2145
558,2102
559,2327
560,1732
561,2640
562,1992
563,2199
564,2393
565,2190
566,2495
567,2390
568,2435
569,1737
570,2052
571,2034
572,1834
573,3005
574,1429
575,2215
576,1902
577,2284
578,1993
579,2059
580,2169
581,1981
582,2098
583,2506
584,1911
585,2560
586,1301
587,1859
588,2286
589,1734
590,2156
591,2402
592,2404
593,3244
594,1977
595,2412
596,2007
597,2014
598,1564
599,2022
600,1772
601,2582
602,1845
603,1621
604,1770
605,2021
606,2355
607,1996
608,2127
609,2113
610,1935
611,2125
612,1786
613,2276
614,2978
615,2542
616,2112
617,1968
618,2368
619,2241
620,2073
621,2122
622,2166
623,2575
624,2500
625,2181
626,1967
627,2072
628,2027
629,2345
630,2024
631,2249
632,2455
633,2265
634,2425
635,2851
636,1997
637,3298
638,2366
639,1853
640,2896
641,2537
642,2300
643,2849
644,2974
645,1931
646,3009
647,2538
648,2782
649,2491
650,2408
651,2003
652,2752
653,2576
654,2818
655,2683
656,2628
657,2994
658,2303
659,2771
660,2607
661,2704
662,2839
663,3256
664,3025
665,2684
666,3006
667,3310
668,3183
669,2523
670,3401
671,2840
672,3193
673,2969
674,3337
675,3464
676,3264
677,3535
678,3089
679,2935
680,3007
681,4000
682,3488
683,2814
684,3382
685,2901
686,4260
687,3785
688,4139
689,3588
690,3343
691,3118
692,3456
693,4150
694,3827
695,3992
696,4667
697,3301
698,3931
699,4496
700,3402
701,3672
702,3550
703,4230
704,3805
705,3352
706,3602
707,4015
708,3548
709,3316
710,3932
711,3596
712,5289
713,3561
714,3990
715,3889
716,3636
717,3799
718,4188
719,5248
720,4176
721,4829
722,4346
723,4224
724,4813
725,3997
726,4357
727,4322
728,4156
729,4630
730,4415
731,4410
732,4724
733,4363
734,4798
735,4749
736,5143
737,4906
738,4309
739,4970
740,4813
741,5392
742,5906
743,4881