-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
517 lines (309 loc) · 772 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Aaron's Blog</title>
<link href="https://aaronos0.github.io/atom.xml" rel="self"/>
<link href="https://aaronos0.github.io/"/>
<updated>2021-10-14T15:32:03.000Z</updated>
<id>https://aaronos0.github.io/</id>
<author>
<name>Aaron</name>
</author>
<generator uri="https://hexo.io/">Hexo</generator>
<entry>
<title>1.Time and Space Complexity</title>
<link href="https://aaronos0.github.io/2021/10/14/1-Time-and-Space-Complexity/"/>
<id>https://aaronos0.github.io/2021/10/14/1-Time-and-Space-Complexity/</id>
<published>2021-10-14T15:32:03.000Z</published>
<updated>2021-10-14T15:32:03.000Z</updated>
<content type="html"><![CDATA[<h1 id="1-Time-Complexity"><a href="#1-Time-Complexity" class="headerlink" title="1.Time Complexity"></a>1.Time Complexity</h1><p>A indicator to evaluate the time cost of an algorithm.</p><h2 id="1-1-O-1"><a href="#1-1-O-1" class="headerlink" title="1.1. O(1)"></a>1.1. O(1)</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="string">"Hello World!"</span>)</span><br></pre></td></tr></table></figure><h2 id="1-2-O-n"><a href="#1-2-O-n" class="headerlink" title="1.2. O(n)"></a>1.2. O(n)</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(n):</span><br><span class="line"> <span class="built_in">print</span>(i)</span><br></pre></td></tr></table></figure><span id="more"></span><h2 id="1-3-O-n-2"><a href="#1-3-O-n-2" class="headerlink" title="1.3. O($n^2$)"></a>1.3. O(<mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.025ex" xmlns="http://www.w3.org/2000/svg" width="2.27ex" height="1.912ex" role="img" focusable="false" viewBox="0 -833.9 1003.6 844.9"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="msup"><g data-mml-node="mi"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mn" transform="translate(600, 363) scale(0.707)"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></g></g></g></g></svg></mjx-container>)</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(n):</span><br><span class="line"> <span class="keyword">for</span> j <span class="keyword">in</span> <span class="built_in">range</span>(n):</span><br><span class="line"> <span class="built_in">print</span>(i,j)</span><br></pre></td></tr></table></figure><h2 id="1-4-O-logn"><a href="#1-4-O-logn" class="headerlink" title="1.4. O(logn)"></a>1.4. O(logn)</h2><p><mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.673ex" xmlns="http://www.w3.org/2000/svg" width="17.042ex" height="2.573ex" role="img" focusable="false" viewBox="0 -840.1 7532.4 1137.4"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="msup"><g data-mml-node="mn"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></g><g data-mml-node="mn" transform="translate(500, 363) scale(0.707)"><path data-c="35" d="M164 157Q164 133 148 117T109 101H102Q148 22 224 22Q294 22 326 82Q345 115 345 210Q345 313 318 349Q292 382 260 382H254Q176 382 136 314Q132 307 129 306T114 304Q97 304 95 310Q93 314 93 485V614Q93 664 98 664Q100 666 102 666Q103 666 123 658T178 642T253 634Q324 634 389 662Q397 666 402 666Q410 666 410 648V635Q328 538 205 538Q174 538 149 544L139 546V374Q158 388 169 396T205 412T256 420Q337 420 393 355T449 201Q449 109 385 44T229 -22Q148 -22 99 32T50 154Q50 178 61 192T84 210T107 214Q132 214 148 197T164 157Z"></path></g></g><g data-mml-node="mo" transform="translate(1181.3, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mn" transform="translate(2237.1, 0)"><path data-c="33" d="M127 463Q100 463 85 480T69 524Q69 579 117 622T233 665Q268 665 277 664Q351 652 390 611T430 522Q430 470 396 421T302 350L299 348Q299 347 308 345T337 336T375 315Q457 262 457 175Q457 96 395 37T238 -22Q158 -22 100 21T42 130Q42 158 60 175T105 193Q133 193 151 175T169 130Q169 119 166 110T159 94T148 82T136 74T126 70T118 67L114 66Q165 21 238 21Q293 21 321 74Q338 107 338 175V195Q338 290 274 322Q259 328 213 329L171 330L168 332Q166 335 166 348Q166 366 174 366Q202 366 232 371Q266 376 294 413T322 525V533Q322 590 287 612Q265 626 240 626Q208 626 181 615T143 592T132 580H135Q138 579 143 578T153 573T165 566T175 555T183 540T186 520Q186 498 172 481T127 463Z"></path><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z" transform="translate(500, 0)"></path></g><g data-mml-node="mo" transform="translate(3237.1, 0)"><path data-c="2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path></g><g data-mml-node="mi" transform="translate(3681.8, 0)"><path data-c="1D459" d="M117 59Q117 26 142 26Q179 26 205 131Q211 151 215 152Q217 153 225 153H229Q238 153 241 153T246 151T248 144Q247 138 245 128T234 90T214 43T183 6T137 -11Q101 -11 70 11T38 85Q38 97 39 102L104 360Q167 615 167 623Q167 626 166 628T162 632T157 634T149 635T141 636T132 637T122 637Q112 637 109 637T101 638T95 641T94 647Q94 649 96 661Q101 680 107 682T179 688Q194 689 213 690T243 693T254 694Q266 694 266 686Q266 675 193 386T118 83Q118 81 118 75T117 65V59Z"></path></g><g data-mml-node="mi" transform="translate(3979.8, 0)"><path data-c="1D45C" d="M201 -11Q126 -11 80 38T34 156Q34 221 64 279T146 380Q222 441 301 441Q333 441 341 440Q354 437 367 433T402 417T438 387T464 338T476 268Q476 161 390 75T201 -11ZM121 120Q121 70 147 48T206 26Q250 26 289 58T351 142Q360 163 374 216T388 308Q388 352 370 375Q346 405 306 405Q243 405 195 347Q158 303 140 230T121 120Z"></path></g><g data-mml-node="msubsup" transform="translate(4464.8, 0)"><g data-mml-node="mi"><path data-c="1D454" d="M311 43Q296 30 267 15T206 0Q143 0 105 45T66 160Q66 265 143 353T314 442Q361 442 401 394L404 398Q406 401 409 404T418 412T431 419T447 422Q461 422 470 413T480 394Q480 379 423 152T363 -80Q345 -134 286 -169T151 -205Q10 -205 10 -137Q10 -111 28 -91T74 -71Q89 -71 102 -80T116 -111Q116 -121 114 -130T107 -144T99 -154T92 -162L90 -164H91Q101 -167 151 -167Q189 -167 211 -155Q234 -144 254 -122T282 -75Q288 -56 298 -13Q311 35 311 43ZM384 328L380 339Q377 350 375 354T369 368T359 382T346 393T328 402T306 405Q262 405 221 352Q191 313 171 233T151 117Q151 38 213 38Q269 38 323 108L331 118L384 328Z"></path></g><g data-mml-node="TeXAtom" transform="translate(477, 369.2) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="33" d="M127 463Q100 463 85 480T69 524Q69 579 117 622T233 665Q268 665 277 664Q351 652 390 611T430 522Q430 470 396 421T302 350L299 348Q299 347 308 345T337 336T375 315Q457 262 457 175Q457 96 395 37T238 -22Q158 -22 100 21T42 130Q42 158 60 175T105 193Q133 193 151 175T169 130Q169 119 166 110T159 94T148 82T136 74T126 70T118 67L114 66Q165 21 238 21Q293 21 321 74Q338 107 338 175V195Q338 290 274 322Q259 328 213 329L171 330L168 332Q166 335 166 348Q166 366 174 366Q202 366 232 371Q266 376 294 413T322 525V533Q322 590 287 612Q265 626 240 626Q208 626 181 615T143 592T132 580H135Q138 579 143 578T153 573T165 566T175 555T183 540T186 520Q186 498 172 481T127 463Z"></path><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z" transform="translate(500, 0)"></path></g></g><g data-mml-node="TeXAtom" transform="translate(477, -297.3) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></g></g></g><g data-mml-node="mo" transform="translate(5976.7, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mn" transform="translate(7032.4, 0)"><path data-c="35" d="M164 157Q164 133 148 117T109 101H102Q148 22 224 22Q294 22 326 82Q345 115 345 210Q345 313 318 349Q292 382 260 382H254Q176 382 136 314Q132 307 129 306T114 304Q97 304 95 310Q93 314 93 485V614Q93 664 98 664Q100 666 102 666Q103 666 123 658T178 642T253 634Q324 634 389 662Q397 666 402 666Q410 666 410 648V635Q328 538 205 538Q174 538 149 544L139 546V374Q158 388 169 396T205 412T256 420Q337 420 393 355T449 201Q449 109 385 44T229 -22Q148 -22 99 32T50 154Q50 178 61 192T84 210T107 214Q132 214 148 197T164 157Z"></path></g></g></g></svg></mjx-container></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">n = <span class="number">32</span></span><br><span class="line"><span class="keyword">while</span> n > <span class="number">1</span>:</span><br><span class="line"> n = n/<span class="number">2</span></span><br><span class="line"> <span class="built_in">print</span>(n)</span><br></pre></td></tr></table></figure><h2 id="1-5-Common-time-complexity-by-order"><a href="#1-5-Common-time-complexity-by-order" class="headerlink" title="1.5. Common time complexity by order"></a>1.5. Common time complexity by order</h2><p><mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex" xmlns="http://www.w3.org/2000/svg" width="67.147ex" height="2.452ex" role="img" focusable="false" viewBox="0 -833.9 29679 1083.9"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mi"><path data-c="1D442" d="M740 435Q740 320 676 213T511 42T304 -22Q207 -22 138 35T51 201Q50 209 50 244Q50 346 98 438T227 601Q351 704 476 704Q514 704 524 703Q621 689 680 617T740 435ZM637 476Q637 565 591 615T476 665Q396 665 322 605Q242 542 200 428T157 216Q157 126 200 73T314 19Q404 19 485 98T608 313Q637 408 637 476Z"></path></g><g data-mml-node="mo" transform="translate(763, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mn" transform="translate(1152, 0)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g><g data-mml-node="mo" transform="translate(1652, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(2318.8, 0)"><path data-c="3C" d="M694 -11T694 -19T688 -33T678 -40Q671 -40 524 29T234 166L90 235Q83 240 83 250Q83 261 91 266Q664 540 678 540Q681 540 687 534T694 519T687 505Q686 504 417 376L151 250L417 124Q686 -4 687 -5Q694 -11 694 -19Z"></path></g><g data-mml-node="mi" transform="translate(3374.6, 0)"><path data-c="1D442" d="M740 435Q740 320 676 213T511 42T304 -22Q207 -22 138 35T51 201Q50 209 50 244Q50 346 98 438T227 601Q351 704 476 704Q514 704 524 703Q621 689 680 617T740 435ZM637 476Q637 565 591 615T476 665Q396 665 322 605Q242 542 200 428T157 216Q157 126 200 73T314 19Q404 19 485 98T608 313Q637 408 637 476Z"></path></g><g data-mml-node="mo" transform="translate(4137.6, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mi" transform="translate(4526.6, 0)"><path data-c="1D459" d="M117 59Q117 26 142 26Q179 26 205 131Q211 151 215 152Q217 153 225 153H229Q238 153 241 153T246 151T248 144Q247 138 245 128T234 90T214 43T183 6T137 -11Q101 -11 70 11T38 85Q38 97 39 102L104 360Q167 615 167 623Q167 626 166 628T162 632T157 634T149 635T141 636T132 637T122 637Q112 637 109 637T101 638T95 641T94 647Q94 649 96 661Q101 680 107 682T179 688Q194 689 213 690T243 693T254 694Q266 694 266 686Q266 675 193 386T118 83Q118 81 118 75T117 65V59Z"></path></g><g data-mml-node="mi" transform="translate(4824.6, 0)"><path data-c="1D45C" d="M201 -11Q126 -11 80 38T34 156Q34 221 64 279T146 380Q222 441 301 441Q333 441 341 440Q354 437 367 433T402 417T438 387T464 338T476 268Q476 161 390 75T201 -11ZM121 120Q121 70 147 48T206 26Q250 26 289 58T351 142Q360 163 374 216T388 308Q388 352 370 375Q346 405 306 405Q243 405 195 347Q158 303 140 230T121 120Z"></path></g><g data-mml-node="mi" transform="translate(5309.6, 0)"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mi" transform="translate(5909.6, 0)"><path data-c="1D454" d="M311 43Q296 30 267 15T206 0Q143 0 105 45T66 160Q66 265 143 353T314 442Q361 442 401 394L404 398Q406 401 409 404T418 412T431 419T447 422Q461 422 470 413T480 394Q480 379 423 152T363 -80Q345 -134 286 -169T151 -205Q10 -205 10 -137Q10 -111 28 -91T74 -71Q89 -71 102 -80T116 -111Q116 -121 114 -130T107 -144T99 -154T92 -162L90 -164H91Q101 -167 151 -167Q189 -167 211 -155Q234 -144 254 -122T282 -75Q288 -56 298 -13Q311 35 311 43ZM384 328L380 339Q377 350 375 354T369 368T359 382T346 393T328 402T306 405Q262 405 221 352Q191 313 171 233T151 117Q151 38 213 38Q269 38 323 108L331 118L384 328Z"></path></g><g data-mml-node="mi" transform="translate(6386.6, 0)"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mo" transform="translate(6986.6, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(7653.3, 0)"><path data-c="3C" d="M694 -11T694 -19T688 -33T678 -40Q671 -40 524 29T234 166L90 235Q83 240 83 250Q83 261 91 266Q664 540 678 540Q681 540 687 534T694 519T687 505Q686 504 417 376L151 250L417 124Q686 -4 687 -5Q694 -11 694 -19Z"></path></g><g data-mml-node="mi" transform="translate(8709.1, 0)"><path data-c="1D442" d="M740 435Q740 320 676 213T511 42T304 -22Q207 -22 138 35T51 201Q50 209 50 244Q50 346 98 438T227 601Q351 704 476 704Q514 704 524 703Q621 689 680 617T740 435ZM637 476Q637 565 591 615T476 665Q396 665 322 605Q242 542 200 428T157 216Q157 126 200 73T314 19Q404 19 485 98T608 313Q637 408 637 476Z"></path></g><g data-mml-node="mo" transform="translate(9472.1, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mi" transform="translate(9861.1, 0)"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mo" transform="translate(10461.1, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(11127.9, 0)"><path data-c="3C" d="M694 -11T694 -19T688 -33T678 -40Q671 -40 524 29T234 166L90 235Q83 240 83 250Q83 261 91 266Q664 540 678 540Q681 540 687 534T694 519T687 505Q686 504 417 376L151 250L417 124Q686 -4 687 -5Q694 -11 694 -19Z"></path></g><g data-mml-node="mi" transform="translate(12183.7, 0)"><path data-c="1D442" d="M740 435Q740 320 676 213T511 42T304 -22Q207 -22 138 35T51 201Q50 209 50 244Q50 346 98 438T227 601Q351 704 476 704Q514 704 524 703Q621 689 680 617T740 435ZM637 476Q637 565 591 615T476 665Q396 665 322 605Q242 542 200 428T157 216Q157 126 200 73T314 19Q404 19 485 98T608 313Q637 408 637 476Z"></path></g><g data-mml-node="mo" transform="translate(12946.7, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mi" transform="translate(13335.7, 0)"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mi" transform="translate(13935.7, 0)"><path data-c="1D459" d="M117 59Q117 26 142 26Q179 26 205 131Q211 151 215 152Q217 153 225 153H229Q238 153 241 153T246 151T248 144Q247 138 245 128T234 90T214 43T183 6T137 -11Q101 -11 70 11T38 85Q38 97 39 102L104 360Q167 615 167 623Q167 626 166 628T162 632T157 634T149 635T141 636T132 637T122 637Q112 637 109 637T101 638T95 641T94 647Q94 649 96 661Q101 680 107 682T179 688Q194 689 213 690T243 693T254 694Q266 694 266 686Q266 675 193 386T118 83Q118 81 118 75T117 65V59Z"></path></g><g data-mml-node="mi" transform="translate(14233.7, 0)"><path data-c="1D45C" d="M201 -11Q126 -11 80 38T34 156Q34 221 64 279T146 380Q222 441 301 441Q333 441 341 440Q354 437 367 433T402 417T438 387T464 338T476 268Q476 161 390 75T201 -11ZM121 120Q121 70 147 48T206 26Q250 26 289 58T351 142Q360 163 374 216T388 308Q388 352 370 375Q346 405 306 405Q243 405 195 347Q158 303 140 230T121 120Z"></path></g><g data-mml-node="mi" transform="translate(14718.7, 0)"><path data-c="1D454" d="M311 43Q296 30 267 15T206 0Q143 0 105 45T66 160Q66 265 143 353T314 442Q361 442 401 394L404 398Q406 401 409 404T418 412T431 419T447 422Q461 422 470 413T480 394Q480 379 423 152T363 -80Q345 -134 286 -169T151 -205Q10 -205 10 -137Q10 -111 28 -91T74 -71Q89 -71 102 -80T116 -111Q116 -121 114 -130T107 -144T99 -154T92 -162L90 -164H91Q101 -167 151 -167Q189 -167 211 -155Q234 -144 254 -122T282 -75Q288 -56 298 -13Q311 35 311 43ZM384 328L380 339Q377 350 375 354T369 368T359 382T346 393T328 402T306 405Q262 405 221 352Q191 313 171 233T151 117Q151 38 213 38Q269 38 323 108L331 118L384 328Z"></path></g><g data-mml-node="mi" transform="translate(15195.7, 0)"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mo" transform="translate(15795.7, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(16462.4, 0)"><path data-c="3C" d="M694 -11T694 -19T688 -33T678 -40Q671 -40 524 29T234 166L90 235Q83 240 83 250Q83 261 91 266Q664 540 678 540Q681 540 687 534T694 519T687 505Q686 504 417 376L151 250L417 124Q686 -4 687 -5Q694 -11 694 -19Z"></path></g><g data-mml-node="mi" transform="translate(17518.2, 0)"><path data-c="1D442" d="M740 435Q740 320 676 213T511 42T304 -22Q207 -22 138 35T51 201Q50 209 50 244Q50 346 98 438T227 601Q351 704 476 704Q514 704 524 703Q621 689 680 617T740 435ZM637 476Q637 565 591 615T476 665Q396 665 322 605Q242 542 200 428T157 216Q157 126 200 73T314 19Q404 19 485 98T608 313Q637 408 637 476Z"></path></g><g data-mml-node="mo" transform="translate(18281.2, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="msup" transform="translate(18670.2, 0)"><g data-mml-node="mi"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mn" transform="translate(600, 363) scale(0.707)"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></g></g><g data-mml-node="mo" transform="translate(19673.8, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(20340.6, 0)"><path data-c="3C" d="M694 -11T694 -19T688 -33T678 -40Q671 -40 524 29T234 166L90 235Q83 240 83 250Q83 261 91 266Q664 540 678 540Q681 540 687 534T694 519T687 505Q686 504 417 376L151 250L417 124Q686 -4 687 -5Q694 -11 694 -19Z"></path></g><g data-mml-node="mi" transform="translate(21396.3, 0)"><path data-c="1D442" d="M740 435Q740 320 676 213T511 42T304 -22Q207 -22 138 35T51 201Q50 209 50 244Q50 346 98 438T227 601Q351 704 476 704Q514 704 524 703Q621 689 680 617T740 435ZM637 476Q637 565 591 615T476 665Q396 665 322 605Q242 542 200 428T157 216Q157 126 200 73T314 19Q404 19 485 98T608 313Q637 408 637 476Z"></path></g><g data-mml-node="mo" transform="translate(22159.3, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="msup" transform="translate(22548.3, 0)"><g data-mml-node="mi"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mn" transform="translate(600, 363) scale(0.707)"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></g></g><g data-mml-node="mi" transform="translate(23551.9, 0)"><path data-c="1D459" d="M117 59Q117 26 142 26Q179 26 205 131Q211 151 215 152Q217 153 225 153H229Q238 153 241 153T246 151T248 144Q247 138 245 128T234 90T214 43T183 6T137 -11Q101 -11 70 11T38 85Q38 97 39 102L104 360Q167 615 167 623Q167 626 166 628T162 632T157 634T149 635T141 636T132 637T122 637Q112 637 109 637T101 638T95 641T94 647Q94 649 96 661Q101 680 107 682T179 688Q194 689 213 690T243 693T254 694Q266 694 266 686Q266 675 193 386T118 83Q118 81 118 75T117 65V59Z"></path></g><g data-mml-node="mi" transform="translate(23849.9, 0)"><path data-c="1D45C" d="M201 -11Q126 -11 80 38T34 156Q34 221 64 279T146 380Q222 441 301 441Q333 441 341 440Q354 437 367 433T402 417T438 387T464 338T476 268Q476 161 390 75T201 -11ZM121 120Q121 70 147 48T206 26Q250 26 289 58T351 142Q360 163 374 216T388 308Q388 352 370 375Q346 405 306 405Q243 405 195 347Q158 303 140 230T121 120Z"></path></g><g data-mml-node="mi" transform="translate(24334.9, 0)"><path data-c="1D454" d="M311 43Q296 30 267 15T206 0Q143 0 105 45T66 160Q66 265 143 353T314 442Q361 442 401 394L404 398Q406 401 409 404T418 412T431 419T447 422Q461 422 470 413T480 394Q480 379 423 152T363 -80Q345 -134 286 -169T151 -205Q10 -205 10 -137Q10 -111 28 -91T74 -71Q89 -71 102 -80T116 -111Q116 -121 114 -130T107 -144T99 -154T92 -162L90 -164H91Q101 -167 151 -167Q189 -167 211 -155Q234 -144 254 -122T282 -75Q288 -56 298 -13Q311 35 311 43ZM384 328L380 339Q377 350 375 354T369 368T359 382T346 393T328 402T306 405Q262 405 221 352Q191 313 171 233T151 117Q151 38 213 38Q269 38 323 108L331 118L384 328Z"></path></g><g data-mml-node="mi" transform="translate(24811.9, 0)"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mo" transform="translate(25411.9, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(26078.7, 0)"><path data-c="3C" d="M694 -11T694 -19T688 -33T678 -40Q671 -40 524 29T234 166L90 235Q83 240 83 250Q83 261 91 266Q664 540 678 540Q681 540 687 534T694 519T687 505Q686 504 417 376L151 250L417 124Q686 -4 687 -5Q694 -11 694 -19Z"></path></g><g data-mml-node="mi" transform="translate(27134.4, 0)"><path data-c="1D442" d="M740 435Q740 320 676 213T511 42T304 -22Q207 -22 138 35T51 201Q50 209 50 244Q50 346 98 438T227 601Q351 704 476 704Q514 704 524 703Q621 689 680 617T740 435ZM637 476Q637 565 591 615T476 665Q396 665 322 605Q242 542 200 428T157 216Q157 126 200 73T314 19Q404 19 485 98T608 313Q637 408 637 476Z"></path></g><g data-mml-node="mo" transform="translate(27897.4, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="msup" transform="translate(28286.4, 0)"><g data-mml-node="mi"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mn" transform="translate(600, 363) scale(0.707)"><path data-c="33" d="M127 463Q100 463 85 480T69 524Q69 579 117 622T233 665Q268 665 277 664Q351 652 390 611T430 522Q430 470 396 421T302 350L299 348Q299 347 308 345T337 336T375 315Q457 262 457 175Q457 96 395 37T238 -22Q158 -22 100 21T42 130Q42 158 60 175T105 193Q133 193 151 175T169 130Q169 119 166 110T159 94T148 82T136 74T126 70T118 67L114 66Q165 21 238 21Q293 21 321 74Q338 107 338 175V195Q338 290 274 322Q259 328 213 329L171 330L168 332Q166 335 166 348Q166 366 174 366Q202 366 232 371Q266 376 294 413T322 525V533Q322 590 287 612Q265 626 240 626Q208 626 181 615T143 592T132 580H135Q138 579 143 578T153 573T165 566T175 555T183 540T186 520Q186 498 172 481T127 463Z"></path></g></g><g data-mml-node="mo" transform="translate(29290, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g></g></g></svg></mjx-container></p><h1 id="2-Space-Complexity"><a href="#2-Space-Complexity" class="headerlink" title="2.Space Complexity"></a>2.Space Complexity</h1><p>A indicator to evaluate the space cost of an algorithm.</p><h2 id="2-1-O-1"><a href="#2-1-O-1" class="headerlink" title="2.1. O(1)"></a>2.1. O(1)</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">i = <span class="number">1</span></span><br><span class="line">j = <span class="number">1</span></span><br><span class="line">...</span><br></pre></td></tr></table></figure><h2 id="2-2-O-n"><a href="#2-2-O-n" class="headerlink" title="2.2. O(n)"></a>2.2. O(n)</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">list</span>(<span class="built_in">range</span>(n))</span><br></pre></td></tr></table></figure><h2 id="2-3-O-nm"><a href="#2-3-O-nm" class="headerlink" title="2.3. O(nm)"></a>2.3. O(nm)</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line">np.random.rand(n, m)</span><br></pre></td></tr></table></figure><h1 id="3-Use-more-Space-to-save-Time"><a href="#3-Use-more-Space-to-save-Time" class="headerlink" title="3. Use more Space to save Time"></a>3. Use more Space to save Time</h1><p>As time is more expensive than memory, we should use memory to save time(if applicable).</p><ul><li>list: search, insert slower, less memory consuming</li><li>dict: search, insert faster, more memory consuming</li></ul><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><h1 id="1-Time-Complexity"><a href="#1-Time-Complexity" class="headerlink" title="1.Time Complexity"></a>1.Time Complexity</h1><p>A indicator to evaluate the time cost of an algorithm.</p>
<h2 id="1-1-O-1"><a href="#1-1-O-1" class="headerlink" title="1.1. O(1)"></a>1.1. O(1)</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="string">"Hello World!"</span>)</span><br></pre></td></tr></table></figure>
<h2 id="1-2-O-n"><a href="#1-2-O-n" class="headerlink" title="1.2. O(n)"></a>1.2. O(n)</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(n):</span><br><span class="line"> <span class="built_in">print</span>(i)</span><br></pre></td></tr></table></figure></summary>
<category term="Computer Science" scheme="https://aaronos0.github.io/categories/Computer-Science/"/>
<category term=" Data Structure and Algorithms" scheme="https://aaronos0.github.io/tags/Data-Structure-and-Algorithms/"/>
</entry>
<entry>
<title>COMP3009 - 3.Mathematics for ML</title>
<link href="https://aaronos0.github.io/2020/10/19/COMP3009-3-Mathematics-for-ML/"/>
<id>https://aaronos0.github.io/2020/10/19/COMP3009-3-Mathematics-for-ML/</id>
<published>2020-10-19T22:43:42.000Z</published>
<updated>2020-10-19T22:43:42.000Z</updated>
<content type="html"><![CDATA[<p>老师简单的过了一遍线性代数、概率论、微分,若想深入请看参考资料。<br>后续有空整理下AI核心数学知识。</p><span id="more"></span><p>Source:</p><ol><li>条件概率: <a href="https://zhuanlan.zhihu.com/p/78097135">https://zhuanlan.zhihu.com/p/78097135</a></li><li>贝叶斯: <a href="https://zhuanlan.zhihu.com/p/78297343">https://zhuanlan.zhihu.com/p/78297343</a></li><li>统计学: <a href="https://seeing-theory.brown.edu/basic-probability/index.html">https://seeing-theory.brown.edu/basic-probability/index.html</a></li><li>矩阵: <a href="https://www.hahack.com/math/math-matrix/">https://www.hahack.com/math/math-matrix/</a></li><li>导数\偏导数: <a href="https://www.shuxuele.com/calculus/index.html">https://www.shuxuele.com/calculus/index.html</a></li><li>梯度下降: <a href="http://fangda.me/">http://fangda.me/</a></li><li>函数绘图: <a href="https://www.geogebra.org/download">https://www.geogebra.org/download</a></li><li>数学知识简单汇总: <a href="https://zhuanlan.zhihu.com/p/25197792">https://zhuanlan.zhihu.com/p/25197792</a></li><li>机器学习: <a href="https://ailearning.apachecn.org/">https://ailearning.apachecn.org</a></li><li>机器学习常用资源: <a href="https://zhuanlan.zhihu.com/p/40419847">https://zhuanlan.zhihu.com/p/40419847</a></li></ol><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><p>老师简单的过了一遍线性代数、概率论、微分,若想深入请看参考资料。<br>后续有空整理下AI核心数学知识。</p></summary>
<category term="UoN" scheme="https://aaronos0.github.io/categories/UoN/"/>
<category term="COMP3009-Machine Learning" scheme="https://aaronos0.github.io/tags/COMP3009-Machine-Learning/"/>
</entry>
<entry>
<title>COMP3009 - 2.Linear Regression</title>
<link href="https://aaronos0.github.io/2020/10/12/COMP3009-2-Linear-Regression/"/>
<id>https://aaronos0.github.io/2020/10/12/COMP3009-2-Linear-Regression/</id>
<published>2020-10-12T21:34:56.000Z</published>
<updated>2020-10-12T21:34:56.000Z</updated>
<content type="html"><![CDATA[<h1 id="1-Univariate-Linear-Regression"><a href="#1-Univariate-Linear-Regression" class="headerlink" title="1. Univariate Linear Regression"></a>1. Univariate Linear Regression</h1><blockquote><p>Univariate linear regression operates on a single dimension/feature of your data.</p></blockquote><span id="more"></span><p><img src="FindingParameter.png" alt="FindingParameter.png"></p><ul><li>单变量线性回归模型</li><li>通过观察预测值h(x)与真实值的差值,来找到最优权重w.</li><li>因此我们引入误差函数的计算</li></ul><h2 id="1-1-Choose-a-Cost-Function"><a href="#1-1-Choose-a-Cost-Function" class="headerlink" title="1.1. Choose a Cost Function"></a>1.1. Choose a Cost Function</h2><p><img src="ChooseCostFunction.png" alt="ChooseCostFunction.png"></p><ul><li>绝对误差(Absolute error)函数: 由于绝对误差函数是一阶,因此坡度(斜率)不变,其结果导致无论初始值选择多少,误差下降(减少)的速度不变</li></ul><p><mjx-container class="MathJax" jax="SVG" display="true"><svg style="vertical-align: -0.566ex" xmlns="http://www.w3.org/2000/svg" width="16.5ex" height="2.262ex" role="img" focusable="false" viewBox="0 -750 7293.1 1000"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mi"><path data-c="210E" d="M137 683Q138 683 209 688T282 694Q294 694 294 685Q294 674 258 534Q220 386 220 383Q220 381 227 388Q288 442 357 442Q411 442 444 415T478 336Q478 285 440 178T402 50Q403 36 407 31T422 26Q450 26 474 56T513 138Q516 149 519 151T535 153Q555 153 555 145Q555 144 551 130Q535 71 500 33Q466 -10 419 -10H414Q367 -10 346 17T325 74Q325 90 361 192T398 345Q398 404 354 404H349Q266 404 205 306L198 293L164 158Q132 28 127 16Q114 -11 83 -11Q69 -11 59 -2T48 16Q48 30 121 320L195 616Q195 629 188 632T149 637H128Q122 643 122 645T124 664Q129 683 137 683Z"></path></g><g data-mml-node="mo" transform="translate(576, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mi" transform="translate(965, 0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="mo" transform="translate(1537, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(2203.8, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="msub" transform="translate(3259.6, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></g></g></g><g data-mml-node="mo" transform="translate(4601.3, 0)"><path data-c="2B" d="M56 237T56 250T70 270H369V420L370 570Q380 583 389 583Q402 583 409 568V270H707Q722 262 722 250T707 230H409V-68Q401 -82 391 -82H389H387Q375 -82 369 -68V230H70Q56 237 56 250Z"></path></g><g data-mml-node="msub" transform="translate(5601.6, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g><g data-mml-node="mi" transform="translate(6721.1, 0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g></g></g></svg></mjx-container><br><mjx-container class="MathJax" jax="SVG" display="true"><svg style="vertical-align: -0.566ex" xmlns="http://www.w3.org/2000/svg" width="13.269ex" height="2.262ex" role="img" focusable="false" viewBox="0 -750 5865 1000"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mi"><path data-c="1D700" d="M190 -22Q124 -22 76 11T27 107Q27 174 97 232L107 239L99 248Q76 273 76 304Q76 364 144 408T290 452H302Q360 452 405 421Q428 405 428 392Q428 381 417 369T391 356Q382 356 371 365T338 383T283 392Q217 392 167 368T116 308Q116 289 133 272Q142 263 145 262T157 264Q188 278 238 278H243Q308 278 308 247Q308 206 223 206Q177 206 142 219L132 212Q68 169 68 112Q68 39 201 39Q253 39 286 49T328 72T345 94T362 105Q376 103 376 88Q376 79 365 62T334 26T275 -8T190 -22Z"></path></g><g data-mml-node="mo" transform="translate(743.8, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mo" transform="translate(1799.6, 0)"><path data-c="7C" d="M139 -249H137Q125 -249 119 -235V251L120 737Q130 750 139 750Q152 750 159 735V-235Q151 -249 141 -249H139Z"></path></g><g data-mml-node="mi" transform="translate(2077.6, 0)"><path data-c="210E" d="M137 683Q138 683 209 688T282 694Q294 694 294 685Q294 674 258 534Q220 386 220 383Q220 381 227 388Q288 442 357 442Q411 442 444 415T478 336Q478 285 440 178T402 50Q403 36 407 31T422 26Q450 26 474 56T513 138Q516 149 519 151T535 153Q555 153 555 145Q555 144 551 130Q535 71 500 33Q466 -10 419 -10H414Q367 -10 346 17T325 74Q325 90 361 192T398 345Q398 404 354 404H349Q266 404 205 306L198 293L164 158Q132 28 127 16Q114 -11 83 -11Q69 -11 59 -2T48 16Q48 30 121 320L195 616Q195 629 188 632T149 637H128Q122 643 122 645T124 664Q129 683 137 683Z"></path></g><g data-mml-node="mo" transform="translate(2653.6, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mi" transform="translate(3042.6, 0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="mo" transform="translate(3614.6, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(4225.8, 0)"><path data-c="2212" d="M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z"></path></g><g data-mml-node="mi" transform="translate(5226, 0)"><path data-c="1D461" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path></g><g data-mml-node="mo" transform="translate(5587, 0)"><path data-c="7C" d="M139 -249H137Q125 -249 119 -235V251L120 737Q130 750 139 750Q152 750 159 735V-235Q151 -249 141 -249H139Z"></path></g></g></g></svg></mjx-container><br><mjx-container class="MathJax" jax="SVG" display="true"><svg style="vertical-align: -2.148ex" xmlns="http://www.w3.org/2000/svg" width="61.362ex" height="5.451ex" role="img" focusable="false" viewBox="0 -1460 27122.2 2409.5"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mfrac"><g data-mml-node="mrow" transform="translate(220, 710)"><g data-mml-node="mi"><path data-c="1D451" d="M366 683Q367 683 438 688T511 694Q523 694 523 686Q523 679 450 384T375 83T374 68Q374 26 402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487H491Q506 153 506 145Q506 140 503 129Q490 79 473 48T445 8T417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157Q33 205 53 255T101 341Q148 398 195 420T280 442Q336 442 364 400Q369 394 369 396Q370 400 396 505T424 616Q424 629 417 632T378 637H357Q351 643 351 645T353 664Q358 683 366 683ZM352 326Q329 405 277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q233 26 290 98L298 109L352 326Z"></path></g><g data-mml-node="mo" transform="translate(520, 0)"><path data-c="7C" d="M139 -249H137Q125 -249 119 -235V251L120 737Q130 750 139 750Q152 750 159 735V-235Q151 -249 141 -249H139Z"></path></g><g data-mml-node="mi" transform="translate(798, 0)"><path data-c="210E" d="M137 683Q138 683 209 688T282 694Q294 694 294 685Q294 674 258 534Q220 386 220 383Q220 381 227 388Q288 442 357 442Q411 442 444 415T478 336Q478 285 440 178T402 50Q403 36 407 31T422 26Q450 26 474 56T513 138Q516 149 519 151T535 153Q555 153 555 145Q555 144 551 130Q535 71 500 33Q466 -10 419 -10H414Q367 -10 346 17T325 74Q325 90 361 192T398 345Q398 404 354 404H349Q266 404 205 306L198 293L164 158Q132 28 127 16Q114 -11 83 -11Q69 -11 59 -2T48 16Q48 30 121 320L195 616Q195 629 188 632T149 637H128Q122 643 122 645T124 664Q129 683 137 683Z"></path></g><g data-mml-node="mo" transform="translate(1374, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mi" transform="translate(1763, 0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="mo" transform="translate(2335, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(2946.2, 0)"><path data-c="2212" d="M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z"></path></g><g data-mml-node="mi" transform="translate(3946.4, 0)"><path data-c="1D461" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path></g><g data-mml-node="mo" transform="translate(4307.4, 0)"><path data-c="7C" d="M139 -249H137Q125 -249 119 -235V251L120 737Q130 750 139 750Q152 750 159 735V-235Q151 -249 141 -249H139Z"></path></g></g><g data-mml-node="mrow" transform="translate(1966.7, -686)"><g data-mml-node="mi"><path data-c="1D451" d="M366 683Q367 683 438 688T511 694Q523 694 523 686Q523 679 450 384T375 83T374 68Q374 26 402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487H491Q506 153 506 145Q506 140 503 129Q490 79 473 48T445 8T417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157Q33 205 53 255T101 341Q148 398 195 420T280 442Q336 442 364 400Q369 394 369 396Q370 400 396 505T424 616Q424 629 417 632T378 637H357Q351 643 351 645T353 664Q358 683 366 683ZM352 326Q329 405 277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q233 26 290 98L298 109L352 326Z"></path></g><g data-mml-node="mi" transform="translate(520, 0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g></g><rect width="4785.4" height="60" x="120" y="220"></rect></g><g data-mml-node="mo" transform="translate(5303.2, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mfrac" transform="translate(6359, 0)"><g data-mml-node="mrow" transform="translate(220, 709.5)"><g data-mml-node="mi"><path data-c="1D451" d="M366 683Q367 683 438 688T511 694Q523 694 523 686Q523 679 450 384T375 83T374 68Q374 26 402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487H491Q506 153 506 145Q506 140 503 129Q490 79 473 48T445 8T417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157Q33 205 53 255T101 341Q148 398 195 420T280 442Q336 442 364 400Q369 394 369 396Q370 400 396 505T424 616Q424 629 417 632T378 637H357Q351 643 351 645T353 664Q358 683 366 683ZM352 326Q329 405 277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q233 26 290 98L298 109L352 326Z"></path></g><g data-mml-node="mo" transform="translate(520, 0)"><path data-c="7C" d="M139 -249H137Q125 -249 119 -235V251L120 737Q130 750 139 750Q152 750 159 735V-235Q151 -249 141 -249H139Z"></path></g><g data-mml-node="msub" transform="translate(798, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></g></g></g><g data-mml-node="mo" transform="translate(2139.8, 0)"><path data-c="2B" d="M56 237T56 250T70 270H369V420L370 570Q380 583 389 583Q402 583 409 568V270H707Q722 262 722 250T707 230H409V-68Q401 -82 391 -82H389H387Q375 -82 369 -68V230H70Q56 237 56 250Z"></path></g><g data-mml-node="msub" transform="translate(3140, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g><g data-mml-node="mi" transform="translate(4259.6, 0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="mo" transform="translate(5053.8, 0)"><path data-c="2212" d="M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z"></path></g><g data-mml-node="mi" transform="translate(6054, 0)"><path data-c="1D461" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path></g><g data-mml-node="mo" transform="translate(6415, 0)"><path data-c="7C" d="M139 -249H137Q125 -249 119 -235V251L120 737Q130 750 139 750Q152 750 159 735V-235Q151 -249 141 -249H139Z"></path></g></g><g data-mml-node="mrow" transform="translate(3020.5, -686)"><g data-mml-node="mi"><path data-c="1D451" d="M366 683Q367 683 438 688T511 694Q523 694 523 686Q523 679 450 384T375 83T374 68Q374 26 402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487H491Q506 153 506 145Q506 140 503 129Q490 79 473 48T445 8T417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157Q33 205 53 255T101 341Q148 398 195 420T280 442Q336 442 364 400Q369 394 369 396Q370 400 396 505T424 616Q424 629 417 632T378 637H357Q351 643 351 645T353 664Q358 683 366 683ZM352 326Q329 405 277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q233 26 290 98L298 109L352 326Z"></path></g><g data-mml-node="mi" transform="translate(520, 0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g></g><rect width="6893" height="60" x="120" y="220"></rect></g><g data-mml-node="mo" transform="translate(13769.8, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mo" transform="translate(14825.6, 0)"><path data-c="7C" d="M139 -249H137Q125 -249 119 -235V251L120 737Q130 750 139 750Q152 750 159 735V-235Q151 -249 141 -249H139Z"></path></g><g data-mml-node="msub" transform="translate(15103.6, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g><g data-mml-node="mo" transform="translate(16223.1, 0)"><path data-c="7C" d="M139 -249H137Q125 -249 119 -235V251L120 737Q130 750 139 750Q152 750 159 735V-235Q151 -249 141 -249H139Z"></path></g><g data-mml-node="mo" transform="translate(16778.9, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mrow" transform="translate(17834.7, 0)"><g data-mml-node="mo"><path data-c="7B" d="M618 -943L612 -949H582L568 -943Q472 -903 411 -841T332 -703Q327 -682 327 -653T325 -350Q324 -28 323 -18Q317 24 301 61T264 124T221 171T179 205T147 225T132 234Q130 238 130 250Q130 255 130 258T131 264T132 267T134 269T139 272T144 275Q207 308 256 367Q310 436 323 519Q324 529 325 851Q326 1124 326 1154T332 1205Q369 1358 566 1443L582 1450H612L618 1444V1429Q618 1413 616 1411L608 1406Q599 1402 585 1393T552 1372T515 1343T479 1305T449 1257T429 1200Q425 1180 425 1152T423 851Q422 579 422 549T416 498Q407 459 388 424T346 364T297 318T250 284T214 264T197 254L188 251L205 242Q290 200 345 138T416 3Q421 -18 421 -48T423 -349Q423 -397 423 -472Q424 -677 428 -694Q429 -697 429 -699Q434 -722 443 -743T465 -782T491 -816T519 -845T548 -868T574 -886T595 -899T610 -908L616 -910Q618 -912 618 -928V-943Z"></path></g><g data-mml-node="mtable" transform="translate(750, 0)"><g data-mml-node="mtr" transform="translate(0, 600)"><g data-mml-node="mtd"><g data-mml-node="msub"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g></g><g data-mml-node="mtd" transform="translate(2897.6, 0)"><g data-mml-node="mtext"><path data-c="68" d="M41 46H55Q94 46 102 60V68Q102 77 102 91T102 124T102 167T103 217T103 272T103 329Q103 366 103 407T103 482T102 542T102 586T102 603Q99 622 88 628T43 637H25V660Q25 683 27 683L37 684Q47 685 66 686T103 688Q120 689 140 690T170 693T181 694H184V367Q244 442 328 442Q451 442 463 329Q464 322 464 190V104Q464 66 466 59T477 49Q498 46 526 46H542V0H534L510 1Q487 2 460 2T422 3Q319 3 310 0H302V46H318Q379 46 379 62Q380 64 380 200Q379 335 378 343Q372 371 358 385T334 402T308 404Q263 404 229 370Q202 343 195 315T187 232V168V108Q187 78 188 68T191 55T200 49Q221 46 249 46H265V0H257L234 1Q210 2 183 2T145 3Q42 3 33 0H25V46H41Z"></path><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z" transform="translate(556, 0)"></path><path data-c="78" d="M201 0Q189 3 102 3Q26 3 17 0H11V46H25Q48 47 67 52T96 61T121 78T139 96T160 122T180 150L226 210L168 288Q159 301 149 315T133 336T122 351T113 363T107 370T100 376T94 379T88 381T80 383Q74 383 44 385H16V431H23Q59 429 126 429Q219 429 229 431H237V385Q201 381 201 369Q201 367 211 353T239 315T268 274L272 270L297 304Q329 345 329 358Q329 364 327 369T322 376T317 380T310 384L307 385H302V431H309Q324 428 408 428Q487 428 493 431H499V385H492Q443 385 411 368Q394 360 377 341T312 257L296 236L358 151Q424 61 429 57T446 50Q464 46 499 46H516V0H510H502Q494 1 482 1T457 2T432 2T414 3Q403 3 377 3T327 1L304 0H295V46H298Q309 46 320 51T331 63Q331 65 291 120L250 175Q249 174 219 133T185 88Q181 83 181 74Q181 63 188 55T206 46Q208 46 208 23V0H201Z" transform="translate(945, 0)"></path><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z" transform="translate(1473, 0)"></path><path data-c="20" d="" transform="translate(1862, 0)"></path><path data-c="2D" d="M11 179V252H277V179H11Z" transform="translate(2112, 0)"></path><path data-c="20" d="" transform="translate(2445, 0)"></path><path data-c="74" d="M27 422Q80 426 109 478T141 600V615H181V431H316V385H181V241Q182 116 182 100T189 68Q203 29 238 29Q282 29 292 100Q293 108 293 146V181H333V146V134Q333 57 291 17Q264 -10 221 -10Q187 -10 162 2T124 33T105 68T98 100Q97 107 97 248V385H18V422H27Z" transform="translate(2695, 0)"></path><path data-c="20" d="" transform="translate(3084, 0)"></path><path data-c="3E" d="M84 520Q84 528 88 533T96 539L99 540Q106 540 253 471T544 334L687 265Q694 260 694 250T687 235Q685 233 395 96L107 -40H101Q83 -38 83 -20Q83 -19 83 -17Q82 -10 98 -1Q117 9 248 71Q326 108 378 132L626 250L378 368Q90 504 86 509Q84 513 84 520Z" transform="translate(3334, 0)"></path><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z" transform="translate(4112, 0)"></path><path data-c="20" d="" transform="translate(4890, 0)"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(5140, 0)"></path></g></g></g><g data-mml-node="mtr" transform="translate(0, -600)"><g data-mml-node="mtd"><g data-mml-node="mo"><path data-c="2212" d="M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z"></path></g><g data-mml-node="msub" transform="translate(778, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g></g><g data-mml-node="mtd" transform="translate(2897.6, 0)"><g data-mml-node="mtext"><path data-c="68" d="M41 46H55Q94 46 102 60V68Q102 77 102 91T102 124T102 167T103 217T103 272T103 329Q103 366 103 407T103 482T102 542T102 586T102 603Q99 622 88 628T43 637H25V660Q25 683 27 683L37 684Q47 685 66 686T103 688Q120 689 140 690T170 693T181 694H184V367Q244 442 328 442Q451 442 463 329Q464 322 464 190V104Q464 66 466 59T477 49Q498 46 526 46H542V0H534L510 1Q487 2 460 2T422 3Q319 3 310 0H302V46H318Q379 46 379 62Q380 64 380 200Q379 335 378 343Q372 371 358 385T334 402T308 404Q263 404 229 370Q202 343 195 315T187 232V168V108Q187 78 188 68T191 55T200 49Q221 46 249 46H265V0H257L234 1Q210 2 183 2T145 3Q42 3 33 0H25V46H41Z"></path><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z" transform="translate(556, 0)"></path><path data-c="78" d="M201 0Q189 3 102 3Q26 3 17 0H11V46H25Q48 47 67 52T96 61T121 78T139 96T160 122T180 150L226 210L168 288Q159 301 149 315T133 336T122 351T113 363T107 370T100 376T94 379T88 381T80 383Q74 383 44 385H16V431H23Q59 429 126 429Q219 429 229 431H237V385Q201 381 201 369Q201 367 211 353T239 315T268 274L272 270L297 304Q329 345 329 358Q329 364 327 369T322 376T317 380T310 384L307 385H302V431H309Q324 428 408 428Q487 428 493 431H499V385H492Q443 385 411 368Q394 360 377 341T312 257L296 236L358 151Q424 61 429 57T446 50Q464 46 499 46H516V0H510H502Q494 1 482 1T457 2T432 2T414 3Q403 3 377 3T327 1L304 0H295V46H298Q309 46 320 51T331 63Q331 65 291 120L250 175Q249 174 219 133T185 88Q181 83 181 74Q181 63 188 55T206 46Q208 46 208 23V0H201Z" transform="translate(945, 0)"></path><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z" transform="translate(1473, 0)"></path><path data-c="20" d="" transform="translate(1862, 0)"></path><path data-c="2D" d="M11 179V252H277V179H11Z" transform="translate(2112, 0)"></path><path data-c="20" d="" transform="translate(2445, 0)"></path><path data-c="74" d="M27 422Q80 426 109 478T141 600V615H181V431H316V385H181V241Q182 116 182 100T189 68Q203 29 238 29Q282 29 292 100Q293 108 293 146V181H333V146V134Q333 57 291 17Q264 -10 221 -10Q187 -10 162 2T124 33T105 68T98 100Q97 107 97 248V385H18V422H27Z" transform="translate(2695, 0)"></path><path data-c="20" d="" transform="translate(3084, 0)"></path><path data-c="3C" d="M694 -11T694 -19T688 -33T678 -40Q671 -40 524 29T234 166L90 235Q83 240 83 250Q83 261 91 266Q664 540 678 540Q681 540 687 534T694 519T687 505Q686 504 417 376L151 250L417 124Q686 -4 687 -5Q694 -11 694 -19Z" transform="translate(3334, 0)"></path><path data-c="20" d="" transform="translate(4112, 0)"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(4362, 0)"></path></g></g></g></g><g data-mml-node="mo" transform="translate(9287.6, 0)"></g></g></g></g></svg></mjx-container></p><p><img src="CostFunction.png" alt="CostFunction.png"></p><ul><li>可微误差(Differentiable error)函数: 二阶函数,便于通过导数(derivative)的大小来判断下降方向</li><li>虽然这两种函数都代表了真实值与预测值的误差,但是选择Differentiable error可以将未知问题转换为已知问题求解</li></ul><h1 id="2-Cost-Function"><a href="#2-Cost-Function" class="headerlink" title="2. Cost Function"></a>2. Cost Function</h1><h2 id="2-1-Squared-error-cost-function"><a href="#2-1-Squared-error-cost-function" class="headerlink" title="2.1. Squared error cost function"></a>2.1. Squared error cost function</h2><p><img src="PartialDerivative.png" alt="PartialDerivative.png"></p><ul><li>平方误差函数</li><li>不同的误差函数适应不同的应用场景,还有许多误差函数</li><li>1/2的引入是便于和微分后产生的2抵消</li></ul><h2 id="2-2-Partial-Derivative"><a href="#2-2-Partial-Derivative" class="headerlink" title="2.2. Partial Derivative"></a>2.2. Partial Derivative</h2><p><img src="AbsoluteError.png" alt="AbsoluteError.png"></p><ul><li><mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex" xmlns="http://www.w3.org/2000/svg" width="7.832ex" height="2.262ex" role="img" focusable="false" viewBox="0 -750 3461.8 1000"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mo"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="msub" transform="translate(389, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></g></g></g><g data-mml-node="mo" transform="translate(1508.6, 0)"><path data-c="2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path></g><g data-mml-node="msub" transform="translate(1953.2, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g><g data-mml-node="mo" transform="translate(3072.8, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g></g></g></svg></mjx-container>均为parameter,因此会产生two Partial Derivatives</li><li>L2: second dimensional; 二维</li></ul><h2 id="2-3-Other-Cost-Functions"><a href="#2-3-Other-Cost-Functions" class="headerlink" title="2.3. Other Cost Functions"></a>2.3. Other Cost Functions</h2><p><img src="OtherCostFunction.png" alt="OtherCostFunction.png"></p><h1 id="3-Multivariate-Linear-Regression"><a href="#3-Multivariate-Linear-Regression" class="headerlink" title="3. Multivariate Linear Regression"></a>3. Multivariate Linear Regression</h1><h2 id="3-1-First-order-Polynomial"><a href="#3-1-First-order-Polynomial" class="headerlink" title="3.1. First-order Polynomial"></a>3.1. First-order Polynomial</h2><p><img src="MultivariateLinearRegressionFirst-order.png" alt="MultivariateLinearRegressionFirst-order.png"></p><ul><li>一阶多项式的多元线性回归</li></ul><h2 id="3-2-Higher-order-Polynomial"><a href="#3-2-Higher-order-Polynomial" class="headerlink" title="3.2. Higher-order Polynomial"></a>3.2. Higher-order Polynomial</h2><p><img src="MultivariateLinearRegressionHigh-order.png" alt="MultivariateLinearRegressionHigh-order.png"></p><ul><li>高阶多项式的多元线性回归</li></ul><h2 id="3-3-Linear-Basis-Functions"><a href="#3-3-Linear-Basis-Functions" class="headerlink" title="3.3. Linear Basis Functions"></a>3.3. Linear Basis Functions</h2><p><img src="LinearBasisFunctions.png" alt="LinearBasisFunctions.png"></p><ul><li>多项式回归模型可以推广为非线性基函数的线性组合</li><li>这里为了简化函数,令<mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex" xmlns="http://www.w3.org/2000/svg" width="8.551ex" height="2.262ex" role="img" focusable="false" viewBox="0 -750 3779.6 1000"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mi"><path data-c="1D719" d="M409 688Q413 694 421 694H429H442Q448 688 448 686Q448 679 418 563Q411 535 404 504T392 458L388 442Q388 441 397 441T429 435T477 418Q521 397 550 357T579 260T548 151T471 65T374 11T279 -10H275L251 -105Q245 -128 238 -160Q230 -192 227 -198T215 -205H209Q189 -205 189 -198Q189 -193 211 -103L234 -11Q234 -10 226 -10Q221 -10 206 -8T161 6T107 36T62 89T43 171Q43 231 76 284T157 370T254 422T342 441Q347 441 348 445L378 567Q409 686 409 688ZM122 150Q122 116 134 91T167 53T203 35T237 27H244L337 404Q333 404 326 403T297 395T255 379T211 350T170 304Q152 276 137 237Q122 191 122 150ZM500 282Q500 320 484 347T444 385T405 400T381 404H378L332 217L284 29Q284 27 285 27Q293 27 317 33T357 47Q400 66 431 100T475 170T494 234T500 282Z"></path></g><g data-mml-node="mo" transform="translate(596, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mi" transform="translate(985, 0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="mo" transform="translate(1557, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(2223.8, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mn" transform="translate(3279.6, 0)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g></svg></mjx-container></li><li>矩阵的叉乘(叉积)</li></ul><h1 id="4-Gradient-Descent"><a href="#4-Gradient-Descent" class="headerlink" title="4. Gradient Descent"></a>4. Gradient Descent</h1><h2 id="4-1-Aim"><a href="#4-1-Aim" class="headerlink" title="4.1. Aim"></a>4.1. Aim</h2><p>Given some cost function <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.05ex" xmlns="http://www.w3.org/2000/svg" width="1.432ex" height="1.595ex" role="img" focusable="false" viewBox="0 -683 633 705"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mi"><path data-c="1D43D" d="M447 625Q447 637 354 637H329Q323 642 323 645T325 664Q329 677 335 683H352Q393 681 498 681Q541 681 568 681T605 682T619 682Q633 682 633 672Q633 670 630 658Q626 642 623 640T604 637Q552 637 545 623Q541 610 483 376Q420 128 419 127Q397 64 333 21T195 -22Q137 -22 97 8T57 88Q57 130 80 152T132 174Q177 174 182 130Q182 98 164 80T123 56Q115 54 115 53T122 44Q148 15 197 15Q235 15 271 47T324 130Q328 142 387 380T447 625Z"></path></g></g></g></svg></mjx-container> , we want: <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -1.849ex" xmlns="http://www.w3.org/2000/svg" width="21.094ex" height="3.545ex" role="img" focusable="false" viewBox="0 -750 9323.7 1567.1"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="munder"><g data-mml-node="mo" transform="translate(377.8, 0)"><path data-c="6D" d="M41 46H55Q94 46 102 60V68Q102 77 102 91T102 122T103 161T103 203Q103 234 103 269T102 328V351Q99 370 88 376T43 385H25V408Q25 431 27 431L37 432Q47 433 65 434T102 436Q119 437 138 438T167 441T178 442H181V402Q181 364 182 364T187 369T199 384T218 402T247 421T285 437Q305 442 336 442Q351 442 364 440T387 434T406 426T421 417T432 406T441 395T448 384T452 374T455 366L457 361L460 365Q463 369 466 373T475 384T488 397T503 410T523 422T546 432T572 439T603 442Q729 442 740 329Q741 322 741 190V104Q741 66 743 59T754 49Q775 46 803 46H819V0H811L788 1Q764 2 737 2T699 3Q596 3 587 0H579V46H595Q656 46 656 62Q657 64 657 200Q656 335 655 343Q649 371 635 385T611 402T585 404Q540 404 506 370Q479 343 472 315T464 232V168V108Q464 78 465 68T468 55T477 49Q498 46 526 46H542V0H534L510 1Q487 2 460 2T422 3Q319 3 310 0H302V46H318Q379 46 379 62Q380 64 380 200Q379 335 378 343Q372 371 358 385T334 402T308 404Q263 404 229 370Q202 343 195 315T187 232V168V108Q187 78 188 68T191 55T200 49Q221 46 249 46H265V0H257L234 1Q210 2 183 2T145 3Q42 3 33 0H25V46H41Z"></path><path data-c="69" d="M69 609Q69 637 87 653T131 669Q154 667 171 652T188 609Q188 579 171 564T129 549Q104 549 87 564T69 609ZM247 0Q232 3 143 3Q132 3 106 3T56 1L34 0H26V46H42Q70 46 91 49Q100 53 102 60T104 102V205V293Q104 345 102 359T88 378Q74 385 41 385H30V408Q30 431 32 431L42 432Q52 433 70 434T106 436Q123 437 142 438T171 441T182 442H185V62Q190 52 197 50T232 46H255V0H247Z" transform="translate(833, 0)"></path><path data-c="6E" d="M41 46H55Q94 46 102 60V68Q102 77 102 91T102 122T103 161T103 203Q103 234 103 269T102 328V351Q99 370 88 376T43 385H25V408Q25 431 27 431L37 432Q47 433 65 434T102 436Q119 437 138 438T167 441T178 442H181V402Q181 364 182 364T187 369T199 384T218 402T247 421T285 437Q305 442 336 442Q450 438 463 329Q464 322 464 190V104Q464 66 466 59T477 49Q498 46 526 46H542V0H534L510 1Q487 2 460 2T422 3Q319 3 310 0H302V46H318Q379 46 379 62Q380 64 380 200Q379 335 378 343Q372 371 358 385T334 402T308 404Q263 404 229 370Q202 343 195 315T187 232V168V108Q187 78 188 68T191 55T200 49Q221 46 249 46H265V0H257L234 1Q210 2 183 2T145 3Q42 3 33 0H25V46H41Z" transform="translate(1111, 0)"></path></g><g data-mml-node="TeXAtom" transform="translate(0, -600) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="msub"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></g></g></g><g data-mml-node="mo" transform="translate(1119.6, 0)"><path data-c="2026" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60ZM525 60Q525 84 542 102T585 120Q609 120 627 104T646 61Q646 36 629 18T586 0T543 17T525 60ZM972 60Q972 84 989 102T1032 120Q1056 120 1074 104T1093 61Q1093 36 1076 18T1033 0T990 17T972 60Z"></path></g><g data-mml-node="msub" transform="translate(2291.6, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D458" d="M121 647Q121 657 125 670T137 683Q138 683 209 688T282 694Q294 694 294 686Q294 679 244 477Q194 279 194 272Q213 282 223 291Q247 309 292 354T362 415Q402 442 438 442Q468 442 485 423T503 369Q503 344 496 327T477 302T456 291T438 288Q418 288 406 299T394 328Q394 353 410 369T442 390L458 393Q446 405 434 405H430Q398 402 367 380T294 316T228 255Q230 254 243 252T267 246T293 238T320 224T342 206T359 180T365 147Q365 130 360 106T354 66Q354 26 381 26Q429 26 459 145Q461 153 479 153H483Q499 153 499 144Q499 139 496 130Q455 -11 378 -11Q333 -11 305 15T277 90Q277 108 280 121T283 145Q283 167 269 183T234 206T200 217T182 220H180Q168 178 159 139T145 81T136 44T129 20T122 7T111 -2Q98 -11 83 -11Q66 -11 57 -1T48 16Q48 26 85 176T158 471L195 616Q196 629 188 632T149 637H144Q134 637 131 637T124 640T121 647Z"></path></g></g></g></g></g><g data-mml-node="mi" transform="translate(2589.2, 0)"><path data-c="1D43D" d="M447 625Q447 637 354 637H329Q323 642 323 645T325 664Q329 677 335 683H352Q393 681 498 681Q541 681 568 681T605 682T619 682Q633 682 633 672Q633 670 630 658Q626 642 623 640T604 637Q552 637 545 623Q541 610 483 376Q420 128 419 127Q397 64 333 21T195 -22Q137 -22 97 8T57 88Q57 130 80 152T132 174Q177 174 182 130Q182 98 164 80T123 56Q115 54 115 53T122 44Q148 15 197 15Q235 15 271 47T324 130Q328 142 387 380T447 625Z"></path></g><g data-mml-node="mo" transform="translate(3222.2, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="msub" transform="translate(3611.2, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></g></g></g><g data-mml-node="mo" transform="translate(4730.7, 0)"><path data-c="2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path></g><g data-mml-node="msub" transform="translate(5175.4, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g><g data-mml-node="mo" transform="translate(6461.6, 0)"><path data-c="2026" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60ZM525 60Q525 84 542 102T585 120Q609 120 627 104T646 61Q646 36 629 18T586 0T543 17T525 60ZM972 60Q972 84 989 102T1032 120Q1056 120 1074 104T1093 61Q1093 36 1076 18T1033 0T990 17T972 60Z"></path></g><g data-mml-node="msub" transform="translate(7800.3, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D458" d="M121 647Q121 657 125 670T137 683Q138 683 209 688T282 694Q294 694 294 686Q294 679 244 477Q194 279 194 272Q213 282 223 291Q247 309 292 354T362 415Q402 442 438 442Q468 442 485 423T503 369Q503 344 496 327T477 302T456 291T438 288Q418 288 406 299T394 328Q394 353 410 369T442 390L458 393Q446 405 434 405H430Q398 402 367 380T294 316T228 255Q230 254 243 252T267 246T293 238T320 224T342 206T359 180T365 147Q365 130 360 106T354 66Q354 26 381 26Q429 26 459 145Q461 153 479 153H483Q499 153 499 144Q499 139 496 130Q455 -11 378 -11Q333 -11 305 15T277 90Q277 108 280 121T283 145Q283 167 269 183T234 206T200 217T182 220H180Q168 178 159 139T145 81T136 44T129 20T122 7T111 -2Q98 -11 83 -11Q66 -11 57 -1T48 16Q48 26 85 176T158 471L195 616Q196 629 188 632T149 637H144Q134 637 131 637T124 640T121 647Z"></path></g></g></g><g data-mml-node="mo" transform="translate(8934.7, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g></g></g></svg></mjx-container></p><ul><li>Start with some initial values for <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.439ex" xmlns="http://www.w3.org/2000/svg" width="12.044ex" height="1.441ex" role="img" focusable="false" viewBox="0 -443 5323.5 637"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="msub"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></g></g></g><g data-mml-node="mo" transform="translate(1119.6, 0)"><path data-c="2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path></g><g data-mml-node="msub" transform="translate(1564.2, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g><g data-mml-node="mo" transform="translate(2850.4, 0)"><path data-c="2026" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60ZM525 60Q525 84 542 102T585 120Q609 120 627 104T646 61Q646 36 629 18T586 0T543 17T525 60ZM972 60Q972 84 989 102T1032 120Q1056 120 1074 104T1093 61Q1093 36 1076 18T1033 0T990 17T972 60Z"></path></g><g data-mml-node="msub" transform="translate(4189.1, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D458" d="M121 647Q121 657 125 670T137 683Q138 683 209 688T282 694Q294 694 294 686Q294 679 244 477Q194 279 194 272Q213 282 223 291Q247 309 292 354T362 415Q402 442 438 442Q468 442 485 423T503 369Q503 344 496 327T477 302T456 291T438 288Q418 288 406 299T394 328Q394 353 410 369T442 390L458 393Q446 405 434 405H430Q398 402 367 380T294 316T228 255Q230 254 243 252T267 246T293 238T320 224T342 206T359 180T365 147Q365 130 360 106T354 66Q354 26 381 26Q429 26 459 145Q461 153 479 153H483Q499 153 499 144Q499 139 496 130Q455 -11 378 -11Q333 -11 305 15T277 90Q277 108 280 121T283 145Q283 167 269 183T234 206T200 217T182 220H180Q168 178 159 139T145 81T136 44T129 20T122 7T111 -2Q98 -11 83 -11Q66 -11 57 -1T48 16Q48 26 85 176T158 471L195 616Q196 629 188 632T149 637H144Q134 637 131 637T124 640T121 647Z"></path></g></g></g></g></g></svg></mjx-container></li><li>Keep updating <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.025ex" xmlns="http://www.w3.org/2000/svg" width="1.62ex" height="1.027ex" role="img" focusable="false" viewBox="0 -443 716 454"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g></g></g></svg></mjx-container> to reduce
<mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex" xmlns="http://www.w3.org/2000/svg" width="15.236ex" height="2.262ex" role="img" focusable="false" viewBox="0 -750 6734.5 1000"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mi"><path data-c="1D43D" d="M447 625Q447 637 354 637H329Q323 642 323 645T325 664Q329 677 335 683H352Q393 681 498 681Q541 681 568 681T605 682T619 682Q633 682 633 672Q633 670 630 658Q626 642 623 640T604 637Q552 637 545 623Q541 610 483 376Q420 128 419 127Q397 64 333 21T195 -22Q137 -22 97 8T57 88Q57 130 80 152T132 174Q177 174 182 130Q182 98 164 80T123 56Q115 54 115 53T122 44Q148 15 197 15Q235 15 271 47T324 130Q328 142 387 380T447 625Z"></path></g><g data-mml-node="mo" transform="translate(633, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="msub" transform="translate(1022, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></g></g></g><g data-mml-node="mo" transform="translate(2141.6, 0)"><path data-c="2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path></g><g data-mml-node="msub" transform="translate(2586.2, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g><g data-mml-node="mo" transform="translate(3872.4, 0)"><path data-c="2026" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60ZM525 60Q525 84 542 102T585 120Q609 120 627 104T646 61Q646 36 629 18T586 0T543 17T525 60ZM972 60Q972 84 989 102T1032 120Q1056 120 1074 104T1093 61Q1093 36 1076 18T1033 0T990 17T972 60Z"></path></g><g data-mml-node="msub" transform="translate(5211.1, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D458" d="M121 647Q121 657 125 670T137 683Q138 683 209 688T282 694Q294 694 294 686Q294 679 244 477Q194 279 194 272Q213 282 223 291Q247 309 292 354T362 415Q402 442 438 442Q468 442 485 423T503 369Q503 344 496 327T477 302T456 291T438 288Q418 288 406 299T394 328Q394 353 410 369T442 390L458 393Q446 405 434 405H430Q398 402 367 380T294 316T228 255Q230 254 243 252T267 246T293 238T320 224T342 206T359 180T365 147Q365 130 360 106T354 66Q354 26 381 26Q429 26 459 145Q461 153 479 153H483Q499 153 499 144Q499 139 496 130Q455 -11 378 -11Q333 -11 305 15T277 90Q277 108 280 121T283 145Q283 167 269 183T234 206T200 217T182 220H180Q168 178 159 139T145 81T136 44T129 20T122 7T111 -2Q98 -11 83 -11Q66 -11 57 -1T48 16Q48 26 85 176T158 471L195 616Q196 629 188 632T149 637H144Q134 637 131 637T124 640T121 647Z"></path></g></g></g><g data-mml-node="mo" transform="translate(6345.5, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g></g></g></svg></mjx-container>hoping to end in the minimum</li><li>We want to know when we’re in this minimum!</li></ul><h2 id="4-2-Gradient-Descent-Algorithm"><a href="#4-2-Gradient-Descent-Algorithm" class="headerlink" title="4.2. Gradient Descent Algorithm"></a>4.2. Gradient Descent Algorithm</h2><p><img src="GradientDescentAlgorithm.png" alt="GradientDescentAlgorithm.png"></p><ul><li><mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.025ex" xmlns="http://www.w3.org/2000/svg" width="1.448ex" height="1.025ex" role="img" focusable="false" viewBox="0 -442 640 453"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mi"><path data-c="1D6FC" d="M34 156Q34 270 120 356T309 442Q379 442 421 402T478 304Q484 275 485 237V208Q534 282 560 374Q564 388 566 390T582 393Q603 393 603 385Q603 376 594 346T558 261T497 161L486 147L487 123Q489 67 495 47T514 26Q528 28 540 37T557 60Q559 67 562 68T577 70Q597 70 597 62Q597 56 591 43Q579 19 556 5T512 -10H505Q438 -10 414 62L411 69L400 61Q390 53 370 41T325 18T267 -2T203 -11Q124 -11 79 39T34 156ZM208 26Q257 26 306 47T379 90L403 112Q401 255 396 290Q382 405 304 405Q235 405 183 332Q156 292 139 224T121 120Q121 71 146 49T208 26Z"></path></g></g></g></svg></mjx-container> 为学习速率</li><li><mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.375ex" xmlns="http://www.w3.org/2000/svg" width="2.533ex" height="1.377ex" role="img" focusable="false" viewBox="0 -443 1119.6 608.6"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="msub"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></g></g></g></g></g></svg></mjx-container>和<mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.339ex" xmlns="http://www.w3.org/2000/svg" width="2.533ex" height="1.342ex" role="img" focusable="false" viewBox="0 -443 1119.6 593"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="msub"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g></g></g></svg></mjx-container> 必须同时更新</li></ul><h2 id="4-3-Learning-Rate-Influence"><a href="#4-3-Learning-Rate-Influence" class="headerlink" title="4.3. Learning Rate Influence"></a>4.3. Learning Rate Influence</h2><p><img src="LearningRate.png" alt="LearningRate.png"></p><ul><li><mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.025ex" xmlns="http://www.w3.org/2000/svg" width="1.448ex" height="1.025ex" role="img" focusable="false" viewBox="0 -442 640 453"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mi"><path data-c="1D6FC" d="M34 156Q34 270 120 356T309 442Q379 442 421 402T478 304Q484 275 485 237V208Q534 282 560 374Q564 388 566 390T582 393Q603 393 603 385Q603 376 594 346T558 261T497 161L486 147L487 123Q489 67 495 47T514 26Q528 28 540 37T557 60Q559 67 562 68T577 70Q597 70 597 62Q597 56 591 43Q579 19 556 5T512 -10H505Q438 -10 414 62L411 69L400 61Q390 53 370 41T325 18T267 -2T203 -11Q124 -11 79 39T34 156ZM208 26Q257 26 306 47T379 90L403 112Q401 255 396 290Q382 405 304 405Q235 405 183 332Q156 292 139 224T121 120Q121 71 146 49T208 26Z"></path></g></g></g></svg></mjx-container>, 学习速率会影响收敛的快慢</li></ul><h2 id="4-4-Partial-Derivative-of-Cost-Function"><a href="#4-4-Partial-Derivative-of-Cost-Function" class="headerlink" title="4.4. Partial Derivative of Cost Function"></a>4.4. Partial Derivative of Cost Function</h2><blockquote><p>The partial derivatives of our cost function for linear regression</p></blockquote><p><mjx-container class="MathJax" jax="SVG" display="true"><svg style="vertical-align: -0.566ex" xmlns="http://www.w3.org/2000/svg" width="17.759ex" height="2.262ex" role="img" focusable="false" viewBox="0 -750 7849.4 1000"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="msub"><g data-mml-node="mi"><path data-c="210E" d="M137 683Q138 683 209 688T282 694Q294 694 294 685Q294 674 258 534Q220 386 220 383Q220 381 227 388Q288 442 357 442Q411 442 444 415T478 336Q478 285 440 178T402 50Q403 36 407 31T422 26Q450 26 474 56T513 138Q516 149 519 151T535 153Q555 153 555 145Q555 144 551 130Q535 71 500 33Q466 -10 419 -10H414Q367 -10 346 17T325 74Q325 90 361 192T398 345Q398 404 354 404H349Q266 404 205 306L198 293L164 158Q132 28 127 16Q114 -11 83 -11Q69 -11 59 -2T48 16Q48 30 121 320L195 616Q195 629 188 632T149 637H128Q122 643 122 645T124 664Q129 683 137 683Z"></path></g><g data-mml-node="TeXAtom" transform="translate(576, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g></g></g><g data-mml-node="mo" transform="translate(1132.3, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mi" transform="translate(1521.3, 0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="mo" transform="translate(2093.3, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(2760.1, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="msub" transform="translate(3815.8, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></g></g></g><g data-mml-node="mo" transform="translate(5157.6, 0)"><path data-c="2B" d="M56 237T56 250T70 270H369V420L370 570Q380 583 389 583Q402 583 409 568V270H707Q722 262 722 250T707 230H409V-68Q401 -82 391 -82H389H387Q375 -82 369 -68V230H70Q56 237 56 250Z"></path></g><g data-mml-node="msub" transform="translate(6157.8, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g><g data-mml-node="mi" transform="translate(7277.4, 0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g></g></g></svg></mjx-container><br><mjx-container class="MathJax" jax="SVG" display="true"><svg style="vertical-align: -2.819ex" xmlns="http://www.w3.org/2000/svg" width="31.281ex" height="6.354ex" role="img" focusable="false" viewBox="0 -1562.5 13826.3 2808.5"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mi"><path data-c="1D43D" d="M447 625Q447 637 354 637H329Q323 642 323 645T325 664Q329 677 335 683H352Q393 681 498 681Q541 681 568 681T605 682T619 682Q633 682 633 672Q633 670 630 658Q626 642 623 640T604 637Q552 637 545 623Q541 610 483 376Q420 128 419 127Q397 64 333 21T195 -22Q137 -22 97 8T57 88Q57 130 80 152T132 174Q177 174 182 130Q182 98 164 80T123 56Q115 54 115 53T122 44Q148 15 197 15Q235 15 271 47T324 130Q328 142 387 380T447 625Z"></path></g><g data-mml-node="mo" transform="translate(633, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="msub" transform="translate(1022, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></g></g></g><g data-mml-node="mo" transform="translate(2141.6, 0)"><path data-c="2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path></g><g data-mml-node="msub" transform="translate(2586.2, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g><g data-mml-node="mo" transform="translate(3705.8, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(4372.6, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mfrac" transform="translate(5428.3, 0)"><g data-mml-node="mn" transform="translate(520, 676)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g><g data-mml-node="mrow" transform="translate(220, -686)"><g data-mml-node="mn"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></g><g data-mml-node="mi" transform="translate(500, 0)"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g></g><rect width="1300" height="60" x="120" y="220"></rect></g><g data-mml-node="munderover" transform="translate(7135, 0)"><g data-mml-node="mo"><path data-c="2211" d="M60 948Q63 950 665 950H1267L1325 815Q1384 677 1388 669H1348L1341 683Q1320 724 1285 761Q1235 809 1174 838T1033 881T882 898T699 902H574H543H251L259 891Q722 258 724 252Q725 250 724 246Q721 243 460 -56L196 -356Q196 -357 407 -357Q459 -357 548 -357T676 -358Q812 -358 896 -353T1063 -332T1204 -283T1307 -196Q1328 -170 1348 -124H1388Q1388 -125 1381 -145T1356 -210T1325 -294L1267 -449L666 -450Q64 -450 61 -448Q55 -446 55 -439Q55 -437 57 -433L590 177Q590 178 557 222T452 366T322 544L56 909L55 924Q55 945 60 948Z"></path></g><g data-mml-node="TeXAtom" transform="translate(148.2, -1087.9) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D456" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mo" transform="translate(345, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mn" transform="translate(1123, 0)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g><g data-mml-node="TeXAtom" transform="translate(509.9, 1150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g></g></g><g data-mml-node="mo" transform="translate(8579, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="msub" transform="translate(8968, 0)"><g data-mml-node="mi"><path data-c="210E" d="M137 683Q138 683 209 688T282 694Q294 694 294 685Q294 674 258 534Q220 386 220 383Q220 381 227 388Q288 442 357 442Q411 442 444 415T478 336Q478 285 440 178T402 50Q403 36 407 31T422 26Q450 26 474 56T513 138Q516 149 519 151T535 153Q555 153 555 145Q555 144 551 130Q535 71 500 33Q466 -10 419 -10H414Q367 -10 346 17T325 74Q325 90 361 192T398 345Q398 404 354 404H349Q266 404 205 306L198 293L164 158Q132 28 127 16Q114 -11 83 -11Q69 -11 59 -2T48 16Q48 30 121 320L195 616Q195 629 188 632T149 637H128Q122 643 122 645T124 664Q129 683 137 683Z"></path></g><g data-mml-node="TeXAtom" transform="translate(576, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g></g></g><g data-mml-node="mo" transform="translate(10100.3, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mi" transform="translate(10489.3, 0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="mo" transform="translate(11061.3, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(11672.5, 0)"><path data-c="2212" d="M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z"></path></g><g data-mml-node="mi" transform="translate(12672.7, 0)"><path data-c="1D461" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path></g><g data-mml-node="msup" transform="translate(13033.7, 0)"><g data-mml-node="mo"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mn" transform="translate(389, 413) scale(0.707)"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></g></g></g></g></svg></mjx-container><br><mjx-container class="MathJax" jax="SVG" display="true"><svg style="vertical-align: -2.819ex" xmlns="http://www.w3.org/2000/svg" width="60.872ex" height="6.354ex" role="img" focusable="false" viewBox="0 -1562.5 26905.3 2808.5"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mfrac"><g data-mml-node="mi" transform="translate(779.8, 676)"><path data-c="1D715" d="M202 508Q179 508 169 520T158 547Q158 557 164 577T185 624T230 675T301 710L333 715H345Q378 715 384 714Q447 703 489 661T549 568T566 457Q566 362 519 240T402 53Q321 -22 223 -22Q123 -22 73 56Q42 102 42 148V159Q42 276 129 370T322 465Q383 465 414 434T455 367L458 378Q478 461 478 515Q478 603 437 639T344 676Q266 676 223 612Q264 606 264 572Q264 547 246 528T202 508ZM430 306Q430 372 401 400T333 428Q270 428 222 382Q197 354 183 323T150 221Q132 149 132 116Q132 21 232 21Q244 21 250 22Q327 35 374 112Q389 137 409 196T430 306Z"></path></g><g data-mml-node="mrow" transform="translate(220, -686)"><g data-mml-node="mi"><path data-c="1D715" d="M202 508Q179 508 169 520T158 547Q158 557 164 577T185 624T230 675T301 710L333 715H345Q378 715 384 714Q447 703 489 661T549 568T566 457Q566 362 519 240T402 53Q321 -22 223 -22Q123 -22 73 56Q42 102 42 148V159Q42 276 129 370T322 465Q383 465 414 434T455 367L458 378Q478 461 478 515Q478 603 437 639T344 676Q266 676 223 612Q264 606 264 572Q264 547 246 528T202 508ZM430 306Q430 372 401 400T333 428Q270 428 222 382Q197 354 183 323T150 221Q132 149 132 116Q132 21 232 21Q244 21 250 22Q327 35 374 112Q389 137 409 196T430 306Z"></path></g><g data-mml-node="msub" transform="translate(566, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></g></g></g></g><rect width="1885.6" height="60" x="120" y="220"></rect></g><g data-mml-node="mi" transform="translate(2125.6, 0)"><path data-c="1D43D" d="M447 625Q447 637 354 637H329Q323 642 323 645T325 664Q329 677 335 683H352Q393 681 498 681Q541 681 568 681T605 682T619 682Q633 682 633 672Q633 670 630 658Q626 642 623 640T604 637Q552 637 545 623Q541 610 483 376Q420 128 419 127Q397 64 333 21T195 -22Q137 -22 97 8T57 88Q57 130 80 152T132 174Q177 174 182 130Q182 98 164 80T123 56Q115 54 115 53T122 44Q148 15 197 15Q235 15 271 47T324 130Q328 142 387 380T447 625Z"></path></g><g data-mml-node="mo" transform="translate(2758.6, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="msub" transform="translate(3147.6, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></g></g></g><g data-mml-node="mo" transform="translate(4267.1, 0)"><path data-c="2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path></g><g data-mml-node="msub" transform="translate(4711.8, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g><g data-mml-node="mo" transform="translate(5831.3, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(6498.1, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mfrac" transform="translate(7553.9, 0)"><g data-mml-node="mi" transform="translate(779.8, 676)"><path data-c="1D715" d="M202 508Q179 508 169 520T158 547Q158 557 164 577T185 624T230 675T301 710L333 715H345Q378 715 384 714Q447 703 489 661T549 568T566 457Q566 362 519 240T402 53Q321 -22 223 -22Q123 -22 73 56Q42 102 42 148V159Q42 276 129 370T322 465Q383 465 414 434T455 367L458 378Q478 461 478 515Q478 603 437 639T344 676Q266 676 223 612Q264 606 264 572Q264 547 246 528T202 508ZM430 306Q430 372 401 400T333 428Q270 428 222 382Q197 354 183 323T150 221Q132 149 132 116Q132 21 232 21Q244 21 250 22Q327 35 374 112Q389 137 409 196T430 306Z"></path></g><g data-mml-node="mrow" transform="translate(220, -686)"><g data-mml-node="mi"><path data-c="1D715" d="M202 508Q179 508 169 520T158 547Q158 557 164 577T185 624T230 675T301 710L333 715H345Q378 715 384 714Q447 703 489 661T549 568T566 457Q566 362 519 240T402 53Q321 -22 223 -22Q123 -22 73 56Q42 102 42 148V159Q42 276 129 370T322 465Q383 465 414 434T455 367L458 378Q478 461 478 515Q478 603 437 639T344 676Q266 676 223 612Q264 606 264 572Q264 547 246 528T202 508ZM430 306Q430 372 401 400T333 428Q270 428 222 382Q197 354 183 323T150 221Q132 149 132 116Q132 21 232 21Q244 21 250 22Q327 35 374 112Q389 137 409 196T430 306Z"></path></g><g data-mml-node="msub" transform="translate(566, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></g></g></g></g><rect width="1885.6" height="60" x="120" y="220"></rect></g><g data-mml-node="mfrac" transform="translate(9679.4, 0)"><g data-mml-node="mn" transform="translate(520, 676)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g><g data-mml-node="mrow" transform="translate(220, -686)"><g data-mml-node="mn"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></g><g data-mml-node="mi" transform="translate(500, 0)"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g></g><rect width="1300" height="60" x="120" y="220"></rect></g><g data-mml-node="munderover" transform="translate(11386.1, 0)"><g data-mml-node="mo"><path data-c="2211" d="M60 948Q63 950 665 950H1267L1325 815Q1384 677 1388 669H1348L1341 683Q1320 724 1285 761Q1235 809 1174 838T1033 881T882 898T699 902H574H543H251L259 891Q722 258 724 252Q725 250 724 246Q721 243 460 -56L196 -356Q196 -357 407 -357Q459 -357 548 -357T676 -358Q812 -358 896 -353T1063 -332T1204 -283T1307 -196Q1328 -170 1348 -124H1388Q1388 -125 1381 -145T1356 -210T1325 -294L1267 -449L666 -450Q64 -450 61 -448Q55 -446 55 -439Q55 -437 57 -433L590 177Q590 178 557 222T452 366T322 544L56 909L55 924Q55 945 60 948Z"></path></g><g data-mml-node="TeXAtom" transform="translate(148.2, -1087.9) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D456" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mo" transform="translate(345, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mn" transform="translate(1123, 0)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g><g data-mml-node="TeXAtom" transform="translate(509.9, 1150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g></g></g><g data-mml-node="mo" transform="translate(12830.1, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="msub" transform="translate(13219.1, 0)"><g data-mml-node="mi"><path data-c="210E" d="M137 683Q138 683 209 688T282 694Q294 694 294 685Q294 674 258 534Q220 386 220 383Q220 381 227 388Q288 442 357 442Q411 442 444 415T478 336Q478 285 440 178T402 50Q403 36 407 31T422 26Q450 26 474 56T513 138Q516 149 519 151T535 153Q555 153 555 145Q555 144 551 130Q535 71 500 33Q466 -10 419 -10H414Q367 -10 346 17T325 74Q325 90 361 192T398 345Q398 404 354 404H349Q266 404 205 306L198 293L164 158Q132 28 127 16Q114 -11 83 -11Q69 -11 59 -2T48 16Q48 30 121 320L195 616Q195 629 188 632T149 637H128Q122 643 122 645T124 664Q129 683 137 683Z"></path></g><g data-mml-node="TeXAtom" transform="translate(576, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g></g></g><g data-mml-node="mo" transform="translate(14351.4, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mi" transform="translate(14740.4, 0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="mo" transform="translate(15312.4, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(15923.6, 0)"><path data-c="2212" d="M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z"></path></g><g data-mml-node="mi" transform="translate(16923.8, 0)"><path data-c="1D461" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path></g><g data-mml-node="msup" transform="translate(17284.8, 0)"><g data-mml-node="mo"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mn" transform="translate(389, 413) scale(0.707)"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></g></g><g data-mml-node="mo" transform="translate(18355.2, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mfrac" transform="translate(19410.9, 0)"><g data-mml-node="mn" transform="translate(270, 676)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g><g data-mml-node="mi" transform="translate(220, -686)"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g><rect width="800" height="60" x="120" y="220"></rect></g><g data-mml-node="munderover" transform="translate(20617.6, 0)"><g data-mml-node="mo"><path data-c="2211" d="M60 948Q63 950 665 950H1267L1325 815Q1384 677 1388 669H1348L1341 683Q1320 724 1285 761Q1235 809 1174 838T1033 881T882 898T699 902H574H543H251L259 891Q722 258 724 252Q725 250 724 246Q721 243 460 -56L196 -356Q196 -357 407 -357Q459 -357 548 -357T676 -358Q812 -358 896 -353T1063 -332T1204 -283T1307 -196Q1328 -170 1348 -124H1388Q1388 -125 1381 -145T1356 -210T1325 -294L1267 -449L666 -450Q64 -450 61 -448Q55 -446 55 -439Q55 -437 57 -433L590 177Q590 178 557 222T452 366T322 544L56 909L55 924Q55 945 60 948Z"></path></g><g data-mml-node="TeXAtom" transform="translate(148.2, -1087.9) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D456" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mo" transform="translate(345, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mn" transform="translate(1123, 0)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g><g data-mml-node="TeXAtom" transform="translate(509.9, 1150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g></g></g><g data-mml-node="mo" transform="translate(22061.6, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="msub" transform="translate(22450.6, 0)"><g data-mml-node="mi"><path data-c="210E" d="M137 683Q138 683 209 688T282 694Q294 694 294 685Q294 674 258 534Q220 386 220 383Q220 381 227 388Q288 442 357 442Q411 442 444 415T478 336Q478 285 440 178T402 50Q403 36 407 31T422 26Q450 26 474 56T513 138Q516 149 519 151T535 153Q555 153 555 145Q555 144 551 130Q535 71 500 33Q466 -10 419 -10H414Q367 -10 346 17T325 74Q325 90 361 192T398 345Q398 404 354 404H349Q266 404 205 306L198 293L164 158Q132 28 127 16Q114 -11 83 -11Q69 -11 59 -2T48 16Q48 30 121 320L195 616Q195 629 188 632T149 637H128Q122 643 122 645T124 664Q129 683 137 683Z"></path></g><g data-mml-node="TeXAtom" transform="translate(576, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g></g></g><g data-mml-node="mo" transform="translate(23582.9, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mi" transform="translate(23971.9, 0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="mo" transform="translate(24543.9, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(25155.1, 0)"><path data-c="2212" d="M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z"></path></g><g data-mml-node="mi" transform="translate(26155.3, 0)"><path data-c="1D461" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path></g><g data-mml-node="mo" transform="translate(26516.3, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g></g></g></svg></mjx-container><br><mjx-container class="MathJax" jax="SVG" display="true"><svg style="vertical-align: -2.819ex" xmlns="http://www.w3.org/2000/svg" width="62.166ex" height="6.354ex" role="img" focusable="false" viewBox="0 -1562.5 27477.3 2808.5"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mfrac"><g data-mml-node="mi" transform="translate(779.8, 676)"><path data-c="1D715" d="M202 508Q179 508 169 520T158 547Q158 557 164 577T185 624T230 675T301 710L333 715H345Q378 715 384 714Q447 703 489 661T549 568T566 457Q566 362 519 240T402 53Q321 -22 223 -22Q123 -22 73 56Q42 102 42 148V159Q42 276 129 370T322 465Q383 465 414 434T455 367L458 378Q478 461 478 515Q478 603 437 639T344 676Q266 676 223 612Q264 606 264 572Q264 547 246 528T202 508ZM430 306Q430 372 401 400T333 428Q270 428 222 382Q197 354 183 323T150 221Q132 149 132 116Q132 21 232 21Q244 21 250 22Q327 35 374 112Q389 137 409 196T430 306Z"></path></g><g data-mml-node="mrow" transform="translate(220, -686)"><g data-mml-node="mi"><path data-c="1D715" d="M202 508Q179 508 169 520T158 547Q158 557 164 577T185 624T230 675T301 710L333 715H345Q378 715 384 714Q447 703 489 661T549 568T566 457Q566 362 519 240T402 53Q321 -22 223 -22Q123 -22 73 56Q42 102 42 148V159Q42 276 129 370T322 465Q383 465 414 434T455 367L458 378Q478 461 478 515Q478 603 437 639T344 676Q266 676 223 612Q264 606 264 572Q264 547 246 528T202 508ZM430 306Q430 372 401 400T333 428Q270 428 222 382Q197 354 183 323T150 221Q132 149 132 116Q132 21 232 21Q244 21 250 22Q327 35 374 112Q389 137 409 196T430 306Z"></path></g><g data-mml-node="msub" transform="translate(566, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g></g><rect width="1885.6" height="60" x="120" y="220"></rect></g><g data-mml-node="mi" transform="translate(2125.6, 0)"><path data-c="1D43D" d="M447 625Q447 637 354 637H329Q323 642 323 645T325 664Q329 677 335 683H352Q393 681 498 681Q541 681 568 681T605 682T619 682Q633 682 633 672Q633 670 630 658Q626 642 623 640T604 637Q552 637 545 623Q541 610 483 376Q420 128 419 127Q397 64 333 21T195 -22Q137 -22 97 8T57 88Q57 130 80 152T132 174Q177 174 182 130Q182 98 164 80T123 56Q115 54 115 53T122 44Q148 15 197 15Q235 15 271 47T324 130Q328 142 387 380T447 625Z"></path></g><g data-mml-node="mo" transform="translate(2758.6, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="msub" transform="translate(3147.6, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></g></g></g><g data-mml-node="mo" transform="translate(4267.1, 0)"><path data-c="2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path></g><g data-mml-node="msub" transform="translate(4711.8, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g><g data-mml-node="mo" transform="translate(5831.3, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(6498.1, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mfrac" transform="translate(7553.9, 0)"><g data-mml-node="mi" transform="translate(779.8, 676)"><path data-c="1D715" d="M202 508Q179 508 169 520T158 547Q158 557 164 577T185 624T230 675T301 710L333 715H345Q378 715 384 714Q447 703 489 661T549 568T566 457Q566 362 519 240T402 53Q321 -22 223 -22Q123 -22 73 56Q42 102 42 148V159Q42 276 129 370T322 465Q383 465 414 434T455 367L458 378Q478 461 478 515Q478 603 437 639T344 676Q266 676 223 612Q264 606 264 572Q264 547 246 528T202 508ZM430 306Q430 372 401 400T333 428Q270 428 222 382Q197 354 183 323T150 221Q132 149 132 116Q132 21 232 21Q244 21 250 22Q327 35 374 112Q389 137 409 196T430 306Z"></path></g><g data-mml-node="mrow" transform="translate(220, -686)"><g data-mml-node="mi"><path data-c="1D715" d="M202 508Q179 508 169 520T158 547Q158 557 164 577T185 624T230 675T301 710L333 715H345Q378 715 384 714Q447 703 489 661T549 568T566 457Q566 362 519 240T402 53Q321 -22 223 -22Q123 -22 73 56Q42 102 42 148V159Q42 276 129 370T322 465Q383 465 414 434T455 367L458 378Q478 461 478 515Q478 603 437 639T344 676Q266 676 223 612Q264 606 264 572Q264 547 246 528T202 508ZM430 306Q430 372 401 400T333 428Q270 428 222 382Q197 354 183 323T150 221Q132 149 132 116Q132 21 232 21Q244 21 250 22Q327 35 374 112Q389 137 409 196T430 306Z"></path></g><g data-mml-node="msub" transform="translate(566, 0)"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="TeXAtom" transform="translate(716, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g></g><rect width="1885.6" height="60" x="120" y="220"></rect></g><g data-mml-node="mfrac" transform="translate(9679.4, 0)"><g data-mml-node="mn" transform="translate(520, 676)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g><g data-mml-node="mrow" transform="translate(220, -686)"><g data-mml-node="mn"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></g><g data-mml-node="mi" transform="translate(500, 0)"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g></g><rect width="1300" height="60" x="120" y="220"></rect></g><g data-mml-node="munderover" transform="translate(11386.1, 0)"><g data-mml-node="mo"><path data-c="2211" d="M60 948Q63 950 665 950H1267L1325 815Q1384 677 1388 669H1348L1341 683Q1320 724 1285 761Q1235 809 1174 838T1033 881T882 898T699 902H574H543H251L259 891Q722 258 724 252Q725 250 724 246Q721 243 460 -56L196 -356Q196 -357 407 -357Q459 -357 548 -357T676 -358Q812 -358 896 -353T1063 -332T1204 -283T1307 -196Q1328 -170 1348 -124H1388Q1388 -125 1381 -145T1356 -210T1325 -294L1267 -449L666 -450Q64 -450 61 -448Q55 -446 55 -439Q55 -437 57 -433L590 177Q590 178 557 222T452 366T322 544L56 909L55 924Q55 945 60 948Z"></path></g><g data-mml-node="TeXAtom" transform="translate(148.2, -1087.9) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D456" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mo" transform="translate(345, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mn" transform="translate(1123, 0)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g><g data-mml-node="TeXAtom" transform="translate(509.9, 1150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g></g></g><g data-mml-node="mo" transform="translate(12830.1, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="msub" transform="translate(13219.1, 0)"><g data-mml-node="mi"><path data-c="210E" d="M137 683Q138 683 209 688T282 694Q294 694 294 685Q294 674 258 534Q220 386 220 383Q220 381 227 388Q288 442 357 442Q411 442 444 415T478 336Q478 285 440 178T402 50Q403 36 407 31T422 26Q450 26 474 56T513 138Q516 149 519 151T535 153Q555 153 555 145Q555 144 551 130Q535 71 500 33Q466 -10 419 -10H414Q367 -10 346 17T325 74Q325 90 361 192T398 345Q398 404 354 404H349Q266 404 205 306L198 293L164 158Q132 28 127 16Q114 -11 83 -11Q69 -11 59 -2T48 16Q48 30 121 320L195 616Q195 629 188 632T149 637H128Q122 643 122 645T124 664Q129 683 137 683Z"></path></g><g data-mml-node="TeXAtom" transform="translate(576, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g></g></g><g data-mml-node="mo" transform="translate(14351.4, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mi" transform="translate(14740.4, 0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="mo" transform="translate(15312.4, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(15923.6, 0)"><path data-c="2212" d="M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z"></path></g><g data-mml-node="mi" transform="translate(16923.8, 0)"><path data-c="1D461" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path></g><g data-mml-node="msup" transform="translate(17284.8, 0)"><g data-mml-node="mo"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mn" transform="translate(389, 413) scale(0.707)"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></g></g><g data-mml-node="mo" transform="translate(18355.2, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mfrac" transform="translate(19410.9, 0)"><g data-mml-node="mn" transform="translate(270, 676)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g><g data-mml-node="mi" transform="translate(220, -686)"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g><rect width="800" height="60" x="120" y="220"></rect></g><g data-mml-node="munderover" transform="translate(20617.6, 0)"><g data-mml-node="mo"><path data-c="2211" d="M60 948Q63 950 665 950H1267L1325 815Q1384 677 1388 669H1348L1341 683Q1320 724 1285 761Q1235 809 1174 838T1033 881T882 898T699 902H574H543H251L259 891Q722 258 724 252Q725 250 724 246Q721 243 460 -56L196 -356Q196 -357 407 -357Q459 -357 548 -357T676 -358Q812 -358 896 -353T1063 -332T1204 -283T1307 -196Q1328 -170 1348 -124H1388Q1388 -125 1381 -145T1356 -210T1325 -294L1267 -449L666 -450Q64 -450 61 -448Q55 -446 55 -439Q55 -437 57 -433L590 177Q590 178 557 222T452 366T322 544L56 909L55 924Q55 945 60 948Z"></path></g><g data-mml-node="TeXAtom" transform="translate(148.2, -1087.9) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D456" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mo" transform="translate(345, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mn" transform="translate(1123, 0)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g><g data-mml-node="TeXAtom" transform="translate(509.9, 1150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g></g></g><g data-mml-node="mo" transform="translate(22061.6, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="msub" transform="translate(22450.6, 0)"><g data-mml-node="mi"><path data-c="210E" d="M137 683Q138 683 209 688T282 694Q294 694 294 685Q294 674 258 534Q220 386 220 383Q220 381 227 388Q288 442 357 442Q411 442 444 415T478 336Q478 285 440 178T402 50Q403 36 407 31T422 26Q450 26 474 56T513 138Q516 149 519 151T535 153Q555 153 555 145Q555 144 551 130Q535 71 500 33Q466 -10 419 -10H414Q367 -10 346 17T325 74Q325 90 361 192T398 345Q398 404 354 404H349Q266 404 205 306L198 293L164 158Q132 28 127 16Q114 -11 83 -11Q69 -11 59 -2T48 16Q48 30 121 320L195 616Q195 629 188 632T149 637H128Q122 643 122 645T124 664Q129 683 137 683Z"></path></g><g data-mml-node="TeXAtom" transform="translate(576, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g></g></g><g data-mml-node="mo" transform="translate(23582.9, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mi" transform="translate(23971.9, 0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="mo" transform="translate(24543.9, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(25155.1, 0)"><path data-c="2212" d="M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z"></path></g><g data-mml-node="mi" transform="translate(26155.3, 0)"><path data-c="1D461" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path></g><g data-mml-node="mo" transform="translate(26516.3, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mi" transform="translate(26905.3, 0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g></g></g></svg></mjx-container></p><h2 id="4-6-Two-variable-Cost-Function"><a href="#4-6-Two-variable-Cost-Function" class="headerlink" title="4.6. Two-variable Cost Function"></a>4.6. Two-variable Cost Function</h2><p><img src="Two-variableCostFunction.png" alt="Two-variableCostFunction.png"></p><h2 id="4-7-Multivariate-Linear-Regression"><a href="#4-7-Multivariate-Linear-Regression" class="headerlink" title="4.7. Multivariate Linear Regression"></a>4.7. Multivariate Linear Regression</h2><p><img src="MultivariateLinearRegression.png" alt="MultivariateLinearRegression.png"></p><h2 id="4-8-Gradient-Descent"><a href="#4-8-Gradient-Descent" class="headerlink" title="4.8. Gradient Descent"></a>4.8. Gradient Descent</h2><p><img src="GradientDescent.png" alt="GradientDescent.png"></p><h2 id="4-9-Local-and-Global-Minimal"><a href="#4-9-Local-and-Global-Minimal" class="headerlink" title="4.9. Local and Global Minimal"></a>4.9. Local and Global Minimal</h2><p><img src="LocalandGlobalMinima.png" alt="LocalandGlobalMinima.png"></p><ul><li>每一个全局最小值都是一个局部最小值,但是每一个局部最小值并不是全局最小值</li><li>每次当程序收敛后,其实并不知道是局部最小值还是全局最小值;因此需要测试多组不同初始值来判定</li></ul><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><h1 id="1-Univariate-Linear-Regression"><a href="#1-Univariate-Linear-Regression" class="headerlink" title="1. Univariate Linear Regression"></a>1. Univariate Linear Regression</h1><blockquote>
<p>Univariate linear regression operates on a single dimension/feature of your data.</p>
</blockquote></summary>
<category term="UoN" scheme="https://aaronos0.github.io/categories/UoN/"/>
<category term="COMP3009-Machine Learning" scheme="https://aaronos0.github.io/tags/COMP3009-Machine-Learning/"/>
</entry>
<entry>
<title>COMP3009 - 1.Introduction</title>
<link href="https://aaronos0.github.io/2020/10/11/COMP3009-1-Introduction/"/>
<id>https://aaronos0.github.io/2020/10/11/COMP3009-1-Introduction/</id>
<published>2020-10-11T17:43:02.000Z</published>
<updated>2020-10-11T17:43:02.000Z</updated>
<content type="html"><![CDATA[<h1 id="1-What-is-Machine-Learning"><a href="#1-What-is-Machine-Learning" class="headerlink" title="1. What is Machine Learning?"></a>1. What is Machine Learning?</h1><h2 id="1-1-Definition"><a href="#1-1-Definition" class="headerlink" title="1.1. Definition"></a>1.1. Definition</h2><ul><li>Arthur Samuel (1959): Field of study that gives computers the ability to learn without being explicitly programmed. <strong><<一个较为宏观的定义>></strong></li><li>Tom Mitchell (1998).Well-posed Learning Problem:A computer program is said to learn from <strong>experience E</strong> with respect to some <strong>task T</strong> and some <strong>performance measure P</strong>, if its performance on T, as measured by P, improves with experience E. <strong><<一个较为practical的定义, 难点在于如何辨别一个problem中的T/P/E>></strong></li></ul><span id="more"></span><h2 id="1-2-Main-goal-Aim"><a href="#1-2-Main-goal-Aim" class="headerlink" title="1.2. Main goal(Aim)"></a>1.2. Main goal(Aim)</h2><ul><li>Learning functions:<div class="wiz-table-container" style="position: relative; padding: 0px;"><div class="wiz-table-body" contenteditable="false"><table style="width: 683px;"><tbody><tr><td align="left" valign="middle" style="width: 81px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div>Features </div></td><td align="left" valign="middle" style="width: 76px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div>Labels</div></td><td align="left" valign="middle" style="width: 147px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div><span style="">Learning functions</span></div></td><td align="left" valign="middle" style="width: 378px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div><span style="">Machine learning<span> </span></span></div></td></tr><tr><td align="left" valign="middle" style="width: 81px;"><div>x</div></td><td align="left" valign="middle" style="width: 76px;"><div>f(x) = y</div></td><td align="left" valign="middle" style="width: 147px;"><div>The relationship between x and y.</div></td><td align="left" valign="middle" style="width: 378px;"><div>ML is the process of learning a function or hypothesis h(x), let the h(x) --> y,best approximates.</div></td></tr></tbody></table></div></div></li><li>Generalisation(泛化): This hypothesis can then make predictions <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.464ex" xmlns="http://www.w3.org/2000/svg" width="1.257ex" height="2.195ex" role="img" focusable="false" viewBox="0 -765 555.6 970"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="TeXAtom" data-mjx-texclass="ORD"><g data-mml-node="mover"><g data-mml-node="mi" transform="translate(5, 0)"><path data-c="1D466" d="M21 287Q21 301 36 335T84 406T158 442Q199 442 224 419T250 355Q248 336 247 334Q247 331 231 288T198 191T182 105Q182 62 196 45T238 27Q261 27 281 38T312 61T339 94Q339 95 344 114T358 173T377 247Q415 397 419 404Q432 431 462 431Q475 431 483 424T494 412T496 403Q496 390 447 193T391 -23Q363 -106 294 -155T156 -205Q111 -205 77 -183T43 -117Q43 -95 50 -80T69 -58T89 -48T106 -45Q150 -45 150 -87Q150 -107 138 -122T115 -142T102 -147L99 -148Q101 -153 118 -160T152 -167H160Q177 -167 186 -165Q219 -156 247 -127T290 -65T313 -9T321 21L315 17Q309 13 296 6T270 -6Q250 -11 231 -11Q185 -11 150 11T104 82Q103 89 103 113Q103 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mo" transform="translate(55.6, -29)"><path data-c="2C6" d="M112 560L249 694L257 686Q387 562 387 560L361 531Q359 532 303 581L250 627L195 580Q182 569 169 557T148 538L140 532Q138 530 125 546L112 560Z"></path></g></g></g></g></g></svg></mjx-container> on new data points <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.025ex" xmlns="http://www.w3.org/2000/svg" width="1.294ex" height="1.756ex" role="img" focusable="false" viewBox="0 -765 572 776"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="TeXAtom" data-mjx-texclass="ORD"><g data-mml-node="mover"><g data-mml-node="mi"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="mo" transform="translate(63.8, -29)"><path data-c="2C6" d="M112 560L249 694L257 686Q387 562 387 560L361 531Q359 532 303 581L250 627L195 580Q182 569 169 557T148 538L140 532Q138 530 125 546L112 560Z"></path></g></g></g></g></g></svg></mjx-container>, such that <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.464ex" xmlns="http://www.w3.org/2000/svg" width="1.257ex" height="2.195ex" role="img" focusable="false" viewBox="0 -765 555.6 970"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="TeXAtom" data-mjx-texclass="ORD"><g data-mml-node="mover"><g data-mml-node="mi" transform="translate(5, 0)"><path data-c="1D466" d="M21 287Q21 301 36 335T84 406T158 442Q199 442 224 419T250 355Q248 336 247 334Q247 331 231 288T198 191T182 105Q182 62 196 45T238 27Q261 27 281 38T312 61T339 94Q339 95 344 114T358 173T377 247Q415 397 419 404Q432 431 462 431Q475 431 483 424T494 412T496 403Q496 390 447 193T391 -23Q363 -106 294 -155T156 -205Q111 -205 77 -183T43 -117Q43 -95 50 -80T69 -58T89 -48T106 -45Q150 -45 150 -87Q150 -107 138 -122T115 -142T102 -147L99 -148Q101 -153 118 -160T152 -167H160Q177 -167 186 -165Q219 -156 247 -127T290 -65T313 -9T321 21L315 17Q309 13 296 6T270 -6Q250 -11 231 -11Q185 -11 150 11T104 82Q103 89 103 113Q103 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mo" transform="translate(55.6, -29)"><path data-c="2C6" d="M112 560L249 694L257 686Q387 562 387 560L361 531Q359 532 303 581L250 627L195 580Q182 569 169 557T148 538L140 532Q138 530 125 546L112 560Z"></path></g></g></g></g></g></svg></mjx-container> is <strong>as close as possible</strong> to its real label y .</li><li>就人类而言,正是因为泛化能力,才让我们有可能<strong>用已知来对抗未知,以有限对抗无限</strong>.</li></ul><h2 id="1-3-Learning-functions"><a href="#1-3-Learning-functions" class="headerlink" title="1.3. Learning functions"></a>1.3. Learning functions</h2><p><img src="LearningFunctionsEg.png" alt="LearningFunctionsEg.png"></p><ol><li>(a): Original data points;</li><li>(b): Fit by three piecewise-linear segments (perfect fit);<ul><li>三个线性分段函数;</li><li>parameter: 3 x 2 = 6 variables;</li></ul></li><li>(c): Fit by third-order polynomial (perfect fit);<ul><li>三阶多项式;</li><li>parameter: 3(polynomial) + 1(bias) = 4 variables;</li></ul></li><li>(d): Fit by first-order polynomial (straight line - not perfect);<ul><li>一阶多项式;</li><li>parameter: 2 variables;</li></ul></li></ol><ul><li><strong>Rule of thumb</strong>: Data points ~= 10 x variables num</li><li>Tips:<ol><li>避免过拟合</li><li>控制参数个数</li></ol></li></ul><h1 id="2-Representation"><a href="#2-Representation" class="headerlink" title="2. Representation"></a>2. Representation</h1><h2 id="2-1-Supervised-Unsupervised-Learning"><a href="#2-1-Supervised-Unsupervised-Learning" class="headerlink" title="2.1. Supervised/Unsupervised Learning"></a>2.1. Supervised/Unsupervised Learning</h2><ul><li>Machine Learning: is learning from experience. It’s also called <strong>supervised learning</strong>. E consists of features and labels, and P and T are well-defined.</li><li>Pattern Recognition: is finding patterns without experience. It’s also called <strong>unsupervised learning</strong>. E consists of only features, and P and T are defined in much broader terms of finding ‘interesting patterns’.</li></ul><p>监督学习(supervised learning):</p><ol><li>已知Label是什么,长什么样子,正确答案\结果是什么</li><li>输入输出之间存在较为明确的关系</li><li>比如:房屋面积和房价</li></ol><p>无监督学习(unsupervised learning)</p><ol><li>对Label的定义不是很明确,或者可以理解为很广泛</li><li>通过数据集中原本存在的某种关系进行归类(Clustering)</li><li>比如:<ul><li>对基因组进行归类</li><li>从音频中分出音乐和对话</li></ul></li></ol><h2 id="2-2-Representation-view-on-ML-Pedro-Domingos-2012"><a href="#2-2-Representation-view-on-ML-Pedro-Domingos-2012" class="headerlink" title="2.2. Representation view on ML (Pedro Domingos, 2012)"></a>2.2. Representation view on ML (Pedro Domingos, 2012)</h2><blockquote><p>Machine Learning = Representation + Evaluation + Optimisation</p></blockquote><ul><li>Representation: 表征, a way of describing the problem and data</li><li>Evaluation: 评估,类似于Tom Mitchell提出的measure P</li><li>Optimisation:优化,使预测值接近真实值的算法</li></ul><h2 id="2-3-Classification"><a href="#2-3-Classification" class="headerlink" title="2.3. Classification"></a>2.3. Classification</h2><blockquote><p>Classification is a ML task where T has a discrete set of outcomes.</p></blockquote><p><mjx-container class="MathJax" jax="SVG" display="true"><svg style="vertical-align: -0.566ex" xmlns="http://www.w3.org/2000/svg" width="13.385ex" height="2.262ex" role="img" focusable="false" viewBox="0 -750 5916.1 1000"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mi"><path data-c="210E" d="M137 683Q138 683 209 688T282 694Q294 694 294 685Q294 674 258 534Q220 386 220 383Q220 381 227 388Q288 442 357 442Q411 442 444 415T478 336Q478 285 440 178T402 50Q403 36 407 31T422 26Q450 26 474 56T513 138Q516 149 519 151T535 153Q555 153 555 145Q555 144 551 130Q535 71 500 33Q466 -10 419 -10H414Q367 -10 346 17T325 74Q325 90 361 192T398 345Q398 404 354 404H349Q266 404 205 306L198 293L164 158Q132 28 127 16Q114 -11 83 -11Q69 -11 59 -2T48 16Q48 30 121 320L195 616Q195 629 188 632T149 637H128Q122 643 122 645T124 664Q129 683 137 683Z"></path></g><g data-mml-node="mo" transform="translate(576, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mi" transform="translate(965, 0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="mo" transform="translate(1537, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(2203.8, 0)"><path data-c="2192" d="M56 237T56 250T70 270H835Q719 357 692 493Q692 494 692 496T691 499Q691 511 708 511H711Q720 511 723 510T729 506T732 497T735 481T743 456Q765 389 816 336T935 261Q944 258 944 250Q944 244 939 241T915 231T877 212Q836 186 806 152T761 85T740 35T732 4Q730 -6 727 -8T711 -11Q691 -11 691 0Q691 7 696 25Q728 151 835 230H70Q56 237 56 250Z"></path></g><g data-mml-node="mi" transform="translate(3481.6, 0)"><path data-c="1D466" d="M21 287Q21 301 36 335T84 406T158 442Q199 442 224 419T250 355Q248 336 247 334Q247 331 231 288T198 191T182 105Q182 62 196 45T238 27Q261 27 281 38T312 61T339 94Q339 95 344 114T358 173T377 247Q415 397 419 404Q432 431 462 431Q475 431 483 424T494 412T496 403Q496 390 447 193T391 -23Q363 -106 294 -155T156 -205Q111 -205 77 -183T43 -117Q43 -95 50 -80T69 -58T89 -48T106 -45Q150 -45 150 -87Q150 -107 138 -122T115 -142T102 -147L99 -148Q101 -153 118 -160T152 -167H160Q177 -167 186 -165Q219 -156 247 -127T290 -65T313 -9T321 21L315 17Q309 13 296 6T270 -6Q250 -11 231 -11Q185 -11 150 11T104 82Q103 89 103 113Q103 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mo" transform="translate(4249.3, 0)"><path data-c="2208" d="M84 250Q84 372 166 450T360 539Q361 539 377 539T419 540T469 540H568Q583 532 583 520Q583 511 570 501L466 500Q355 499 329 494Q280 482 242 458T183 409T147 354T129 306T124 272V270H568Q583 262 583 250T568 230H124V228Q124 207 134 177T167 112T231 48T328 7Q355 1 466 0H570Q583 -10 583 -20Q583 -32 568 -40H471Q464 -40 446 -40T417 -41Q262 -41 172 45Q84 127 84 250Z"></path></g><g data-mml-node="TeXAtom" data-mjx-texclass="ORD" transform="translate(5194.1, 0)"><g data-mml-node="mi"><path data-c="2115" d="M20 664Q20 666 31 683H142Q256 683 258 681Q259 680 279 653T342 572T422 468L582 259V425Q582 451 582 490T583 541Q583 611 573 628T522 648Q500 648 493 654Q484 665 493 679L500 683H691Q702 676 702 666Q702 657 698 652Q688 648 680 648Q633 648 627 612Q624 601 624 294V-8Q616 -20 607 -20Q601 -20 596 -15Q593 -13 371 270L156 548L153 319Q153 284 153 234T152 167Q152 103 156 78T172 44T213 34Q236 34 242 28Q253 17 242 3L236 -1H36Q24 6 24 16Q24 34 56 34Q58 35 69 36T86 40T100 50T109 72Q111 83 111 345V603L96 619Q72 643 44 648Q20 648 20 664ZM413 419L240 648H120L136 628Q137 626 361 341T587 54L589 68Q589 78 589 121V192L413 419Z"></path></g></g></g></g></svg></mjx-container></p><ul><li>Often classification is binary: [0,1]</li><li>Examples:<ul><li>face detection</li><li>smile detection</li><li>spam classification</li><li>hot/cold
</li></ul></li></ul><h2 id="2-4-Regression"><a href="#2-4-Regression" class="headerlink" title="2.4. Regression"></a>2.4. Regression</h2><blockquote><p>Regression is a ML task where T has a real-valued outcome on some continuous sub-space:</p></blockquote><p><mjx-container class="MathJax" jax="SVG" display="true"><svg style="vertical-align: -0.566ex" xmlns="http://www.w3.org/2000/svg" width="13.385ex" height="2.262ex" role="img" focusable="false" viewBox="0 -750 5916.1 1000"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mi"><path data-c="210E" d="M137 683Q138 683 209 688T282 694Q294 694 294 685Q294 674 258 534Q220 386 220 383Q220 381 227 388Q288 442 357 442Q411 442 444 415T478 336Q478 285 440 178T402 50Q403 36 407 31T422 26Q450 26 474 56T513 138Q516 149 519 151T535 153Q555 153 555 145Q555 144 551 130Q535 71 500 33Q466 -10 419 -10H414Q367 -10 346 17T325 74Q325 90 361 192T398 345Q398 404 354 404H349Q266 404 205 306L198 293L164 158Q132 28 127 16Q114 -11 83 -11Q69 -11 59 -2T48 16Q48 30 121 320L195 616Q195 629 188 632T149 637H128Q122 643 122 645T124 664Q129 683 137 683Z"></path></g><g data-mml-node="mo" transform="translate(576, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mi" transform="translate(965, 0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="mo" transform="translate(1537, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(2203.8, 0)"><path data-c="2192" d="M56 237T56 250T70 270H835Q719 357 692 493Q692 494 692 496T691 499Q691 511 708 511H711Q720 511 723 510T729 506T732 497T735 481T743 456Q765 389 816 336T935 261Q944 258 944 250Q944 244 939 241T915 231T877 212Q836 186 806 152T761 85T740 35T732 4Q730 -6 727 -8T711 -11Q691 -11 691 0Q691 7 696 25Q728 151 835 230H70Q56 237 56 250Z"></path></g><g data-mml-node="mi" transform="translate(3481.6, 0)"><path data-c="1D466" d="M21 287Q21 301 36 335T84 406T158 442Q199 442 224 419T250 355Q248 336 247 334Q247 331 231 288T198 191T182 105Q182 62 196 45T238 27Q261 27 281 38T312 61T339 94Q339 95 344 114T358 173T377 247Q415 397 419 404Q432 431 462 431Q475 431 483 424T494 412T496 403Q496 390 447 193T391 -23Q363 -106 294 -155T156 -205Q111 -205 77 -183T43 -117Q43 -95 50 -80T69 -58T89 -48T106 -45Q150 -45 150 -87Q150 -107 138 -122T115 -142T102 -147L99 -148Q101 -153 118 -160T152 -167H160Q177 -167 186 -165Q219 -156 247 -127T290 -65T313 -9T321 21L315 17Q309 13 296 6T270 -6Q250 -11 231 -11Q185 -11 150 11T104 82Q103 89 103 113Q103 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mo" transform="translate(4249.3, 0)"><path data-c="2208" d="M84 250Q84 372 166 450T360 539Q361 539 377 539T419 540T469 540H568Q583 532 583 520Q583 511 570 501L466 500Q355 499 329 494Q280 482 242 458T183 409T147 354T129 306T124 272V270H568Q583 262 583 250T568 230H124V228Q124 207 134 177T167 112T231 48T328 7Q355 1 466 0H570Q583 -10 583 -20Q583 -32 568 -40H471Q464 -40 446 -40T417 -41Q262 -41 172 45Q84 127 84 250Z"></path></g><g data-mml-node="TeXAtom" data-mjx-texclass="ORD" transform="translate(5194.1, 0)"><g data-mml-node="mi"><path data-c="211D" d="M17 665Q17 672 28 683H221Q415 681 439 677Q461 673 481 667T516 654T544 639T566 623T584 607T597 592T607 578T614 565T618 554L621 548Q626 530 626 497Q626 447 613 419Q578 348 473 326L455 321Q462 310 473 292T517 226T578 141T637 72T686 35Q705 30 705 16Q705 7 693 -1H510Q503 6 404 159L306 310H268V183Q270 67 271 59Q274 42 291 38Q295 37 319 35Q344 35 353 28Q362 17 353 3L346 -1H28Q16 5 16 16Q16 35 55 35Q96 38 101 52Q106 60 106 341T101 632Q95 645 55 648Q17 648 17 665ZM241 35Q238 42 237 45T235 78T233 163T233 337V621L237 635L244 648H133Q136 641 137 638T139 603T141 517T141 341Q141 131 140 89T134 37Q133 36 133 35H241ZM457 496Q457 540 449 570T425 615T400 634T377 643Q374 643 339 648Q300 648 281 635Q271 628 270 610T268 481V346H284Q327 346 375 352Q421 364 439 392T457 496ZM492 537T492 496T488 427T478 389T469 371T464 361Q464 360 465 360Q469 360 497 370Q593 400 593 495Q593 592 477 630L457 637L461 626Q474 611 488 561Q492 537 492 496ZM464 243Q411 317 410 317Q404 317 401 315Q384 315 370 312H346L526 35H619L606 50Q553 109 464 243Z"></path></g></g></g></g></svg></mjx-container></p><ul><li>Examples:<ul><li>age estimation</li><li>stock value prediction</li><li>temperature prediction</li><li>energy consumption prediction</li></ul></li></ul><h1 id="3-Features-labels-tasks"><a href="#3-Features-labels-tasks" class="headerlink" title="3. Features, labels, tasks"></a>3. Features, labels, tasks</h1><h2 id="3-1-Features-and-Labels"><a href="#3-1-Features-and-Labels" class="headerlink" title="3.1. Features and Labels"></a>3.1. Features and Labels</h2><ul><li>Data points or instances make up the data used to learn a hypothesis h or find a pattern g</li><li>In Machine Learning, a data point consists of feature/label tuples {x, y}</li><li>A single data point comes from one measurement/observation</li><li>Many data points together make a dataset</li></ul><h2 id="3-2-Labels"><a href="#3-2-Labels" class="headerlink" title="3.2. Labels"></a>3.2. Labels</h2><blockquote><p>Labels y are the values that h(x) aims to predict.</p></blockquote><ul><li>Obtaining labels is usually an arduous task<ul><li>Often manual</li><li>Repetitive</li><li>Complicated experiments Difficult to obtain data</li></ul></li><li>Example:<ul><li>Facial expressions of pain</li><li>Impact of diet on astronauts in space</li><li>Predictions of house prices</li></ul></li></ul><h2 id="3-3-Features-Attributes"><a href="#3-3-Features-Attributes" class="headerlink" title="3.3. Features/Attributes"></a>3.3. Features/Attributes</h2><blockquote><p>Features/Attributes are measurable values of variables for which some form of pattern exists, that can be used to infer the associated label y.</p></blockquote><ul><li>Sender domain in spam detection</li><li>Mouth corner location in smile detection Temperature in forest fire prediction</li><li>Pixel value in face detection</li><li>Head pose estimation from facial point locations</li></ul><h2 id="3-4-Features-Definition"><a href="#3-4-Features-Definition" class="headerlink" title="3.4. Features Definition"></a>3.4. Features Definition</h2><ul><li>For a given problem, all data points must have the same, fixed-length(a row-vector with d elements) set of features x :<br><mjx-container class="MathJax" jax="SVG" display="true"><svg style="vertical-align: -0.566ex" xmlns="http://www.w3.org/2000/svg" width="17.766ex" height="2.262ex" role="img" focusable="false" viewBox="0 -750 7852.4 1000"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mi"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="mo" transform="translate(849.8, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mo" transform="translate(1905.6, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="msub" transform="translate(2294.6, 0)"><g data-mml-node="mi"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="TeXAtom" transform="translate(572, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g><g data-mml-node="mo" transform="translate(3270.1, 0)"><path data-c="2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path></g><g data-mml-node="msub" transform="translate(3714.8, 0)"><g data-mml-node="mi"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="TeXAtom" transform="translate(572, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></g></g></g><g data-mml-node="mo" transform="translate(4690.3, 0)"><path data-c="2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path></g><g data-mml-node="mo" transform="translate(5135, 0)"><path data-c="2026" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60ZM525 60Q525 84 542 102T585 120Q609 120 627 104T646 61Q646 36 629 18T586 0T543 17T525 60ZM972 60Q972 84 989 102T1032 120Q1056 120 1074 104T1093 61Q1093 36 1076 18T1033 0T990 17T972 60Z"></path></g><g data-mml-node="msub" transform="translate(6473.7, 0)"><g data-mml-node="mi"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="TeXAtom" transform="translate(572, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D451" d="M366 683Q367 683 438 688T511 694Q523 694 523 686Q523 679 450 384T375 83T374 68Q374 26 402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487H491Q506 153 506 145Q506 140 503 129Q490 79 473 48T445 8T417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157Q33 205 53 255T101 341Q148 398 195 420T280 442Q336 442 364 400Q369 394 369 396Q370 400 396 505T424 616Q424 629 417 632T378 637H357Q351 643 351 645T353 664Q358 683 366 683ZM352 326Q329 405 277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q233 26 290 98L298 109L352 326Z"></path></g></g></g><g data-mml-node="mo" transform="translate(7463.4, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g></g></g></svg></mjx-container></li><li>A dataset with n data points is then denoted as a n x d matrix:<br><mjx-container class="MathJax" jax="SVG" display="true"><svg style="vertical-align: -0.566ex" xmlns="http://www.w3.org/2000/svg" width="19.795ex" height="2.583ex" role="img" focusable="false" viewBox="0 -891.7 8749.5 1141.7"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mi"><path data-c="1D44B" d="M42 0H40Q26 0 26 11Q26 15 29 27Q33 41 36 43T55 46Q141 49 190 98Q200 108 306 224T411 342Q302 620 297 625Q288 636 234 637H206Q200 643 200 645T202 664Q206 677 212 683H226Q260 681 347 681Q380 681 408 681T453 682T473 682Q490 682 490 671Q490 670 488 658Q484 643 481 640T465 637Q434 634 411 620L488 426L541 485Q646 598 646 610Q646 628 622 635Q617 635 609 637Q594 637 594 648Q594 650 596 664Q600 677 606 683H618Q619 683 643 683T697 681T738 680Q828 680 837 683H845Q852 676 852 672Q850 647 840 637H824Q790 636 763 628T722 611T698 593L687 584Q687 585 592 480L505 384Q505 383 536 304T601 142T638 56Q648 47 699 46Q734 46 734 37Q734 35 732 23Q728 7 725 4T711 1Q708 1 678 1T589 2Q528 2 496 2T461 1Q444 1 444 10Q444 11 446 25Q448 35 450 39T455 44T464 46T480 47T506 54Q523 62 523 64Q522 64 476 181L429 299Q241 95 236 84Q232 76 232 72Q232 53 261 47Q262 47 267 47T273 46Q276 46 277 46T280 45T283 42T284 35Q284 26 282 19Q279 6 276 4T261 1Q258 1 243 1T201 2T142 2Q64 2 42 0Z"></path></g><g data-mml-node="mo" transform="translate(1129.8, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mo" transform="translate(2185.6, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="msub" transform="translate(2574.6, 0)"><g data-mml-node="mi"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="TeXAtom" transform="translate(572, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g><g data-mml-node="mo" transform="translate(3550.1, 0)"><path data-c="2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path></g><g data-mml-node="msub" transform="translate(3994.8, 0)"><g data-mml-node="mi"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="TeXAtom" transform="translate(572, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></g></g></g><g data-mml-node="mo" transform="translate(4970.3, 0)"><path data-c="2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path></g><g data-mml-node="mo" transform="translate(5415, 0)"><path data-c="2026" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60ZM525 60Q525 84 542 102T585 120Q609 120 627 104T646 61Q646 36 629 18T586 0T543 17T525 60ZM972 60Q972 84 989 102T1032 120Q1056 120 1074 104T1093 61Q1093 36 1076 18T1033 0T990 17T972 60Z"></path></g><g data-mml-node="msub" transform="translate(6753.7, 0)"><g data-mml-node="mi"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="TeXAtom" transform="translate(572, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g></g></g><g data-mml-node="msup" transform="translate(7799.9, 0)"><g data-mml-node="mo"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="TeXAtom" transform="translate(389, 413) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="TeXAtom" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="54" d="M36 443Q37 448 46 558T55 671V677H666V671Q667 666 676 556T685 443V437H645V443Q645 445 642 478T631 544T610 593Q593 614 555 625Q534 630 478 630H451H443Q417 630 414 618Q413 616 413 339V63Q420 53 439 50T528 46H558V0H545L361 3Q186 1 177 0H164V46H194Q264 46 283 49T309 63V339V550Q309 620 304 625T271 630H244H224Q154 630 119 601Q101 585 93 554T81 486T76 443V437H36V443Z"></path></g></g></g></g></g></g></svg></mjx-container></li></ul><h2 id="3-5-Labels-Definition"><a href="#3-5-Labels-Definition" class="headerlink" title="3.5. Labels Definition"></a>3.5. Labels Definition</h2><p>For a given problem with a singular task, the set of labels y accompanying the set of features X is given as:<br><mjx-container class="MathJax" jax="SVG" display="true"><svg style="vertical-align: -0.566ex" xmlns="http://www.w3.org/2000/svg" width="19.239ex" height="2.583ex" role="img" focusable="false" viewBox="0 -891.7 8503.5 1141.7"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mi"><path data-c="1D44B" d="M42 0H40Q26 0 26 11Q26 15 29 27Q33 41 36 43T55 46Q141 49 190 98Q200 108 306 224T411 342Q302 620 297 625Q288 636 234 637H206Q200 643 200 645T202 664Q206 677 212 683H226Q260 681 347 681Q380 681 408 681T453 682T473 682Q490 682 490 671Q490 670 488 658Q484 643 481 640T465 637Q434 634 411 620L488 426L541 485Q646 598 646 610Q646 628 622 635Q617 635 609 637Q594 637 594 648Q594 650 596 664Q600 677 606 683H618Q619 683 643 683T697 681T738 680Q828 680 837 683H845Q852 676 852 672Q850 647 840 637H824Q790 636 763 628T722 611T698 593L687 584Q687 585 592 480L505 384Q505 383 536 304T601 142T638 56Q648 47 699 46Q734 46 734 37Q734 35 732 23Q728 7 725 4T711 1Q708 1 678 1T589 2Q528 2 496 2T461 1Q444 1 444 10Q444 11 446 25Q448 35 450 39T455 44T464 46T480 47T506 54Q523 62 523 64Q522 64 476 181L429 299Q241 95 236 84Q232 76 232 72Q232 53 261 47Q262 47 267 47T273 46Q276 46 277 46T280 45T283 42T284 35Q284 26 282 19Q279 6 276 4T261 1Q258 1 243 1T201 2T142 2Q64 2 42 0Z"></path></g><g data-mml-node="mo" transform="translate(1129.8, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mo" transform="translate(2185.6, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="msub" transform="translate(2574.6, 0)"><g data-mml-node="mi"><path data-c="1D466" d="M21 287Q21 301 36 335T84 406T158 442Q199 442 224 419T250 355Q248 336 247 334Q247 331 231 288T198 191T182 105Q182 62 196 45T238 27Q261 27 281 38T312 61T339 94Q339 95 344 114T358 173T377 247Q415 397 419 404Q432 431 462 431Q475 431 483 424T494 412T496 403Q496 390 447 193T391 -23Q363 -106 294 -155T156 -205Q111 -205 77 -183T43 -117Q43 -95 50 -80T69 -58T89 -48T106 -45Q150 -45 150 -87Q150 -107 138 -122T115 -142T102 -147L99 -148Q101 -153 118 -160T152 -167H160Q177 -167 186 -165Q219 -156 247 -127T290 -65T313 -9T321 21L315 17Q309 13 296 6T270 -6Q250 -11 231 -11Q185 -11 150 11T104 82Q103 89 103 113Q103 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="TeXAtom" transform="translate(490, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></g><g data-mml-node="mo" transform="translate(3468.1, 0)"><path data-c="2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path></g><g data-mml-node="msub" transform="translate(3912.8, 0)"><g data-mml-node="mi"><path data-c="1D466" d="M21 287Q21 301 36 335T84 406T158 442Q199 442 224 419T250 355Q248 336 247 334Q247 331 231 288T198 191T182 105Q182 62 196 45T238 27Q261 27 281 38T312 61T339 94Q339 95 344 114T358 173T377 247Q415 397 419 404Q432 431 462 431Q475 431 483 424T494 412T496 403Q496 390 447 193T391 -23Q363 -106 294 -155T156 -205Q111 -205 77 -183T43 -117Q43 -95 50 -80T69 -58T89 -48T106 -45Q150 -45 150 -87Q150 -107 138 -122T115 -142T102 -147L99 -148Q101 -153 118 -160T152 -167H160Q177 -167 186 -165Q219 -156 247 -127T290 -65T313 -9T321 21L315 17Q309 13 296 6T270 -6Q250 -11 231 -11Q185 -11 150 11T104 82Q103 89 103 113Q103 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="TeXAtom" transform="translate(490, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></g></g></g><g data-mml-node="mo" transform="translate(4806.3, 0)"><path data-c="2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path></g><g data-mml-node="mo" transform="translate(5251, 0)"><path data-c="2026" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60ZM525 60Q525 84 542 102T585 120Q609 120 627 104T646 61Q646 36 629 18T586 0T543 17T525 60ZM972 60Q972 84 989 102T1032 120Q1056 120 1074 104T1093 61Q1093 36 1076 18T1033 0T990 17T972 60Z"></path></g><g data-mml-node="msub" transform="translate(6589.7, 0)"><g data-mml-node="mi"><path data-c="1D466" d="M21 287Q21 301 36 335T84 406T158 442Q199 442 224 419T250 355Q248 336 247 334Q247 331 231 288T198 191T182 105Q182 62 196 45T238 27Q261 27 281 38T312 61T339 94Q339 95 344 114T358 173T377 247Q415 397 419 404Q432 431 462 431Q475 431 483 424T494 412T496 403Q496 390 447 193T391 -23Q363 -106 294 -155T156 -205Q111 -205 77 -183T43 -117Q43 -95 50 -80T69 -58T89 -48T106 -45Q150 -45 150 -87Q150 -107 138 -122T115 -142T102 -147L99 -148Q101 -153 118 -160T152 -167H160Q177 -167 186 -165Q219 -156 247 -127T290 -65T313 -9T321 21L315 17Q309 13 296 6T270 -6Q250 -11 231 -11Q185 -11 150 11T104 82Q103 89 103 113Q103 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="TeXAtom" transform="translate(490, -150) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g></g></g><g data-mml-node="msup" transform="translate(7553.9, 0)"><g data-mml-node="mo"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="TeXAtom" transform="translate(389, 413) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="TeXAtom" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="54" d="M36 443Q37 448 46 558T55 671V677H666V671Q667 666 676 556T685 443V437H645V443Q645 445 642 478T631 544T610 593Q593 614 555 625Q534 630 478 630H451H443Q417 630 414 618Q413 616 413 339V63Q420 53 439 50T528 46H558V0H545L361 3Q186 1 177 0H164V46H194Q264 46 283 49T309 63V339V550Q309 620 304 625T271 630H244H224Q154 630 119 601Q101 585 93 554T81 486T76 443V437H36V443Z"></path></g></g></g></g></g></g></svg></mjx-container></p><h1 id="4-Linear-Regression-Intro"><a href="#4-Linear-Regression-Intro" class="headerlink" title="4. Linear Regression Intro"></a>4. Linear Regression Intro</h1><h2 id="4-1-Simplest-Example-Latitude-and-Temperature"><a href="#4-1-Simplest-Example-Latitude-and-Temperature" class="headerlink" title="4.1. Simplest Example -Latitude and Temperature"></a>4.1. Simplest Example -Latitude and Temperature</h2><p><img src="SimplestExampleLinearRegression.png" alt="SimplestExampleLinearRegression.png"></p><h2 id="4-2-Training-Set-and-Meaning-of-Symbol"><a href="#4-2-Training-Set-and-Meaning-of-Symbol" class="headerlink" title="4.2. Training Set and Meaning of Symbol"></a>4.2. Training Set and Meaning of Symbol</h2><p><img src="TrainingSetAndMeaningofSymbol.png" alt="TrainingSetAndMeaningofSymbol.png"></p><h2 id="4-3-Learning-Flow"><a href="#4-3-Learning-Flow" class="headerlink" title="4.3. Learning Flow"></a>4.3. Learning Flow</h2><p>Univariate Linear Regression: One feature.<br><img src="LearningFlow.png" alt="LearningFlow.png"></p><h2 id="4-4-Training-Algorithm-Minimises-the-Cost-Function"><a href="#4-4-Training-Algorithm-Minimises-the-Cost-Function" class="headerlink" title="4.4. Training Algorithm - Minimises the Cost Function"></a>4.4. Training Algorithm - Minimises the Cost Function</h2><p>Given a model h with solution space <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.027ex" xmlns="http://www.w3.org/2000/svg" width="1.258ex" height="1.615ex" role="img" focusable="false" viewBox="0 -702 556 714"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="TeXAtom" data-mjx-texclass="ORD"><g data-mml-node="mi"><path data-c="1D54A" d="M54 238Q72 238 72 212Q72 174 106 121Q113 110 132 90T166 59Q221 23 264 23Q315 23 348 41Q368 50 384 79Q393 102 393 129Q393 181 356 219T221 299Q120 343 74 390T28 501Q28 561 55 610Q98 682 212 699Q214 699 231 700T261 701Q309 698 340 687T408 675Q431 678 445 690T465 702Q474 702 481 690V497L477 490Q464 481 450 490Q446 500 446 501Q446 546 386 606T260 666Q215 666 182 639T148 565Q148 528 186 496T319 428Q352 414 370 405T418 379T468 338T506 284Q528 239 528 191Q528 102 456 46T266 -10Q211 -10 176 2T110 15Q86 9 73 -1T53 -12Q44 -12 37 -1V112V182Q37 214 40 226T54 238ZM446 619Q446 648 444 648Q439 646 435 644Q425 644 415 639H404L417 624Q435 606 439 601L446 592V619ZM124 619L128 635Q126 635 108 617Q64 576 64 502Q64 489 65 479T76 449T102 414T150 376T228 335Q335 291 381 245T427 128Q427 94 419 75L415 61Q421 61 448 88Q490 127 490 190Q490 233 475 264Q456 299 430 321Q402 349 369 367T287 404T204 441Q138 481 119 526Q113 544 113 565Q113 596 124 619ZM75 43Q76 43 90 46T110 50H119L106 64L74 101Q72 101 72 72T75 43Z"></path></g></g></g></g></svg></mjx-container> and a training set {X,y}, a learning algorithm finds the solution <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.023ex" xmlns="http://www.w3.org/2000/svg" width="1.257ex" height="1.753ex" role="img" focusable="false" viewBox="0 -765 555.6 775"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="TeXAtom" data-mjx-texclass="ORD"><g data-mml-node="mover"><g data-mml-node="mi" transform="translate(15.5, 0)"><path data-c="1D460" d="M131 289Q131 321 147 354T203 415T300 442Q362 442 390 415T419 355Q419 323 402 308T364 292Q351 292 340 300T328 326Q328 342 337 354T354 372T367 378Q368 378 368 379Q368 382 361 388T336 399T297 405Q249 405 227 379T204 326Q204 301 223 291T278 274T330 259Q396 230 396 163Q396 135 385 107T352 51T289 7T195 -10Q118 -10 86 19T53 87Q53 126 74 143T118 160Q133 160 146 151T160 120Q160 94 142 76T111 58Q109 57 108 57T107 55Q108 52 115 47T146 34T201 27Q237 27 263 38T301 66T318 97T323 122Q323 150 302 164T254 181T195 196T148 231Q131 256 131 289Z"></path></g><g data-mml-node="mo" transform="translate(55.6, -29)"><path data-c="2C6" d="M112 560L249 694L257 686Q387 562 387 560L361 531Q359 532 303 581L250 627L195 580Q182 569 169 557T148 538L140 532Q138 530 125 546L112 560Z"></path></g></g></g></g></g></svg></mjx-container> that minimises the cost function J(S).</p><h2 id="4-5-Intrinsic-Hyper-Parameters"><a href="#4-5-Intrinsic-Hyper-Parameters" class="headerlink" title="4.5. Intrinsic/Hyper Parameters"></a>4.5. Intrinsic/Hyper Parameters</h2><ol><li><p>Intrinsic parameters</p><ul><li>Can be efficiently learned on the training set</li><li>Large in number</li><li>E.g. weights in linear regression or Artificial Neural Network</li><li>固有参数是指模型可以通过数据自动学习出的变量;Eg. 深度学习的权重、偏差</li></ul></li><li><p>Hyper-parameters</p><ul><li>Must be learned by establishing generalisation error</li><li>No efficient search possible</li><li>Smaller in number</li><li>E.g. the number of nodes in an ANN or the degree of a polynomial linear regression model</li><li>超参数是用来确定模型的参数,超参数不同,模型不同,一般是根据经验确定的;Eg. 学习速率,迭代次数,层数,每层神经元的个数等</li></ul></li></ol><h2 id="4-6-Brute-Force-Search-暴力查找"><a href="#4-6-Brute-Force-Search-暴力查找" class="headerlink" title="4.6. Brute Force Search(暴力查找)"></a>4.6. Brute Force Search(暴力查找)</h2><p><mjx-container class="MathJax" jax="SVG" display="true"><svg style="vertical-align: -0.566ex" xmlns="http://www.w3.org/2000/svg" width="32.646ex" height="2.367ex" role="img" focusable="false" viewBox="0 -796 14429.6 1046"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="TeXAtom" data-mjx-texclass="ORD"><g data-mml-node="mover"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="mo" transform="translate(163.3, -48)"><path data-c="2C6" d="M279 669Q273 669 142 610T9 551L0 569Q-8 585 -8 587Q-8 588 -7 588L12 598Q30 608 66 628T136 666L277 744L564 587L555 569Q549 556 547 554T544 552Q539 555 410 612T279 669Z"></path></g></g></g><g data-mml-node="mo" transform="translate(997.1, 0)"><path data-c="3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></g><g data-mml-node="mi" transform="translate(2052.9, 0)"><path data-c="1D44E" d="M33 157Q33 258 109 349T280 441Q331 441 370 392Q386 422 416 422Q429 422 439 414T449 394Q449 381 412 234T374 68Q374 43 381 35T402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487Q506 153 506 144Q506 138 501 117T481 63T449 13Q436 0 417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157ZM351 328Q351 334 346 350T323 385T277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q217 26 254 59T298 110Q300 114 325 217T351 328Z"></path></g><g data-mml-node="mi" transform="translate(2581.9, 0)"><path data-c="1D45F" d="M21 287Q22 290 23 295T28 317T38 348T53 381T73 411T99 433T132 442Q161 442 183 430T214 408T225 388Q227 382 228 382T236 389Q284 441 347 441H350Q398 441 422 400Q430 381 430 363Q430 333 417 315T391 292T366 288Q346 288 334 299T322 328Q322 376 378 392Q356 405 342 405Q286 405 239 331Q229 315 224 298T190 165Q156 25 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 114 189T154 366Q154 405 128 405Q107 405 92 377T68 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mi" transform="translate(3032.9, 0)"><path data-c="1D454" d="M311 43Q296 30 267 15T206 0Q143 0 105 45T66 160Q66 265 143 353T314 442Q361 442 401 394L404 398Q406 401 409 404T418 412T431 419T447 422Q461 422 470 413T480 394Q480 379 423 152T363 -80Q345 -134 286 -169T151 -205Q10 -205 10 -137Q10 -111 28 -91T74 -71Q89 -71 102 -80T116 -111Q116 -121 114 -130T107 -144T99 -154T92 -162L90 -164H91Q101 -167 151 -167Q189 -167 211 -155Q234 -144 254 -122T282 -75Q288 -56 298 -13Q311 35 311 43ZM384 328L380 339Q377 350 375 354T369 368T359 382T346 393T328 402T306 405Q262 405 221 352Q191 313 171 233T151 117Q151 38 213 38Q269 38 323 108L331 118L384 328Z"></path></g><g data-mml-node="mi" transform="translate(3509.9, 0)"><path data-c="1D45A" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mi" transform="translate(4387.9, 0)"><path data-c="1D456" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mi" transform="translate(4732.9, 0)"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mo" transform="translate(5332.9, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mi" transform="translate(5721.9, 0)"><path data-c="1D43D" d="M447 625Q447 637 354 637H329Q323 642 323 645T325 664Q329 677 335 683H352Q393 681 498 681Q541 681 568 681T605 682T619 682Q633 682 633 672Q633 670 630 658Q626 642 623 640T604 637Q552 637 545 623Q541 610 483 376Q420 128 419 127Q397 64 333 21T195 -22Q137 -22 97 8T57 88Q57 130 80 152T132 174Q177 174 182 130Q182 98 164 80T123 56Q115 54 115 53T122 44Q148 15 197 15Q235 15 271 47T324 130Q328 142 387 380T447 625Z"></path></g><g data-mml-node="mo" transform="translate(6354.9, 0)"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mi" transform="translate(6743.9, 0)"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="mo" transform="translate(7459.9, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(7848.9, 0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(8237.9, 0)"><path data-c="2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path></g><g data-mml-node="mtext" transform="translate(8682.5, 0)"><path data-c="A0" d=""></path></g><g data-mml-node="TeXAtom" data-mjx-texclass="ORD" transform="translate(8932.5, 0)"><g data-mml-node="mover"><g data-mml-node="mi"><path data-c="1D464" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z"></path></g><g data-mml-node="mo" transform="translate(163.3, -48)"><path data-c="2C6" d="M279 669Q273 669 142 610T9 551L0 569Q-8 585 -8 587Q-8 588 -7 588L12 598Q30 608 66 628T136 666L277 744L564 587L555 569Q549 556 547 554T544 552Q539 555 410 612T279 669Z"></path></g></g></g><g data-mml-node="mo" transform="translate(9929.6, 0)"><text data-variant="normal" transform="matrix(1 0 0 -1 0 0)" font-size="884px" font-family="serif">表</text><text data-variant="normal" transform="translate(900, 0) matrix(1 0 0 -1 0 0)" font-size="884px" font-family="serif">示</text><text data-variant="normal" transform="translate(1800, 0) matrix(1 0 0 -1 0 0)" font-size="884px" font-family="serif">参</text><text data-variant="normal" transform="translate(2700, 0) matrix(1 0 0 -1 0 0)" font-size="884px" font-family="serif">数</text><text data-variant="normal" transform="translate(3600, 0) matrix(1 0 0 -1 0 0)" font-size="884px" font-family="serif">集</text></g></g></g></svg></mjx-container><br>Note-思考问题:</p><ul><li>You can’t do this for hyper-parameters using the above formulation (why not?)</li><li>Clearly you can’t search all possible values (why not?)</li><li>This is a very small formula, but there are some hidden caveats. Can you write matlab/pseudo code for this?</li></ul><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><h1 id="1-What-is-Machine-Learning"><a href="#1-What-is-Machine-Learning" class="headerlink" title="1. What is Machine Learning?"></a>1. What is Machine Learning?</h1><h2 id="1-1-Definition"><a href="#1-1-Definition" class="headerlink" title="1.1. Definition"></a>1.1. Definition</h2><ul>
<li>Arthur Samuel (1959): Field of study that gives computers the ability to learn without being explicitly programmed. <strong>&lt;&lt;一个较为宏观的定义&gt;&gt;</strong></li>
<li>Tom Mitchell (1998).Well-posed Learning Problem:A computer program is said to learn from <strong>experience E</strong> with respect to some <strong>task T</strong> and some <strong>performance measure P</strong>, if its performance on T, as measured by P, improves with experience E. <strong>&lt;&lt;一个较为practical的定义, 难点在于如何辨别一个problem中的T/P/E&gt;&gt;</strong></li>
</ul></summary>
<category term="UoN" scheme="https://aaronos0.github.io/categories/UoN/"/>
<category term="COMP3009-Machine Learning" scheme="https://aaronos0.github.io/tags/COMP3009-Machine-Learning/"/>
</entry>
<entry>
<title>COMP4035 - 1.Basics</title>
<link href="https://aaronos0.github.io/2020/10/08/COMP4035-1-Basics/"/>
<id>https://aaronos0.github.io/2020/10/08/COMP4035-1-Basics/</id>
<published>2020-10-08T11:17:57.000Z</published>
<updated>2020-10-08T11:17:57.000Z</updated>
<content type="html"><![CDATA[<h1 id="1-Challenges-of-Computer-Networks"><a href="#1-Challenges-of-Computer-Networks" class="headerlink" title="1. Challenges of Computer Networks"></a>1. Challenges of Computer Networks</h1><h2 id="1-1-What-is-a-Computer-Network"><a href="#1-1-What-is-a-Computer-Network" class="headerlink" title="1.1.What is a Computer Network?"></a>1.1.What is a Computer Network?</h2><p>Wikipedia: A computer network is a group of computers that use a set of common communication protocols over digital interconnections for the purpose of sharing resources located on or provided by the network nodes.</p><h2 id="1-2-Classifying-Networks"><a href="#1-2-Classifying-Networks" class="headerlink" title="1.2. Classifying Networks"></a>1.2. Classifying Networks</h2><ul><li>By size: Local area networks (LANs) versus Wide area networks (WANs)</li><li>By connectivity: Point to point versus broadcast networks</li><li>By communication medium: Wired, wireless, satellite, Bluetooth, even your TV remote control!!</li><li>By mobility: Fixed versus mobile</li></ul><span id="more"></span><h2 id="1-3-Top-level-challenges"><a href="#1-3-Top-level-challenges" class="headerlink" title="1.3. Top level challenges"></a>1.3. Top level challenges</h2><ul><li>Security!</li><li>Problem determination/isolation</li><li>Heterogeneity</li><li>Providing a unified service environment</li></ul><h1 id="2-Challenges-of-sending-data"><a href="#2-Challenges-of-sending-data" class="headerlink" title="2. Challenges of sending data"></a>2. Challenges of sending data</h1><h2 id="2-1-Common-Issues-in-Networking"><a href="#2-1-Common-Issues-in-Networking" class="headerlink" title="2.1. Common Issues in Networking"></a>2.1. Common Issues in Networking</h2><ul><li>Framing and encoding</li><li>Addressing</li><li>Routing</li><li>Error detection and correction</li><li>Flow and congestion</li></ul><h1 id="3-Distributed-Systems"><a href="#3-Distributed-Systems" class="headerlink" title="3. Distributed Systems"></a>3. Distributed Systems</h1><h2 id="3-1-Definition"><a href="#3-1-Definition" class="headerlink" title="3.1. Definition"></a>3.1. Definition</h2><p>Wikipedia: A distributed system is a system whose components are located on different networked computers, which communicate and coordinate their actions by passing messages to one another. The components interact with one another in order to achieve a common goal.</p><h2 id="3-2-Advantages"><a href="#3-2-Advantages" class="headerlink" title="3.2. Advantages"></a>3.2. Advantages</h2><ul><li>Access to remote resources (sharing)</li><li>Access to computing power that you don’t have locally</li><li>Scalability</li><li>Robustness (duplication of resources)</li></ul><h2 id="3-3-What-is-IP-address"><a href="#3-3-What-is-IP-address" class="headerlink" title="3.3. What is IP address?"></a>3.3. What is IP address?</h2><p>Wikipedia: An Internet Protocol address is a numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication.</p><h2 id="3-4-Types-of-Address"><a href="#3-4-Types-of-Address" class="headerlink" title="3.4. Types of Address"></a>3.4. Types of Address</h2><ul><li>Hostname address, e.g. severn.cs.nott.ac.uk</li><li>IP address, e.g. 128.243.10.4</li><li>Hardware address, e.g. 00-8C-39-7C-B3-14</li></ul><h2 id="3-5-Identifying-Applications"><a href="#3-5-Identifying-Applications" class="headerlink" title="3.5. Identifying Applications"></a>3.5. Identifying Applications</h2><p>Port Numbers:</p><ul><li>80/443</li><li>8080 …</li></ul><h2 id="3-6-IP-Addresses-Sending-to-Many"><a href="#3-6-IP-Addresses-Sending-to-Many" class="headerlink" title="3.6. IP Addresses - Sending to Many"></a>3.6. IP Addresses - Sending to Many</h2><p>There are special IP addresses for sending to more than one host (computer):</p><ul><li>A Multicast address allows a message to be sent to a group</li><li>A Broadcast address allows a message to be sent to all hosts on a network</li></ul><h2 id="3-7-Types-of-IP-V4-Addresses"><a href="#3-7-Types-of-IP-V4-Addresses" class="headerlink" title="3.7. Types of IP(V4) Addresses"></a>3.7. Types of IP(V4) Addresses</h2><table><thead><tr><th>Address Type</th><th>Meaning and Packet Delivery</th></tr></thead><tbody><tr><td>Unicast</td><td>Uniquely identifies a single computer, and specifies that only the identified computer should receive a copy of the packet</td></tr><tr><td>Broadcast</td><td>Corresponds to all computers, and specifies that each computer on the network should receive a copy of the packet</td></tr><tr><td>Multicast</td><td>Identifies a subset of the computers on a given network, and specifies that each computer in the subset should receive a copy of the packet</td></tr></tbody></table><h1 id="4-Introduction-of-Unix"><a href="#4-Introduction-of-Unix" class="headerlink" title="4. Introduction of Unix"></a>4. Introduction of Unix</h1><h2 id="4-1-Operating-Systems"><a href="#4-1-Operating-Systems" class="headerlink" title="4.1. Operating Systems"></a>4.1. Operating Systems</h2><ul><li>The program that controls all the other parts of the computer system, manages hardware and software</li><li>Enables you to use the computer system and its facilities</li></ul><h2 id="4-2-What-does-the-Kernel-do"><a href="#4-2-What-does-the-Kernel-do" class="headerlink" title="4.2. What does the Kernel do?"></a>4.2. What does the Kernel do?</h2><ul><li>Managing the machine’s memory and allocating it to each process</li><li>Scheduling the work done by the CPU so that the work of each user is carried out as efficiently as possible</li><li>Organising the transfer of data from one part of the machine to another</li><li>Accepting instructions from the user interface and carrying them out.</li><li>Enforcing the access permissions that are set on the file system</li></ul><h2 id="4-3-Command-involved"><a href="#4-3-Command-involved" class="headerlink" title="4.3. Command involved"></a>4.3. Command involved</h2><ul><li>ssh</li><li>sftp</li><li>passwd</li></ul><h1 id="5-Unix-Filestore"><a href="#5-Unix-Filestore" class="headerlink" title="5. Unix Filestore"></a>5. Unix Filestore</h1><h2 id="5-1-Files"><a href="#5-1-Files" class="headerlink" title="5.1. Files"></a>5.1. Files</h2><ul><li>From the user’s point of view, all information on the computer is stored in files</li><li>Files may contain many kinds of information, including programs, data and documents</li><li>Like paper files, they have a name (chosen by the user) and some content</li><li>By convention, the filename suffix suggests the type of content</li></ul><h2 id="5-2-Directories"><a href="#5-2-Directories" class="headerlink" title="5.2. Directories"></a>5.2. Directories</h2><ul><li>Files are stored in directories (folders)</li><li>Each directory may contain many files and also other directories</li><li>By convention, directory names do not usually have suffixes</li></ul><h2 id="5-3-Command-involved"><a href="#5-3-Command-involved" class="headerlink" title="5.3. Command involved"></a>5.3. Command involved</h2><ul><li>cat</li><li>more\less</li><li>head\tail</li></ul><h1 id="6-Absolute-and-Relative-Pathnames"><a href="#6-Absolute-and-Relative-Pathnames" class="headerlink" title="6. Absolute and Relative Pathnames"></a>6. Absolute and Relative Pathnames</h1><h2 id="6-1-Two-ways-of-specifying-filenames"><a href="#6-1-Two-ways-of-specifying-filenames" class="headerlink" title="6.1. Two ways of specifying filenames"></a>6.1. Two ways of specifying filenames</h2><ul><li>From the current directory - relative pathnames</li><li>From the root directory - absolute pathnames</li></ul><h2 id="6-2-Command-involved"><a href="#6-2-Command-involved" class="headerlink" title="6.2. Command involved"></a>6.2. Command involved</h2><ul><li>pwd</li><li>cd</li><li>ls</li><li>mv</li><li>cp</li><li>rm</li><li>mkdir</li><li>rmdir</li></ul><h1 id="Lab1"><a href="#Lab1" class="headerlink" title="Lab1"></a>Lab1</h1><div class="pdf" target="UnixLab1-2020.pdf" height=""></div><hr><h1 id="7-Unix-File-Operations"><a href="#7-Unix-File-Operations" class="headerlink" title="7. Unix File Operations"></a>7. Unix File Operations</h1><h2 id="7-1-UNIX-thinks-everything-is-a-file"><a href="#7-1-UNIX-thinks-everything-is-a-file" class="headerlink" title="7.1. UNIX thinks everything is a file!"></a>7.1. UNIX thinks everything is a file!</h2><ul><li>Directories and programs are all files!</li><li>Input and Output channels are files</li><li>All of these things can be manipulated like files</li></ul><h2 id="7-2-Shell-Metacharacters"><a href="#7-2-Shell-Metacharacters" class="headerlink" title="7.2. Shell Metacharacters"></a>7.2. Shell Metacharacters</h2><ul><li>*</li><li>?</li></ul><h2 id="7-3-Command-involved"><a href="#7-3-Command-involved" class="headerlink" title="7.3. Command involved"></a>7.3. Command involved</h2><ul><li>file</li><li>wc</li></ul><h1 id="8-Security-and-File-Permissions"><a href="#8-Security-and-File-Permissions" class="headerlink" title="8. Security and File Permissions"></a>8. Security and File Permissions</h1><h2 id="8-1-Permissions-lists"><a href="#8-1-Permissions-lists" class="headerlink" title="8.1. Permissions lists"></a>8.1. Permissions lists</h2><ul><li>ugo</li><li>rwx == 421</li></ul><h2 id="8-2-The-effect-of-directory-permissions"><a href="#8-2-The-effect-of-directory-permissions" class="headerlink" title="8.2. The effect of directory permissions"></a>8.2. The effect of directory permissions</h2><ul><li>Execute lets you change (cd) into that directory<ul><li>(i.e. you are searching the directory)</li></ul></li><li>Read lets you list files in the directory<ul><li>(i.e. you are reading the directory contents)</li></ul></li><li>Write lets you create and delete files<ul><li>(i.e. you are writing to the directory contents)</li></ul></li></ul><h2 id="8-3-Command-involved"><a href="#8-3-Command-involved" class="headerlink" title="8.3. Command involved"></a>8.3. Command involved</h2><ul><li>chmod</li></ul><h1 id="9-Unix-Input-Output"><a href="#9-Unix-Input-Output" class="headerlink" title="9. Unix Input/Output"></a>9. Unix Input/Output</h1><h2 id="9-1-Input-and-Output-in-UNIX"><a href="#9-1-Input-and-Output-in-UNIX" class="headerlink" title="9.1. Input and Output in UNIX"></a>9.1. Input and Output in UNIX</h2><blockquote><p>UNIX considers input and output to programs to be “streams of data”.</p></blockquote><ul><li>Could be from the user</li><li>Could be from a file</li><li>Could be from another program</li></ul><h2 id="9-2-Redirecting-I-O"><a href="#9-2-Redirecting-I-O" class="headerlink" title="9.2.Redirecting I/O"></a>9.2.Redirecting I/O</h2><blockquote><p>Each UNIX command has a number of input and output channels, these are files that are specified in arguments.</p></blockquote><ul><li>STDIN (0)</li><li>STDOUT (1)</li><li>STDERR (2)</li></ul><h2 id="9-3-STDIN"><a href="#9-3-STDIN" class="headerlink" title="9.3. STDIN"></a>9.3. STDIN</h2><ul><li>Stands for Standard Input</li><li>This is where programs expect to find their input</li><li>Everything you type at the command line goes onto STDIN</li><li>If you want to read the input from a file instead, use <</li></ul><h2 id="9-4-STDOUT"><a href="#9-4-STDOUT" class="headerlink" title="9.4. STDOUT"></a>9.4. STDOUT</h2><ul><li>Stands for standard output</li><li>This is where programs (usually) write any output they generate. By default STDOUT appears in your terminal window</li><li>If you want to save the output to a file instead, use ></li><li>You can also use >> which appends the output onto a file’s contents</li></ul><h2 id="9-5-STDERR"><a href="#9-5-STDERR" class="headerlink" title="9.5. STDERR"></a>9.5. STDERR</h2><ul><li>Stands for standard error</li><li>This is where programs usually write error messages. So even if you are redirecting the normal output, you can still see error messages</li><li>You can redirect STDERR using 2></li></ul><h2 id="9-6-Redirecting-symbol"><a href="#9-6-Redirecting-symbol" class="headerlink" title="9.6. Redirecting symbol"></a>9.6. Redirecting symbol</h2><table><thead><tr><th>Symbol</th><th>Meaning</th></tr></thead><tbody><tr><td>></td><td>redirect STDOUT</td></tr><tr><td>>></td><td>appends</td></tr><tr><td>2>&1</td><td>redirect both(STDOUT&STDERR) outputs</td></tr><tr><td><</td><td>redirects the standard input</td></tr></tbody></table><h2 id="9-7-Piping"><a href="#9-7-Piping" class="headerlink" title="9.7. Piping"></a>9.7. Piping</h2><ul><li>|</li></ul><h1 id="10-Searching-and-RegularExpressions"><a href="#10-Searching-and-RegularExpressions" class="headerlink" title="10. Searching and RegularExpressions"></a>10. Searching and RegularExpressions</h1><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><h1 id="1-Challenges-of-Computer-Networks"><a href="#1-Challenges-of-Computer-Networks" class="headerlink" title="1. Challenges of Computer Networks"></a>1. Challenges of Computer Networks</h1><h2 id="1-1-What-is-a-Computer-Network"><a href="#1-1-What-is-a-Computer-Network" class="headerlink" title="1.1.What is a Computer Network?"></a>1.1.What is a Computer Network?</h2><p>Wikipedia: A computer network is a group of computers that use a set of common communication protocols over digital interconnections for the purpose of sharing resources located on or provided by the network nodes.</p>
<h2 id="1-2-Classifying-Networks"><a href="#1-2-Classifying-Networks" class="headerlink" title="1.2. Classifying Networks"></a>1.2. Classifying Networks</h2><ul>
<li>By size: Local area networks (LANs) versus Wide area networks (WANs)</li>
<li>By connectivity: Point to point versus broadcast networks</li>
<li>By communication medium: Wired, wireless, satellite, Bluetooth, even your TV remote control!!</li>
<li>By mobility: Fixed versus mobile</li>
</ul></summary>
<category term="UoN" scheme="https://aaronos0.github.io/categories/UoN/"/>
<category term="COMP4035-System&Network" scheme="https://aaronos0.github.io/tags/COMP4035-System-Network/"/>
</entry>
<entry>
<title>COMP3009 - 0.Module Overview</title>
<link href="https://aaronos0.github.io/2020/10/08/COMP3009-0-Module-Overview/"/>
<id>https://aaronos0.github.io/2020/10/08/COMP3009-0-Module-Overview/</id>
<published>2020-10-07T23:04:42.000Z</published>
<updated>2020-10-07T23:04:42.000Z</updated>
<content type="html"><![CDATA[<h1 id="1-Teaching"><a href="#1-Teaching" class="headerlink" title="1. Teaching"></a>1. Teaching</h1><h2 id="1-1-Theoretical-Part"><a href="#1-1-Theoretical-Part" class="headerlink" title="1.1. Theoretical Part:"></a>1.1. Theoretical Part:</h2><ul><li>2 x 2 hours/week online videos</li><li>lecture activities</li></ul><h2 id="1-2-Practical-Part-Lab-and-3-group-Coursework"><a href="#1-2-Practical-Part-Lab-and-3-group-Coursework" class="headerlink" title="1.2. Practical Part: Lab and 3 group Coursework"></a>1.2. Practical Part: Lab and 3 group Coursework</h2><ul><li>ANN - Artifical Neural Networks</li><li>Decision Trees</li><li>Support Vector Machines</li></ul><span id="more"></span><h1 id="2-Assessment"><a href="#2-Assessment" class="headerlink" title="2. Assessment"></a>2. Assessment</h1><ul><li>2h Exam(70%)<ul><li>Contents from all lectures examinable, as well as the relevant chapters in the two core text (Bishop and Duda & Hart)</li><li>Guest lectures can be used as examples but not examinable in detail.</li><li>Three questions - no optional question</li></ul></li><li>One coursework (30%)<ul><li>Reports on 3 Lab topics</li><li>Peer-review evaluation</li></ul></li></ul><h1 id="3-Resources"><a href="#3-Resources" class="headerlink" title="3. Resources"></a>3. Resources</h1><ul><li><p>Core text:</p><ul><li>Machine Learning & Pattern Recognition (Christopher Bishop)<div class="pdf" target="Bishop-PatternRecognitionAndMachineLearning-Springer2006.pdf" height=""></div></li></ul></li><li><p>Additional Reading:</p><ul><li>Machine Learning (Tom Mitchell)</li><li>Pattern Classification (Duda & Hart)</li></ul></li></ul><h1 id="4-Structure"><a href="#4-Structure" class="headerlink" title="4. Structure"></a>4. Structure</h1><ol><li>Introduction</li><li>Linear Regression</li><li>Mathematics for Machine Learning</li><li>Linear Classification</li><li>Decision Trees</li><li>Artificial Neural Networks I</li><li>Artificial Neural Networks II</li><li>Deep Learning and Convolutional Neural Networks</li><li>Evaluating Hypotheses</li><li>Unsupervised Learning</li><li>Data mining</li><li>Dimensionality Reduction</li><li>Kernel Methods</li><li>Support Vector Machines</li><li>Bayesian Learning</li><li>Probabilistic Graphical Models</li><li>Sequential Data</li><li>Advanced Topics</li><li>Exam Revision</li></ol><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><h1 id="1-Teaching"><a href="#1-Teaching" class="headerlink" title="1. Teaching"></a>1. Teaching</h1><h2 id="1-1-Theoretical-Part"><a href="#1-1-Theoretical-Part" class="headerlink" title="1.1. Theoretical Part:"></a>1.1. Theoretical Part:</h2><ul>
<li>2 x 2 hours/week online videos</li>
<li>lecture activities</li>
</ul>
<h2 id="1-2-Practical-Part-Lab-and-3-group-Coursework"><a href="#1-2-Practical-Part-Lab-and-3-group-Coursework" class="headerlink" title="1.2. Practical Part: Lab and 3 group Coursework"></a>1.2. Practical Part: Lab and 3 group Coursework</h2><ul>
<li>ANN - Artifical Neural Networks</li>
<li>Decision Trees</li>
<li>Support Vector Machines</li>
</ul></summary>
<category term="UoN" scheme="https://aaronos0.github.io/categories/UoN/"/>
<category term="COMP3009-Machine Learning" scheme="https://aaronos0.github.io/tags/COMP3009-Machine-Learning/"/>
</entry>
<entry>
<title>COMP4035 - 0.Module Overview</title>
<link href="https://aaronos0.github.io/2020/10/08/COMP4035-0-Module-Overview/"/>
<id>https://aaronos0.github.io/2020/10/08/COMP4035-0-Module-Overview/</id>
<published>2020-10-07T23:04:21.000Z</published>
<updated>2020-10-07T23:04:21.000Z</updated>
<content type="html"><![CDATA[<h1 id="1-Teaching"><a href="#1-Teaching" class="headerlink" title="1. Teaching"></a>1. Teaching</h1><ul><li>20 credit Module</li><li>3 sessions per week – Lecture engagement<ul><li>Videos</li><li>Online lectures</li><li>Moodle Quizzes</li><li>Some live sessions</li></ul></li><li>1 workshop per week<ul><li>UNIX labs (Online)</li><li>Small group teaching/discussions</li><li>New material</li><li>Other activities</li></ul></li></ul><span id="more"></span><h1 id="2-Assessment"><a href="#2-Assessment" class="headerlink" title="2. Assessment"></a>2. Assessment</h1><ul><li>One exam – take home (75%)</li><li>One coursework (25%)</li></ul><h1 id="3-Structure"><a href="#3-Structure" class="headerlink" title="3. Structure"></a>3. Structure</h1><ol><li>Basics<ul><li>Introduction</li><li>Fundamentals</li><li>Unix Filestore</li><li>WORKSHOP – UNIX lab (Online)</li><li>Unix and I/O (Input/Output) Redirecting</li><li>Searching and Regular Expressions</li><li>Processes and Signals</li></ul></li><li>Data Communication</li><li>Packet Switching and Network Technologies</li><li>Internetworking</li><li>System Administration</li><li>Sharing Resources/Distributed Systems</li></ol><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><h1 id="1-Teaching"><a href="#1-Teaching" class="headerlink" title="1. Teaching"></a>1. Teaching</h1><ul>
<li>20 credit Module</li>
<li>3 sessions per week – Lecture engagement<ul>
<li>Videos</li>
<li>Online lectures</li>
<li>Moodle Quizzes</li>
<li>Some live sessions</li>
</ul>
</li>
<li>1 workshop per week<ul>
<li>UNIX labs (Online)</li>
<li>Small group teaching/discussions</li>
<li>New material</li>
<li>Other activities</li>
</ul>
</li>
</ul></summary>
<category term="UoN" scheme="https://aaronos0.github.io/categories/UoN/"/>
<category term="COMP4035-System&Network" scheme="https://aaronos0.github.io/tags/COMP4035-System-Network/"/>
</entry>
<entry>
<title>COMP4039 - 1.Databases</title>
<link href="https://aaronos0.github.io/2020/10/07/COMP4039-1-Databases/"/>
<id>https://aaronos0.github.io/2020/10/07/COMP4039-1-Databases/</id>
<published>2020-10-07T22:58:43.000Z</published>
<updated>2020-10-07T22:58:43.000Z</updated>
<content type="html"><![CDATA[<h1 id="1-Wkiipedia-Definition"><a href="#1-Wkiipedia-Definition" class="headerlink" title="1. Wkiipedia Definition"></a>1. Wkiipedia Definition</h1><p>A database is an organized collection of data, generally stored and accessed electronically from a computer system.</p><h1 id="2-Programming-data-structures-and-databases"><a href="#2-Programming-data-structures-and-databases" class="headerlink" title="2. Programming data structures and databases"></a>2. Programming data structures and databases</h1><ul><li>You have to find a way to map data structures in your programming language to data structures held by the (relational) database.</li><li>OO data structure <——> DBs</li></ul><span id="more"></span><h1 id="3-The-world’s-simplest-databases"><a href="#3-The-world’s-simplest-databases" class="headerlink" title="3. The world’s simplest databases"></a>3. The world’s simplest databases</h1><h2 id="3-1-File-based-databases"><a href="#3-1-File-based-databases" class="headerlink" title="3.1. File-based databases"></a>3.1. File-based databases</h2><ul><li>Linux configuration files: /etc/xxx</li><li>DNS/Network configuration files …</li></ul><h2 id="3-2-Problems"><a href="#3-2-Problems" class="headerlink" title="3.2. Problems"></a>3.2. Problems</h2><ul><li>No standards</li><li>Data duplication</li><li>Data dependence</li><li>No way to generate complex queries (e.g., JOIN)</li><li>No provision for concurrency, security, recovery, etc.</li></ul><h1 id="4-DBMS-Database-Management-Systems"><a href="#4-DBMS-Database-Management-Systems" class="headerlink" title="4. DBMS - Database Management Systems"></a>4. DBMS - Database Management Systems</h1><h2 id="4-1-Definition"><a href="#4-1-Definition" class="headerlink" title="4.1. Definition"></a>4.1. Definition</h2><blockquote><p>A database is a system to allow multiple users to share the same information<br>A Database Management System (DBMS) is the software that implements a database</p></blockquote><ul><li>a collaborative system</li><li>uniqueness</li><li>data integrity/consistency</li></ul><h2 id="4-2-What-a-DBMS-does"><a href="#4-2-What-a-DBMS-does" class="headerlink" title="4.2. What a DBMS does?"></a>4.2. What a DBMS does?</h2><ul><li>store data</li><li>manage change (updates)</li><li>organise data</li><li>retrieve data</li><li>retain privacy (security)</li><li>ensure reliability & consistency (no redundancy, transactional model, etc.)</li></ul><h2 id="4-3-What-a-DBMS-provides"><a href="#4-3-What-a-DBMS-provides" class="headerlink" title="4.3. What a DBMS provides?"></a>4.3. What a DBMS provides?</h2><blockquote><p>SQL=(DDL, DML, DCL)</p></blockquote><ul><li>Data Definition Language (DDL)<ul><li>e.g., ‘CREATE’ table</li></ul></li><li>Data Manipulation Language (DML)<ul><li>e.g., ‘INSERT’ row</li></ul></li><li>Data Control Language (DCL)<ul><li>e.g., ‘GRANT’ privilege on table to user</li></ul></li></ul><h2 id="4-4-DBMS-components"><a href="#4-4-DBMS-components" class="headerlink" title="4.4. DBMS components"></a>4.4. DBMS components</h2><ul><li>Data dictionary: stores information about database objects</li><li>Data dictionary compiler: extracts database information and stores it in the data dictionary</li><li>Query processor: parses / interprets and optimises user queries (caching?)</li><li>Transaction manager: communicates with recovery manager and scheduler to ensure data consistency in multi-user environments</li><li>Storage manager: deals with concrete file I/O etc.</li></ul><h2 id="4-5-ANSI-SPARC-architecture"><a href="#4-5-ANSI-SPARC-architecture" class="headerlink" title="4.5. ANSI / SPARC architecture"></a>4.5. ANSI / SPARC architecture</h2><p>Three tier architecture:</p><ul><li>internal level/schema: For systems designers (OS, file system level , provides platform independent service for conceptual level)</li><li>Conceptual level/schema: For database designers (structure for all users, abstracted from internal level)</li><li>External level/schema: For database users (user view, subject to access permissions, multiple possible views)<br>Not fully implemented in any DBMS: Eg. external level blended with conceptual level (MySQL)</li></ul><h2 id="4-6-Database-systems-programmers"><a href="#4-6-Database-systems-programmers" class="headerlink" title="4.6. Database systems programmers"></a>4.6. Database systems programmers</h2><ul><li>Database systems programmers: writes the DBMS software</li><li>Database administrators (DBA): designs and manages the database system</li><li>Applications developers: write the client software that allows end users to interface with the DBMS</li><li>End users: use the information provided by the database to achieve a personal or organisational goal</li></ul><h1 id="5-Lab-Exercise"><a href="#5-Lab-Exercise" class="headerlink" title="5. Lab Exercise"></a>5. Lab Exercise</h1><h2 id="5-1-Exercise-Guide"><a href="#5-1-Exercise-Guide" class="headerlink" title="5.1. Exercise Guide"></a>5.1. Exercise Guide</h2><div class="pdf" target="lab-1.pdf" height=""></div><ul><li><a href="lab-1-countries-data.csv">Download lab-1-countries-data.csv</a></li></ul><h2 id="5-2-My-Answer"><a href="#5-2-My-Answer" class="headerlink" title="5.2. My Answer"></a>5.2. My Answer</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- Create Database</span></span><br><span class="line"><span class="keyword">CREATE</span> DATABASE COMP4039;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- Use Database</span></span><br><span class="line">USE COMP4039;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- Create Table</span></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> countries (</span><br><span class="line"> countries_name <span class="type">VARCHAR</span>(<span class="number">50</span>),</span><br><span class="line"> countries_area <span class="type">INT</span>,</span><br><span class="line"> countries_population <span class="type">INT</span>,</span><br><span class="line"> countries_gdp <span class="type">BIGINT</span>(<span class="number">20</span>),</span><br><span class="line"> countries_currency <span class="type">VARCHAR</span>(<span class="number">20</span>),</span><br><span class="line"> countries_capital <span class="type">VARCHAR</span>(<span class="number">50</span>),</span><br><span class="line"> ountries_tld <span class="type">VARCHAR</span>(<span class="number">10</span>)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 1. Print the GDP of all countries in the database.</span></span><br><span class="line"><span class="keyword">SELECT</span> countries_name, countries_gdp</span><br><span class="line"><span class="keyword">FROM</span> COMP4039.countries;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 2. Print the GDP of all countries,in descending order.</span></span><br><span class="line"><span class="keyword">SELECT</span> countries_name, countries_gdp</span><br><span class="line"><span class="keyword">FROM</span> COMP4039.countries</span><br><span class="line"><span class="keyword">ORDER</span> <span class="keyword">BY</span> countries_gdp <span class="keyword">DESC</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 3. Print the per-capita GDP of all countries in the database, in descending order.</span></span><br><span class="line"><span class="keyword">SELECT</span> countries_name, countries_gdp<span class="operator">/</span>countries_population per_capitaGDP</span><br><span class="line"><span class="keyword">FROM</span> COMP4039.countries</span><br><span class="line"><span class="keyword">ORDER</span> <span class="keyword">BY</span> per_capitaGDP <span class="keyword">DESC</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 4. Display 2 places of decimals</span></span><br><span class="line"><span class="keyword">SELECT</span> countries_name, ROUND(countries_gdp<span class="operator">/</span>countries_population, <span class="number">2</span>) per_capitaGDP</span><br><span class="line"><span class="keyword">FROM</span> COMP4039.countries</span><br><span class="line"><span class="keyword">ORDER</span> <span class="keyword">BY</span> per_capitaGDP <span class="keyword">DESC</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 5. Ignores very small countries with a land mass of less than 20,000.</span></span><br><span class="line"><span class="keyword">SELECT</span> countries_name, ROUND(countries_gdp<span class="operator">/</span>countries_population, <span class="number">2</span>) per_capitaGDP</span><br><span class="line"><span class="keyword">FROM</span> COMP4039.countries</span><br><span class="line"><span class="keyword">WHERE</span> countries_area <span class="operator">>=</span> <span class="number">20000</span></span><br><span class="line"><span class="keyword">ORDER</span> <span class="keyword">BY</span> per_capitaGDP <span class="keyword">DESC</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 6. Shows the names of countries that have a land mass in between 100,000 km2 and 200,000 km2.</span></span><br><span class="line"><span class="keyword">SELECT</span> countries_name</span><br><span class="line"><span class="keyword">FROM</span> COMP4039.countries</span><br><span class="line"><span class="keyword">WHERE</span> countries_area <span class="keyword">BETWEEN</span> <span class="number">100000</span> <span class="keyword">AND</span> <span class="number">200000</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 7. Shows the names of countries that start with the letter‘M’.</span></span><br><span class="line"><span class="keyword">SELECT</span> countries_name</span><br><span class="line"><span class="keyword">FROM</span> COMP4039.countries</span><br><span class="line"><span class="keyword">WHERE</span> countries_name <span class="keyword">LIKE</span> <span class="string">'M%'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 8. Lists the names of countries that end with‘...land’.</span></span><br><span class="line"><span class="keyword">SELECT</span> countries_name</span><br><span class="line"><span class="keyword">FROM</span> COMP4039.countries</span><br><span class="line"><span class="keyword">WHERE</span> countries_name <span class="keyword">LIKE</span> <span class="string">'%land'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 9. The name of the country is the same as the name of the capital.</span></span><br><span class="line"><span class="keyword">SELECT</span> countries_name</span><br><span class="line"><span class="keyword">FROM</span> COMP4039.countries</span><br><span class="line"><span class="keyword">WHERE</span> countries_name<span class="operator">=</span>countries_capital;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 10. Total population of all of the countries.</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">SUM</span>(countries_population)</span><br><span class="line"><span class="keyword">FROM</span> COMP4039.countries;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 11. Find the number of countries that have a total land mass of at least 500,000.</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>) num</span><br><span class="line"><span class="keyword">FROM</span> COMP4039.countries</span><br><span class="line"><span class="keyword">WHERE</span> countries_area <span class="operator">>=</span> <span class="number">500000</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 12. Show the countries which have a per capita GDP more than that of Poland, in descending order of wealth.</span></span><br><span class="line"><span class="keyword">SELECT</span> countries_name, countries_gdp<span class="operator">/</span>countries_population</span><br><span class="line"><span class="keyword">FROM</span> COMP4039.countries</span><br><span class="line"><span class="keyword">WHERE</span> countries_gdp<span class="operator">/</span>countries_population <span class="operator">></span> (<span class="keyword">SELECT</span> countries_gdp<span class="operator">/</span>countries_population</span><br><span class="line"> <span class="keyword">FROM</span> COMP4039.countries</span><br><span class="line"> <span class="keyword">WHERE</span> countries_name<span class="operator">=</span><span class="string">'Poland'</span>)</span><br><span class="line"><span class="keyword">ORDER</span> <span class="keyword">BY</span> countries_gdp<span class="operator">/</span>countries_population <span class="keyword">DESC</span>;</span><br></pre></td></tr></table></figure><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><h1 id="1-Wkiipedia-Definition"><a href="#1-Wkiipedia-Definition" class="headerlink" title="1. Wkiipedia Definition"></a>1. Wkiipedia Definition</h1><p>A database is an organized collection of data, generally stored and accessed electronically from a computer system.</p>
<h1 id="2-Programming-data-structures-and-databases"><a href="#2-Programming-data-structures-and-databases" class="headerlink" title="2. Programming data structures and databases"></a>2. Programming data structures and databases</h1><ul>
<li>You have to find a way to map data structures in your programming language to data structures held by the (relational) database.</li>
<li>OO data structure &lt;——&gt; DBs</li>
</ul></summary>
<category term="UoN" scheme="https://aaronos0.github.io/categories/UoN/"/>
<category term="COMP4039-DB&WebInterface Design" scheme="https://aaronos0.github.io/tags/COMP4039-DB-WebInterface-Design/"/>
</entry>
<entry>
<title>COMP4039 - 0.Module Overview</title>
<link href="https://aaronos0.github.io/2020/10/07/COMP4039-0-Module-Overview/"/>
<id>https://aaronos0.github.io/2020/10/07/COMP4039-0-Module-Overview/</id>
<published>2020-10-07T22:58:16.000Z</published>
<updated>2020-10-07T22:58:16.000Z</updated>
<content type="html"><![CDATA[<h1 id="1-Teaching"><a href="#1-Teaching" class="headerlink" title="1. Teaching"></a>1. Teaching</h1><ul><li>20 credit Module = 200 hours of study time</li><li>3-14 Weeks of Teaching</li></ul><h1 id="2-Assessment"><a href="#2-Assessment" class="headerlink" title="2. Assessment"></a>2. Assessment</h1><ul><li>Open book exam (details TBA) – 40%</li><li>Coursework 1: 2-part coursework – total 20%<ul><li>Part A (10%): database design</li><li>Part B (10%): database manipulation</li></ul></li><li>Coursework 2: Project coursework – 40%<ul><li>Database + web interface</li></ul></li></ul><span id="more"></span><h1 id="3-Resources"><a href="#3-Resources" class="headerlink" title="3. Resources"></a>3. Resources</h1><ul><li><a href="https://nostarch.com/mg_databases.htm">The Manga Guide to Databases</a></li><li><a href="https://opentextbc.ca/dbdesign01/">Database Design - 2nd Edition</a></li></ul><h1 id="4-Software"><a href="#4-Software" class="headerlink" title="4. Software"></a>4. Software</h1><ul><li>Login shell to (mersey.cs.nott.ac.uk)</li><li>MySQL (mysql.cs.nott.ac.uk:3306)</li><li>MySQL Workbench</li><li>Visual Paradigm (ER, Use Cases)</li><li>PHP</li><li>HTML / CSS</li><li>JS</li></ul><h1 id="5-Course-amp-amp-Structure"><a href="#5-Course-amp-amp-Structure" class="headerlink" title="5. Course&&Structure"></a>5. Course&&Structure</h1><ol><li>Databases: Relational/Nonrelational DBs</li><li>Interfaces</li><li>Software design principles: Requirements and web-based front-end web design</li></ol><h1 id="6-Databases-and-Categorisation"><a href="#6-Databases-and-Categorisation" class="headerlink" title="6. Databases and Categorisation"></a>6. Databases and Categorisation</h1><blockquote><p>Databases are ways of modelling and shaping the nature of reality</p></blockquote><ul><li>Categories and categorisations of the world</li><li>The structure of databases structures relations between people, systems, things, circumstances, activities, etc.</li></ul><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><h1 id="1-Teaching"><a href="#1-Teaching" class="headerlink" title="1. Teaching"></a>1. Teaching</h1><ul>
<li>20 credit Module = 200 hours of study time</li>
<li>3-14 Weeks of Teaching</li>
</ul>
<h1 id="2-Assessment"><a href="#2-Assessment" class="headerlink" title="2. Assessment"></a>2. Assessment</h1><ul>
<li>Open book exam (details TBA) – 40%</li>
<li>Coursework 1: 2-part coursework – total 20%<ul>
<li>Part A (10%): database design</li>
<li>Part B (10%): database manipulation</li>
</ul>
</li>
<li>Coursework 2: Project coursework – 40%<ul>
<li>Database + web interface</li>
</ul>
</li>
</ul></summary>
<category term="UoN" scheme="https://aaronos0.github.io/categories/UoN/"/>
<category term="COMP4039-DB&WebInterface Design" scheme="https://aaronos0.github.io/tags/COMP4039-DB-WebInterface-Design/"/>
</entry>
<entry>
<title>Python - Theme 12 virtualenv</title>
<link href="https://aaronos0.github.io/2020/10/04/Python-Theme-12-virtualenv/"/>
<id>https://aaronos0.github.io/2020/10/04/Python-Theme-12-virtualenv/</id>
<published>2020-10-04T17:21:24.000Z</published>
<updated>2020-10-04T17:21:24.000Z</updated>
<content type="html"><![CDATA[<blockquote><p>在使用Python时候,不同的应用程序所需要的包都会被pip安装在Python的site-package目录下。长久以来会造成管理的混乱,甚至冲突。因此virtualenv是在基于系统的python之上,创建一套虚拟环境,因此来实现每个应用可以独自拥有一套自己的python环境。</p></blockquote><h1 id="1-安装"><a href="#1-安装" class="headerlink" title="1. 安装"></a>1. 安装</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">➜ ~pip3 install virtualenv</span><br></pre></td></tr></table></figure><span id="more"></span><h1 id="2-创建一个独立的Python环境"><a href="#2-创建一个独立的Python环境" class="headerlink" title="2. 创建一个独立的Python环境"></a>2. 创建一个独立的Python环境</h1><ul><li>为了区分可以为起不同标识的名字</li><li>–no-site-packages表示不会将已经安装到系统中的第三方包复制过来</li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">➜ ~virtualenv --no-site-packages venv3<span class="number">.7</span></span><br></pre></td></tr></table></figure><h1 id="3-使用此环境"><a href="#3-使用此环境" class="headerlink" title="3. 使用此环境"></a>3. 使用此环境</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">➜ ~source venv3<span class="number">.7</span>/<span class="built_in">bin</span>/activate</span><br><span class="line">(venv3<span class="number">.7</span>) ➜ ~</span><br></pre></td></tr></table></figure><h1 id="4-退出环境"><a href="#4-退出环境" class="headerlink" title="4. 退出环境"></a>4. 退出环境</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">(venv3<span class="number">.7</span>) ➜ ~ deactivate</span><br><span class="line">➜ ~</span><br></pre></td></tr></table></figure><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><blockquote>
<p>在使用Python时候,不同的应用程序所需要的包都会被pip安装在Python的site-package目录下。长久以来会造成管理的混乱,甚至冲突。因此virtualenv是在基于系统的python之上,创建一套虚拟环境,因此来实现每个应用可以独自拥有一套自己的python环境。</p>
</blockquote>
<h1 id="1-安装"><a href="#1-安装" class="headerlink" title="1. 安装"></a>1. 安装</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">➜ ~pip3 install virtualenv</span><br></pre></td></tr></table></figure></summary>
<category term="Computer Science" scheme="https://aaronos0.github.io/categories/Computer-Science/"/>
<category term="Python Learning" scheme="https://aaronos0.github.io/tags/Python-Learning/"/>
</entry>
<entry>
<title>Python - Theme 11 Package</title>
<link href="https://aaronos0.github.io/2020/10/04/Python-Theme-11-Package/"/>
<id>https://aaronos0.github.io/2020/10/04/Python-Theme-11-Package/</id>
<published>2020-10-04T17:20:48.000Z</published>
<updated>2020-10-04T17:20:48.000Z</updated>
<content type="html"><![CDATA[<blockquote><p>Batteries Included Philosophy - having a rich and versatile standard library which is immediately available, without making the user download separate packages.</p></blockquote><h1 id="1-datetime"><a href="#1-datetime" class="headerlink" title="1. datetime"></a>1. datetime</h1><blockquote><p>Python处理时间和日期的标准库</p></blockquote><span id="more"></span><h2 id="1-1-获取当前时间和日期"><a href="#1-1-获取当前时间和日期" class="headerlink" title="1.1. 获取当前时间和日期"></a>1.1. 获取当前时间和日期</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">from</span> datetime <span class="keyword">import</span> datetime <span class="comment">#datetime模块包含一个datetime类</span></span><br><span class="line"><span class="meta">>>> </span>now = datetime.now()</span><br><span class="line"><span class="meta">>>> </span>now</span><br><span class="line">datetime.datetime(<span class="number">2020</span>, <span class="number">7</span>, <span class="number">4</span>, <span class="number">10</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">512524</span>)</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(now)</span><br><span class="line"><span class="number">2020</span>-07-04 <span class="number">10</span>:07:<span class="number">08.512524</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(<span class="built_in">type</span>(now))</span><br><span class="line"><<span class="class"><span class="keyword">class</span> '<span class="title">datetime</span>.<span class="title">datetime</span>'></span></span><br></pre></td></tr></table></figure><h2 id="1-2-获取指定日期和时间"><a href="#1-2-获取指定日期和时间" class="headerlink" title="1.2. 获取指定日期和时间"></a>1.2. 获取指定日期和时间</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span>dt = datetime(<span class="number">2020</span>, <span class="number">6</span>, <span class="number">6</span>, <span class="number">6</span>, <span class="number">6</span>) <span class="comment">#指定日期</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(dt)</span><br><span class="line"><span class="number">2020</span>-06-06 06:06:<span class="number">00</span></span><br></pre></td></tr></table></figure><h2 id="1-3-datetime转timestamp"><a href="#1-3-datetime转timestamp" class="headerlink" title="1.3. datetime转timestamp"></a>1.3. datetime转timestamp</h2><p>在计算机中,时间的表示是相对于Epoch time来计算的。Epoch time: 也称Unix time,是1970年1月1号 00:00:00 UTC+00:00 的时间。当前时间就是相对于Epoch time的秒数。<br>全球各地计算机的任意时刻timestap都是相同的,这也就是为什么计算机的时间都是用timestamp表示了。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span>dt = datetime(<span class="number">2020</span>, <span class="number">6</span>, <span class="number">6</span>, <span class="number">6</span>, <span class="number">6</span>)</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(dt)</span><br><span class="line"><span class="number">2020</span>-06-06 06:06:<span class="number">00</span></span><br><span class="line"><span class="meta">>>> </span>dt.timestamp() <span class="comment">#转换为timestamp</span></span><br><span class="line"><span class="number">1591419960.0</span> <span class="comment">#小数表示毫秒</span></span><br><span class="line"></span><br><span class="line"><span class="meta">>>> </span>t = <span class="number">1591419960.0</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(datetime.fromtimestamp(t)) <span class="comment">#本人在英国,夏时令为UTC+1</span></span><br><span class="line"><span class="number">2020</span>-06-06 06:06:<span class="number">00</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(datetime.utcfromtimestamp(t)) <span class="comment">#UTC时间</span></span><br><span class="line"><span class="number">2020</span>-06-06 05:06:<span class="number">00</span></span><br></pre></td></tr></table></figure><h2 id="1-4-str转datetime"><a href="#1-4-str转datetime" class="headerlink" title="1.4. str转datetime"></a>1.4. str转datetime</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span>day = datetime.strptime(<span class="string">'2020-06-06 06:06:00'</span>, <span class="string">'%Y-%m-%d %H:%M:%S'</span>) <span class="comment">#日期的str与格式化字符串</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(day)</span><br><span class="line"><span class="meta">>>> </span><span class="number">2020</span>-06-06 06:06:<span class="number">00</span></span><br></pre></td></tr></table></figure><h2 id="1-5-datetime转str"><a href="#1-5-datetime转str" class="headerlink" title="1.5. datetime转str"></a>1.5. datetime转str</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span>now = datetime.now()</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(now.strftime(<span class="string">'%a, %b %d %H:%M'</span>))</span><br><span class="line">Sat, Jul 04 <span class="number">10</span>:<span class="number">40</span></span><br></pre></td></tr></table></figure><h2 id="1-6-datetime加减"><a href="#1-6-datetime加减" class="headerlink" title="1.6. datetime加减"></a>1.6. datetime加减</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">from</span> datetime <span class="keyword">import</span> datetime, timedelta <span class="comment">#通过timedelta类来进行加减运算</span></span><br><span class="line"><span class="meta">>>> </span>now = datetime.now()</span><br><span class="line"><span class="meta">>>> </span>now</span><br><span class="line">datetime.datetime(<span class="number">2020</span>, <span class="number">7</span>, <span class="number">4</span>, <span class="number">10</span>, <span class="number">44</span>, <span class="number">24</span>, <span class="number">184738</span>)</span><br><span class="line"><span class="meta">>>> </span>now + timedelta(hours = <span class="number">12</span>)</span><br><span class="line">datetime.datetime(<span class="number">2020</span>, <span class="number">7</span>, <span class="number">4</span>, <span class="number">22</span>, <span class="number">44</span>, <span class="number">24</span>, <span class="number">184738</span>)</span><br><span class="line"><span class="meta">>>> </span>now - timedelta(days=<span class="number">1</span>)</span><br><span class="line">datetime.datetime(<span class="number">2020</span>, <span class="number">7</span>, <span class="number">3</span>, <span class="number">10</span>, <span class="number">44</span>, <span class="number">24</span>, <span class="number">184738</span>)</span><br><span class="line"><span class="meta">>>> </span>now + timedelta(days = <span class="number">1</span>, hours = <span class="number">12</span>)</span><br><span class="line">datetime.datetime(<span class="number">2020</span>, <span class="number">7</span>, <span class="number">5</span>, <span class="number">22</span>, <span class="number">44</span>, <span class="number">24</span>, <span class="number">184738</span>)</span><br></pre></td></tr></table></figure><h2 id="1-7-本地时间转UTC"><a href="#1-7-本地时间转UTC" class="headerlink" title="1.7. 本地时间转UTC"></a>1.7. 本地时间转UTC</h2><p>目前本地时间为英国夏时令(UTC+1)。datetime类型有一个时区属性tzinfo,默认为None。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">from</span> datetime <span class="keyword">import</span> datetime, timedelta, timezone</span><br><span class="line"><span class="meta">>>> </span>tz_utc_1 = timezone(timedelta(hours = <span class="number">1</span>)) <span class="comment">#创建时区UTC+1:00</span></span><br><span class="line"><span class="meta">>>> </span>now = datetime.now()</span><br><span class="line"><span class="meta">>>> </span>now</span><br><span class="line">datetime.datetime(<span class="number">2020</span>, <span class="number">7</span>, <span class="number">4</span>, <span class="number">10</span>, <span class="number">53</span>, <span class="number">38</span>, <span class="number">549365</span>)</span><br><span class="line"><span class="meta">>>> </span>dt = now.replace(tzinfo = tz_utc_1) <span class="comment">#强制设置为UTC+1:00</span></span><br><span class="line"><span class="meta">>>> </span>dt</span><br><span class="line">datetime.datetime(<span class="number">2020</span>, <span class="number">7</span>, <span class="number">4</span>, <span class="number">10</span>, <span class="number">53</span>, <span class="number">38</span>, <span class="number">549365</span>, tzinfo=datetime.timezone(datetime.timedelta(seconds=<span class="number">3600</span>)))</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(dt)</span><br><span class="line"><span class="number">2020</span>-07-04 <span class="number">10</span>:<span class="number">53</span>:<span class="number">38.549365</span>+01:<span class="number">00</span></span><br></pre></td></tr></table></figure><h2 id="1-8-时区转换"><a href="#1-8-时区转换" class="headerlink" title="1.8. 时区转换"></a>1.8. 时区转换</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span>utc_dt = datetime.utcnow().replace(tzinfo = timezone.utc) <span class="comment">#拿到UTC时间,并强制设置时区为UTC+0:00</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(utc_dt)</span><br><span class="line"><span class="number">2020</span>-07-04 <span class="number">10</span>:<span class="number">00</span>:<span class="number">56.720584</span>+<span class="number">00</span>:<span class="number">00</span></span><br><span class="line"><span class="meta">>>> </span>bj_dt = utc_dt.astimezone(timezone(timedelta(hours=<span class="number">8</span>))) <span class="comment">#转换为北京时间</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(bj_dt)</span><br><span class="line"><span class="number">2020</span>-07-04 <span class="number">18</span>:<span class="number">00</span>:<span class="number">56.720584</span>+08:<span class="number">00</span></span><br></pre></td></tr></table></figure><h2 id="1-9-tips"><a href="#1-9-tips" class="headerlink" title="1.9. tips"></a>1.9. tips</h2><p>datetime表示的时间需要时区信息才能确定一个时间,否则只能视为本地时间。<br>如果要存储datetime,最佳方法为timestamp,这样就与时区无关了。</p><h1 id="2-collections"><a href="#2-collections" class="headerlink" title="2. collections"></a>2. collections</h1><blockquote><p>Python内建的集合模块</p></blockquote><h2 id="2-1-namedtuple-tuple的一个子类,既可以用下标访问也可以使用自定义的属性来访问"><a href="#2-1-namedtuple-tuple的一个子类,既可以用下标访问也可以使用自定义的属性来访问" class="headerlink" title="2.1. namedtuple - tuple的一个子类,既可以用下标访问也可以使用自定义的属性来访问"></a>2.1. namedtuple - tuple的一个子类,既可以用下标访问也可以使用自定义的属性来访问</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#表示坐标</span></span><br><span class="line"><span class="meta">>>> </span><span class="keyword">from</span> collections <span class="keyword">import</span> namedtuple</span><br><span class="line"><span class="meta">>>> </span>Point = namedtuple(<span class="string">'Point'</span>, [<span class="string">'x'</span>, <span class="string">'y'</span>]) <span class="comment">#namedtuple('名称', [属性list])</span></span><br><span class="line"><span class="meta">>>> </span>p = Point(<span class="number">1</span>,<span class="number">2</span>)</span><br><span class="line"><span class="meta">>>> </span>p.x</span><br><span class="line"><span class="number">1</span></span><br><span class="line">>>>p[<span class="number">0</span>]</span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="meta">>>> </span>p.y</span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>(p, Point)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>(p, <span class="built_in">tuple</span>)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#表示学生</span></span><br><span class="line"><span class="meta">>>> </span>Stu = namedtuple(<span class="string">'Student'</span>, [<span class="string">'name'</span>, <span class="string">'age'</span>])</span><br><span class="line"><span class="meta">>>> </span>s = Stu(<span class="string">'Bob'</span>, <span class="string">'20'</span>)</span><br><span class="line"><span class="meta">>>> </span>s</span><br><span class="line">Student(name=<span class="string">'Bob'</span>, age=<span class="string">'20'</span>)</span><br><span class="line"><span class="meta">>>> </span>s.name</span><br><span class="line"><span class="string">'Bob'</span></span><br></pre></td></tr></table></figure><h2 id="2-2-deque"><a href="#2-2-deque" class="headerlink" title="2.2. deque"></a>2.2. deque</h2><blockquote><p>由于list为线性存储,数据量很大时,插入和删除的效率会很低。deque是为了实现插入和删除操作的双向列表,适用于队列和栈。</p></blockquote><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">from</span> collections <span class="keyword">import</span> deque</span><br><span class="line"><span class="meta">>>> </span>q = deque([<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>])</span><br><span class="line"><span class="meta">>>> </span>q.append(<span class="string">'x'</span>)</span><br><span class="line"><span class="meta">>>> </span>q.appendleft(<span class="string">'y'</span>)</span><br><span class="line"><span class="meta">>>> </span>q</span><br><span class="line">deque([<span class="string">'y'</span>, <span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>, <span class="string">'x'</span>])</span><br><span class="line"><span class="meta">>>> </span>q.popleft()</span><br><span class="line"><span class="string">'y'</span></span><br><span class="line"><span class="meta">>>> </span>q</span><br><span class="line">deque([<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>, <span class="string">'x'</span>])</span><br></pre></td></tr></table></figure><h2 id="2-3-defaultdict"><a href="#2-3-defaultdict" class="headerlink" title="2.3. defaultdict"></a>2.3. defaultdict</h2><blockquote><p>使用dict时,如果引用的Key不存在,就会抛出KeyError。使用defaultdict使得程序不抛错误,返回一个默认值。</p></blockquote><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">from</span> collections <span class="keyword">import</span> defaultdict</span><br><span class="line"><span class="meta">>>> </span>dd = defaultdict(<span class="string">'Key not exit'</span>)</span><br><span class="line">Traceback (most recent call last):</span><br><span class="line"> File <span class="string">"<input>"</span>, line <span class="number">1</span>, <span class="keyword">in</span> <module></span><br><span class="line">TypeError: first argument must be <span class="built_in">callable</span> <span class="keyword">or</span> <span class="literal">None</span></span><br><span class="line"><span class="meta">>>> </span>dd = defaultdict(<span class="keyword">lambda</span>: <span class="string">'Key not exit'</span>) <span class="comment">#必须是callable或者None,因此这里需要用函数</span></span><br><span class="line"><span class="meta">>>> </span>dd[<span class="string">'name'</span>] = <span class="string">'Bob'</span></span><br><span class="line"><span class="meta">>>> </span>dd[<span class="string">'name'</span>] <span class="comment">#key存在,和普通dict一样</span></span><br><span class="line"><span class="string">'Bob'</span></span><br><span class="line"><span class="meta">>>> </span>dd[<span class="string">'age'</span>] <span class="comment">#key不存在,返回自己定义的返回值</span></span><br><span class="line"><span class="string">'Key not exit'</span></span><br></pre></td></tr></table></figure><h2 id="2-4-OrderedDict"><a href="#2-4-OrderedDict" class="headerlink" title="2.4. OrderedDict"></a>2.4. OrderedDict</h2><p>OrderedDict:保持key的顺序;<br>不过在python 3.6之后,普通dict也是有序的。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> collections</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">'\nRegular dictionary:'</span>)</span><br><span class="line">d = {}</span><br><span class="line">d[<span class="string">'a'</span>] = <span class="string">'A'</span></span><br><span class="line">d[<span class="string">'b'</span>] = <span class="string">'B'</span></span><br><span class="line">d[<span class="string">'c'</span>] = <span class="string">'C'</span></span><br><span class="line"><span class="keyword">for</span> k, v <span class="keyword">in</span> d.items():</span><br><span class="line"> <span class="built_in">print</span>(k, v)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">'\nOrderedDict:'</span>)</span><br><span class="line">d = collections.OrderedDict()</span><br><span class="line">d[<span class="string">'a'</span>] = <span class="string">'A'</span></span><br><span class="line">d[<span class="string">'b'</span>] = <span class="string">'B'</span></span><br><span class="line">d[<span class="string">'c'</span>] = <span class="string">'C'</span></span><br><span class="line"><span class="keyword">for</span> k, v <span class="keyword">in</span> d.items():</span><br><span class="line"> <span class="built_in">print</span>(k,v)</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出:</span></span><br><span class="line">Regular dictionary:</span><br><span class="line">a A</span><br><span class="line">b B</span><br><span class="line">c C</span><br><span class="line"></span><br><span class="line">OrderedDict:</span><br><span class="line">a A</span><br><span class="line">b B</span><br><span class="line">c C</span><br></pre></td></tr></table></figure><h2 id="2-5-ChainMap"><a href="#2-5-ChainMap" class="headerlink" title="2.5. ChainMap"></a>2.5. ChainMap</h2><blockquote><p>可以将多个字典映射组合在一起,组成一个逻辑上的dict,maps中存储字典组成的列表。查找的时候,会按照dict中的逻辑顺序查找。</p></blockquote><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">from</span> collections <span class="keyword">import</span> ChainMap</span><br><span class="line"><span class="meta">>>> </span>a = {<span class="string">"name"</span>: <span class="string">"Bob"</span>}</span><br><span class="line"><span class="meta">>>> </span>b = {<span class="string">"age"</span>: <span class="number">18</span>}</span><br><span class="line"><span class="meta">>>> </span>c = {<span class="string">"gender"</span>: <span class="string">"man"</span>}</span><br><span class="line"><span class="meta">>>> </span>pylook = ChainMap(a,b,c) <span class="comment">#将多组字典合并</span></span><br><span class="line"><span class="meta">>>> </span>pylook</span><br><span class="line">ChainMap({<span class="string">'name'</span>: <span class="string">'Bob'</span>}, {<span class="string">'age'</span>: <span class="number">18</span>}, {<span class="string">'gender'</span>: <span class="string">'man'</span>})</span><br><span class="line"><span class="meta">>>> </span>pylook[<span class="string">'name'</span>] <span class="comment">#搜索key为name的value</span></span><br><span class="line"><span class="string">'Bob'</span></span><br><span class="line"><span class="meta">>>> </span>pylook.update({<span class="string">'name'</span>: <span class="string">'Mack'</span>}) <span class="comment">#更新key为name的value(注意这里会从第一个字典开始找)</span></span><br><span class="line"><span class="meta">>>> </span>pylook <span class="comment">#第一个字典中存在key为name的元素,更新成功</span></span><br><span class="line">ChainMap({<span class="string">'name'</span>: <span class="string">'Mack'</span>}, {<span class="string">'age'</span>: <span class="number">18</span>}, {<span class="string">'gender'</span>: <span class="string">'man'</span>})</span><br><span class="line"><span class="meta">>>> </span>pylook.update({<span class="string">'age'</span>: <span class="string">'20'</span>}) <span class="comment">#发现第一个字典中无key为age的元素,则添加</span></span><br><span class="line"><span class="meta">>>> </span>pylook</span><br><span class="line">ChainMap({<span class="string">'name'</span>: <span class="string">'Mack'</span>, <span class="string">'age'</span>: <span class="string">'20'</span>}, {<span class="string">'age'</span>: <span class="number">18</span>}, {<span class="string">'gender'</span>: <span class="string">'man'</span>})</span><br><span class="line"><span class="meta">>>> </span>b[<span class="string">'age'</span>] = <span class="number">22</span> <span class="comment">#指定更新某个字典</span></span><br><span class="line"><span class="meta">>>> </span>pylook</span><br><span class="line">ChainMap({<span class="string">'name'</span>: <span class="string">'Mack'</span>, <span class="string">'age'</span>: <span class="string">'20'</span>}, {<span class="string">'age'</span>: <span class="number">22</span>}, {<span class="string">'gender'</span>: <span class="string">'man'</span>})</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">type</span>(pylook.maps) <span class="comment">#maps中存着用list保存的字典</span></span><br><span class="line"><<span class="class"><span class="keyword">class</span> '<span class="title">list</span>'></span></span><br><span class="line"><span class="class">>>> <span class="title">pylook</span>.<span class="title">maps</span>[0]['<span class="title">age</span>'] = 25</span></span><br><span class="line"><span class="class">>>> <span class="title">pylook</span>.<span class="title">maps</span>[1]['<span class="title">age</span>'] = 35</span></span><br><span class="line"><span class="class">>>> <span class="title">pylook</span>.<span class="title">maps</span></span></span><br><span class="line"><span class="class">[{'<span class="title">name</span>':</span> <span class="string">'Mack'</span>, <span class="string">'age'</span>: <span class="number">25</span>}, {<span class="string">'age'</span>: <span class="number">35</span>}, {<span class="string">'gender'</span>: <span class="string">'man'</span>}]</span><br><span class="line"><span class="meta">>>> </span>pylook[<span class="string">'age'</span>] <span class="comment">#尽管整个字典映射中有两个age,但是永远都从第一个开始找</span></span><br><span class="line"><span class="number">25</span></span><br><span class="line"><span class="meta">>>> </span>pylook.parents <span class="comment">#类似于maps[1:]</span></span><br><span class="line">ChainMap({<span class="string">'age'</span>: <span class="number">35</span>}, {<span class="string">'gender'</span>: <span class="string">'man'</span>})</span><br></pre></td></tr></table></figure><p>应用:</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#(1). 模拟Python内部查找链示例</span></span><br><span class="line"><span class="keyword">import</span> builtins</span><br><span class="line">pylookup = ChainMap(<span class="built_in">locals</span>(), <span class="built_in">globals</span>(), <span class="built_in">vars</span>(builtins))</span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#(2). 参数优先级查找(test5.py)</span></span><br><span class="line"><span class="keyword">from</span> collections <span class="keyword">import</span> ChainMap</span><br><span class="line"><span class="keyword">import</span> os, argparse</span><br><span class="line"></span><br><span class="line"><span class="comment">#构造缺省参数</span></span><br><span class="line">defaults = {</span><br><span class="line"> <span class="string">'color'</span>: <span class="string">'red'</span>,</span><br><span class="line"> <span class="string">'user'</span>: <span class="string">'guest'</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">#构造命令行参数</span></span><br><span class="line">parser = argparse.ArgumentParser()</span><br><span class="line">parser.add_argument(<span class="string">'-u'</span>, <span class="string">'--user'</span>)</span><br><span class="line">parser.add_argument(<span class="string">'-c'</span>, <span class="string">'--color'</span>)</span><br><span class="line">namespace = parser.parse_args()</span><br><span class="line"><span class="comment">#命令行参数会存储在namespace中</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">vars</span>(namespace).items())</span><br><span class="line"><span class="comment">#命令函参数字典</span></span><br><span class="line">command_line_args = {k: v <span class="keyword">for</span> k, v <span class="keyword">in</span> <span class="built_in">vars</span>(namespace).items() <span class="keyword">if</span> v}</span><br><span class="line"></span><br><span class="line"><span class="comment">#利用这里来设置优先级: 命令函 > 系统环境 > 默认参数</span></span><br><span class="line">combined = ChainMap(command_line_args, os.environ, defaults)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">'color=%s'</span> % combined[<span class="string">'color'</span>])</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'user=%s'</span> % combined[<span class="string">'user'</span>])</span><br><span class="line"></span><br><span class="line">----------</span><br><span class="line"><span class="comment">#输入(无参数)</span></span><br><span class="line">(venv3<span class="number">.7</span>) ➜ python test5.py </span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">dict_items([(<span class="string">'user'</span>, <span class="literal">None</span>), (<span class="string">'color'</span>, <span class="literal">None</span>)])</span><br><span class="line">color=red</span><br><span class="line">user=guest</span><br><span class="line"></span><br><span class="line"><span class="comment">#输入(命令行参数)</span></span><br><span class="line">(venv3<span class="number">.7</span>) ➜ python test5.py -u Aaron </span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">dict_items([(<span class="string">'user'</span>, <span class="string">'Aaron'</span>), (<span class="string">'color'</span>, <span class="literal">None</span>)])</span><br><span class="line">color=red</span><br><span class="line">user=Aaron</span><br><span class="line"></span><br><span class="line"><span class="comment">#输入(环境变量+命令行参数)</span></span><br><span class="line">(venv3<span class="number">.7</span>) ➜ user=admin color=red python test5.py -u Aaron</span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">dict_items([(<span class="string">'user'</span>, <span class="string">'Aaron'</span>), (<span class="string">'color'</span>, <span class="literal">None</span>)])</span><br><span class="line">color=red</span><br><span class="line">user=Aaron</span><br></pre></td></tr></table></figure><h2 id="2-6-Counter-计数器"><a href="#2-6-Counter-计数器" class="headerlink" title="2.6. Counter -计数器"></a>2.6. Counter -计数器</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">from</span> collections <span class="keyword">import</span> Counter</span><br><span class="line"><span class="meta">>>> </span>c = Counter(<span class="string">'hello, word!'</span>)</span><br><span class="line"><span class="meta">>>> </span>c</span><br><span class="line">Counter({<span class="string">'l'</span>: <span class="number">2</span>, <span class="string">'o'</span>: <span class="number">2</span>, <span class="string">'h'</span>: <span class="number">1</span>, <span class="string">'e'</span>: <span class="number">1</span>, <span class="string">','</span>: <span class="number">1</span>, <span class="string">' '</span>: <span class="number">1</span>, <span class="string">'w'</span>: <span class="number">1</span>, <span class="string">'r'</span>: <span class="number">1</span>, <span class="string">'d'</span>: <span class="number">1</span>, <span class="string">'!'</span>: <span class="number">1</span>})</span><br><span class="line"><span class="meta">>>> </span>c.update(<span class="string">'aaaaaa'</span>) <span class="comment">#可以继续添加字符,统计则会自动更新</span></span><br><span class="line"><span class="meta">>>> </span>c</span><br><span class="line">Counter({<span class="string">'a'</span>: <span class="number">6</span>, <span class="string">'l'</span>: <span class="number">2</span>, <span class="string">'o'</span>: <span class="number">2</span>, <span class="string">'h'</span>: <span class="number">1</span>, <span class="string">'e'</span>: <span class="number">1</span>, <span class="string">','</span>: <span class="number">1</span>, <span class="string">' '</span>: <span class="number">1</span>, <span class="string">'w'</span>: <span class="number">1</span>, <span class="string">'r'</span>: <span class="number">1</span>, <span class="string">'d'</span>: <span class="number">1</span>, <span class="string">'!'</span>: <span class="number">1</span>})</span><br><span class="line"><span class="meta">>>> </span>c.get(<span class="string">'a'</span>) <span class="comment">#查找指定的key</span></span><br><span class="line"><span class="number">6</span></span><br></pre></td></tr></table></figure><h1 id="3-hashlib-摘要算法"><a href="#3-hashlib-摘要算法" class="headerlink" title="3. hashlib - 摘要算法"></a>3. hashlib - 摘要算法</h1><blockquote><p>摘要算法:就像是一篇文章的摘要一样,永远不会变。摘要算法又称哈希算法、散列算法。它是通过一个函数把任意长度的数据转换为固定长度的数据串。只要原始数据没有发生改变,那么生成的数据串也不会变。Python的hashlib提供了常见的摘要算法,如:MD5,SHA1等等。摘要算法一般应用于防篡改。</p></blockquote><h2 id="3-1-MD5"><a href="#3-1-MD5" class="headerlink" title="3.1. MD5"></a>3.1. MD5</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">import</span> hashlib</span><br><span class="line"><span class="meta">>>> </span>md5 = hashlib.md5()</span><br><span class="line"><span class="meta">>>> </span>md5.update(<span class="string">'hello, world!'</span>.encode(<span class="string">'utf-8'</span>)) <span class="comment">#计算哈希值</span></span><br><span class="line"><span class="meta">>>> </span>md5.hexdigest()</span><br><span class="line"><span class="string">'3adbbad1791fbae3ec908894c4963870'</span></span><br><span class="line"><span class="meta">>>> </span>md5</span><br><span class="line"><md5 HASH <span class="built_in">object</span> @ <span class="number">0x10dec70c0</span>></span><br><span class="line"></span><br><span class="line"><span class="comment">#如果数据过长也可多次调用update,结果一致</span></span><br><span class="line"><span class="meta">>>> </span>md5 = hashlib.md5()</span><br><span class="line"><span class="meta">>>> </span>md5</span><br><span class="line"><span class="meta">>>> </span><md5 HASH <span class="built_in">object</span> @ <span class="number">0x10dec7600</span>></span><br><span class="line"><span class="meta">>>> </span>md5.update(<span class="string">'hello, '</span>.encode(<span class="string">'utf-8'</span>))</span><br><span class="line"><span class="meta">>>> </span>md5.update(<span class="string">'world!'</span>.encode(<span class="string">'utf-8'</span>))</span><br><span class="line"><span class="meta">>>> </span>md5.hexdigest()</span><br><span class="line"><span class="string">'3adbbad1791fbae3ec908894c4963870'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#如果字符串有小小的改动,计算结果会完全不同</span></span><br><span class="line"><span class="meta">>>> </span>md5 = hashlib.md5()</span><br><span class="line"><span class="meta">>>> </span>md5.update(<span class="string">'Hello, world!'</span>.encode(<span class="string">'utf-8'</span>))</span><br><span class="line"><span class="meta">>>> </span>md5.hexdigest()</span><br><span class="line"><span class="string">'6cd3556deb0da54bca060b4c39479839'</span></span><br></pre></td></tr></table></figure><h2 id="3-2-SHA1"><a href="#3-2-SHA1" class="headerlink" title="3.2. SHA1"></a>3.2. SHA1</h2><blockquote><p>SHA1算法和MD5使用方法类似,不过生成的摘要更长,摘要越长越安全,但算法会越慢</p></blockquote><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">import</span> hashlib</span><br><span class="line"><span class="meta">>>> </span>sha1 = hashlib.sha1()</span><br><span class="line"><span class="meta">>>> </span>sha1.update(<span class="string">'hello, world!'</span>.encode(<span class="string">'utf-8'</span>))</span><br><span class="line"><span class="meta">>>> </span>asha1.hexdigest()</span><br><span class="line"><span class="string">'1f09d30c707d53f3d16c530dd73d70a6ce7596a9'</span></span><br></pre></td></tr></table></figure><h2 id="3-3-Salt-加盐"><a href="#3-3-Salt-加盐" class="headerlink" title="3.3. Salt(加盐)"></a>3.3. Salt(加盐)</h2><p>md5(password+’the-salt ‘): 这样黑客在不知道’the-salt’ 的情况下,是很难通过MD5反推明文的。不过为了避免两个用户使用了相同的password后生成相同的MD5值,在确定用户无法修改登录名的情况下,会将部分用户名作为’the-salt’的一部分,这样就能保证相同口令的用户存储成不同的MD5值。</p><h1 id="4-hmac"><a href="#4-hmac" class="headerlink" title="4. hmac"></a>4. hmac</h1><blockquote><p>Hmac算法:Keyed-Hashing for Message Authentication,通过一个标准算法,把key混入计算过程中,代替我们的salt算法,可以使得算法更加标准化。</p></blockquote><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">import</span> hmac</span><br><span class="line"><span class="meta">>>> </span>message = <span class="string">b'Hello, world!'</span></span><br><span class="line"><span class="meta">>>> </span>key = <span class="string">b'secret'</span> <span class="comment">#在生产环境中,随机盐是明文保存的。</span></span><br><span class="line"><span class="meta">>>> </span>h = hmac.new(key, message, digestmod=<span class="string">'MD5'</span>)</span><br><span class="line"><span class="meta">>>> </span>h</span><br><span class="line"><hmac.HMAC <span class="built_in">object</span> at <span class="number">0x1087f71d0</span>></span><br><span class="line"><span class="meta">>>> </span>h.hexdigest()</span><br><span class="line"><span class="string">'fa4ee7d173f2d97ee79022d1a7355bcf'</span></span><br></pre></td></tr></table></figure><h1 id="5-itertools-用于操作迭代对象的函数"><a href="#5-itertools-用于操作迭代对象的函数" class="headerlink" title="5. itertools - 用于操作迭代对象的函数"></a>5. itertools - 用于操作迭代对象的函数</h1><h2 id="5-1-count-无限迭代器"><a href="#5-1-count-无限迭代器" class="headerlink" title="5.1. count() - 无限迭代器"></a>5.1. count() - 无限迭代器</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">import</span> itertools</span><br><span class="line"><span class="meta">>>> </span>nums = itertools.count(<span class="number">1</span>) <span class="comment">#从1开始的自然数</span></span><br><span class="line"><span class="meta">>>> </span><span class="keyword">for</span> n <span class="keyword">in</span> nums:</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(n)</span><br><span class="line">...</span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line">...Ctrl + c</span><br></pre></td></tr></table></figure><h2 id="5-2-cycle-将传入的序列无限循环"><a href="#5-2-cycle-将传入的序列无限循环" class="headerlink" title="5.2. cycle() - 将传入的序列无限循环"></a>5.2. cycle() - 将传入的序列无限循环</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span>sequence = itertools.cycle(<span class="string">'abc'</span>)</span><br><span class="line"><span class="meta">>>> </span><span class="keyword">for</span> c <span class="keyword">in</span> sequence:</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(c)</span><br><span class="line"><span class="meta">... </span> </span><br><span class="line">a</span><br><span class="line">b</span><br><span class="line">c</span><br><span class="line">a</span><br><span class="line">...Ctrl + c</span><br></pre></td></tr></table></figure><h2 id="5-3-repeat-提供参数限制重复次数"><a href="#5-3-repeat-提供参数限制重复次数" class="headerlink" title="5.3. repeat() -提供参数限制重复次数"></a>5.3. repeat() -提供参数限制重复次数</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span>ns = itertools.repeat(<span class="string">'ab'</span>, <span class="number">3</span>)</span><br><span class="line"><span class="meta">>>> </span><span class="keyword">for</span> n <span class="keyword">in</span> ns:</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(n)</span><br><span class="line"><span class="meta">... </span> </span><br><span class="line">ab</span><br><span class="line">ab</span><br><span class="line">ab</span><br></pre></td></tr></table></figure><h2 id="5-4-takewhile-在无限迭代中设置结束条件"><a href="#5-4-takewhile-在无限迭代中设置结束条件" class="headerlink" title="5.4. takewhile() - 在无限迭代中设置结束条件"></a>5.4. takewhile() - 在无限迭代中设置结束条件</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span>nums = itertools.count(<span class="number">1</span>)</span><br><span class="line"><span class="meta">>>> </span>ns = itertools.takewhile(<span class="keyword">lambda</span> x: x < <span class="number">4</span>, nums)</span><br><span class="line"><span class="meta">>>> </span>ns</span><br><span class="line"><itertools.takewhile <span class="built_in">object</span> at <span class="number">0x1087e1d70</span>></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">list</span>(ns)</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br></pre></td></tr></table></figure><h2 id="5-5-chain-串联一组迭代对象,形成更大的迭代器"><a href="#5-5-chain-串联一组迭代对象,形成更大的迭代器" class="headerlink" title="5.5. chain() - 串联一组迭代对象,形成更大的迭代器"></a>5.5. chain() - 串联一组迭代对象,形成更大的迭代器</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">for</span> c <span class="keyword">in</span> itertools.chain(<span class="string">'AB'</span>, <span class="string">'YZ'</span>):</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(c)</span><br><span class="line"><span class="meta">... </span> </span><br><span class="line">A</span><br><span class="line">B</span><br><span class="line">Y</span><br><span class="line">Z</span><br></pre></td></tr></table></figure><h2 id="5-6-groupby-把迭代器中相邻的重复元素提取出来"><a href="#5-6-groupby-把迭代器中相邻的重复元素提取出来" class="headerlink" title="5.6. groupby() - 把迭代器中相邻的重复元素提取出来"></a>5.6. groupby() - 把迭代器中相邻的重复元素提取出来</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">for</span> key, group <span class="keyword">in</span> itertools.groupby(<span class="string">'AAABBCAA'</span>):</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(key, <span class="built_in">list</span>(group))</span><br><span class="line">...</span><br><span class="line">A [<span class="string">'A'</span>, <span class="string">'A'</span>, <span class="string">'A'</span>]</span><br><span class="line">B [<span class="string">'B'</span>, <span class="string">'B'</span>]</span><br><span class="line">C [<span class="string">'C'</span>]</span><br><span class="line">A [<span class="string">'A'</span>, <span class="string">'A'</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">#忽略大小写</span></span><br><span class="line"><span class="meta">>>> </span><span class="keyword">for</span> key, group <span class="keyword">in</span> itertools.groupby(<span class="string">'AaaBbCaA'</span>, <span class="keyword">lambda</span> c: c.upper()): <span class="comment">#挑选函数也是通过元素的返回值来判断是否相等的,因此可以通过函数使得'A'和'a'的返回值一致。</span></span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(key, <span class="built_in">list</span>(group))</span><br><span class="line">...</span><br><span class="line">A [<span class="string">'A'</span>, <span class="string">'a'</span>, <span class="string">'a'</span>]</span><br><span class="line">B [<span class="string">'B'</span>, <span class="string">'b'</span>]</span><br><span class="line">C [<span class="string">'C'</span>]</span><br><span class="line">A [<span class="string">'a'</span>, <span class="string">'A'</span>]</span><br></pre></td></tr></table></figure><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><blockquote>
<p>Batteries Included Philosophy - having a rich and versatile standard library which is immediately available, without making the user download separate packages.</p>
</blockquote>
<h1 id="1-datetime"><a href="#1-datetime" class="headerlink" title="1. datetime"></a>1. datetime</h1><blockquote>
<p>Python处理时间和日期的标准库</p>
</blockquote></summary>
<category term="Computer Science" scheme="https://aaronos0.github.io/categories/Computer-Science/"/>
<category term="Python Learning" scheme="https://aaronos0.github.io/tags/Python-Learning/"/>
</entry>
<entry>
<title>Python - Theme 10 RegExp</title>
<link href="https://aaronos0.github.io/2020/09/30/Python-Theme-10-RegExp/"/>
<id>https://aaronos0.github.io/2020/09/30/Python-Theme-10-RegExp/</id>
<published>2020-09-30T21:06:13.000Z</published>
<updated>2020-09-30T21:06:13.000Z</updated>
<content type="html"><![CDATA[<h1 id="1-基本知识"><a href="#1-基本知识" class="headerlink" title="1. 基本知识"></a>1. 基本知识</h1><p>字符串是编程时涉及到的最多的一种数据结构。正则表达式正是一种用来匹配字符串的强有力的武器。它的设计思想是用一种描述性语言来给字符串定义一个规则,用来验证是否匹配。</p><h1 id="2-进阶知识"><a href="#2-进阶知识" class="headerlink" title="2. 进阶知识"></a>2. 进阶知识</h1><p>[0-9a-zA-Z_] : 可以匹配一个数字、字母、下划线;<br>[0-9a-zA-Z_]+ : 可以匹配至少由一个数字、字母、下划线组成的字符串;<br>[a-zA-Z_] [0-9a-zA-Z_] : 可以匹配由字母数字下划线开头,后面至少由一个数字、字母、下划线组成的字符串,即: Python的合法变量;<br>(P|p)ython : 表示Python 或 python<br>^: 开始符,表示行的开头<br>$: 结束符,表示行的结束</p><span id="more"></span><h1 id="3-re模块"><a href="#3-re模块" class="headerlink" title="3. re模块"></a>3. re模块</h1><p>Python提供re模块,包含了所有正则表达式的功能</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">import</span> re</span><br><span class="line"><span class="meta">>>> </span>re.match(<span class="string">r'^\d{3}\-\d{3,8}'</span>, <span class="string">'010-12345'</span>) <span class="comment">#匹配成功返回一个Match对象</span></span><br><span class="line"><re.Match <span class="built_in">object</span>; span=(<span class="number">0</span>, <span class="number">9</span>), match=<span class="string">'010-12345'</span>></span><br><span class="line"><span class="meta">>>> </span>re.match(<span class="string">r'^\d{3}\-\d{3,8}'</span>, <span class="string">'010 12345'</span>) <span class="comment">#匹配失败返回None</span></span><br><span class="line">>>></span><br></pre></td></tr></table></figure><p>常见应用</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">test = <span class="string">'用户输入的字符串'</span></span><br><span class="line"><span class="keyword">if</span> re.match(<span class="string">r'正则表达式'</span>, test):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'ok'</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'failed'</span>)</span><br></pre></td></tr></table></figure><h1 id="4-切分字符串"><a href="#4-切分字符串" class="headerlink" title="4. 切分字符串"></a>4. 切分字符串</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="string">'a b c'</span>.split(<span class="string">' '</span>)</span><br><span class="line">[<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">''</span>, <span class="string">''</span>, <span class="string">'c'</span>]</span><br><span class="line"><span class="meta">>>> </span>re.split(<span class="string">r'\s+'</span>, <span class="string">'a b c'</span>)</span><br><span class="line">[<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>]</span><br><span class="line"><span class="meta">>>> </span>re.split(<span class="string">r'[\s\,]+'</span>, <span class="string">'a,b c d'</span>)</span><br><span class="line">[<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>, <span class="string">'d'</span>]</span><br></pre></td></tr></table></figure><h1 id="5-分组"><a href="#5-分组" class="headerlink" title="5. 分组"></a>5. 分组</h1><p>正则表达式还具有提取子串的强大功能。()表示要提取的Group。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#提取区号和本地号码</span></span><br><span class="line"><span class="meta">>>> </span>m = re.match(<span class="string">r'^(\d{3})-(\d{3,8})$'</span>, <span class="string">'010-12345'</span>)</span><br><span class="line"><span class="meta">>>> </span>m</span><br><span class="line"><re.Match <span class="built_in">object</span>; span=(<span class="number">0</span>, <span class="number">9</span>), match=<span class="string">'010-12345'</span>></span><br><span class="line"><span class="meta">>>> </span>m.group(<span class="number">0</span>)</span><br><span class="line"><span class="string">'010-12345'</span></span><br><span class="line"><span class="meta">>>> </span>m.group(<span class="number">1</span>)</span><br><span class="line"><span class="string">'010'</span></span><br><span class="line"><span class="meta">>>> </span>m.group(<span class="number">2</span>)</span><br><span class="line"><span class="string">'12345'</span></span><br><span class="line"><span class="meta">>>> </span>m.group()</span><br><span class="line"><span class="string">'010-12345'</span></span><br><span class="line"><span class="meta">>>> </span>m.groups()</span><br><span class="line">(<span class="string">'010'</span>, <span class="string">'12345'</span>)</span><br></pre></td></tr></table></figure><h1 id="6-贪婪匹配"><a href="#6-贪婪匹配" class="headerlink" title="6. 贪婪匹配"></a>6. 贪婪匹配</h1><p>正则表达式会尽可能多的匹配字符</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span>re.match(<span class="string">r'^(\d+)(0*)$'</span>, <span class="string">'102300'</span>).groups()</span><br><span class="line">(<span class="string">'102300'</span>, <span class="string">''</span>)</span><br><span class="line"><span class="meta">>>> </span>re.match(<span class="string">r'^(\d+?)(0*)$'</span>, <span class="string">'102300'</span>).groups() <span class="comment">#贪婪符后面放置'?',采用了非贪婪匹配</span></span><br><span class="line">(<span class="string">'1023'</span>, <span class="string">'00'</span>)</span><br></pre></td></tr></table></figure><h1 id="7-编译"><a href="#7-编译" class="headerlink" title="7. 编译"></a>7. 编译</h1><p>当需要大量重复的使用正则表达式时,可以采用预编译方式,提升效率</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span>re_telephone = re.<span class="built_in">compile</span>(<span class="string">r'^(\d{3})-(\d{3,8})$'</span>) <span class="comment">#编译</span></span><br><span class="line"><span class="meta">>>> </span>re_telephone.match(<span class="string">'010-12345'</span>).groups()</span><br><span class="line">(<span class="string">'010'</span>, <span class="string">'12345'</span>)</span><br><span class="line"><span class="meta">>>> </span>re_telephone.match(<span class="string">'010 12345'</span>).groups() <span class="comment">#不合法,会报错</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line"> File <span class="string">"<input>"</span>, line <span class="number">1</span>, <span class="keyword">in</span> <module></span><br><span class="line">AttributeError: <span class="string">'NoneType'</span> <span class="built_in">object</span> has no attribute <span class="string">'groups'</span></span><br></pre></td></tr></table></figure><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><h1 id="1-基本知识"><a href="#1-基本知识" class="headerlink" title="1. 基本知识"></a>1. 基本知识</h1><p>字符串是编程时涉及到的最多的一种数据结构。正则表达式正是一种用来匹配字符串的强有力的武器。它的设计思想是用一种描述性语言来给字符串定义一个规则,用来验证是否匹配。</p>
<h1 id="2-进阶知识"><a href="#2-进阶知识" class="headerlink" title="2. 进阶知识"></a>2. 进阶知识</h1><p>[0-9a-zA-Z_] : 可以匹配一个数字、字母、下划线;<br>[0-9a-zA-Z_]+ : 可以匹配至少由一个数字、字母、下划线组成的字符串;<br>[a-zA-Z_] [0-9a-zA-Z_] : 可以匹配由字母数字下划线开头,后面至少由一个数字、字母、下划线组成的字符串,即: Python的合法变量;<br>(P|p)ython : 表示Python 或 python<br>^: 开始符,表示行的开头<br>$: 结束符,表示行的结束</p></summary>
<category term="Computer Science" scheme="https://aaronos0.github.io/categories/Computer-Science/"/>
<category term="Python Learning" scheme="https://aaronos0.github.io/tags/Python-Learning/"/>
</entry>
<entry>
<title>Python - Theme 9 Process and Thread</title>
<link href="https://aaronos0.github.io/2020/09/30/Python-Theme-9-Process-and-Thread/"/>
<id>https://aaronos0.github.io/2020/09/30/Python-Theme-9-Process-and-Thread/</id>
<published>2020-09-30T21:05:08.000Z</published>
<updated>2020-09-30T21:05:08.000Z</updated>
<content type="html"><![CDATA[<h1 id="0-背景知识"><a href="#0-背景知识" class="headerlink" title="0. 背景知识"></a>0. 背景知识</h1><p>现代操作系统之所以可以执行多任务其实是轮流让各个任务交替执行,由于CPU速度足够快,所以看起来就像是所有任务在同时进行一样。<br>多任务的实现方式:</p><ul><li>多进程模式</li><li>多线程模式</li><li>多进程+多线程模式<br>进程、线程之间的通信、协调、同步、数据共享等问题,增加了编程的复杂性。</li></ul><span id="more"></span><h1 id="1-多进程"><a href="#1-多进程" class="headerlink" title="1. 多进程"></a>1. 多进程</h1><h2 id="1-1-fork-Unix"><a href="#1-1-fork-Unix" class="headerlink" title="1.1. fork() - Unix"></a>1.1. fork() - Unix</h2><p>在Unix操作环境下,系统提供fork()系统调用,将当前进程(也称为父进程)复制一份(称为子进程)。父进程返回子进程的ID,子进程返回0,子进程可以通过getppid()来获取父进程的ID。<br>Apache服务器就是由父进程监听端口,有请求进来时,就会fork出子进程来处理。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">'Process (%s) start...'</span> % os.getpid())</span><br><span class="line">pid = os.fork()</span><br><span class="line"><span class="keyword">if</span> pid == <span class="number">0</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'I am child process (%s) and my parent is %s.'</span> % (os.getpid(), os.getppid()))</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'I (%s) just created a child process (%s).'</span> % (os.getpid(), pid))</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">Process (<span class="number">15807</span>) start...</span><br><span class="line">I (<span class="number">15807</span>) just created a child process (<span class="number">15808</span>).</span><br><span class="line">I am child process (<span class="number">15808</span>) <span class="keyword">and</span> my parent <span class="keyword">is</span> <span class="number">15807.</span></span><br></pre></td></tr></table></figure><h2 id="1-2-multiprocessing-跨平台多进程模块"><a href="#1-2-multiprocessing-跨平台多进程模块" class="headerlink" title="1.2. multiprocessing - 跨平台多进程模块"></a>1.2. multiprocessing - 跨平台多进程模块</h2><blockquote><p>multiprocessing提供一个Process类代表一个进程对象</p></blockquote><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> multiprocessing <span class="keyword">import</span> Process</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 子进程要执行的代码</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">run_proc</span>(<span class="params">name</span>):</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Run child process %s (%s)...'</span> % (name, os.getpid()))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Parent process %s.'</span> % os.getpid())</span><br><span class="line"> p = Process(target=run_proc, args=(<span class="string">'test'</span>,)) <span class="comment">#创建子进程时,只需要传入一个执行函数和函数的参数</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Child process will start.'</span>)</span><br><span class="line"> p.start()</span><br><span class="line"> p.join() <span class="comment">#等待子进程p结束后才向下进行;常用于进程间的同步</span></span><br><span class="line"> <span class="comment">#p.join(5) 可以设置阻塞时间</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Child process end.'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">Parent process <span class="number">54062.</span></span><br><span class="line">Child process will start.</span><br><span class="line">Run child process test (<span class="number">54063</span>)...</span><br><span class="line">Child process end.</span><br></pre></td></tr></table></figure><h2 id="1-3-Pool-进程池"><a href="#1-3-Pool-进程池" class="headerlink" title="1.3. Pool - 进程池"></a>1.3. Pool - 进程池</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> multiprocessing <span class="keyword">import</span> Pool</span><br><span class="line"><span class="keyword">import</span> os, time, random</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">long_time_task</span>(<span class="params">name</span>):</span> <span class="comment">#运行时间</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Run task %s (%s)...'</span> % (name, os.getpid()))</span><br><span class="line"> start = time.time()</span><br><span class="line"> time.sleep(random.random())</span><br><span class="line"> end = time.time()</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Task %s runs %0.2f seconds.'</span> % (name, (end - start)))</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Parent process %s.'</span> % os.getpid())</span><br><span class="line"> p = Pool(<span class="number">4</span>) <span class="comment">#设置进程池为4; 进程池默认为CPU核数</span></span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">5</span>):</span><br><span class="line"> p.apply_async(long_time_task, args=(i,)) <span class="comment">#apply_async为异步非阻塞;apply为阻塞的,类似于单进程串行</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Waiting for all subprocesses done...'</span>)</span><br><span class="line"> p.close() <span class="comment">#关闭进程池后不再接收新的进程(已启动的子进程还会继续)</span></span><br><span class="line"> p.join() <span class="comment">#等待进程池内的所有子进程执行完毕</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'All subprocesses done.'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">Parent process <span class="number">54349.</span></span><br><span class="line">Waiting <span class="keyword">for</span> <span class="built_in">all</span> subprocesses done...</span><br><span class="line">Run task <span class="number">0</span> (<span class="number">54350</span>)...</span><br><span class="line">Run task <span class="number">1</span> (<span class="number">54351</span>)...</span><br><span class="line">Run task <span class="number">2</span> (<span class="number">54352</span>)...</span><br><span class="line">Run task <span class="number">3</span> (<span class="number">54353</span>)...</span><br><span class="line">Task <span class="number">3</span> runs <span class="number">0.46</span> seconds.</span><br><span class="line">Run task <span class="number">4</span> (<span class="number">54353</span>)...</span><br><span class="line">Task <span class="number">0</span> runs <span class="number">0.59</span> seconds.</span><br><span class="line">Task <span class="number">1</span> runs <span class="number">0.68</span> seconds.</span><br><span class="line">Task <span class="number">2</span> runs <span class="number">0.75</span> seconds.</span><br><span class="line">Task <span class="number">4</span> runs <span class="number">0.56</span> seconds.</span><br><span class="line">All subprocesses done.</span><br></pre></td></tr></table></figure><h2 id="1-4-subprocess"><a href="#1-4-subprocess" class="headerlink" title="1.4. subprocess"></a>1.4. subprocess</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#(1). subprocess.run()</span></span><br><span class="line"><span class="meta">>>> </span>subprocess.run([<span class="string">'echo'</span>,<span class="string">'hello'</span>])</span><br><span class="line">hello</span><br><span class="line">CompletedProcess(args=[<span class="string">'echo'</span>, <span class="string">'hello'</span>], returncode=<span class="number">0</span>)</span><br><span class="line"><span class="meta">>>> </span>subprocess.run(<span class="string">'echo hello'</span>, shell = <span class="literal">True</span>)</span><br><span class="line">hello</span><br><span class="line">CompletedProcess(args=<span class="string">'echo hello'</span>, returncode=<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#(2). subprocess.call() - 返回值</span></span><br><span class="line"><span class="meta">>>> </span>res = subprocess.call([<span class="string">'echo'</span>,<span class="string">'hello'</span>])</span><br><span class="line">hello</span><br><span class="line"><span class="meta">>>> </span>res</span><br><span class="line"><span class="number">0</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#(3). subprocess.Popen() - subprocess的使用方法都是对subprocess.Popen的封装</span></span><br><span class="line"><span class="comment">#(3.1).利用subprocess.PIPE可以将多个子进程的输入和输出连接在一起。subprocess.PIPE实际上为文本流提供了一个缓存区</span></span><br><span class="line"><span class="keyword">import</span> subprocess</span><br><span class="line"></span><br><span class="line">child1 = subprocess.Popen([<span class="string">'echo'</span>, <span class="string">'hello\nworld'</span>], stdout=subprocess.PIPE) <span class="comment">#将child1的stdout输出到缓存区</span></span><br><span class="line">child2 = subprocess.Popen([<span class="string">"wc"</span>], stdin=child1.stdout, stdout=subprocess.PIPE) <span class="comment">#child2的stdin读取 PIPE</span></span><br><span class="line">res = child2.communicate() <span class="comment">#communicate()方法从PIPE中读取内容返回;communicate()会阻塞父进程</span></span><br><span class="line"><span class="built_in">print</span>(res)</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">(<span class="string">b' 2 2 12\n'</span>, <span class="literal">None</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#(3.2). 可以利用communicate()方法来使用PIPE给子进程输入</span></span><br><span class="line"><span class="keyword">import</span> subprocess</span><br><span class="line"></span><br><span class="line">p = subprocess.Popen([<span class="string">'python'</span>], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)</span><br><span class="line">output, err = p.communicate(<span class="string">b'print("hello, python")'</span>)</span><br><span class="line"><span class="built_in">print</span>(output, err)</span><br><span class="line"><span class="built_in">print</span>(output.decode(<span class="string">'utf-8'</span>))</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'Exit code:'</span>, p.returncode)</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出: 相当于打开python交互模式, 执行print("hello, python")</span></span><br><span class="line"><span class="string">b'hello, python\n'</span> <span class="string">b''</span></span><br><span class="line">hello, python</span><br><span class="line"></span><br><span class="line">Exit code: <span class="number">0</span></span><br></pre></td></tr></table></figure><h2 id="1-5-进程间通信"><a href="#1-5-进程间通信" class="headerlink" title="1.5. 进程间通信"></a>1.5. 进程间通信</h2><blockquote><p>Python的multiprocessing提供了Queue,Pipes等多种方式来交换数据。</p></blockquote><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> multiprocessing <span class="keyword">import</span> Process, Queue</span><br><span class="line"><span class="keyword">import</span> os, time, random</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 写数据进程执行的代码:</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">write</span>(<span class="params">q</span>):</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Process to write: %s'</span> % os.getpid())</span><br><span class="line"> <span class="keyword">for</span> value <span class="keyword">in</span> [<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>]:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Put %s to queue...'</span> % value)</span><br><span class="line"> q.put(value)</span><br><span class="line"> time.sleep(random.random())</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 读数据进程执行的代码:</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">read</span>(<span class="params">q</span>):</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Process to read: %s'</span> % os.getpid())</span><br><span class="line"> <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line"> value = q.get(<span class="literal">True</span>)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Get %s from queue.'</span> % value)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__==<span class="string">'__main__'</span>:</span><br><span class="line"> <span class="comment"># 父进程创建Queue,并传给各个子进程:</span></span><br><span class="line"> q = Queue()</span><br><span class="line"> pw = Process(target=write, args=(q,))</span><br><span class="line"> pr = Process(target=read, args=(q,))</span><br><span class="line"> <span class="comment"># 启动子进程pw,写入:</span></span><br><span class="line"> pw.start()</span><br><span class="line"> <span class="comment"># 启动子进程pr,读取:</span></span><br><span class="line"> pr.start()</span><br><span class="line"> <span class="comment"># 等待pw结束:</span></span><br><span class="line"> pw.join()</span><br><span class="line"> <span class="comment"># pr进程里是死循环,无法等待其结束,只能强行终止:</span></span><br><span class="line"> pr.terminate()</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">Process to write: <span class="number">5305</span></span><br><span class="line">Put A to queue...</span><br><span class="line">Process to read: <span class="number">5306</span></span><br><span class="line">Get A <span class="keyword">from</span> queue.</span><br><span class="line">Put B to queue...</span><br><span class="line">Get B <span class="keyword">from</span> queue.</span><br><span class="line">Put C to queue...</span><br><span class="line">Get C <span class="keyword">from</span> queue.</span><br></pre></td></tr></table></figure><h1 id="2-多线程"><a href="#2-多线程" class="headerlink" title="2. 多线程"></a>2. 多线程</h1><blockquote><p>线程是操作系统直接支持的执行单元,一个进程至少有一个线程。</p></blockquote><h2 id="2-1-高级模块-threading"><a href="#2-1-高级模块-threading" class="headerlink" title="2.1. 高级模块-threading"></a>2.1. 高级模块-threading</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time, threading</span><br><span class="line"></span><br><span class="line"><span class="comment"># 新线程执行的代码:</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">loop</span>():</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'thread %s is running...'</span> % threading.current_thread().name)</span><br><span class="line"> n = <span class="number">0</span></span><br><span class="line"> <span class="keyword">while</span> n < <span class="number">3</span>:</span><br><span class="line"> n = n + <span class="number">1</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'thread %s >>> %s'</span> % (threading.current_thread().name, n))</span><br><span class="line"> time.sleep(<span class="number">1</span>)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'thread %s ended.'</span> % threading.current_thread().name)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">'thread %s is running...'</span> % threading.current_thread().name) <span class="comment">#任何进程默认会启动一个线程,称为主线程 MainThread</span></span><br><span class="line">t = threading.Thread(target=loop, name=<span class="string">'LoopThread'</span>) <span class="comment">#这里传入子线程要执行的函数;并且可以自定义线程名。若不定义,系统会自动命名为:Thread-1,Thread-2</span></span><br><span class="line">t.start()</span><br><span class="line">t.join()</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'thread %s ended.'</span> % threading.current_thread().name)</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">thread MainThread <span class="keyword">is</span> running...</span><br><span class="line">thread LoopThread <span class="keyword">is</span> running...</span><br><span class="line">thread LoopThread >>> <span class="number">1</span></span><br><span class="line">thread LoopThread >>> <span class="number">2</span></span><br><span class="line">thread LoopThread >>> <span class="number">3</span></span><br><span class="line">thread LoopThread ended.</span><br><span class="line">thread MainThread ended.</span><br></pre></td></tr></table></figure><h2 id="2-2-Lock"><a href="#2-2-Lock" class="headerlink" title="2.2. Lock"></a>2.2. Lock</h2><p>多进程:同一个变量,各自拷贝一份存于各个进程中,互不影响。<br>多线程:所有的变量由所有线程共享,因此可能会造成多个线程同时修改一个变量,导致出错</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> threading</span><br><span class="line"></span><br><span class="line"><span class="comment"># 假定这是银行存款:</span></span><br><span class="line">balance = <span class="number">0</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">change_it</span>(<span class="params">n</span>):</span></span><br><span class="line"> <span class="comment"># 先存后取,结果应该为0:</span></span><br><span class="line"> <span class="keyword">global</span> balance</span><br><span class="line"> balance = balance + n</span><br><span class="line"> balance = balance - n</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">run_thread</span>(<span class="params">n</span>):</span></span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">100000</span>):</span><br><span class="line"> change_it(n)</span><br><span class="line"></span><br><span class="line">t1 = threading.Thread(target=run_thread, args=(<span class="number">100</span>,))</span><br><span class="line">t2 = threading.Thread(target=run_thread, args=(<span class="number">50</span>,))</span><br><span class="line">t1.start()</span><br><span class="line">t2.start()</span><br><span class="line">t1.join()</span><br><span class="line">t2.join()</span><br><span class="line"><span class="built_in">print</span>(balance)</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line"><span class="number">0</span>/<span class="number">50</span>/-<span class="number">50</span>/<span class="number">100</span> ... <span class="comment">#由于两个线程在同时操作一个变量,因此出现了错误</span></span><br></pre></td></tr></table></figure><p>threading.Lock() 创建锁: 由于创建了锁,因此其他线程不能同时执行change_it(n)函数;又由于锁只有一个,同一时刻只有一个线程持有该锁。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> threading</span><br><span class="line"></span><br><span class="line"><span class="comment"># 假定这是银行存款:</span></span><br><span class="line">balance = <span class="number">0</span></span><br><span class="line">lock = threading.Lock()</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">change_it</span>(<span class="params">n</span>):</span></span><br><span class="line"> <span class="comment"># 先存后取,结果应该为0:</span></span><br><span class="line"> <span class="keyword">global</span> balance</span><br><span class="line"> balance = balance + n</span><br><span class="line"> balance = balance - n</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">run_thread</span>(<span class="params">n</span>):</span></span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">100000</span>):</span><br><span class="line"> lock.acquire() <span class="comment">#获取锁</span></span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> change_it(n)</span><br><span class="line"> <span class="keyword">finally</span>:</span><br><span class="line"> lock.release() <span class="comment">#释放锁</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">t1 = threading.Thread(target=run_thread, args=(<span class="number">100</span>,))</span><br><span class="line">t2 = threading.Thread(target=run_thread, args=(<span class="number">50</span>,))</span><br><span class="line">t1.start()</span><br><span class="line">t2.start()</span><br><span class="line">t1.join()</span><br><span class="line">t2.join()</span><br><span class="line"><span class="built_in">print</span>(balance)</span><br></pre></td></tr></table></figure><h2 id="2-3-锁的优劣"><a href="#2-3-锁的优劣" class="headerlink" title="2.3. 锁的优劣"></a>2.3. 锁的优劣</h2><p>好处:确保了某段关键代码同时只能由一个线程执行<br>坏处:</p><ul><li>阻止了多线程的并发能力,大大降低了效率</li><li>当存在多个锁时,不同线程持有不同的锁,当试图获取对方持有的锁时,可能会造成死锁,导致多个线程全部挂起</li></ul><h2 id="2-4-多核CPU"><a href="#2-4-多核CPU" class="headerlink" title="2.4. 多核CPU"></a>2.4. 多核CPU</h2><p>由于Python有GIL(Global Interpreter Lock)全局锁,因此不能利用多线程实现多核任务。只能使用多进程来实现。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#多线程(尽管创建了和系统核数相同数量的线程,但是也只能跑满单核):</span></span><br><span class="line"><span class="keyword">import</span> threading, multiprocessing</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">loop</span>():</span></span><br><span class="line"> <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(multiprocessing.cpu_count()):</span><br><span class="line"> t = threading.Thread(target=loop)</span><br><span class="line"> t.start()</span><br><span class="line"></span><br><span class="line"><span class="comment">#多进程(跑满CPU)</span></span><br><span class="line"><span class="keyword">import</span> multiprocessing, os</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">loop</span>():</span></span><br><span class="line"> <span class="built_in">print</span>(os.getpid())</span><br><span class="line"> <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line">numCpu = multiprocessing.cpu_count()</span><br><span class="line">p = multiprocessing.Pool(numCpu)</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(numCpu):</span><br><span class="line"> p.apply_async(loop)</span><br><span class="line"></span><br><span class="line">p.close()</span><br><span class="line">p.join()</span><br></pre></td></tr></table></figure><h1 id="3-ThreadLocal"><a href="#3-ThreadLocal" class="headerlink" title="3. ThreadLocal"></a>3. ThreadLocal</h1><p>多线程环境下,每个线程使用自己的局部变量比全局变量好,这样一来能减少各个线程间的相互影响。<br>ThreadLocal 解决了参数在一个线程中的各个函数之间相互传递的问题。ThreadLocal虽然是全局变量,但是每个线程只能读写自己的独立副本。<br>ThreadLocal 最常用的地方就是为每个线程绑定一个数据库连接、Http请求,用户身份信息等,这样一个线程所有用到的处理函数就可以非常方便的访问这些资源了。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> threading</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建全局ThreadLocal对象:</span></span><br><span class="line">local_school = threading.local()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">process_student</span>():</span></span><br><span class="line"> <span class="comment"># 获取当前线程关联的student:</span></span><br><span class="line"> std = local_school.student</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Hello, %s (in %s)'</span> % (std, threading.current_thread().name))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">process_thread</span>(<span class="params">name</span>):</span></span><br><span class="line"> <span class="comment"># 绑定ThreadLocal的student:</span></span><br><span class="line"> local_school.student = name</span><br><span class="line"> process_student()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">t1 = threading.Thread(target=process_thread, args=(<span class="string">'Alice'</span>,), name=<span class="string">'Thread-A'</span>)</span><br><span class="line">t2 = threading.Thread(target=process_thread, args=(<span class="string">'Bob'</span>,), name=<span class="string">'Thread-B'</span>)</span><br><span class="line">t1.start()</span><br><span class="line">t2.start()</span><br><span class="line">t1.join()</span><br><span class="line">t2.join()</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">Hello, Alice (<span class="keyword">in</span> Thread-A)</span><br><span class="line">Hello, Bob (<span class="keyword">in</span> Thread-B)</span><br></pre></td></tr></table></figure><h1 id="4-进程Vs线程"><a href="#4-进程Vs线程" class="headerlink" title="4. 进程Vs线程"></a>4. 进程Vs线程</h1><h2 id="4-1-多任务"><a href="#4-1-多任务" class="headerlink" title="4.1. 多任务"></a>4.1. 多任务</h2><p>通常多任务都会设计为:Master-Worker 模式,Master来分配任务,worker来执行任务。<br>多进程:稳定性高,资源消耗大。(早期的Apache服务)<br>多线程:由于共享的模式,子线程可能会导致整个进程挂掉,但是通常比多进程快一些</p><h2 id="4-2-任务切换"><a href="#4-2-任务切换" class="headerlink" title="4.2. 任务切换"></a>4.2. 任务切换</h2><p>无论是多进程还是多线程,只要数量一多,效率就会下降。因为切换作业是有代价的,需要保存现场环境(CPU寄存器状态、内存页等等),切换的速度虽然快,但如果作业一多,也是相当耗费时间。</p><h2 id="4-3-计算密集型-Vs-IO密集型"><a href="#4-3-计算密集型-Vs-IO密集型" class="headerlink" title="4.3. 计算密集型 Vs IO密集型"></a>4.3. 计算密集型 Vs IO密集型</h2><p>计算密集型:这类任务通常需要大量计算,消耗CPU,如:视频解码。这种类型虽然也可以多任务完成,但是任务越多,花在切换任务的时间越多,CPU执行效率越低。如果要高效的利用CPU,就要使计算任务的数量等于CPU核心数。同时,代码的执行效率也至关重要,选择效率高的语言,如:C,比较合适。<br>IO密集型:这类任务通常是主要涉及网络、磁盘的任务。对CPU消耗少,大部分时间都用在等待IO操作完成,因此一般情况下,任务越多,效率越高,比如:Wed应用。但是,在这种情况下使用执行效率高的编程语言并不会产生很大作用,因此可以选择有利于开发效率的语言,例如:python</p><h2 id="4-4-异步IO"><a href="#4-4-异步IO" class="headerlink" title="4.4. 异步IO"></a>4.4. 异步IO</h2><p>由于CPU和IO之间的速度差异,大多数时间都会花在IO等待上,因此我们才需要多进程和多线程来并发的执行任务。不过现代操作系统也做出了巨大改进,支持了异步IO。这种全新的模型称为事件驱动模型,如Nginx,在单核CPU上采用单进程模式就可以高效的支持多任务了。Python语言中的单线程异步模型称为: 协程,这样一来就可以基于事件驱动的来编写多任务程序了。</p><h1 id="5-分布式进程"><a href="#5-分布式进程" class="headerlink" title="5. 分布式进程"></a>5. 分布式进程</h1><p>Python的multiprocessing模块不但支持多进程,其中的managers子模块还支持把多进程分布到多个机器上。依靠网络通信,一个服务进程可以作为调度者,将任务分布到其他机器上。<br>将通过Queue通信的多进程分布到两台机器上: 通过managers模块把Queue通过网络暴露出去即可。</p><h2 id="5-1-Master机器-进程"><a href="#5-1-Master机器-进程" class="headerlink" title="5.1. Master机器(进程)"></a>5.1. Master机器(进程)</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> random, time, queue</span><br><span class="line"><span class="keyword">from</span> multiprocessing.managers <span class="keyword">import</span> BaseManager</span><br><span class="line"></span><br><span class="line"><span class="comment"># 发送任务的队列</span></span><br><span class="line">task_queue = queue.Queue()</span><br><span class="line"><span class="comment"># 接收结果的队列</span></span><br><span class="line">result_queue = queue.Queue()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 继承BaseManager</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">QueueManager</span>(<span class="params">BaseManager</span>):</span></span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 将两个Queue注册到网络上,callable参数关联了Queue对象</span></span><br><span class="line">QueueManager.register(<span class="string">'get_task_queue'</span>, <span class="built_in">callable</span>=<span class="keyword">lambda</span>: task_queue)</span><br><span class="line">QueueManager.register(<span class="string">'get_result_queue'</span>, <span class="built_in">callable</span>=<span class="keyword">lambda</span>: result_queue)</span><br><span class="line"><span class="comment"># 绑定端口8000,设置验证码: 'test'</span></span><br><span class="line">manager = QueueManager(address=(<span class="string">''</span>, <span class="number">8000</span>), authkey=<span class="string">b'test'</span>)</span><br><span class="line"><span class="comment"># 启动Queue</span></span><br><span class="line">manager.start()</span><br><span class="line"><span class="comment"># 通过网络访问的Queue对象</span></span><br><span class="line">task = manager.get_task_queue()</span><br><span class="line">result = manager.get_result_queue()</span><br><span class="line"><span class="comment"># 放入几个任务</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">5</span>):</span><br><span class="line"> n = random.randint(<span class="number">0</span>, <span class="number">1000</span>)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Put task %d...'</span> % n)</span><br><span class="line"> task.put(n)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 从result队列读取结果</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">'Try get results...'</span>)</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">5</span>):</span><br><span class="line"> r = result.get(timeout=<span class="number">10</span>)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Result: %s'</span> % r)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 关闭</span></span><br><span class="line">manager.shutdown()</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'Master Exit.'</span>)</span><br></pre></td></tr></table></figure><h2 id="5-2-Worker机器-进程-因为Queue对象存储在Master的进程中,因此worker中并没有创建Queue的代码"><a href="#5-2-Worker机器-进程-因为Queue对象存储在Master的进程中,因此worker中并没有创建Queue的代码" class="headerlink" title="5.2. Worker机器(进程) : 因为Queue对象存储在Master的进程中,因此worker中并没有创建Queue的代码"></a>5.2. Worker机器(进程) : 因为Queue对象存储在Master的进程中,因此worker中并没有创建Queue的代码</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time, sys, queue</span><br><span class="line"><span class="keyword">from</span> multiprocessing.managers <span class="keyword">import</span> BaseManager</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建类似的QueueManger</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">QueueManager</span>(<span class="params">BaseManager</span>):</span></span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 由于QueueManager只从网络上获取Queue, 因此注册时只提供名字</span></span><br><span class="line">QueueManager.register(<span class="string">'get_task_queue'</span>)</span><br><span class="line">QueueManager.register(<span class="string">'get_result_queue'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 连接到Master服务器</span></span><br><span class="line">server_addr = <span class="string">'127.0.0.1'</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">'Connect to server %s...'</span> % server_addr)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 端口和验证码与Master保持一致</span></span><br><span class="line">m = QueueManager(address=(<span class="string">''</span>, <span class="number">8000</span>), authkey=<span class="string">b'test'</span>)</span><br><span class="line"><span class="comment"># 通过网络连接</span></span><br><span class="line">m.connect()</span><br><span class="line"><span class="comment"># 获取Queue的对象</span></span><br><span class="line">task = m.get_task_queue()</span><br><span class="line">result = m.get_result_queue()</span><br><span class="line"><span class="comment"># 从task队列读取任务,并把结果写入result队列</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">5</span>):</span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> n = task.get(timeout=<span class="number">1</span>)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Run task %d * %d'</span> % (n, n))</span><br><span class="line"> r = <span class="string">'%d * %d = %d'</span> % (n, n, n*n)</span><br><span class="line"> time.sleep(<span class="number">1</span>)</span><br><span class="line"> result.put(r)</span><br><span class="line"> <span class="keyword">except</span> queue.Empty:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Task queue is empty.'</span>)</span><br><span class="line"><span class="comment"># 处理结果</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">'Worker Exit.'</span>)</span><br></pre></td></tr></table></figure><h2 id="5-3-输出"><a href="#5-3-输出" class="headerlink" title="5.3. 输出"></a>5.3. 输出</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">(<span class="number">1</span>). Master</span><br><span class="line">Try get results...</span><br><span class="line">Result: <span class="number">463</span> * <span class="number">463</span> = <span class="number">214369</span></span><br><span class="line">Result: <span class="number">322</span> * <span class="number">322</span> = <span class="number">103684</span></span><br><span class="line">Result: <span class="number">567</span> * <span class="number">567</span> = <span class="number">321489</span></span><br><span class="line">Result: <span class="number">372</span> * <span class="number">372</span> = <span class="number">138384</span></span><br><span class="line">Result: <span class="number">120</span> * <span class="number">120</span> = <span class="number">14400</span></span><br><span class="line">Master Exit.</span><br><span class="line"></span><br><span class="line">(<span class="number">2</span>). Worker</span><br><span class="line">Connect to server <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>...</span><br><span class="line">Run task <span class="number">463</span> * <span class="number">463</span></span><br><span class="line">Run task <span class="number">322</span> * <span class="number">322</span></span><br><span class="line">Run task <span class="number">567</span> * <span class="number">567</span></span><br><span class="line">Run task <span class="number">372</span> * <span class="number">372</span></span><br><span class="line">Run task <span class="number">120</span> * <span class="number">120</span></span><br><span class="line">Worker Exit.</span><br></pre></td></tr></table></figure><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><h1 id="0-背景知识"><a href="#0-背景知识" class="headerlink" title="0. 背景知识"></a>0. 背景知识</h1><p>现代操作系统之所以可以执行多任务其实是轮流让各个任务交替执行,由于CPU速度足够快,所以看起来就像是所有任务在同时进行一样。<br>多任务的实现方式:</p>
<ul>
<li>多进程模式</li>
<li>多线程模式</li>
<li>多进程+多线程模式<br>进程、线程之间的通信、协调、同步、数据共享等问题,增加了编程的复杂性。</li>
</ul></summary>
<category term="Computer Science" scheme="https://aaronos0.github.io/categories/Computer-Science/"/>
<category term="Python Learning" scheme="https://aaronos0.github.io/tags/Python-Learning/"/>
</entry>
<entry>
<title>Python - Theme 8 IO programming</title>
<link href="https://aaronos0.github.io/2020/09/30/Python-Theme-8-IO-programming/"/>
<id>https://aaronos0.github.io/2020/09/30/Python-Theme-8-IO-programming/</id>
<published>2020-09-30T21:03:45.000Z</published>
<updated>2020-09-30T21:03:45.000Z</updated>
<content type="html"><![CDATA[<h1 id="0-知识背景"><a href="#0-知识背景" class="headerlink" title="0. 知识背景"></a>0. 知识背景</h1><p>IO- Input/Output: 通常涉及数据交换的地方都需要IO接口,例如: 磁盘、网络等<br>基本概念:input, output, stream<br>存在问题:输入和输出速度不匹配<br>解决方法:同步、异步(回调: 好了叫我, 异步: 好了没…好了没)</p><span id="more"></span><h1 id="1-文件读写"><a href="#1-文件读写" class="headerlink" title="1. 文件读写"></a>1. 文件读写</h1><p>在磁盘上读写文件的功能都是由操作系统提供的,不过不允许程序直接操作磁盘。因此,读写文件其实是程序请求操作系统打开一个文件对象(文件描述符)来实现读写。</p><h2 id="1-1-读文件"><a href="#1-1-读文件" class="headerlink" title="1.1. 读文件"></a>1.1. 读文件</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#示例文件内容(当前路径下)</span></span><br><span class="line"><span class="comment"># This is for read and write test</span></span><br><span class="line">This <span class="keyword">is</span> 1st line</span><br><span class="line">This <span class="keyword">is</span> 2nd line</span><br><span class="line">This <span class="keyword">is</span> 3rd line</span><br><span class="line"></span><br><span class="line"><span class="comment">#一次简单的读文件过程</span></span><br><span class="line"><span class="meta">>>> </span>f = <span class="built_in">open</span>(<span class="string">'test'</span>, <span class="string">'r'</span>) <span class="comment">#打开当前目录下名为test的文件</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line"> File <span class="string">"<input>"</span>, line <span class="number">1</span>, <span class="keyword">in</span> <module></span><br><span class="line">FileNotFoundError: [Errno <span class="number">2</span>] No such file <span class="keyword">or</span> directory: <span class="string">'test'</span> <span class="comment">#如果文件不存在,就会抛出错误</span></span><br><span class="line"><span class="meta">>>> </span>f = <span class="built_in">open</span>(<span class="string">'rwtest'</span>, <span class="string">'r'</span>) <span class="comment">#成功打开了一个文件,'r':读</span></span><br><span class="line"><span class="meta">>>> </span>f.read() <span class="comment">#一次性把文件内容全部读入内存,然后打印出来</span></span><br><span class="line"><span class="string">'# This is for read and write test\nThis is 1st line\nThis is 2nd line\nThis is 3rd line'</span></span><br><span class="line"><span class="meta">>>> </span>f.close() <span class="comment">#关闭文件,避免资源浪费</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#由于在读写文件的时候无论如何都要close(),python提供了一种简洁通用的写法</span></span><br><span class="line">(<span class="number">1</span>). read() -小文件一次性读取</span><br><span class="line"><span class="meta">>>> </span><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">'rwtest'</span>, <span class="string">'r'</span>) <span class="keyword">as</span> f:</span><br><span class="line"><span class="meta">... </span> content = f.read()</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(<span class="built_in">type</span>(content))</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(content)</span><br><span class="line">...</span><br><span class="line"><<span class="class"><span class="keyword">class</span> '<span class="title">str</span>'></span></span><br><span class="line"><span class="class"># <span class="title">This</span> <span class="title">is</span> <span class="title">for</span> <span class="title">read</span> <span class="title">and</span> <span class="title">write</span> <span class="title">test</span></span></span><br><span class="line"><span class="class"><span class="title">This</span> <span class="title">is</span> 1<span class="title">st</span> <span class="title">line</span></span></span><br><span class="line"><span class="class"><span class="title">This</span> <span class="title">is</span> 2<span class="title">nd</span> <span class="title">line</span></span></span><br><span class="line"><span class="class"><span class="title">This</span> <span class="title">is</span> 3<span class="title">rd</span> <span class="title">line</span></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class">(<span class="params"><span class="number">2</span></span>). <span class="title">read</span>(<span class="params">size</span>) -因内存考虑,可以反复的按照<span class="title">bytes</span>读取</span></span><br><span class="line"><span class="class">>>> <span class="title">with</span> <span class="title">open</span>(<span class="params"><span class="string">'rwtest'</span>, <span class="string">'r'</span></span>) <span class="title">as</span> <span class="title">f</span>:</span></span><br><span class="line"><span class="meta">... </span> content = f.read(<span class="number">8</span>) <span class="comment">#size: bytes</span></span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(<span class="built_in">type</span>(content))</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(content)</span><br><span class="line">...</span><br><span class="line"><<span class="class"><span class="keyword">class</span> '<span class="title">str</span>'></span></span><br><span class="line"><span class="class"># <span class="title">This</span> <span class="title">i</span></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class">(<span class="params"><span class="number">2</span></span>). <span class="title">readline</span>() - 文件逐行读入,并将'/<span class="title">n</span>'也读入了,因此最好<span class="title">splitlines</span>()掉换行符</span></span><br><span class="line"><span class="class">>>> <span class="title">with</span> <span class="title">open</span>(<span class="params"><span class="string">'rwtest'</span>, <span class="string">'r'</span></span>) <span class="title">as</span> <span class="title">f</span>:</span></span><br><span class="line"><span class="meta">... </span> line = f.readline()</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(<span class="built_in">type</span>(line))</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(line)</span><br><span class="line"><span class="meta">... </span> <span class="keyword">while</span> line:</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(line)</span><br><span class="line"><span class="meta">... </span> line = f.readline()</span><br><span class="line">...</span><br><span class="line"><<span class="class"><span class="keyword">class</span> '<span class="title">str</span>'></span></span><br><span class="line"><span class="class"># <span class="title">This</span> <span class="title">is</span> <span class="title">for</span> <span class="title">read</span> <span class="title">and</span> <span class="title">write</span> <span class="title">test</span></span></span><br><span class="line"><span class="class"># <span class="title">This</span> <span class="title">is</span> <span class="title">for</span> <span class="title">read</span> <span class="title">and</span> <span class="title">write</span> <span class="title">test</span></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"><span class="title">This</span> <span class="title">is</span> 1<span class="title">st</span> <span class="title">line</span></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"><span class="title">This</span> <span class="title">is</span> 2<span class="title">nd</span> <span class="title">line</span></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"><span class="title">This</span> <span class="title">is</span> 3<span class="title">rd</span> <span class="title">line</span></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class">(<span class="params"><span class="number">3</span></span>). <span class="title">readlines</span>() - 读取整个文件所有行,存入<span class="title">list</span>,每行为一个元素</span></span><br><span class="line"><span class="class">>>> <span class="title">with</span> <span class="title">open</span>(<span class="params"><span class="string">'rwtest'</span>, <span class="string">'r'</span></span>) <span class="title">as</span> <span class="title">f</span>:</span></span><br><span class="line"><span class="meta">... </span> lines = f.readlines()</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(<span class="built_in">type</span>(lines))</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(lines)</span><br><span class="line">...</span><br><span class="line"><<span class="class"><span class="keyword">class</span> '<span class="title">list</span>'></span></span><br><span class="line"><span class="class">['# <span class="title">This</span> <span class="title">is</span> <span class="title">for</span> <span class="title">read</span> <span class="title">and</span> <span class="title">write</span> <span class="title">test</span>\<span class="title">n</span>', '<span class="title">This</span> <span class="title">is</span> 1<span class="title">st</span> <span class="title">line</span>\<span class="title">n</span>', '<span class="title">This</span> <span class="title">is</span> 2<span class="title">nd</span> <span class="title">line</span>\<span class="title">n</span>', '<span class="title">This</span> <span class="title">is</span> 3<span class="title">rd</span> <span class="title">line</span>']</span></span><br></pre></td></tr></table></figure><h2 id="1-2-写文件"><a href="#1-2-写文件" class="headerlink" title="1.2. 写文件"></a>1.2. 写文件</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">'test'</span>,<span class="string">'w'</span>) <span class="keyword">as</span> f: <span class="comment">#如果文件不存在就直接创建并写入,如果存在就覆盖</span></span><br><span class="line"><span class="meta">... </span> f.write(<span class="string">'Hello'</span>)</span><br><span class="line"><span class="meta">... </span> </span><br><span class="line"><span class="meta">>>> </span><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">'test'</span>,<span class="string">'r'</span>) <span class="keyword">as</span> f:</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(f.read())</span><br><span class="line">...</span><br><span class="line">Hello</span><br><span class="line"></span><br><span class="line"><span class="meta">>>> </span><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">'rwtest'</span>,<span class="string">'a'</span>) <span class="keyword">as</span> f: <span class="comment">#'a':在文件后追加</span></span><br><span class="line"><span class="meta">... </span> f.write(<span class="string">'Hello'</span>)</span><br><span class="line">...</span><br><span class="line"><span class="meta">>>> </span><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">'rwtest'</span>,<span class="string">'r'</span>) <span class="keyword">as</span> f:</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(f.read())</span><br><span class="line">...</span><br><span class="line"><span class="comment"># This is for read and write test</span></span><br><span class="line">This <span class="keyword">is</span> 1st line</span><br><span class="line">This <span class="keyword">is</span> 2nd line</span><br><span class="line">This <span class="keyword">is</span> 3rd lineHello</span><br></pre></td></tr></table></figure><h2 id="1-3-二进制文件"><a href="#1-3-二进制文件" class="headerlink" title="1.3. 二进制文件"></a>1.3. 二进制文件</h2><blockquote><p>图片、视频等都为二进制文件</p></blockquote><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">'WechatIMG191.png'</span>, <span class="string">'rb'</span>) <span class="keyword">as</span> f:</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(f.read())</span><br><span class="line">...</span><br><span class="line"><span class="string">b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x01,......'</span></span><br></pre></td></tr></table></figure><h2 id="1-4-字符编码"><a href="#1-4-字符编码" class="headerlink" title="1.4. 字符编码"></a>1.4. 字符编码</h2><blockquote><p>读取非UTF-8的文本文件,需要给open()函数传入’encoding=’ 参数</p></blockquote><h1 id="2-StringIO-和-BytesIO-python特性-类文件对象-file-like-object"><a href="#2-StringIO-和-BytesIO-python特性-类文件对象-file-like-object" class="headerlink" title="2. StringIO 和 BytesIO - python特性: 类文件对象(file-like object)"></a>2. StringIO 和 BytesIO - python特性: 类文件对象(file-like object)</h1><p>如果短时间的重复利用,也不希望持久化且对速度的要求较高,则可以使用内存级别的IO。</p><h2 id="2-1-StringIO"><a href="#2-1-StringIO" class="headerlink" title="2.1. StringIO"></a>2.1. StringIO</h2><p>在内存中有一个标志位的概念,向里面写入,标志位会后移到下一个空白处;然而读取数据的时候也是从标志位开始读的,因此需要手动移动标志位。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">from</span> io <span class="keyword">import</span> StringIO</span><br><span class="line">s = StringIO()</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">type</span>(s)</span><br><span class="line"><<span class="class"><span class="keyword">class</span> '<span class="title">_io</span>.<span class="title">StringIO</span>'></span></span><br><span class="line"><span class="class">>>> <span class="title">s</span>.<span class="title">write</span>(<span class="params"><span class="string">'Hello\nWorld\n!'</span></span>)</span></span><br><span class="line"><span class="class">13</span></span><br><span class="line"><span class="class">>>> <span class="title">s</span>.<span class="title">seek</span>(<span class="params"><span class="number">0</span></span>) #操作标志位移动</span></span><br><span class="line"><span class="class">0</span></span><br><span class="line"><span class="class">>>> <span class="title">s</span>.<span class="title">read</span>()</span></span><br><span class="line"><span class="class">'<span class="title">Hello</span>\<span class="title">nWorld</span>\<span class="title">n</span>!'</span></span><br><span class="line"><span class="class">>>> <span class="title">s</span>.<span class="title">getvalue</span>() #直接获取全部值</span></span><br><span class="line"><span class="class">'<span class="title">Hello</span>\<span class="title">nWorld</span>\<span class="title">n</span>!'</span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class">>>> <span class="title">f</span> = <span class="title">StringIO</span>(<span class="params"><span class="string">'Hello\nWorld\n!'</span></span>)</span></span><br><span class="line"><span class="class">>>> <span class="title">while</span> <span class="title">True</span>:</span></span><br><span class="line"><span class="meta">... </span> s = f.readline() <span class="comment">#这里和读写文件的方法一致</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">if</span> s == <span class="string">""</span>:</span><br><span class="line"><span class="meta">... </span> <span class="keyword">break</span></span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(s.strip())</span><br><span class="line">...</span><br><span class="line">Hello</span><br><span class="line">World</span><br><span class="line">!</span><br></pre></td></tr></table></figure><h2 id="2-2-BytesIO"><a href="#2-2-BytesIO" class="headerlink" title="2.2. BytesIO"></a>2.2. BytesIO</h2><p>StringIO用于字符串的存储,对于图像、视频等Bytes类型的内容就需要用到BytesIO对象。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">from</span> io <span class="keyword">import</span> BytesIO</span><br><span class="line"><span class="meta">>>> </span>f = BytesIO()</span><br><span class="line"><span class="meta">>>> </span>f.write(<span class="string">"中国"</span>.encode(<span class="string">'utf-8'</span>))</span><br><span class="line"><span class="number">6</span></span><br><span class="line"><span class="meta">>>> </span>f.getvalue()</span><br><span class="line"><span class="string">b'\xe4\xb8\xad\xe5\x9b\xbd'</span></span><br><span class="line"><span class="meta">>>> </span>f.close()</span><br></pre></td></tr></table></figure><h1 id="3-操作文件和目录"><a href="#3-操作文件和目录" class="headerlink" title="3. 操作文件和目录"></a>3. 操作文件和目录</h1><p>python的os模块可以直接调用操作系统提供的接口函数</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">import</span> os</span><br><span class="line"><span class="meta">>>> </span>os.name <span class="comment">#posix指Unix系列</span></span><br><span class="line"><span class="string">'posix'</span></span><br><span class="line"><span class="meta">>>> </span>os.uname() <span class="comment">#显示了更详细的信息</span></span><br><span class="line">posix.uname_result(sysname=<span class="string">'Darwin'</span>, nodename=<span class="string">'AarondeMacBookPro.local'</span>,....太多了省略)</span><br><span class="line"><span class="meta">>>> </span>os.environ <span class="comment">#显示系统环境信息</span></span><br><span class="line">environ({<span class="string">'PATH'</span>: <span class="string">'/Users/...'</span>, ....,<span class="string">'HOME'</span>: <span class="string">'...'</span>})</span><br><span class="line"><span class="meta">>>> </span>os.environ.get(<span class="string">'PATH'</span>) <span class="comment">#可以只获取某个信息</span></span><br><span class="line"><span class="string">'/Users/...'</span></span><br><span class="line"><span class="meta">>>> </span>os.path.abspath(<span class="string">'.'</span>) <span class="comment">#显示当前绝对路径</span></span><br><span class="line"><span class="string">'/Users/...'</span></span><br><span class="line"><span class="meta">>>> </span>os.path.join(<span class="string">'/user/user1'</span>,<span class="string">'testdir'</span>) <span class="comment">#拼凑路径字符串</span></span><br><span class="line"><span class="string">'/user/user1/testdir'</span></span><br><span class="line"><span class="meta">>>> </span>os.path.split(<span class="string">'/user/user1/testdir'</span>) <span class="comment">#拆分路径字符串</span></span><br><span class="line">(<span class="string">'/user/user1'</span>, <span class="string">'testdir'</span>)</span><br><span class="line"><span class="meta">>>> </span>os.path.splitext(<span class="string">'/user/user1/test.txt'</span>) <span class="comment">#文件后缀</span></span><br><span class="line">(<span class="string">'/user/user1/test'</span>, <span class="string">'.txt'</span>)</span><br><span class="line"><span class="meta">>>> </span>os.listdir() <span class="comment">#当前路径下文件</span></span><br><span class="line">[<span class="string">'rwtest'</span>, <span class="string">'WechatIMG191.png'</span>, <span class="string">'.DS_Store'</span>, <span class="string">'test'</span>, <span class="string">'test4.py'</span>, <span class="string">'Bitcoin.py'</span>, <span class="string">'test1.py'</span>, <span class="string">'testpackage'</span>, <span class="string">'test2.py'</span>, <span class="string">'test3.py'</span>, <span class="string">'.idea'</span>]</span><br><span class="line"><span class="meta">>>> </span>[x <span class="keyword">for</span> x <span class="keyword">in</span> os.listdir(<span class="string">'.'</span>) <span class="keyword">if</span> os.path.isfile(x) <span class="keyword">and</span> os.path.splitext(x)[<span class="number">1</span>]==<span class="string">'.py'</span>] <span class="comment">#查找当前目录下的.py文件</span></span><br><span class="line">[<span class="string">'test4.py'</span>, <span class="string">'Bitcoin.py'</span>, <span class="string">'test1.py'</span>, <span class="string">'test2.py'</span>, <span class="string">'test3.py'</span>]</span><br></pre></td></tr></table></figure><h1 id="4-序列化"><a href="#4-序列化" class="headerlink" title="4. 序列化"></a>4. 序列化</h1><p>序列化(pickling) : 把变量从内存中变成可存储或传输的过程<br>反序列化(unpickling) : 把变量从序列化对象重新读到内存中</p><h2 id="4-1-pickle-只能用于python"><a href="#4-1-pickle-只能用于python" class="headerlink" title="4.1. pickle - 只能用于python"></a>4.1. pickle - 只能用于python</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">import</span> pickle</span><br><span class="line"><span class="meta">>>> </span>d = <span class="built_in">dict</span>(name = <span class="string">'Tom'</span>, age = <span class="number">18</span>)</span><br><span class="line"><span class="meta">>>> </span>pickle.dumps(d) <span class="comment">#将一个字典序列化</span></span><br><span class="line"><span class="string">b'\x80\x03}q\x00(X\x04\x00\x00\x00nameq\x01X\x03\x00\x00\x00Tomq\x02X\x03\x00\x00\x00ageq\x03K\x12u.'</span></span><br><span class="line"><span class="meta">>>> </span>f = <span class="built_in">open</span>(<span class="string">'dump.txt'</span>, <span class="string">'wb'</span>) <span class="comment">#以写入bytes的方式打开文件dump.txt</span></span><br><span class="line"><span class="meta">>>> </span>pickle.dump(d, f) <span class="comment">#将字典以序列化的方式写入文件</span></span><br><span class="line"><span class="meta">>>> </span>f.close()</span><br><span class="line"><span class="meta">>>> </span>f = <span class="built_in">open</span>(<span class="string">'dump.txt'</span>, <span class="string">'rb'</span>) <span class="comment">#以读bytes的方式打开dump.txt</span></span><br><span class="line"><span class="meta">>>> </span>d = pickle.load(f) <span class="comment">#反序列化</span></span><br><span class="line"><span class="meta">>>> </span>f.close()</span><br><span class="line"><span class="meta">>>> </span>d <span class="comment">#得到了内容相同的d变量</span></span><br><span class="line">{<span class="string">'name'</span>: <span class="string">'Tom'</span>, <span class="string">'age'</span>: <span class="number">18</span>}</span><br></pre></td></tr></table></figure><h2 id="4-2-JSON-一种通用型序列化标准格式"><a href="#4-2-JSON-一种通用型序列化标准格式" class="headerlink" title="4.2. JSON - 一种通用型序列化标准格式"></a>4.2. JSON - 一种通用型序列化标准格式</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">import</span> json</span><br><span class="line"><span class="meta">>>> </span>d = <span class="built_in">dict</span>(name = <span class="string">'Tom'</span>, age = <span class="number">18</span>)</span><br><span class="line"><span class="meta">>>> </span>json.dumps(d) <span class="comment">#返回一个标准json格式的str</span></span><br><span class="line"><span class="string">'{"name": "Tom", "age": 18}'</span></span><br><span class="line"><span class="meta">>>> </span>json_str = json.dumps(d)</span><br><span class="line"><span class="meta">>>> </span>json.loads(json_str) <span class="comment">#反序列化</span></span><br><span class="line">{<span class="string">'name'</span>: <span class="string">'Tom'</span>, <span class="string">'age'</span>: <span class="number">18</span>}</span><br></pre></td></tr></table></figure><p>一般我们习惯用类(class)来定义对象:</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> json</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">People</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name, age</span>):</span></span><br><span class="line"> self.name = name</span><br><span class="line"> self.age = age</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">peopel2dict</span>(<span class="params">people</span>):</span> <span class="comment">#将实例转化为dict</span></span><br><span class="line"> <span class="keyword">return</span> {</span><br><span class="line"> <span class="string">'name'</span>: people.name,</span><br><span class="line"> <span class="string">'age'</span>: people.age</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">dict2people</span>(<span class="params">p</span>):</span></span><br><span class="line"> <span class="keyword">return</span> People(p[<span class="string">'name'</span>], p[<span class="string">'age'</span>])</span><br><span class="line"></span><br><span class="line">p = People(<span class="string">'Tom'</span>, <span class="number">18</span>)</span><br><span class="line"><span class="built_in">print</span>(p)</span><br><span class="line"></span><br><span class="line"><span class="comment">#序列化</span></span><br><span class="line"><span class="built_in">print</span>(json.dumps(p, default=peopel2dict)) <span class="comment">#使用定义的转化函数</span></span><br><span class="line"><span class="built_in">print</span>(p.__dict__)</span><br><span class="line"><span class="built_in">print</span>(json.dumps(p, default=<span class="keyword">lambda</span> obj: obj.__dict__)) <span class="comment">#也可以直接使用实例自带的__dict__属性</span></span><br><span class="line"><span class="comment">#反序列化</span></span><br><span class="line">json_str = json.dumps(p, default=peopel2dict)</span><br><span class="line"><span class="built_in">print</span>(json.loads(json_str, object_hook=dict2people))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line"><__main__.People <span class="built_in">object</span> at <span class="number">0x1083f4550</span>></span><br><span class="line">{<span class="string">"name"</span>: <span class="string">"Tom"</span>, <span class="string">"age"</span>: <span class="number">18</span>}</span><br><span class="line">{<span class="string">'name'</span>: <span class="string">'Tom'</span>, <span class="string">'age'</span>: <span class="number">18</span>}</span><br><span class="line">{<span class="string">"name"</span>: <span class="string">"Tom"</span>, <span class="string">"age"</span>: <span class="number">18</span>}</span><br><span class="line"><__main__.People <span class="built_in">object</span> at <span class="number">0x10840c3d0</span>></span><br></pre></td></tr></table></figure><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><h1 id="0-知识背景"><a href="#0-知识背景" class="headerlink" title="0. 知识背景"></a>0. 知识背景</h1><p>IO- Input/Output: 通常涉及数据交换的地方都需要IO接口,例如: 磁盘、网络等<br>基本概念:input, output, stream<br>存在问题:输入和输出速度不匹配<br>解决方法:同步、异步(回调: 好了叫我, 异步: 好了没…好了没)</p></summary>
<category term="Computer Science" scheme="https://aaronos0.github.io/categories/Computer-Science/"/>
<category term="Python Learning" scheme="https://aaronos0.github.io/tags/Python-Learning/"/>
</entry>
<entry>
<title>Python - Theme 7 Error and Debugging</title>
<link href="https://aaronos0.github.io/2020/09/28/Python-Theme-7-Error-and-Debugging/"/>
<id>https://aaronos0.github.io/2020/09/28/Python-Theme-7-Error-and-Debugging/</id>
<published>2020-09-28T21:57:54.000Z</published>
<updated>2020-09-28T21:57:54.000Z</updated>
<content type="html"><![CDATA[<h1 id="1-错误处理"><a href="#1-错误处理" class="headerlink" title="1. 错误处理"></a>1. 错误处理</h1><h2 id="1-1-异常"><a href="#1-1-异常" class="headerlink" title="1.1. 异常"></a>1.1. 异常</h2><p>异常: 因为程序出现了错误而在正常控制流以外采取的行为<br>异常也是一个类,类的继承关系:<a href="https://docs.python.org/3/library/exceptions.html#exception-hierarchy">https://docs.python.org/3/library/exceptions.html#exception-hierarchy</a></p><span id="more"></span><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">div</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> <span class="built_in">print</span>(a / b) <span class="comment">#在try块中出现错误会直接去找except块,不执行try块中剩下代码</span></span><br><span class="line"> <span class="built_in">print</span>(b)</span><br><span class="line"> <span class="keyword">except</span> ZeroDivisionError: <span class="comment">#精准捕获后可自定义输出提示</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"Error: b should not be 0 !!"</span>)</span><br><span class="line"> <span class="keyword">except</span> Exception <span class="keyword">as</span> e: <span class="comment">#没有被捕获的异常全部在Exception中,这时最好将捕获到的信息输出提示</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"Unexpected Error: {}"</span>.<span class="built_in">format</span>(e))</span><br><span class="line"> <span class="keyword">else</span>: <span class="comment">#如果没有异常会执行else语句</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Run into else only when everything goes well'</span>)</span><br><span class="line"> <span class="keyword">finally</span>: <span class="comment">#finally语句一定会被执行; 一般会用于最后释放资源(文件、网络连接)</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Always run into finally block.'</span>)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"END"</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#Test1</span></span><br><span class="line">div(<span class="number">2</span>, <span class="number">0</span>)</span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">Error: b should <span class="keyword">not</span> be <span class="number">0</span> !!</span><br><span class="line">Always run into <span class="keyword">finally</span> block.</span><br><span class="line">END</span><br><span class="line"></span><br><span class="line"><span class="comment">#Test2</span></span><br><span class="line">div(<span class="number">2</span>, <span class="string">'bad type'</span>)</span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">Unexpected Error: unsupported operand <span class="built_in">type</span>(s) <span class="keyword">for</span> /: <span class="string">'int'</span> <span class="keyword">and</span> <span class="string">'str'</span></span><br><span class="line">Always run into <span class="keyword">finally</span> block.</span><br><span class="line">END</span><br><span class="line"></span><br><span class="line"><span class="comment">#Test3</span></span><br><span class="line">div(<span class="number">1</span>, <span class="number">2</span>)</span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line"><span class="number">0.5</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line">Run into <span class="keyword">else</span> only when everything goes well</span><br><span class="line">Always run into <span class="keyword">finally</span> block.</span><br><span class="line">END</span><br></pre></td></tr></table></figure><h2 id="1-2-程序的调用栈"><a href="#1-2-程序的调用栈" class="headerlink" title="1.2. 程序的调用栈"></a>1.2. 程序的调用栈</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">div</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> <span class="keyword">return</span> a/b</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">double</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> <span class="keyword">return</span> div(a, b) * <span class="number">2</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> <span class="keyword">return</span> double(a, b)</span><br><span class="line"></span><br><span class="line">main(<span class="number">2</span>, <span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#如果没有捕获异常,则由python解释器抛出的错误。错误一直向上抛,直至被解释器捕获</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line"> File <span class="string">"/Users/peterson/PycharmProjects/LiaoxuefengLearn/test3.py"</span>, line <span class="number">18</span>, <span class="keyword">in</span> <module></span><br><span class="line"> <span class="built_in">print</span>(main(<span class="number">2</span>, <span class="number">0</span>))</span><br><span class="line"> File <span class="string">"/Users/peterson/PycharmProjects/LiaoxuefengLearn/test3.py"</span>, line <span class="number">14</span>, <span class="keyword">in</span> main</span><br><span class="line"> <span class="keyword">return</span> double(a, b)</span><br><span class="line"> File <span class="string">"/Users/peterson/PycharmProjects/LiaoxuefengLearn/test3.py"</span>, line <span class="number">10</span>, <span class="keyword">in</span> double</span><br><span class="line"> <span class="keyword">return</span> div(a, b) * <span class="number">2</span></span><br><span class="line"> File <span class="string">"/Users/peterson/PycharmProjects/LiaoxuefengLearn/test3.py"</span>, line <span class="number">6</span>, <span class="keyword">in</span> div</span><br><span class="line"> <span class="keyword">return</span> a/b</span><br><span class="line">ZeroDivisionError: division by zero</span><br></pre></td></tr></table></figure><h2 id="1-3-在合适的地方捕获错误"><a href="#1-3-在合适的地方捕获错误" class="headerlink" title="1.3. 在合适的地方捕获错误"></a>1.3. 在合适的地方捕获错误</h2><p>python捕获可以跨越多层调用,也就是说只需要在合适的层次捕获错误即可</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">div</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> <span class="keyword">return</span> a/b</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">double</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> <span class="keyword">return</span> div(a, b) * <span class="number">2</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> <span class="keyword">return</span> double(a, b)</span><br><span class="line"> <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"Unexpected Error: {}"</span>.<span class="built_in">format</span>(e))</span><br><span class="line"></span><br><span class="line"><span class="comment">#Test1</span></span><br><span class="line">main(<span class="number">2</span>, <span class="number">0</span>)</span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">Unexpected Error: division by zero</span><br><span class="line"></span><br><span class="line"><span class="comment">#Test2</span></span><br><span class="line">main(<span class="number">2</span>, <span class="string">'bad type'</span>)</span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">Unexpected Error: unsupported operand <span class="built_in">type</span>(s) <span class="keyword">for</span> /: <span class="string">'int'</span> <span class="keyword">and</span> <span class="string">'str'</span></span><br></pre></td></tr></table></figure><h2 id="1-4-错误记录-log"><a href="#1-4-错误记录-log" class="headerlink" title="1.4. 错误记录(log)"></a>1.4. 错误记录(log)</h2><p>异常捕获可以把错误栈记录下来,同时让程序继续执行下去</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> logging</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">div</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> <span class="keyword">return</span> a/b</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">double</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> <span class="keyword">return</span> div(a, b) * <span class="number">2</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> <span class="keyword">return</span> double(a, b)</span><br><span class="line"> <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line"> logging.exception(e)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"="</span> * <span class="number">6</span>, <span class="string">'END'</span>, <span class="string">"="</span> * <span class="number">6</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">main(<span class="number">2</span>, <span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出(在没有触发程序中断的情况下并记录了log)</span></span><br><span class="line">====== END ======</span><br><span class="line">ERROR:root:division by zero</span><br><span class="line">Traceback (most recent call last):</span><br><span class="line"> File <span class="string">"/Users/xxx/test3.py"</span>, line <span class="number">16</span>, <span class="keyword">in</span> main</span><br><span class="line"> <span class="keyword">return</span> double(a, b)</span><br><span class="line"> File <span class="string">"/Users/xxx/test3.py"</span>, line <span class="number">11</span>, <span class="keyword">in</span> double</span><br><span class="line"> <span class="keyword">return</span> div(a, b) * <span class="number">2</span></span><br><span class="line"> File <span class="string">"/Users/xxx/test3.py"</span>, line <span class="number">7</span>, <span class="keyword">in</span> div</span><br><span class="line"> <span class="keyword">return</span> a/b</span><br><span class="line">ZeroDivisionError: division by zero</span><br><span class="line"></span><br><span class="line">Process finished <span class="keyword">with</span> exit code <span class="number">0</span></span><br></pre></td></tr></table></figure><h2 id="1-5-自定义错误类并抛出错误"><a href="#1-5-自定义错误类并抛出错误" class="headerlink" title="1.5. 自定义错误类并抛出错误"></a>1.5. 自定义错误类并抛出错误</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 一般只有在特别必要的时候才自己定义</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyError</span>(<span class="params">BaseException</span>):</span></span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">error</span>():</span></span><br><span class="line"> <span class="keyword">raise</span> MyError(<span class="string">"This is an error raised by myself"</span>)</span><br><span class="line"></span><br><span class="line">error()</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line"> File <span class="string">"/Users/xxx/test3.py"</span>, line <span class="number">12</span>, <span class="keyword">in</span> <module></span><br><span class="line"> error()</span><br><span class="line"> File <span class="string">"/Users/xxx/test3.py"</span>, line <span class="number">10</span>, <span class="keyword">in</span> error</span><br><span class="line"> <span class="keyword">raise</span> MyError(<span class="string">"This is an error raised by myself"</span>)</span><br><span class="line">__main__.MyError: This <span class="keyword">is</span> an error raised by myself</span><br></pre></td></tr></table></figure><h1 id="2-调试"><a href="#2-调试" class="headerlink" title="2. 调试"></a>2. 调试</h1><h2 id="2-1-print-简单粗暴"><a href="#2-1-print-简单粗暴" class="headerlink" title="2.1. print() - 简单粗暴"></a>2.1. print() - 简单粗暴</h2><p>可以直接把可能有问题的变量打印出来。但是未来还需要手动删除这些print,在运行结果中也包含了过多垃圾信息。</p><h2 id="2-2-assert-断言"><a href="#2-2-assert-断言" class="headerlink" title="2.2. assert - 断言"></a>2.2. assert - 断言</h2><p>凡是用print()的地方都可以替换成assert,如果断言失败就会抛出 AssertionError。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">div</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> <span class="keyword">assert</span> b != <span class="number">0</span>, <span class="string">'b is zero!'</span> <span class="comment">#设置断言</span></span><br><span class="line"> <span class="keyword">return</span> a/b</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">double</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> <span class="keyword">return</span> div(a, b) * <span class="number">2</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> <span class="keyword">return</span> double(a, b)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"="</span> * <span class="number">6</span>, <span class="string">'END'</span>, <span class="string">"="</span> * <span class="number">6</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">main(<span class="number">2</span>, <span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line"> File <span class="string">"/Users/xxx/test3.py"</span>, line <span class="number">20</span>, <span class="keyword">in</span> <module></span><br><span class="line"> main(<span class="number">2</span>, <span class="number">0</span>)</span><br><span class="line"> File <span class="string">"/Users/xxx/test3.py"</span>, line <span class="number">16</span>, <span class="keyword">in</span> main</span><br><span class="line"> <span class="keyword">return</span> double(a, b)</span><br><span class="line"> File <span class="string">"/Users/xxx/test3.py"</span>, line <span class="number">12</span>, <span class="keyword">in</span> double</span><br><span class="line"> <span class="keyword">return</span> div(a, b) * <span class="number">2</span></span><br><span class="line"> File <span class="string">"/Users/xxx/test3.py"</span>, line <span class="number">7</span>, <span class="keyword">in</span> div</span><br><span class="line"> <span class="keyword">assert</span> b != <span class="number">0</span>, <span class="string">'b is zero!'</span></span><br><span class="line">AssertionError: b <span class="keyword">is</span> zero! <span class="comment">#断言在此</span></span><br><span class="line"></span><br><span class="line">Process finished <span class="keyword">with</span> exit code <span class="number">1</span></span><br></pre></td></tr></table></figure><p>关闭断言: python -O xx.py</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span>python3 -O test3.py</span><br><span class="line">Traceback (most recent call last):</span><br><span class="line"> File <span class="string">"test3.py"</span>, line <span class="number">20</span>, <span class="keyword">in</span> <module></span><br><span class="line"> main(<span class="number">2</span>, <span class="number">0</span>)</span><br><span class="line"> File <span class="string">"test3.py"</span>, line <span class="number">16</span>, <span class="keyword">in</span> main</span><br><span class="line"> <span class="keyword">return</span> double(a, b)</span><br><span class="line"> File <span class="string">"test3.py"</span>, line <span class="number">12</span>, <span class="keyword">in</span> double</span><br><span class="line"> <span class="keyword">return</span> div(a, b) * <span class="number">2</span></span><br><span class="line"> File <span class="string">"test3.py"</span>, line <span class="number">8</span>, <span class="keyword">in</span> div</span><br><span class="line"> <span class="keyword">return</span> a/b</span><br><span class="line">ZeroDivisionError: division by zero <span class="comment">#这样一来就忽略了所有 assert</span></span><br></pre></td></tr></table></figure><h2 id="2-3-logging-记录日志分析"><a href="#2-3-logging-记录日志分析" class="headerlink" title="2.3. logging - 记录日志分析"></a>2.3. logging - 记录日志分析</h2><p>和assert相比,logging并不会抛出错误而是记录,可以选择记录形式、保存文件等方式。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> logging</span><br><span class="line">logging.basicConfig(level=logging.INFO)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">div</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> logging.info(<span class="string">'b=%d'</span> % b)</span><br><span class="line"> <span class="keyword">return</span> a/b</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">double</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> <span class="keyword">return</span> div(a, b) * <span class="number">2</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> <span class="keyword">return</span> double(a, b)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"="</span> * <span class="number">6</span>, <span class="string">'END'</span>, <span class="string">"="</span> * <span class="number">6</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">main(<span class="number">2</span>, <span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">INFO:root:b=<span class="number">0</span> <span class="comment">#日志输出在此</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line"> File <span class="string">"/Users/xxx/test3.py"</span>, line <span class="number">21</span>, <span class="keyword">in</span> <module></span><br><span class="line"> main(<span class="number">2</span>, <span class="number">0</span>)</span><br><span class="line"> File <span class="string">"/Users/xxx/test3.py"</span>, line <span class="number">17</span>, <span class="keyword">in</span> main</span><br><span class="line"> <span class="keyword">return</span> double(a, b)</span><br><span class="line"> File <span class="string">"/Users/xxx/test3.py"</span>, line <span class="number">13</span>, <span class="keyword">in</span> double</span><br><span class="line"> <span class="keyword">return</span> div(a, b) * <span class="number">2</span></span><br><span class="line"> File <span class="string">"/Users/xxx/test3.py"</span>, line <span class="number">9</span>, <span class="keyword">in</span> div</span><br><span class="line"> <span class="keyword">return</span> a/b</span><br><span class="line">ZeroDivisionError: division by zero</span><br><span class="line"></span><br><span class="line">Process finished <span class="keyword">with</span> exit code <span class="number">1</span></span><br></pre></td></tr></table></figure><p>日志配置tips:</p><ol><li>日志级别:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET</li><li>可以将日志配置保存到一个文件中方便管理</li><li>自定义日志的记录格式方便日后的分析</li></ol><h2 id="2-4-pdb-python调试器"><a href="#2-4-pdb-python调试器" class="headerlink" title="2.4. pdb - python调试器"></a>2.4. pdb - python调试器</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span>python3 -m pdb test3.py <span class="comment">#启动调试器</span></span><br><span class="line">> /Users/peterson/PycharmProjects/LiaoxuefengLearn/test3.py(<span class="number">5</span>)<module>()</span><br><span class="line">-> <span class="function"><span class="keyword">def</span> <span class="title">div</span>(<span class="params">a, b</span>):</span></span><br><span class="line">(Pdb) n <span class="comment">#单步调试</span></span><br><span class="line">> /Users/peterson/PycharmProjects/LiaoxuefengLearn/test3.py(<span class="number">9</span>)<module>()</span><br><span class="line">-> <span class="function"><span class="keyword">def</span> <span class="title">double</span>(<span class="params">a, b</span>):</span></span><br><span class="line">(Pdb) n</span><br><span class="line">> /Users/peterson/PycharmProjects/LiaoxuefengLearn/test3.py(<span class="number">13</span>)<module>()</span><br><span class="line">-> <span class="function"><span class="keyword">def</span> <span class="title">main</span>(<span class="params">a, b</span>):</span></span><br><span class="line">(Pdb) n</span><br><span class="line">> /Users/peterson/PycharmProjects/LiaoxuefengLearn/test3.py(<span class="number">18</span>)<module>()</span><br><span class="line">-> main(<span class="number">2</span>, <span class="number">0</span>)</span><br><span class="line">(Pdb) n</span><br><span class="line">ZeroDivisionError: division by zero</span><br><span class="line">> /Users/peterson/PycharmProjects/LiaoxuefengLearn/test3.py(<span class="number">18</span>)<module>()</span><br><span class="line">-> main(<span class="number">2</span>, <span class="number">0</span>)</span><br><span class="line">(Pdb) q <span class="comment">#退出</span></span><br></pre></td></tr></table></figure><p>设置断点:pdb.set_trace()</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pdb</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">div</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> pdb.set_trace() <span class="comment">#程序运行到这里会自动暂停</span></span><br><span class="line"> <span class="keyword">return</span> a/b</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">double</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> <span class="keyword">return</span> div(a, b) * <span class="number">2</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span>(<span class="params">a, b</span>):</span></span><br><span class="line"> <span class="keyword">return</span> double(a, b)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"="</span> * <span class="number">6</span>, <span class="string">'END'</span>, <span class="string">"="</span> * <span class="number">6</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">main(<span class="number">2</span>, <span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">-> <span class="keyword">return</span> a/b</span><br><span class="line">(Pdb) p b <span class="comment">#当程序暂停到这里时,可以查看b变量的内容</span></span><br><span class="line"><span class="number">0</span></span><br></pre></td></tr></table></figure><h2 id="2-5-IDE-最佳推荐-Pycharm"><a href="#2-5-IDE-最佳推荐-Pycharm" class="headerlink" title="2.5. IDE - 最佳推荐(Pycharm)"></a>2.5. IDE - 最佳推荐(Pycharm)</h2><h1 id="3-单元测试"><a href="#3-单元测试" class="headerlink" title="3. 单元测试"></a>3. 单元测试</h1><p>单元测试就是对一个模块、一个函数或者一个类进行正确性检测的工作。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> unittest</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TestStringMethods</span>(<span class="params">unittest.TestCase</span>):</span> <span class="comment">#编写一个测试类,继承unittest.TestCase就创建了一个测试样例</span></span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">setUp</span>(<span class="params">self</span>):</span> <span class="comment">#测试前置方法: 测试框架会应用于每一个测试方法</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'setUp...'</span>)</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">test_upper</span>(<span class="params">self</span>):</span> <span class="comment">#测试方法以"test"开头</span></span><br><span class="line"> self.assertEqual(<span class="string">'foo'</span>.upper(), <span class="string">'FOO'</span>)</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">test_isupper</span>(<span class="params">self</span>):</span></span><br><span class="line"> self.assertTrue(<span class="string">'FOO'</span>.isupper())</span><br><span class="line"> self.assertFalse(<span class="string">'Foo'</span>.isupper())</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">test_split</span>(<span class="params">self</span>):</span></span><br><span class="line"> s = <span class="string">'hello world'</span></span><br><span class="line"> self.assertEqual(s.split(), [<span class="string">'hello'</span>, <span class="string">'world'</span>])</span><br><span class="line"> <span class="comment"># check that s.split fails when the separator is not a string</span></span><br><span class="line"> <span class="keyword">with</span> self.assertRaises(TypeError): <span class="comment">#抛出一个特定异常</span></span><br><span class="line"> s.split(<span class="number">2</span>)</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">tearDown</span>(<span class="params">self</span>):</span> <span class="comment">#一旦setUp()运行成功,无论如何都会执行后续清理工作</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'tearDown...'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> unittest.main()</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出:</span></span><br><span class="line"><span class="meta">>>> </span>python test4.py -v</span><br><span class="line">test_isupper (__main__.TestStringMethods) ... setUp...</span><br><span class="line">tearDown...</span><br><span class="line">ok</span><br><span class="line">test_split (__main__.TestStringMethods) ... setUp...</span><br><span class="line">tearDown...</span><br><span class="line">ok</span><br><span class="line">test_upper (__main__.TestStringMethods) ... setUp...</span><br><span class="line">tearDown...</span><br><span class="line">ok</span><br><span class="line"></span><br><span class="line">----------------------------------------------------------------------</span><br><span class="line">Ran <span class="number">3</span> tests <span class="keyword">in</span> <span class="number">0.000</span>s</span><br><span class="line"></span><br><span class="line">OK</span><br></pre></td></tr></table></figure><p>(1). 单元测试可以有效地测试某个程序的行为,也为重构代码提供了便利。<br>(2). 单元测试要覆盖常用的输入输出组合、边界条件和异常<br>(3). 单元测试要尽可能简单以避免测试代码出现bug</p><h1 id="4-文档测试"><a href="#4-文档测试" class="headerlink" title="4. 文档测试"></a>4. 文档测试</h1><p>可以看到在很多python官方文档中都会附有示例代码,这些代码不仅可以粘贴出来在命令行执行,而且也可以直接执行。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">This is the "example" module.</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">The example module supplies one function, factorial(). For example,</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"><span class="meta">>>> </span>factorial(5)</span></span><br><span class="line"><span class="string">120</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">factorial</span>(<span class="params">n</span>):</span></span><br><span class="line"> <span class="string">"""Return the factorial of n, an exact integer >= 0.</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> >>> [factorial(n) for n in range(6)]</span></span><br><span class="line"><span class="string"> [1, 1, 2, 6, 24, 120]</span></span><br><span class="line"><span class="string"> >>> factorial(30)</span></span><br><span class="line"><span class="string"> 265252859812191058636308480000000</span></span><br><span class="line"><span class="string"> >>> factorial(-1)</span></span><br><span class="line"><span class="string"> Traceback (most recent call last):</span></span><br><span class="line"><span class="string"> ...</span></span><br><span class="line"><span class="string"> ValueError: n must be >= 0</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> Factorials of floats are OK, but the float must be an exact integer:</span></span><br><span class="line"><span class="string"> >>> factorial(30.1)</span></span><br><span class="line"><span class="string"> Traceback (most recent call last):</span></span><br><span class="line"><span class="string"> ...</span></span><br><span class="line"><span class="string"> ValueError: n must be exact integer</span></span><br><span class="line"><span class="string"> >>> factorial(30.0)</span></span><br><span class="line"><span class="string"> 265252859812191058636308480000000</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> It must also not be ridiculously large:</span></span><br><span class="line"><span class="string"> >>> factorial(1e100)</span></span><br><span class="line"><span class="string"> Traceback (most recent call last):</span></span><br><span class="line"><span class="string"> ...</span></span><br><span class="line"><span class="string"> OverflowError: n too large</span></span><br><span class="line"><span class="string"> """</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">import</span> math</span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> n >= <span class="number">0</span>:</span><br><span class="line"> <span class="keyword">raise</span> ValueError(<span class="string">"n must be >= 0"</span>)</span><br><span class="line"> <span class="keyword">if</span> math.floor(n) != n:</span><br><span class="line"> <span class="keyword">raise</span> ValueError(<span class="string">"n must be exact integer"</span>)</span><br><span class="line"> <span class="keyword">if</span> n+<span class="number">1</span> == n: <span class="comment"># catch a value like 1e300</span></span><br><span class="line"> <span class="keyword">raise</span> OverflowError(<span class="string">"n too large"</span>)</span><br><span class="line"> result = <span class="number">1</span></span><br><span class="line"> factor = <span class="number">2</span></span><br><span class="line"> <span class="keyword">while</span> factor <= n:</span><br><span class="line"> result *= factor</span><br><span class="line"> factor += <span class="number">1</span></span><br><span class="line"> <span class="keyword">return</span> result</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">"__main__"</span>:</span><br><span class="line"> <span class="keyword">import</span> doctest</span><br><span class="line"> doctest.testmod()</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line"><span class="meta">>>> </span>python test4.py -v <span class="comment">#写在注释中的示例都会被测试</span></span><br><span class="line">Trying:</span><br><span class="line"> factorial(<span class="number">5</span>)</span><br><span class="line">Expecting:</span><br><span class="line"> <span class="number">120</span></span><br><span class="line">ok</span><br><span class="line">Trying:</span><br><span class="line"> [factorial(n) <span class="keyword">for</span> n <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">6</span>)]</span><br><span class="line">Expecting:</span><br><span class="line"> [<span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">6</span>, <span class="number">24</span>, <span class="number">120</span>]</span><br><span class="line">ok</span><br><span class="line">Trying:</span><br><span class="line"> factorial(<span class="number">30</span>)</span><br><span class="line">Expecting:</span><br><span class="line"> <span class="number">265252859812191058636308480000000</span></span><br><span class="line">ok</span><br><span class="line">Trying:</span><br><span class="line"> factorial(-<span class="number">1</span>)</span><br><span class="line">Expecting:</span><br><span class="line"> Traceback (most recent call last):</span><br><span class="line"> ...</span><br><span class="line"> ValueError: n must be >= <span class="number">0</span></span><br><span class="line">ok</span><br><span class="line">Trying:</span><br><span class="line"> factorial(<span class="number">30.1</span>)</span><br><span class="line">Expecting:</span><br><span class="line"> Traceback (most recent call last):</span><br><span class="line"> ...</span><br><span class="line"> ValueError: n must be exact integer</span><br><span class="line">ok</span><br><span class="line">Trying:</span><br><span class="line"> factorial(<span class="number">30.0</span>)</span><br><span class="line">Expecting:</span><br><span class="line"> <span class="number">265252859812191058636308480000000</span></span><br><span class="line">ok</span><br><span class="line">Trying:</span><br><span class="line"> factorial(<span class="number">1e100</span>)</span><br><span class="line">Expecting:</span><br><span class="line"> Traceback (most recent call last):</span><br><span class="line"> ...</span><br><span class="line"> OverflowError: n too large</span><br><span class="line">ok</span><br><span class="line"><span class="number">2</span> items passed <span class="built_in">all</span> tests:</span><br><span class="line"> <span class="number">1</span> tests <span class="keyword">in</span> __main__</span><br><span class="line"> <span class="number">6</span> tests <span class="keyword">in</span> __main__.factorial</span><br><span class="line"><span class="number">7</span> tests <span class="keyword">in</span> <span class="number">2</span> items.</span><br><span class="line"><span class="number">7</span> passed <span class="keyword">and</span> <span class="number">0</span> failed.</span><br><span class="line">Test passed.</span><br></pre></td></tr></table></figure><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><h1 id="1-错误处理"><a href="#1-错误处理" class="headerlink" title="1. 错误处理"></a>1. 错误处理</h1><h2 id="1-1-异常"><a href="#1-1-异常" class="headerlink" title="1.1. 异常"></a>1.1. 异常</h2><p>异常: 因为程序出现了错误而在正常控制流以外采取的行为<br>异常也是一个类,类的继承关系:<a href="https://docs.python.org/3/library/exceptions.html#exception-hierarchy">https://docs.python.org/3/library/exceptions.html#exception-hierarchy</a></p></summary>
<category term="Computer Science" scheme="https://aaronos0.github.io/categories/Computer-Science/"/>
<category term="Python Learning" scheme="https://aaronos0.github.io/tags/Python-Learning/"/>
</entry>
<entry>
<title>Python - Theme 6 OOP</title>
<link href="https://aaronos0.github.io/2020/09/28/Python-Theme-6-OOP/"/>
<id>https://aaronos0.github.io/2020/09/28/Python-Theme-6-OOP/</id>
<published>2020-09-28T21:55:04.000Z</published>
<updated>2020-09-28T21:55:04.000Z</updated>
<content type="html"><![CDATA[<blockquote><p>OOP- Object Oriented Programming. 把对象作为程序的基本单元,一个对象包含数据和函数,计算机程序执行就是消息在各个对象之间的传递。</p></blockquote><span id="more"></span><h1 id="1-类和实例及访问限制"><a href="#1-类和实例及访问限制" class="headerlink" title="1. 类和实例及访问限制"></a>1. 类和实例及访问限制</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/usr/bin/env python</span></span><br><span class="line"><span class="comment"># -*- coding : utf-8 -*-</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#Student类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span>(<span class="params"><span class="built_in">object</span></span>):</span> <span class="comment">#object类--所有类最终都会继承的类, 这里可以将object看做父类</span></span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name, score</span>):</span> <span class="comment">#创建实例的时候需要传入的参数,会绑定给实例</span></span><br><span class="line"> self.__name = name <span class="comment">#__name, 两个下划线开头表示私有变量,外部无法访问</span></span><br><span class="line"> self.score = score</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">get_name</span>(<span class="params">self</span>):</span> <span class="comment">#由于外部无法访问私有变量, 因此通过内部方法来实现;</span></span><br><span class="line"> <span class="keyword">return</span> self.__name</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">set_name</span>(<span class="params">self, name</span>):</span> <span class="comment">#这样的好处在于可以规定传入参数的要求</span></span><br><span class="line"> self.__name = name</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">print_score</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'%s: %s'</span> % (self.__name, self.score))</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">get_grade</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="keyword">if</span> self.score >= <span class="number">90</span>:</span><br><span class="line"> <span class="keyword">return</span> <span class="string">'A'</span></span><br><span class="line"> <span class="keyword">elif</span> self.score >= <span class="number">60</span>:</span><br><span class="line"> <span class="keyword">return</span> <span class="string">'B'</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">return</span> <span class="string">'C'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line"><span class="meta">>>> </span>nick = Student(<span class="string">'Nick Yang'</span>, <span class="number">50</span>) <span class="comment">#生成一个名为nick的实例, 变量nick指向这个实例.</span></span><br><span class="line"><span class="meta">>>> </span>nick</span><br><span class="line"><__main__.Student <span class="built_in">object</span> at <span class="number">0x10d4c1090</span>></span><br><span class="line"><span class="meta">>>> </span>Student</span><br><span class="line"><<span class="class"><span class="keyword">class</span> '<span class="title">__main__</span>.<span class="title">Student</span>'></span></span><br><span class="line"><span class="class">>>> <span class="title">nick</span>.<span class="title">name</span> #私有变量不能通过外部访问</span></span><br><span class="line"><span class="class"><span class="title">Traceback</span> (<span class="params">most recent call last</span>):</span></span><br><span class="line"> File <span class="string">"<input>"</span>, line <span class="number">1</span>, <span class="keyword">in</span> <module></span><br><span class="line">AttributeError: <span class="string">'Student'</span> <span class="built_in">object</span> has no attribute <span class="string">'name'</span></span><br><span class="line"><span class="meta">>>> </span>nick.get_name()</span><br><span class="line"><span class="string">'Nick Yang'</span></span><br><span class="line"><span class="meta">>>> </span>nick.print_score()</span><br><span class="line">Nick Yang: <span class="number">50</span></span><br><span class="line"><span class="meta">>>> </span>nick.score <span class="comment">#score不是私有变量, 因此可以直接访问</span></span><br><span class="line"><span class="number">50</span></span><br><span class="line"><span class="meta">>>> </span>nick.score = <span class="number">60</span> <span class="comment">#可直接赋新值</span></span><br><span class="line"><span class="meta">>>> </span>nick.score</span><br><span class="line"><span class="number">60</span></span><br><span class="line"><span class="meta">>>> </span>nick.get_grade()</span><br><span class="line"><span class="string">'B'</span></span><br><span class="line"><span class="meta">>>> </span>nick.age = <span class="number">18</span> <span class="comment">#可以自由的给实例绑定属性</span></span><br><span class="line"><span class="meta">>>> </span>nick.age</span><br><span class="line"><span class="number">18</span></span><br></pre></td></tr></table></figure><h1 id="2-继承和多态"><a href="#2-继承和多态" class="headerlink" title="2. 继承和多态"></a>2. 继承和多态</h1><blockquote><p>多态:使用单一接口操作具有相似特性数据的能力</p></blockquote><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/usr/bin/env python</span></span><br><span class="line"><span class="comment"># -*- coding : utf-8 -*-</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Ielts</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self</span>):</span></span><br><span class="line"> self.__totalScore = <span class="number">9</span></span><br><span class="line"> self.bringItems = [<span class="string">'ID document'</span>, <span class="string">'a pencil and a pen'</span>, <span class="string">'an eraser'</span>]</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">print_total_score</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Total_score: %s'</span> % self.__totalScore)</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">print_bring_items</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Necessary_Items: %s'</span> % self.bringItems)</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">exam</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'It is IELTS exam.'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Reading</span>(<span class="params">Ielts</span>):</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">exam</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'It is Reading exam.'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Writing</span>(<span class="params">Ielts</span>):</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">exam</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'It is Writing exam.'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Listening</span>(<span class="params">Ielts</span>):</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">exam</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'It is Listening exam.'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Speaking</span>(<span class="params">Ielts</span>):</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self</span>):</span></span><br><span class="line"> self.bringItems = [<span class="string">'ID document'</span>]</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">exam</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'It is Speaking exam.'</span>)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">exam</span>(<span class="params">obj</span>):</span></span><br><span class="line"> obj.exam() <span class="comment">#多态: 让不同功能的函数使用相同的函数名, 这样可以用一个函数名来调用不同功能(内容)的函数.</span></span><br><span class="line"></span><br><span class="line">examination = Ielts()</span><br><span class="line">examination.exam()</span><br><span class="line">examination.print_bring_items()</span><br><span class="line"></span><br><span class="line">speakingExam = Speaking()</span><br><span class="line">speakingExam.print_bring_items() <span class="comment">#继承了父类的方法</span></span><br><span class="line">speakingExam.exam()</span><br><span class="line">exam(speakingExam)</span><br><span class="line"><span class="comment">#speakingExam.print_total_score() #AttributeError: 'Speaking' object has no attribute '_Ielts__totalScore', 子类不会继承父类的私有变量</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">It <span class="keyword">is</span> IELTS exam.</span><br><span class="line">Necessary_Items: [<span class="string">'ID document'</span>, <span class="string">'a pencil and a pen'</span>, <span class="string">'an eraser'</span>]</span><br><span class="line">Necessary_Items: [<span class="string">'ID document'</span>]</span><br><span class="line">It <span class="keyword">is</span> Speaking exam.</span><br><span class="line">It <span class="keyword">is</span> Speaking exam.</span><br></pre></td></tr></table></figure><h1 id="3-获取对象信息"><a href="#3-获取对象信息" class="headerlink" title="3. 获取对象信息"></a>3. 获取对象信息</h1><h2 id="3-1-type"><a href="#3-1-type" class="headerlink" title="3.1. type()"></a>3.1. type()</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#判断基本类型</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">type</span>(<span class="number">10</span>)</span><br><span class="line"><<span class="class"><span class="keyword">class</span> '<span class="title">int</span>'></span></span><br><span class="line"><span class="class">>>> <span class="title">type</span>(<span class="params"><span class="string">'10'</span></span>)</span></span><br><span class="line"><span class="class"><<span class="title">class</span> '<span class="title">str</span>'></span></span><br><span class="line"><span class="class">>>> <span class="title">type</span>(<span class="params">[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span>)</span></span><br><span class="line"><span class="class"><<span class="title">class</span> '<span class="title">list</span>'></span></span><br><span class="line"><span class="class">>>> <span class="title">type</span>(<span class="params">(<span class="params"><span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span></span>)</span>)</span></span><br><span class="line"><span class="class"><<span class="title">class</span> '<span class="title">tuple</span>'></span></span><br><span class="line"><span class="class">>>> <span class="title">type</span>(<span class="params"><span class="literal">None</span></span>)</span></span><br><span class="line"><span class="class"><<span class="title">class</span> '<span class="title">NoneType</span>'></span></span><br><span class="line"><span class="class">>>> <span class="title">type</span>(<span class="params"><span class="built_in">abs</span></span>)</span></span><br><span class="line"><span class="class"><<span class="title">class</span> '<span class="title">builtin_function_or_method</span>'></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class">#判断函数类型</span></span><br><span class="line"><span class="class">>>> <span class="title">import</span> <span class="title">types</span></span></span><br><span class="line"><span class="class">>>> <span class="title">def</span> <span class="title">f</span>():</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">pass</span></span><br><span class="line">...</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">type</span>(f)</span><br><span class="line"><<span class="class"><span class="keyword">class</span> '<span class="title">function</span>'></span></span><br><span class="line"><span class="class">>>> <span class="title">type</span>(<span class="params">f</span>) == <span class="title">types</span>.<span class="title">FunctionType</span></span></span><br><span class="line"><span class="class"><span class="title">True</span></span></span><br><span class="line"><span class="class">>>> <span class="title">type</span>(<span class="params"><span class="built_in">abs</span></span>) == <span class="title">types</span>.<span class="title">FunctionType</span></span></span><br><span class="line"><span class="class"><span class="title">False</span></span></span><br><span class="line"><span class="class">>>> <span class="title">type</span>(<span class="params"><span class="built_in">abs</span></span>) == <span class="title">types</span>.<span class="title">BuiltinFunctionType</span></span></span><br><span class="line"><span class="class"><span class="title">True</span></span></span><br><span class="line"><span class="class">>>> <span class="title">type</span>(<span class="params"><span class="keyword">lambda</span> x:x</span>) == <span class="title">types</span>.<span class="title">LambdaType</span></span></span><br><span class="line"><span class="class"><span class="title">True</span></span></span><br><span class="line"><span class="class">>>> <span class="title">type</span>(<span class="params">(<span class="params">x <span class="keyword">for</span> x <span class="keyword">in</span> <span class="built_in">range</span>(<span class="params"><span class="number">10</span></span>)</span>)</span>) == <span class="title">types</span>.<span class="title">GeneratorType</span></span></span><br><span class="line"><span class="class"><span class="title">True</span></span></span><br></pre></td></tr></table></figure><h2 id="3-2-isinstance"><a href="#3-2-isinstance" class="headerlink" title="3.2. isinstance()"></a>3.2. isinstance()</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#type()能判断的,isinstance()也可以, 但是应用场景不同</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>(<span class="string">'10'</span>, <span class="built_in">str</span>)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>(<span class="number">10</span>, <span class="built_in">int</span>)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>], <span class="built_in">list</span>)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#判断class的类型, 尤其是在继承关系中</span></span><br><span class="line"><span class="meta">>>> </span><span class="class"><span class="keyword">class</span> <span class="title">Animal</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">pass</span></span><br><span class="line"><span class="meta">... </span><span class="class"><span class="keyword">class</span> <span class="title">Cat</span>(<span class="params">Animal</span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">pass</span></span><br><span class="line"><span class="meta">... </span><span class="class"><span class="keyword">class</span> <span class="title">Kitty</span>(<span class="params">Cat</span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">pass</span></span><br><span class="line">...</span><br><span class="line"><span class="meta">>>> </span>a = Animal()</span><br><span class="line"><span class="meta">>>> </span>c = Cat()</span><br><span class="line"><span class="meta">>>> </span>k = Kitty()</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>(k, Cat) <span class="keyword">and</span> <span class="built_in">isinstance</span>(k, Animal)</span><br><span class="line"><span class="meta">>>> </span><span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#可以判断变量是否属于某些类型</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>((<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>), (<span class="built_in">list</span>, <span class="built_in">tuple</span>))</span><br><span class="line"><span class="literal">True</span></span><br></pre></td></tr></table></figure><h2 id="3-3-dir"><a href="#3-3-dir" class="headerlink" title="3.3. dir()"></a>3.3. dir()</h2><blockquote><p>获取一个对象的所有属性和方法</p></blockquote><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="built_in">dir</span>(<span class="string">'abc'</span>)</span><br><span class="line">[<span class="string">'__add__'</span>, <span class="string">'__class__'</span>, <span class="string">'__contains__'</span>, <span class="string">'__delattr__'</span>, <span class="string">'__dir__'</span>, <span class="string">'__doc__'</span>......]</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">len</span>(<span class="string">'abc'</span>) <span class="comment">#实际上是去调用了对象的__Len__()方法</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="meta">>>> </span><span class="string">'abc'</span>.__len__()</span><br><span class="line"><span class="number">3</span></span><br></pre></td></tr></table></figure><h1 id="4-实例属性和类属性"><a href="#4-实例属性和类属性" class="headerlink" title="4. 实例属性和类属性"></a>4. 实例属性和类属性</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#通常使用self变量或者实例变量来给实例绑定属性; 而这种直接在类中定义的属性为类属性;</span></span><br><span class="line"><span class="meta">>>> </span><span class="class"><span class="keyword">class</span> <span class="title">Test</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"><span class="meta">... </span> x = <span class="number">1</span></span><br><span class="line"><span class="meta">... </span> </span><br><span class="line"><span class="meta">>>> </span>t = Test()</span><br><span class="line"><span class="meta">>>> </span>t.x <span class="comment">#因为实例没有x属性, 所以会向上查找class的x属性</span></span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="meta">>>> </span>Test.x <span class="comment">#类属性</span></span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="meta">>>> </span>t.x = <span class="number">2</span> <span class="comment">#给实例绑定x属性</span></span><br><span class="line"><span class="meta">>>> </span>t.x <span class="comment">#实例属性优先</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="meta">>>> </span>Test.x <span class="comment">#类属性不变</span></span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="meta">>>> </span><span class="keyword">del</span> t.x <span class="comment">#删除实例属性</span></span><br><span class="line"><span class="meta">>>> </span>t.x</span><br><span class="line"><span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#每创建一个学生实例,则计数一次</span></span><br><span class="line"><span class="comment">#!/usr/bin/env python</span></span><br><span class="line"><span class="comment"># -*- coding : utf-8 -*-</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"> count = <span class="number">0</span></span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name</span>):</span></span><br><span class="line"> self.name = name</span><br><span class="line"> Student.count += <span class="number">1</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">tom = Student(<span class="string">'Tom'</span>)</span><br><span class="line">bob = Student(<span class="string">'Bob'</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'Student.count = '</span>, Student.count)</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">Student.count = <span class="number">2</span></span><br></pre></td></tr></table></figure><hr><h1 id="5-slots-限制实例属性"><a href="#5-slots-限制实例属性" class="headerlink" title="5. slots 限制实例属性"></a>5. <strong>slots</strong> 限制实例属性</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/usr/bin/env python</span></span><br><span class="line"><span class="comment"># -*- coding : utf-8 -*-</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"> __slots__ = (<span class="string">'__name'</span>, <span class="string">'gender'</span>, <span class="string">'age'</span>)</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name</span>):</span></span><br><span class="line"> self.__name = name</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">get_name</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="keyword">return</span> self.__name</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Master</span>(<span class="params">Student</span>):</span></span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Doctor</span>(<span class="params">Student</span>):</span></span><br><span class="line"> __slots__ = (<span class="string">'major'</span>,)</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出 </span></span><br><span class="line"><span class="meta">>>> </span>s = Student(<span class="string">'Tom'</span>)</span><br><span class="line"><span class="meta">>>> </span>s.get_name()</span><br><span class="line"><span class="string">'Tom'</span></span><br><span class="line"><span class="meta">>>> </span>s.age = <span class="number">18</span></span><br><span class="line"><span class="meta">>>> </span>s.score = <span class="number">100</span> <span class="comment">#因为加了__slots__限制</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line"> File <span class="string">"<input>"</span>, line <span class="number">1</span>, <span class="keyword">in</span> <module></span><br><span class="line">AttributeError: <span class="string">'Student'</span> <span class="built_in">object</span> has no attribute <span class="string">'score'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#__slots__限制不继承</span></span><br><span class="line"><span class="meta">>>> </span>m = Master(<span class="string">'Tom'</span>)</span><br><span class="line"><span class="meta">>>> </span>m.score = <span class="number">100</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#如果子类也有__slots__, 则会继承</span></span><br><span class="line"><span class="meta">>>> </span>d = Doctor(<span class="string">'Tom'</span>)</span><br><span class="line"><span class="meta">>>> </span>d.score = <span class="number">100</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line"> File <span class="string">"<input>"</span>, line <span class="number">1</span>, <span class="keyword">in</span> <module></span><br><span class="line">AttributeError: <span class="string">'Doctor'</span> <span class="built_in">object</span> has no attribute <span class="string">'score'</span></span><br><span class="line"><span class="meta">>>> </span>d.age = <span class="number">28</span></span><br><span class="line"><span class="meta">>>> </span>d.major = <span class="string">'CS'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#类方法也同理</span></span><br><span class="line"><span class="meta">>>> </span><span class="function"><span class="keyword">def</span> <span class="title">set_age</span>(<span class="params">self, age</span>):</span></span><br><span class="line"><span class="meta">... </span> self.age = age</span><br><span class="line"><span class="meta">... </span> </span><br><span class="line"><span class="meta">>>> </span><span class="keyword">from</span> types <span class="keyword">import</span> MethodType</span><br><span class="line"><span class="meta">>>> </span>s.set_age = MethodType(set_age, s)</span><br><span class="line">Traceback (most recent call last):</span><br><span class="line"> File <span class="string">"<input>"</span>, line <span class="number">1</span>, <span class="keyword">in</span> <module></span><br><span class="line">AttributeError: <span class="string">'Student'</span> <span class="built_in">object</span> has no attribute <span class="string">'set_age'</span></span><br><span class="line"></span><br><span class="line"><span class="meta">>>> </span>m.set_age = MethodType(set_age, m)</span><br><span class="line"><span class="meta">>>> </span>m.set_age(<span class="number">18</span>)</span><br><span class="line"><span class="meta">>>> </span>m.age</span><br><span class="line"><span class="number">18</span></span><br><span class="line"></span><br><span class="line"><span class="meta">>>> </span>d.set_age = MethodType(set_age, d)</span><br><span class="line">Traceback (most recent call last):</span><br><span class="line"> File <span class="string">"<input>"</span>, line <span class="number">1</span>, <span class="keyword">in</span> <module></span><br><span class="line">AttributeError: <span class="string">'Doctor'</span> <span class="built_in">object</span> has no attribute <span class="string">'set_age'</span></span><br></pre></td></tr></table></figure><h1 id="6-property-像访问属性一样的访问类方法-且约束访问权限"><a href="#6-property-像访问属性一样的访问类方法-且约束访问权限" class="headerlink" title="6. @property 像访问属性一样的访问类方法,且约束访问权限"></a>6. @property 像访问属性一样的访问类方法,且约束访问权限</h1><h2 id="6-1-属性无法被保护,且没有参数检测"><a href="#6-1-属性无法被保护,且没有参数检测" class="headerlink" title="6.1. 属性无法被保护,且没有参数检测"></a>6.1. 属性无法被保护,且没有参数检测</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Screen</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, width, height</span>):</span></span><br><span class="line"> self._width = width</span><br><span class="line"> self._height = height</span><br><span class="line"> self.resolution = self._width * self._height</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line"><span class="meta">>>> </span>s = Screen(<span class="number">1024</span>,<span class="number">768</span>)</span><br><span class="line"><span class="meta">>>> </span>s._width</span><br><span class="line"><span class="number">1024</span></span><br><span class="line"><span class="meta">>>> </span>s.resolution</span><br><span class="line"><span class="number">786432</span></span><br></pre></td></tr></table></figure><h2 id="6-2-属性被保护了-但是访问属性的方式变了-–-Method"><a href="#6-2-属性被保护了-但是访问属性的方式变了-–-Method" class="headerlink" title="6.2. 属性被保护了,但是访问属性的方式变了 – Method"></a>6.2. 属性被保护了,但是访问属性的方式变了 – Method</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Screen</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, width, height</span>):</span></span><br><span class="line"> self.__width = width</span><br><span class="line"> self.__height = height</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">get_width</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="keyword">return</span> self.__width</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">set_width</span>(<span class="params">self, value</span>):</span></span><br><span class="line"> <span class="keyword">if</span> value < <span class="number">0</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'the value of width is wrong'</span>)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> self.__width = value</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">get_height</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="keyword">return</span> self.__height</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">set_height</span>(<span class="params">self, value</span>):</span></span><br><span class="line"> <span class="keyword">if</span> value < <span class="number">0</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'the value of height is wrong'</span>)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> self.__height = value</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">resolution</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="keyword">return</span> self.__width * self.__height</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line"><span class="meta">>>> </span>s = Screen(<span class="number">1024</span>, <span class="number">786</span>)</span><br><span class="line"><span class="meta">>>> </span>s.get_width()</span><br><span class="line"><span class="number">1024</span></span><br><span class="line"><span class="meta">>>> </span>s.set_width(<span class="number">1080</span>)</span><br><span class="line"><span class="meta">>>> </span>s.get_width()</span><br><span class="line"><span class="number">1080</span></span><br><span class="line"><span class="meta">>>> </span>s.resolution()</span><br><span class="line"><span class="number">848880</span></span><br></pre></td></tr></table></figure><h2 id="6-3-利用property"><a href="#6-3-利用property" class="headerlink" title="6.3. 利用property"></a>6.3. 利用property</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/usr/bin/env python</span></span><br><span class="line"><span class="comment"># -*- coding : utf-8 -*-</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Screen</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, width, height</span>):</span></span><br><span class="line"> self.__width = width</span><br><span class="line"> self.__height = height</span><br><span class="line"></span><br><span class="line"><span class="meta"> @property </span><span class="comment">#getter方法</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">width</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="keyword">return</span> self.__width</span><br><span class="line"></span><br><span class="line"><span class="meta"> @width.setter </span><span class="comment">#setter方法</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">width</span>(<span class="params">self, value</span>):</span></span><br><span class="line"> <span class="keyword">if</span> value < <span class="number">0</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'the value of width is wrong'</span>)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> self.__width = value</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">get_height</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="keyword">return</span> self.__height</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">set_height</span>(<span class="params">self, value</span>):</span></span><br><span class="line"> <span class="keyword">if</span> value < <span class="number">0</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'the value of height is wrong'</span>)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> self.__height = value</span><br><span class="line"></span><br><span class="line"> height = <span class="built_in">property</span>(fget=get_height, fset=set_height) <span class="comment">#利用了property类的原型, 这样也不用修改函数名了</span></span><br><span class="line"></span><br><span class="line"><span class="meta"> @property</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">resolution</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="keyword">return</span> self.__width * self.__height</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line"><span class="meta">>>> </span>s = Screen(<span class="number">1024</span>, <span class="number">768</span>)</span><br><span class="line"><span class="meta">>>> </span>s.width</span><br><span class="line"><span class="number">1024</span></span><br><span class="line"><span class="meta">>>> </span>s.height</span><br><span class="line"><span class="number">768</span></span><br><span class="line"><span class="meta">>>> </span>s.width = <span class="number">1080</span></span><br><span class="line"><span class="meta">>>> </span>s.resolution</span><br><span class="line"><span class="number">829440</span></span><br></pre></td></tr></table></figure><h2 id="6-4-property类原型"><a href="#6-4-property类原型" class="headerlink" title="6.4. property类原型"></a>6.4. property类原型</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, fget=<span class="literal">None</span>, fset=<span class="literal">None</span>, fdel=<span class="literal">None</span>, doc=<span class="literal">None</span></span>)</span></span><br></pre></td></tr></table></figure><h1 id="7-多重继承与MixIn设计"><a href="#7-多重继承与MixIn设计" class="headerlink" title="7. 多重继承与MixIn设计"></a>7. 多重继承与MixIn设计</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/usr/bin/env python</span></span><br><span class="line"><span class="comment"># -*- coding : utf-8 -*-</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Transportation</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Public</span>(<span class="params">Transportation</span>):</span></span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Private</span>(<span class="params">Transportation</span>):</span></span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">RoadMixIn</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">road</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'On the road'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SeaMixIn</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">sea</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'On the sea'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SkyMixIn</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">sky</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'In the sky'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Bus</span>(<span class="params">Public, RoadMixIn</span>):</span></span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span>(<span class="params">Private, RoadMixIn</span>):</span></span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Airplane</span>(<span class="params">Public, SkyMixIn</span>):</span></span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Steamship</span>(<span class="params">Public, SeaMixIn</span>):</span></span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line"><span class="meta">>>> </span>c = Car()</span><br><span class="line"><span class="meta">>>> </span>c.road()</span><br><span class="line">On the road</span><br><span class="line"><span class="meta">>>> </span>a = Airplane()</span><br><span class="line"><span class="meta">>>> </span>a.sky()</span><br><span class="line">In the sky</span><br><span class="line"><span class="meta">>>> </span>s = Steamship()</span><br><span class="line"><span class="meta">>>> </span>s.sea()</span><br><span class="line">On the sea</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#实际应用</span></span><br><span class="line">(<span class="number">1</span>).多进程TCP服务</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyTCPServer</span>(<span class="params">TCPServer, ForkingMixIn</span>):</span></span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line">(<span class="number">2</span>).多线程UDP服务</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyUDPServer</span>(<span class="params">UDPServer, ThreadingMixIn</span>):</span></span><br><span class="line"> <span class="keyword">pass</span></span><br></pre></td></tr></table></figure><h1 id="8-定制类"><a href="#8-定制类" class="headerlink" title="8. 定制类"></a>8. 定制类</h1><h2 id="8-1-str-与-repr-描述对象信息"><a href="#8-1-str-与-repr-描述对象信息" class="headerlink" title="8.1. str 与 repr 描述对象信息"></a>8.1. <strong>str</strong> 与 <strong>repr</strong> 描述对象信息</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#__str__ : 强调可读性,结果是让人看的</span></span><br><span class="line"><span class="comment">#__repr__ : 强调准确性,结果是让解释器用的. (默认内置类型)</span></span><br><span class="line"><span class="meta">>>> </span><span class="keyword">import</span> datetime</span><br><span class="line"><span class="meta">>>> </span>today = datetime.datetime.now()</span><br><span class="line"><span class="meta">>>> </span>today</span><br><span class="line">datetime.datetime(<span class="number">2020</span>, <span class="number">5</span>, <span class="number">19</span>, <span class="number">11</span>, <span class="number">9</span>, <span class="number">14</span>, <span class="number">161591</span>)</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">repr</span>(today)</span><br><span class="line"><span class="string">'datetime.datetime(2020, 5, 19, 11, 9, 14, 161591)'</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">str</span>(today)</span><br><span class="line"><span class="string">'2020-05-19 11:09:14.161591'</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#__str__</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">str</span>(<span class="string">'4'</span>)</span><br><span class="line"><span class="string">'4'</span></span><br><span class="line"><span class="meta">>>> </span><span class="string">'4'</span>.__str__()</span><br><span class="line"><span class="string">'4'</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">str</span>(<span class="number">4</span>)</span><br><span class="line"><span class="string">'4'</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#__repr__</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">repr</span>(<span class="string">'4'</span>)</span><br><span class="line"><span class="string">"'4'"</span></span><br><span class="line"><span class="meta">>>> </span><span class="string">'4'</span>.__repr__()</span><br><span class="line"><span class="string">"'4'"</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">repr</span>(<span class="number">4</span>)</span><br><span class="line"><span class="string">'4'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#关系</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">str</span>(<span class="number">4</span>) == <span class="built_in">repr</span>(<span class="number">4</span>) <span class="comment">#整数的返回值是一致的</span></span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">str</span>(<span class="string">'4'</span>) == <span class="built_in">repr</span>(<span class="string">'4'</span>) <span class="comment">#字符串的返回值不同</span></span><br><span class="line"><span class="literal">False</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#原始状态</span></span><br><span class="line"><span class="meta">>>> </span><span class="class"><span class="keyword">class</span> <span class="title">Student</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name</span>):</span></span><br><span class="line"><span class="meta">... </span> self.name = name</span><br><span class="line">...</span><br><span class="line"><span class="meta">>>> </span>s = Student(<span class="string">'Mike'</span>)</span><br><span class="line"><span class="meta">>>> </span>s</span><br><span class="line"><__main__.Student <span class="built_in">object</span> at <span class="number">0x10281ba50</span>></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(s)</span><br><span class="line"><__main__.Student <span class="built_in">object</span> at <span class="number">0x10281ba50</span>></span><br><span class="line"></span><br><span class="line"><span class="comment">#重写了__str__后,在调用print函数时生效</span></span><br><span class="line"><span class="meta">>>> </span><span class="class"><span class="keyword">class</span> <span class="title">Student</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name</span>):</span></span><br><span class="line"><span class="meta">... </span> self.name = name</span><br><span class="line">...</span><br><span class="line"><span class="meta">... </span> <span class="function"><span class="keyword">def</span> <span class="title">__str__</span>(<span class="params">self</span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> <span class="string">'Student object (name: %s)'</span> % self.name</span><br><span class="line"><span class="meta">... </span> </span><br><span class="line"><span class="meta">>>> </span>s = Student(<span class="string">'Mike'</span>)</span><br><span class="line"><span class="meta">>>> </span>s</span><br><span class="line"><__main__.Student <span class="built_in">object</span> at <span class="number">0x102831250</span>></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(s)</span><br><span class="line">Student <span class="built_in">object</span> (name: Mike)</span><br><span class="line"></span><br><span class="line"><span class="comment">#重写了__repr__后, 交互模式和print函数均生效</span></span><br><span class="line"><span class="meta">>>> </span><span class="class"><span class="keyword">class</span> <span class="title">Student</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name</span>):</span></span><br><span class="line"><span class="meta">... </span> self.name = name</span><br><span class="line">...</span><br><span class="line"><span class="meta">... </span> <span class="function"><span class="keyword">def</span> <span class="title">__repr__</span>(<span class="params">self</span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> <span class="string">'Student object (name: %s)'</span> % self.name</span><br><span class="line"><span class="meta">... </span> </span><br><span class="line"><span class="meta">>>> </span>s = Student(<span class="string">'Mike'</span>)</span><br><span class="line"><span class="meta">>>> </span>s</span><br><span class="line">Student <span class="built_in">object</span> (name: Mike)</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(s)</span><br><span class="line">Student <span class="built_in">object</span> (name: Mike)</span><br></pre></td></tr></table></figure><h2 id="8-2-iter"><a href="#8-2-iter" class="headerlink" title="8.2. iter"></a>8.2. <strong>iter</strong></h2><blockquote><p>如果一个类想被用于for循环就必须实现一个<strong>iter</strong>()方法,该方法返回一个迭代对象,for循环会不断的调用该迭代对象的<strong>next</strong>()方法,直到遇到StopIteration退出。</p></blockquote><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="class"><span class="keyword">class</span> <span class="title">Fib</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self</span>):</span></span><br><span class="line"><span class="meta">... </span> self.a = <span class="number">0</span></span><br><span class="line"><span class="meta">... </span> self.b = <span class="number">1</span></span><br><span class="line">...</span><br><span class="line"><span class="meta">... </span> <span class="function"><span class="keyword">def</span> <span class="title">__iter__</span>(<span class="params">self</span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> self</span><br><span class="line">...</span><br><span class="line"><span class="meta">... </span> <span class="function"><span class="keyword">def</span> <span class="title">__next__</span>(<span class="params">self</span>):</span></span><br><span class="line"><span class="meta">... </span> self.a, self.b = self.b, self.a + self.b</span><br><span class="line"><span class="meta">... </span> <span class="keyword">if</span> self.a > <span class="number">100</span>:</span><br><span class="line"><span class="meta">... </span> <span class="keyword">raise</span> StopIteration()</span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> self.a</span><br><span class="line"><span class="meta">... </span> </span><br><span class="line"><span class="meta">>>> </span>f = Fib()</span><br><span class="line"><span class="meta">>>> </span>f</span><br><span class="line"><__main__.Fib <span class="built_in">object</span> at <span class="number">0x10bd40b50</span>></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">type</span>(f)</span><br><span class="line"><<span class="class"><span class="keyword">class</span> '<span class="title">__main__</span>.<span class="title">Fib</span>'></span></span><br><span class="line"><span class="class">>>> <span class="title">for</span> <span class="title">i</span> <span class="title">in</span> <span class="title">f</span>:</span></span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(i)</span><br><span class="line"><span class="meta">... </span> </span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="number">5</span></span><br><span class="line"><span class="number">8</span></span><br><span class="line"><span class="number">13</span></span><br><span class="line"><span class="number">21</span></span><br><span class="line"><span class="number">34</span></span><br><span class="line"><span class="number">55</span></span><br><span class="line"><span class="number">89</span></span><br></pre></td></tr></table></figure><h2 id="8-3-getitem"><a href="#8-3-getitem" class="headerlink" title="8.3. getitem"></a>8.3. <strong>getitem</strong></h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#通过__iter__()实现的例子虽然能用于for循环,但是却无法像List一样的使用</span></span><br><span class="line"><span class="meta">>>> </span>Fib()[<span class="number">3</span>]</span><br><span class="line">Traceback (most recent call last):</span><br><span class="line"> File <span class="string">"<input>"</span>, line <span class="number">1</span>, <span class="keyword">in</span> <module></span><br><span class="line">TypeError: <span class="string">'Fib'</span> <span class="built_in">object</span> <span class="keyword">is</span> <span class="keyword">not</span> subscriptable</span><br><span class="line"></span><br><span class="line"><span class="comment">#__getitem__()</span></span><br><span class="line"><span class="meta">>>> </span><span class="class"><span class="keyword">class</span> <span class="title">Fib</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="function"><span class="keyword">def</span> <span class="title">__getitem__</span>(<span class="params">self, n</span>):</span></span><br><span class="line"><span class="meta">... </span> a, b = <span class="number">1</span>, <span class="number">1</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">for</span> x <span class="keyword">in</span> <span class="built_in">range</span>(n):</span><br><span class="line"><span class="meta">... </span> a, b = b, a + b</span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> a</span><br><span class="line"><span class="meta">... </span> </span><br><span class="line"><span class="meta">>>> </span>Fib()[<span class="number">3</span>]</span><br><span class="line"><span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#如果想在类中实现List,dict,tuple等,可以重写__getitem__、__setitem__、__delitem__</span></span><br></pre></td></tr></table></figure><h2 id="8-4-getattr"><a href="#8-4-getattr" class="headerlink" title="8.4. getattr"></a>8.4. <strong>getattr</strong></h2><blockquote><p>可以用于把一个类的属性和方法动态化处理</p></blockquote><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#如果找不到类属性or类方法, 则会去__getattr__中寻找; 因此加了判断。通过这种方法来实现动态。</span></span><br><span class="line"><span class="meta">>>> </span><span class="class"><span class="keyword">class</span> <span class="title">Student</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line">...</span><br><span class="line"><span class="meta">... </span> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self</span>):</span></span><br><span class="line"><span class="meta">... </span> self.name = <span class="string">'Michael'</span></span><br><span class="line">...</span><br><span class="line"><span class="meta">... </span> <span class="function"><span class="keyword">def</span> <span class="title">__getattr__</span>(<span class="params">self, attr</span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">if</span> attr == <span class="string">'score'</span>:</span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> <span class="number">100</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">raise</span> AttributeError(<span class="string">'\'Student\' object has no attribute \'%s\''</span> % attr)</span><br><span class="line"><span class="meta">... </span> </span><br><span class="line"><span class="meta">>>> </span>s = Student()</span><br><span class="line"><span class="meta">>>> </span>s.name</span><br><span class="line"><span class="string">'Michael'</span></span><br><span class="line"><span class="meta">>>> </span>s.score</span><br><span class="line"><span class="number">100</span></span><br><span class="line"><span class="meta">>>> </span>s.age</span><br><span class="line">Traceback (most recent call last):</span><br><span class="line"> File <span class="string">"<input>"</span>, line <span class="number">1</span>, <span class="keyword">in</span> <module></span><br><span class="line"> File <span class="string">"<input>"</span>, line <span class="number">9</span>, <span class="keyword">in</span> __getattr__</span><br><span class="line">AttributeError: <span class="string">'Student'</span> <span class="built_in">object</span> has no attribute <span class="string">'age'</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#链式调用: REST API; 这样就不需要给每一个URL的API都写一个方法了</span></span><br><span class="line"><span class="comment">#Eg. http://api.server/user/timeline/list</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Chain</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, path=<span class="string">''</span></span>):</span></span><br><span class="line"> self._path = path</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__getattr__</span>(<span class="params">self, path</span>):</span></span><br><span class="line"> <span class="keyword">return</span> Chain(<span class="string">'%s/%s'</span> % (self._path, path))</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__str__</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="keyword">return</span> self._path</span><br><span class="line"></span><br><span class="line"> __repr__ = __str__</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(Chain().status.user.timeline.<span class="built_in">list</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">/status/user/timeline/<span class="built_in">list</span></span><br></pre></td></tr></table></figure><h2 id="8-5-call-实现类实例本身的调用"><a href="#8-5-call-实现类实例本身的调用" class="headerlink" title="8.5. call 实现类实例本身的调用"></a>8.5. <strong>call</strong> 实现类实例本身的调用</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="class"><span class="keyword">class</span> <span class="title">Student</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name</span>):</span></span><br><span class="line"><span class="meta">... </span> self.name = name</span><br><span class="line">...</span><br><span class="line"><span class="meta">... </span> <span class="function"><span class="keyword">def</span> <span class="title">__call__</span>(<span class="params">self</span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(<span class="string">'My name is %s.'</span> % self.name)</span><br><span class="line"><span class="meta">... </span> </span><br><span class="line"><span class="meta">>>> </span>s = Student(<span class="string">'Mike'</span>)</span><br><span class="line"><span class="meta">>>> </span>s() <span class="comment">#像函数一样调用</span></span><br><span class="line">My name <span class="keyword">is</span> Mike.</span><br><span class="line"></span><br><span class="line"><span class="comment">#判断一个对象是否能被调用</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">callable</span>(Student(<span class="string">'Mike'</span>))</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">callable</span>(<span class="built_in">abs</span>)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">callable</span>([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>])</span><br><span class="line"><span class="literal">False</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">callable</span>(<span class="string">'str'</span>)</span><br><span class="line"><span class="literal">False</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#实现 Chain().users('michael').repos的链式调用</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Chain</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, path=<span class="string">''</span></span>):</span></span><br><span class="line"> self.__path = path</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__getattr__</span>(<span class="params">self, path</span>):</span></span><br><span class="line"> <span class="keyword">return</span> Chain(<span class="string">'%s/%s'</span> % (self.__path, path))</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__call__</span>(<span class="params">self, path</span>):</span></span><br><span class="line"> <span class="keyword">return</span> Chain(<span class="string">'%s/%s'</span> % (self.__path, path))</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__str__</span>(<span class="params">self</span>):</span></span><br><span class="line"> <span class="keyword">return</span> self.__path</span><br><span class="line"></span><br><span class="line"> __repr__ = __str__</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(Chain().users(<span class="string">'michael'</span>).repos)</span><br></pre></td></tr></table></figure><h1 id="9-枚举类"><a href="#9-枚举类" class="headerlink" title="9. 枚举类"></a>9. 枚举类</h1><p>枚举类型可以看做是一种标签或一组常量集合,例如:月份、星期、状态等。<br>字典和类也可实现,但是存在几个问题:</p><ul><li>变量可更改</li><li>字符串所占内存比数字大<h2 id="9-1-使用枚举类"><a href="#9-1-使用枚举类" class="headerlink" title="9.1. 使用枚举类"></a>9.1. 使用枚举类</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#使用字典</span></span><br><span class="line">Week = {</span><br><span class="line"> <span class="string">'Sun'</span>: <span class="number">0</span>,</span><br><span class="line"> <span class="string">'Mon'</span>: <span class="number">1</span>,</span><br><span class="line"> <span class="string">'Tue'</span>: <span class="number">2</span>,</span><br><span class="line"> <span class="string">'Wed'</span>: <span class="number">3</span>,</span><br><span class="line"> <span class="string">'Thu'</span>: <span class="number">4</span>,</span><br><span class="line"> <span class="string">'Fri'</span>: <span class="number">5</span>,</span><br><span class="line"> <span class="string">'Sat'</span>: <span class="number">6</span>,</span><br><span class="line">}</span><br><span class="line"><span class="comment">#使用类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Week</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"> Sun = <span class="number">0</span></span><br><span class="line"> Mon = <span class="number">1</span></span><br><span class="line"> Tue = <span class="number">2</span></span><br><span class="line"> Wed = <span class="number">3</span></span><br><span class="line"> Thu = <span class="number">4</span></span><br><span class="line"> Fri = <span class="number">5</span></span><br><span class="line"> Sat = <span class="number">6</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#使用 枚举类</span></span><br><span class="line"><span class="keyword">from</span> enum <span class="keyword">import</span> Enum</span><br><span class="line"><span class="comment">#(1).方式一</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Week</span>(<span class="params">Enum</span>):</span></span><br><span class="line"> Sun = <span class="number">0</span></span><br><span class="line"> Mon = <span class="number">1</span></span><br><span class="line"> Tue = <span class="number">2</span></span><br><span class="line"> Wed = <span class="number">3</span></span><br><span class="line"> Thu = <span class="number">4</span></span><br><span class="line"> Fri = <span class="number">5</span></span><br><span class="line"> Sat = <span class="number">6</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> d <span class="keyword">in</span> Week:</span><br><span class="line"> <span class="built_in">print</span>(d.name, <span class="string">'='</span>, d.value)</span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">Sun = <span class="number">0</span></span><br><span class="line">Mon = <span class="number">1</span></span><br><span class="line">Tue = <span class="number">2</span></span><br><span class="line">Wed = <span class="number">3</span></span><br><span class="line">Thu = <span class="number">4</span></span><br><span class="line">Fri = <span class="number">5</span></span><br><span class="line">Sat = <span class="number">6</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#(2).方式二</span></span><br><span class="line">Week = Enum(<span class="string">'Week'</span>, (<span class="string">'Sun'</span>, <span class="string">'Mon'</span>, <span class="string">'Tue'</span>, <span class="string">'Wed'</span>, <span class="string">'Thu'</span>, <span class="string">'Fri'</span>, <span class="string">'Sat'</span>))</span><br><span class="line"><span class="keyword">for</span> name, member <span class="keyword">in</span> Week.__members__.items():</span><br><span class="line"> <span class="built_in">print</span>(name, <span class="string">'=>'</span>, member, <span class="string">','</span>, member.value - <span class="number">1</span>)</span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">Sun => Week.Sun , <span class="number">0</span></span><br><span class="line">Mon => Week.Mon , <span class="number">1</span></span><br><span class="line">Tue => Week.Tue , <span class="number">2</span></span><br><span class="line">Wed => Week.Wed , <span class="number">3</span></span><br><span class="line">Thu => Week.Thu , <span class="number">4</span></span><br><span class="line">Fri => Week.Fri , <span class="number">5</span></span><br><span class="line">Sat => Week.Sat , <span class="number">6</span></span><br></pre></td></tr></table></figure></li></ul><h2 id="9-2-定义枚举类"><a href="#9-2-定义枚举类" class="headerlink" title="9.2. 定义枚举类"></a>9.2. 定义枚举类</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#成员名(key)不能重复</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Week</span>(<span class="params">Enum</span>):</span></span><br><span class="line"> Sun = <span class="number">0</span></span><br><span class="line"> Mon = <span class="number">1</span></span><br><span class="line"> Tue = <span class="number">2</span></span><br><span class="line"> Wed = <span class="number">3</span></span><br><span class="line"> Thu = <span class="number">4</span></span><br><span class="line"> Fri = <span class="number">5</span></span><br><span class="line"> Sat = <span class="number">6</span></span><br><span class="line"> Sun = <span class="number">10</span> <span class="comment">#Attempted to reuse key: 'Sun'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#成员值(value)可以重复,但是相当于别名</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Week</span>(<span class="params">Enum</span>):</span></span><br><span class="line"> Sun = <span class="number">0</span></span><br><span class="line"> Mon = <span class="number">1</span></span><br><span class="line"> Tue = <span class="number">2</span></span><br><span class="line"> Wed = <span class="number">3</span></span><br><span class="line"> Thu = <span class="number">4</span></span><br><span class="line"> Fri = <span class="number">5</span></span><br><span class="line"> Sat = <span class="number">6</span></span><br><span class="line"> Hol = <span class="number">0</span></span><br><span class="line"></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(Week.Sun)</span><br><span class="line">Week.Sun</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(Week.Hol) <span class="comment">#相当于别名</span></span><br><span class="line">Week.Sun</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(Week.Sun <span class="keyword">is</span> Week.Hol)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(Week(<span class="number">0</span>)) <span class="comment">#虽然值一样,但只会显示第一个</span></span><br><span class="line">Week.Sun</span><br><span class="line"></span><br><span class="line"><span class="comment">#unique可使成员值(value)不可重复</span></span><br><span class="line"><span class="keyword">from</span> enum <span class="keyword">import</span> Enum, unique</span><br><span class="line"><span class="meta">@unique</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Week</span>(<span class="params">Enum</span>):</span></span><br><span class="line"> Sun = <span class="number">0</span></span><br><span class="line"> Mon = <span class="number">1</span></span><br><span class="line"> Tue = <span class="number">2</span></span><br><span class="line"> Wed = <span class="number">3</span></span><br><span class="line"> Thu = <span class="number">4</span></span><br><span class="line"> Fri = <span class="number">5</span></span><br><span class="line"> Sat = <span class="number">6</span></span><br><span class="line"> Hol = <span class="number">0</span> <span class="comment">#ValueError: duplicate values found in <enum 'Week'>: Hol -> Sun</span></span><br></pre></td></tr></table></figure><h2 id="9-3-枚举取值"><a href="#9-3-枚举取值" class="headerlink" title="9.3. 枚举取值"></a>9.3. 枚举取值</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#通过成员名</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(Week.Sat)</span><br><span class="line">Week.Sat</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(Week[<span class="string">'Sat'</span>])</span><br><span class="line">Week.Sat</span><br><span class="line"></span><br><span class="line"><span class="comment">#通过成员值</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(Week(<span class="number">6</span>))</span><br><span class="line">Week.Sat</span><br><span class="line"></span><br><span class="line"><span class="comment">#成员均有name和value属性</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(Week.Sat.name)</span><br><span class="line">Sat</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">print</span>(Week.Sat.value)</span><br><span class="line"><span class="number">6</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#遍历</span></span><br><span class="line"><span class="comment">#(1).直接遍历,如果有重复值成员,则只显示第一个</span></span><br><span class="line"><span class="meta">>>> </span><span class="keyword">for</span> d <span class="keyword">in</span> Week:</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(d)</span><br><span class="line"><span class="meta">... </span> </span><br><span class="line">Week.Sun</span><br><span class="line">Week.Mon</span><br><span class="line">Week.Tue</span><br><span class="line">Week.Wed</span><br><span class="line">Week.Thu</span><br><span class="line">Week.Fri</span><br><span class="line">Week.Sat</span><br><span class="line"><span class="comment">#(2).__members__ 特殊属性,是一个将名称映射到成员的有序字典,显示全部成员</span></span><br><span class="line"><span class="meta">>>> </span><span class="keyword">for</span> d <span class="keyword">in</span> Week.__members__.items():</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(d)</span><br><span class="line"><span class="meta">... </span> </span><br><span class="line">(<span class="string">'Sun'</span>, <Week.Sun: <span class="number">0</span>>)</span><br><span class="line">(<span class="string">'Mon'</span>, <Week.Mon: <span class="number">1</span>>)</span><br><span class="line">(<span class="string">'Tue'</span>, <Week.Tue: <span class="number">2</span>>)</span><br><span class="line">(<span class="string">'Wed'</span>, <Week.Wed: <span class="number">3</span>>)</span><br><span class="line">(<span class="string">'Thu'</span>, <Week.Thu: <span class="number">4</span>>)</span><br><span class="line">(<span class="string">'Fri'</span>, <Week.Fri: <span class="number">5</span>>)</span><br><span class="line">(<span class="string">'Sat'</span>, <Week.Sat: <span class="number">6</span>>)</span><br><span class="line">(<span class="string">'Hol'</span>, <Week.Sun: <span class="number">0</span>>) <span class="comment">#重复值成员在此</span></span><br></pre></td></tr></table></figure><h2 id="9-4-枚举比较"><a href="#9-4-枚举比较" class="headerlink" title="9.4. 枚举比较"></a>9.4. 枚举比较</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#is: 同一性比较</span></span><br><span class="line"><span class="meta">>>> </span>Week.Sun <span class="keyword">is</span> Week.Hol</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span>Week.Sun <span class="keyword">is</span> Week.Sat</span><br><span class="line"><span class="literal">False</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#==: 等值比较</span></span><br><span class="line"><span class="meta">>>> </span>Week.Sun == Week.Hol</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span>Week.Sun != Week.Sat</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#大小比较</span></span><br><span class="line"><span class="meta">>>> </span>Week.Sun < Week.Sat</span><br><span class="line">TypeError: <span class="string">'<'</span> <span class="keyword">not</span> supported between instances of <span class="string">'Week'</span> <span class="keyword">and</span> <span class="string">'Week'</span></span><br></pre></td></tr></table></figure><h2 id="9-5-IntEnum,扩展类,支持比较"><a href="#9-5-IntEnum,扩展类,支持比较" class="headerlink" title="9.5. IntEnum,扩展类,支持比较"></a>9.5. IntEnum,扩展类,支持比较</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">from</span> enum <span class="keyword">import</span> IntEnum</span><br><span class="line"><span class="meta">>>> </span><span class="class"><span class="keyword">class</span> <span class="title">Week</span>(<span class="params">IntEnum</span>):</span></span><br><span class="line"><span class="meta">... </span> Sun = <span class="number">0</span></span><br><span class="line"><span class="meta">... </span> Mon = <span class="number">1</span></span><br><span class="line"><span class="meta">... </span> Tue = <span class="number">2</span></span><br><span class="line"><span class="meta">... </span> Wed = <span class="number">3</span></span><br><span class="line"><span class="meta">... </span> Thu = <span class="number">4</span></span><br><span class="line"><span class="meta">... </span> Fri = <span class="number">5</span></span><br><span class="line"><span class="meta">... </span> Sat = <span class="number">6</span></span><br><span class="line"><span class="meta">... </span> </span><br><span class="line"><span class="meta">>>> </span>Week.Sun < Week.Sat</span><br><span class="line"><span class="literal">True</span></span><br></pre></td></tr></table></figure><h2 id="10-元类"><a href="#10-元类" class="headerlink" title="10. 元类"></a>10. 元类</h2><p>ToDo</p><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><blockquote>
<p>OOP- Object Oriented Programming. 把对象作为程序的基本单元,一个对象包含数据和函数,计算机程序执行就是消息在各个对象之间的传递。</p>
</blockquote></summary>
<category term="Computer Science" scheme="https://aaronos0.github.io/categories/Computer-Science/"/>
<category term="Python Learning" scheme="https://aaronos0.github.io/tags/Python-Learning/"/>
</entry>
<entry>
<title>Python - Theme 5 Module and Package</title>
<link href="https://aaronos0.github.io/2020/09/28/Python-Theme-5-Module-and-Package/"/>
<id>https://aaronos0.github.io/2020/09/28/Python-Theme-5-Module-and-Package/</id>
<published>2020-09-28T21:54:27.000Z</published>
<updated>2020-09-28T21:54:27.000Z</updated>
<content type="html"><![CDATA[<h1 id="1-模块和包"><a href="#1-模块和包" class="headerlink" title="1. 模块和包"></a>1. 模块和包</h1><span id="more"></span><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#1.模块和包能大大减少代码的管理维护成本且便于阅读</span></span><br><span class="line"><span class="comment">#2.模块可以避免函数名\变量名的冲突, 但切记避免和内置函数同名.</span></span><br><span class="line"></span><br><span class="line">(venv3<span class="number">.7</span>) ➜ testproject tree</span><br><span class="line">.</span><br><span class="line">├── a.py</span><br><span class="line">├── database <span class="comment">#包: Package;</span></span><br><span class="line">│ └── __init__.py</span><br><span class="line">├── method</span><br><span class="line">│ └── __init__.py</span><br><span class="line">└── web</span><br><span class="line"> ├── __init__.py <span class="comment">#表示这是一个包,而不是普通目录.</span></span><br><span class="line"> └── b.py <span class="comment">#b模块, testproject.web.b;</span></span><br></pre></td></tr></table></figure><h1 id="2-模块的使用"><a href="#2-模块的使用" class="headerlink" title="2. 模块的使用"></a>2. 模块的使用</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#内置模块只要安装完毕即可使用, 也可自行创建模块.举例如下:</span></span><br><span class="line"><span class="comment">#testmodule.py</span></span><br><span class="line">----------------</span><br><span class="line"></span><br><span class="line"><span class="comment">#!/usr/bin/env python</span></span><br><span class="line"><span class="comment"># -*- coding : utf-8 -*-</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="string">"""This is a python file to demonstrate how to use Module and Package in python"""</span> <span class="comment">#注释内容, 可以调用__doc__显示</span></span><br><span class="line"></span><br><span class="line">__all__ = [<span class="string">"greeting"</span>,<span class="string">"string"</span>] <span class="comment">#当使用from xx import *时候, 会导入这些.</span></span><br><span class="line"></span><br><span class="line">__author__ = <span class="string">"xxx@email"</span> <span class="comment">#作者</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"></span><br><span class="line">string = <span class="string">"I'm a test program"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#以_或者__开头的被认为是内部函数, 不希望被调用;但是由于python没有这层限制, 因此只是人为约束</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">_private_hello</span>():</span></span><br><span class="line"> <span class="keyword">return</span> <span class="string">'Hello, World!'</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">__private_hi</span>(<span class="params">name</span>):</span></span><br><span class="line"> <span class="keyword">return</span> <span class="string">'Hi, %s'</span> % name</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">greeting</span>():</span></span><br><span class="line"> args = sys.argv</span><br><span class="line"> <span class="keyword">if</span> <span class="built_in">len</span>(args) == <span class="number">1</span>:</span><br><span class="line"> <span class="keyword">return</span> _private_hello()</span><br><span class="line"> <span class="keyword">elif</span> <span class="built_in">len</span>(args) == <span class="number">2</span>:</span><br><span class="line"> <span class="keyword">return</span> __private_hi(args[<span class="number">1</span>])</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'Too many arguments!'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>: <span class="comment">#在作为模块使用时,不执行; 因此一般用于运行测试.</span></span><br><span class="line"> result = greeting()</span><br><span class="line"> <span class="built_in">print</span>(result)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 命令行输出</span></span><br><span class="line">(venv3<span class="number">.7</span>) ➜ testpackage python testmodule.py</span><br><span class="line">Hello, World!</span><br><span class="line">(venv3<span class="number">.7</span>) ➜ testpackage python testmodule.py Aaron</span><br><span class="line">Hi, Aaron</span><br><span class="line"></span><br><span class="line"><span class="comment">#python交互环境</span></span><br><span class="line"><span class="meta">>>> </span><span class="keyword">import</span> testmodule</span><br><span class="line"><span class="meta">>>> </span>testmodule.greeting()</span><br><span class="line"><span class="string">'Hello, World!'</span></span><br><span class="line"><span class="meta">>>> </span>testmodule.string</span><br><span class="line"><span class="string">"I'm a test program"</span></span><br><span class="line"><span class="meta">>>> </span>testmodule.__all__</span><br><span class="line">[<span class="string">'greeting'</span>, <span class="string">'string'</span>]</span><br><span class="line"><span class="meta">>>> </span>testmodule.__doc__</span><br><span class="line"><span class="string">'This is a python file to demonstrate how to use Module and Package in python'</span></span><br><span class="line"><span class="meta">>>> </span>testmodule._private_hello()</span><br><span class="line"><span class="string">'Hello, World!'</span></span><br></pre></td></tr></table></figure><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><h1 id="1-模块和包"><a href="#1-模块和包" class="headerlink" title="1. 模块和包"></a>1. 模块和包</h1></summary>
<category term="Computer Science" scheme="https://aaronos0.github.io/categories/Computer-Science/"/>
<category term="Python Learning" scheme="https://aaronos0.github.io/tags/Python-Learning/"/>
</entry>
<entry>
<title>Python - Theme 4 Functional Programming</title>
<link href="https://aaronos0.github.io/2020/09/27/Python-Theme-4-Functional-Programming/"/>
<id>https://aaronos0.github.io/2020/09/27/Python-Theme-4-Functional-Programming/</id>
<published>2020-09-27T22:37:06.000Z</published>
<updated>2020-09-27T22:37:06.000Z</updated>
<content type="html"><![CDATA[<blockquote><p>函数式编程的一个特点是,允许把函数作为参数传入另一个函数,还允许返回一个函数。</p></blockquote><h1 id="1-高阶函数"><a href="#1-高阶函数" class="headerlink" title="1. 高阶函数"></a>1. 高阶函数</h1><span id="more"></span><h2 id="1-1-变量可以指向函数"><a href="#1-1-变量可以指向函数" class="headerlink" title="1.1. 变量可以指向函数"></a>1.1. 变量可以指向函数</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 变量可以指向函数</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">abs</span></span><br><span class="line"><built-<span class="keyword">in</span> function <span class="built_in">abs</span>></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">abs</span>(-<span class="number">6</span>)</span><br><span class="line"><span class="number">6</span></span><br><span class="line"></span><br><span class="line"><span class="meta">>>> </span>f = <span class="built_in">abs</span></span><br><span class="line"><span class="meta">>>> </span>f</span><br><span class="line"><built-<span class="keyword">in</span> function <span class="built_in">abs</span>></span><br><span class="line"><span class="meta">>>> </span>f(-<span class="number">6</span>)</span><br><span class="line"><span class="number">6</span></span><br></pre></td></tr></table></figure><h2 id="1-2-函数名也是变量"><a href="#1-2-函数名也是变量" class="headerlink" title="1.2. 函数名也是变量"></a>1.2. 函数名也是变量</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 如果改变原指向函数的变量 abs 指向其他对象</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">abs</span></span><br><span class="line"><built-<span class="keyword">in</span> function <span class="built_in">abs</span>></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">abs</span> = <span class="number">6</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">abs</span></span><br><span class="line"><span class="number">6</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">abs</span>(-<span class="number">6</span>)</span><br><span class="line">Traceback (most recent call last):</span><br><span class="line"> File <span class="string">"<input>"</span>, line <span class="number">1</span>, <span class="keyword">in</span> <module></span><br><span class="line">TypeError: <span class="string">'int'</span> <span class="built_in">object</span> <span class="keyword">is</span> <span class="keyword">not</span> <span class="built_in">callable</span></span><br></pre></td></tr></table></figure><h2 id="1-3-给函数传入函数"><a href="#1-3-给函数传入函数" class="headerlink" title="1.3. 给函数传入函数"></a>1.3. 给函数传入函数</h2><p>既然变量可以指向函数,函数的参数可以接收变量,因此一个函数就可以接收另一个函数作为参数,即–高阶函数。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="function"><span class="keyword">def</span> <span class="title">addabsolute</span>(<span class="params">x, y, f</span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> f(x) + f(y)</span><br><span class="line"><span class="meta">... </span><span class="built_in">print</span>(addabsolute(-<span class="number">2</span>, <span class="number">2</span>, <span class="built_in">abs</span>))</span><br><span class="line"><span class="number">4</span></span><br></pre></td></tr></table></figure><h2 id="1-4-map-reduce"><a href="#1-4-map-reduce" class="headerlink" title="1.4. map/reduce"></a>1.4. map/reduce</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># map(func, Iterable) -> Iterator. map将函数func依次作用在每一个Iterable元素上, 返回一个Iterator.</span></span><br><span class="line"><span class="meta">>>> </span><span class="function"><span class="keyword">def</span> <span class="title">f</span>(<span class="params">x</span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> x * x * x</span><br><span class="line">...</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">map</span>(f,[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>])</span><br><span class="line"><<span class="built_in">map</span> <span class="built_in">object</span> at <span class="number">0x11068c590</span>></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">list</span>(<span class="built_in">map</span>(f,[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]))</span><br><span class="line">[<span class="number">1</span>, <span class="number">8</span>, <span class="number">27</span>, <span class="number">64</span>]</span><br><span class="line"><span class="meta">>>> </span><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">map</span>(f,[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]):</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(i)</span><br><span class="line"><span class="meta">... </span> </span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="number">8</span></span><br><span class="line"><span class="number">27</span></span><br><span class="line"><span class="number">64</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># reduce(func, Iterable\Iterator)</span></span><br><span class="line"><span class="comment"># 连加</span></span><br><span class="line"><span class="meta">>>> </span><span class="keyword">from</span> functools <span class="keyword">import</span> reduce</span><br><span class="line"><span class="meta">>>> </span><span class="function"><span class="keyword">def</span> <span class="title">add</span>(<span class="params">x, y</span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> x + y</span><br><span class="line">...</span><br><span class="line"><span class="meta">>>> </span>reduce(add, [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>])</span><br><span class="line"><span class="number">15</span></span><br><span class="line"><span class="comment"># 连乘</span></span><br><span class="line"><span class="meta">>>> </span><span class="function"><span class="keyword">def</span> <span class="title">prod</span>(<span class="params">x, y</span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> x * y</span><br><span class="line">...</span><br><span class="line"><span class="meta">>>> </span>reduce(prod, [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>])</span><br><span class="line"><span class="number">120</span></span><br></pre></td></tr></table></figure><h2 id="1-5-fliter"><a href="#1-5-fliter" class="headerlink" title="1.5. fliter"></a>1.5. fliter</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># fliter(func, Iterable) -> Iterator. fliter 将函数作用于序列中的每一个元素,根据返回值是T/F来决定保留或去除该元素.</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 去除偶数</span></span><br><span class="line"><span class="meta">>>> </span><span class="function"><span class="keyword">def</span> <span class="title">is_odd</span>(<span class="params">n</span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> n % <span class="number">2</span> == <span class="number">1</span></span><br><span class="line">...</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">list</span>(<span class="built_in">filter</span>(is_odd, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">9</span>, <span class="number">10</span>]))</span><br><span class="line">[<span class="number">1</span>, <span class="number">5</span>, <span class="number">9</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 回文数 --<绝妙代码></span></span><br><span class="line"><span class="meta">>>> </span><span class="function"><span class="keyword">def</span> <span class="title">is_palindrome</span>(<span class="params">n</span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> <span class="built_in">str</span>(n) == <span class="built_in">str</span>(n)[::-<span class="number">1</span>]</span><br><span class="line">...</span><br><span class="line"><span class="meta">>>> </span>output = <span class="built_in">filter</span>(is_palindrome, <span class="built_in">range</span>(<span class="number">1</span>, <span class="number">1000</span>))</span><br></pre></td></tr></table></figure><h2 id="1-6-sorted"><a href="#1-6-sorted" class="headerlink" title="1.6. sorted"></a>1.6. sorted</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 高阶排序算法 - sorted()排序其实是实现了一个映射函数</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 1.对数字进行排序</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">sorted</span>([<span class="number">13</span>,-<span class="number">2</span>,<span class="number">9</span>,<span class="number">35</span>,-<span class="number">22</span>,<span class="number">0</span>,-<span class="number">0.5</span>])</span><br><span class="line">[-<span class="number">22</span>, -<span class="number">2</span>, -<span class="number">0.5</span>, <span class="number">0</span>, <span class="number">9</span>, <span class="number">13</span>, <span class="number">35</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 2.接收key函数实现自定义排序</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">sorted</span>([<span class="number">13</span>,-<span class="number">2</span>,<span class="number">9</span>,<span class="number">35</span>,-<span class="number">22</span>,<span class="number">0</span>,-<span class="number">0.5</span>], key=<span class="built_in">abs</span>)</span><br><span class="line">[<span class="number">0</span>, -<span class="number">0.5</span>, -<span class="number">2</span>, <span class="number">9</span>, <span class="number">13</span>, -<span class="number">22</span>, <span class="number">35</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 3.高阶用法</span></span><br><span class="line"><span class="comment"># 由于大写字母的ASCII比小写字母小</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">sorted</span>([<span class="string">'Bob'</span>, <span class="string">'Aaron'</span>, <span class="string">'Tom'</span>, <span class="string">'Zoom'</span>, <span class="string">'alen'</span>])</span><br><span class="line">[<span class="string">'Aaron'</span>, <span class="string">'Bob'</span>, <span class="string">'Tom'</span>, <span class="string">'Zoom'</span>, <span class="string">'alen'</span>]</span><br><span class="line"><span class="comment"># 统一大小写进行比较</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">sorted</span>([<span class="string">'Bob'</span>, <span class="string">'Aaron'</span>, <span class="string">'Tom'</span>, <span class="string">'Zoom'</span>, <span class="string">'alen'</span>], key=<span class="built_in">str</span>.lower)</span><br><span class="line">[<span class="string">'Aaron'</span>, <span class="string">'alen'</span>, <span class="string">'Bob'</span>, <span class="string">'Tom'</span>, <span class="string">'Zoom'</span>]</span><br><span class="line"><span class="comment"># 排序翻转</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">sorted</span>([<span class="string">'Bob'</span>, <span class="string">'Aaron'</span>, <span class="string">'Tom'</span>, <span class="string">'Zoom'</span>, <span class="string">'alen'</span>], key=<span class="built_in">str</span>.lower, reverse=<span class="literal">True</span>)</span><br><span class="line">[<span class="string">'Zoom'</span>, <span class="string">'Tom'</span>, <span class="string">'Bob'</span>, <span class="string">'alen'</span>, <span class="string">'Aaron'</span>]</span><br></pre></td></tr></table></figure><h1 id="2-返回函数"><a href="#2-返回函数" class="headerlink" title="2. 返回函数"></a>2. 返回函数</h1><h2 id="2-1-高阶函数可将函数作为结果返回"><a href="#2-1-高阶函数可将函数作为结果返回" class="headerlink" title="2.1. 高阶函数可将函数作为结果返回"></a>2.1. 高阶函数可将函数作为结果返回</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 累加函数</span></span><br><span class="line"><span class="meta">>>> </span><span class="function"><span class="keyword">def</span> <span class="title">get_sum</span>(<span class="params">*args</span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="function"><span class="keyword">def</span> <span class="title">cal_sum</span>():</span></span><br><span class="line"><span class="meta">... </span> s = <span class="number">0</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">for</span> i <span class="keyword">in</span> args:</span><br><span class="line"><span class="meta">... </span> s = s + i</span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> s</span><br><span class="line">...</span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> cal_sum</span><br><span class="line">...</span><br><span class="line"><span class="meta">>>> </span>f = get_sum(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>)</span><br><span class="line"><span class="meta">>>> </span>f <span class="comment">#返回的是求和函数而并非求和结果</span></span><br><span class="line"><function get_sum.<<span class="built_in">locals</span>>.cal_sum at <span class="number">0x1099d8dd0</span>></span><br><span class="line"><span class="meta">>>> </span>f() <span class="comment"># 调用函数,返回结果</span></span><br><span class="line"><span class="number">15</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 在函数 get_sum 中定义和函数 cal_sum , 内部函数cal_sum可以引用外部函数get_sum的参数和局部变量;</span></span><br><span class="line"><span class="comment"># 当get_sum返回cal_sum时候,相应的参数和变量都保存在返回函数中,这种程序结构称为闭包: Closure;</span></span><br></pre></td></tr></table></figure><h2 id="2-2-闭包"><a href="#2-2-闭包" class="headerlink" title="2.2. 闭包"></a>2.2. 闭包</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 闭包结构中,返回的函数并没有立即执行, 调用f()才执行</span></span><br><span class="line"><span class="meta">>>> </span><span class="function"><span class="keyword">def</span> <span class="title">squ</span>():</span></span><br><span class="line"><span class="meta">... </span> l = []</span><br><span class="line"><span class="meta">... </span> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">4</span>):</span><br><span class="line"><span class="meta">... </span> <span class="function"><span class="keyword">def</span> <span class="title">f</span>():</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> i*i</span><br><span class="line"><span class="meta">... </span> l.append(f)</span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> l</span><br><span class="line">...</span><br><span class="line"></span><br><span class="line"><span class="comment"># 最终调用f()时候, i 已经为3了</span></span><br><span class="line"><span class="meta">>>> </span>f1, f2, f3, f4 = squ()</span><br><span class="line"><span class="meta">>>> </span>f1()</span><br><span class="line"><span class="number">9</span></span><br><span class="line"><span class="meta">>>> </span>f2()</span><br><span class="line"><span class="number">9</span></span><br><span class="line"><span class="meta">>>> </span>f3()</span><br><span class="line"><span class="number">9</span></span><br><span class="line"><span class="meta">>>> </span>f4()</span><br><span class="line"><span class="number">9</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 闭包结构中, 返回函数不要引用任何循环变量, 或后续会发生改变的量</span></span><br><span class="line"><span class="comment"># 解决问题的方法是: 再创建一个函数, 计算出变量的值</span></span><br><span class="line"><span class="meta">>>> </span><span class="function"><span class="keyword">def</span> <span class="title">squ</span>():</span></span><br><span class="line"><span class="meta">... </span> <span class="function"><span class="keyword">def</span> <span class="title">f</span>(<span class="params">j</span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="function"><span class="keyword">def</span> <span class="title">g</span>():</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> j*j</span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> g</span><br><span class="line">...</span><br><span class="line"><span class="meta">... </span> l = []</span><br><span class="line"><span class="meta">... </span> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">4</span>):</span><br><span class="line"><span class="meta">... </span> l.append(f(i))</span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> l</span><br><span class="line">...</span><br><span class="line"><span class="meta">>>> </span>f1, f2, f3, f4 = squ()</span><br><span class="line"><span class="meta">>>> </span>f1()</span><br><span class="line"><span class="number">0</span></span><br><span class="line"><span class="meta">>>> </span>f2()</span><br><span class="line"><span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 闭包内函数修改外函数的局部变量 - nonlocal</span></span><br><span class="line"><span class="comment">#!/usr/bin/env python</span></span><br><span class="line"><span class="comment"># -*- coding : utf-8 -*-</span></span><br><span class="line"></span><br><span class="line">x = <span class="number">20</span></span><br><span class="line">y = -<span class="number">20</span></span><br><span class="line">z = <span class="number">10</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">outer</span>():</span></span><br><span class="line"> x = <span class="number">5</span></span><br><span class="line"> y = -<span class="number">5</span></span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">inner</span>():</span></span><br><span class="line"> <span class="keyword">global</span> x</span><br><span class="line"> x += <span class="number">5</span></span><br><span class="line"> <span class="keyword">nonlocal</span> y</span><br><span class="line"> y -= <span class="number">5</span></span><br><span class="line"> z = <span class="number">0</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"inner x:"</span>, x)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"inner y:"</span>, y)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"inner z:"</span>, z)</span><br><span class="line"> <span class="keyword">return</span> inner</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">f = outer()</span><br><span class="line"><span class="built_in">print</span>(<span class="string">"1."</span>+<span class="string">"="</span>*<span class="number">10</span>)</span><br><span class="line">f()</span><br><span class="line"><span class="built_in">print</span>(<span class="string">"2."</span>+<span class="string">"="</span>*<span class="number">10</span>)</span><br><span class="line">f()</span><br><span class="line"><span class="built_in">print</span>(<span class="string">"3."</span>+<span class="string">"="</span>*<span class="number">10</span>)</span><br><span class="line">f()</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line"><span class="number">1.</span>==========</span><br><span class="line">inner x: <span class="number">25</span></span><br><span class="line">inner y: -<span class="number">10</span></span><br><span class="line">inner z: <span class="number">0</span></span><br><span class="line"><span class="number">2.</span>==========</span><br><span class="line">inner x: <span class="number">30</span></span><br><span class="line">inner y: -<span class="number">15</span></span><br><span class="line">inner z: <span class="number">0</span></span><br><span class="line"><span class="number">3.</span>==========</span><br><span class="line">inner x: <span class="number">35</span></span><br><span class="line">inner y: -<span class="number">20</span></span><br><span class="line">inner z: <span class="number">0</span></span><br></pre></td></tr></table></figure><h1 id="3-匿名函数-lambda"><a href="#3-匿名函数-lambda" class="headerlink" title="3. 匿名函数 - lambda"></a>3. 匿名函数 - lambda</h1><blockquote><p>匿名函数无需显示的定义, 冒号前面的表示函数参数, 返回值即表达式的结果.</p></blockquote><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="built_in">list</span>(<span class="built_in">filter</span>(<span class="keyword">lambda</span> x: x % <span class="number">2</span> ==<span class="number">1</span>, <span class="built_in">range</span>(<span class="number">1</span>, <span class="number">10</span>)))</span><br><span class="line">[<span class="number">1</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">9</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 匿名函数作为返回值</span></span><br><span class="line"><span class="meta">>>> </span><span class="function"><span class="keyword">def</span> <span class="title">perimeter</span>(<span class="params">x, y</span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> <span class="keyword">lambda</span> : <span class="number">2</span> * (x+y)</span><br><span class="line">...</span><br><span class="line"><span class="meta">>>> </span>perimeter(<span class="number">1</span>,<span class="number">2</span>)</span><br><span class="line"><function perimeter.<<span class="built_in">locals</span>>.<<span class="keyword">lambda</span>> at <span class="number">0x108c57ef0</span>></span><br><span class="line"><span class="meta">>>> </span>perimeter(<span class="number">1</span>,<span class="number">2</span>)()</span><br><span class="line"><span class="number">6</span></span><br></pre></td></tr></table></figure><h1 id="4-装饰器-decorator"><a href="#4-装饰器-decorator" class="headerlink" title="4. 装饰器 - decorator"></a>4. 装饰器 - decorator</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 装饰器其实就是一个返回函数的高阶函数</span></span><br><span class="line"><span class="comment"># 1.不含参数</span></span><br><span class="line"><span class="comment">#!/usr/bin/env python</span></span><br><span class="line"><span class="comment"># -*- coding : utf-8 -*-</span></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> functools</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">process_time</span>(<span class="params">func</span>):</span></span><br><span class="line"><span class="meta"> @functools.wraps(<span class="params">func</span>) </span><span class="comment">#相当于: wrapper.__name__ = func.__name__</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span>(<span class="params">*args, **kw</span>):</span></span><br><span class="line"> start = time.time()</span><br><span class="line"> results = func(*args)</span><br><span class="line"> end = time.time()</span><br><span class="line"> costtime = end - start</span><br><span class="line"> <span class="built_in">print</span>(func.__name__, <span class="string">"cost:"</span>, costtime)</span><br><span class="line"> <span class="keyword">return</span> results</span><br><span class="line"> <span class="keyword">return</span> wrapper</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@process_time </span><span class="comment">#相当于: squ = process_time(squ), 更换了原指向函数的变量squ的指向.</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">squ</span>(<span class="params">x</span>):</span></span><br><span class="line"> <span class="keyword">return</span> x*x</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">square = squ(<span class="number">5</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'square ='</span>,square)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'func name ='</span>,squ.__name__)</span><br><span class="line"></span><br><span class="line"><span class="comment">#输出</span></span><br><span class="line">squ cost: <span class="number">9.5367431640625e-07</span></span><br><span class="line">square = <span class="number">25</span></span><br><span class="line">func name = squ</span><br><span class="line"></span><br><span class="line"><span class="comment"># 2.含参数</span></span><br><span class="line"><span class="comment">#!/usr/bin/env python</span></span><br><span class="line"><span class="comment"># -*- coding : utf-8 -*-</span></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> functools</span><br><span class="line"><span class="keyword">import</span> datetime</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">process_time</span>(<span class="params">date</span>):</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">decorator</span>(<span class="params">func</span>):</span></span><br><span class="line"><span class="meta"> @functools.wraps(<span class="params">func</span>)</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span>(<span class="params">*args, **kw</span>):</span></span><br><span class="line"> start = time.time()</span><br><span class="line"> results = func(*args)</span><br><span class="line"> end = time.time()</span><br><span class="line"> costtime = end - start</span><br><span class="line"> <span class="built_in">print</span>(date,func.__name__, <span class="string">"cost:"</span>, costtime)</span><br><span class="line"> <span class="keyword">return</span> results</span><br><span class="line"> <span class="keyword">return</span> wrapper</span><br><span class="line"> <span class="keyword">return</span> decorator</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@process_time(<span class="params">datetime.datetime.now(<span class="params"></span>)</span>) </span><span class="comment">#相当于: squ = process_time(datetime.datetime.now())(squ)</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">squ</span>(<span class="params">x</span>):</span></span><br><span class="line"> <span class="keyword">return</span> x*x</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">square = squ(<span class="number">5</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'square ='</span>,square)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'func name ='</span>,squ.__name__)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 输出</span></span><br><span class="line"><span class="number">2020</span>-05-08 <span class="number">22</span>:<span class="number">58</span>:<span class="number">39.325757</span> squ cost: <span class="number">1.1920928955078125e-06</span></span><br><span class="line">square = <span class="number">25</span></span><br><span class="line">func name = squ</span><br></pre></td></tr></table></figure><h1 id="5-偏函数-Partial-function"><a href="#5-偏函数-Partial-function" class="headerlink" title="5. 偏函数 - Partial function"></a>5. 偏函数 - Partial function</h1><blockquote><p>Python的 functools 模块提供了很多功能, 偏函数就是其一。</p></blockquote><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 偏函数的主要作用即将含参的函数设置默认参数后生成新函数, 简化调用.</span></span><br><span class="line"><span class="comment"># int(value, base)</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">int</span>(<span class="string">'10'</span>, base = <span class="number">2</span>)</span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">int</span>(<span class="string">'10'</span>, base = <span class="number">16</span>)</span><br><span class="line"><span class="number">16</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过定义函数简化</span></span><br><span class="line"><span class="meta">>>> </span><span class="function"><span class="keyword">def</span> <span class="title">int2</span>(<span class="params">x, base=<span class="number">2</span></span>):</span></span><br><span class="line"><span class="meta">... </span> <span class="keyword">return</span> <span class="built_in">int</span>(x, base)</span><br><span class="line">...</span><br><span class="line"><span class="meta">>>> </span>int2(<span class="string">'10'</span>)</span><br><span class="line"><span class="number">2</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 偏函数</span></span><br><span class="line"><span class="meta">>>> </span><span class="keyword">import</span> functools</span><br><span class="line"><span class="meta">>>> </span>int2 = functools.partial(<span class="built_in">int</span>, base=<span class="number">2</span>)</span><br><span class="line"><span class="meta">>>> </span>int2(<span class="string">'10'</span>)</span><br><span class="line"><span class="number">2</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 偏函数可接收函数对象、*args和**kw.</span></span><br><span class="line"><span class="number">1.</span> **kw: base = <span class="number">2</span> 相当于 {<span class="string">'base'</span>: <span class="number">2</span>}</span><br><span class="line"><span class="number">2.</span> *args: <span class="string">'10'</span> 相当于传给了 <span class="built_in">int</span>(value, base) 函数的value.</span><br><span class="line"><span class="meta">>>> </span>int2 = functools.partial(<span class="built_in">int</span>, <span class="string">'10'</span>)</span><br><span class="line"><span class="meta">>>> </span>int2()</span><br><span class="line"><span class="number">10</span></span><br></pre></td></tr></table></figure><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><blockquote>
<p>函数式编程的一个特点是,允许把函数作为参数传入另一个函数,还允许返回一个函数。</p>
</blockquote>
<h1 id="1-高阶函数"><a href="#1-高阶函数" class="headerlink" title="1. 高阶函数"></a>1. 高阶函数</h1></summary>
<category term="Computer Science" scheme="https://aaronos0.github.io/categories/Computer-Science/"/>
<category term="Python Learning" scheme="https://aaronos0.github.io/tags/Python-Learning/"/>
</entry>
<entry>
<title>UoN - CS Guide</title>
<link href="https://aaronos0.github.io/2020/09/27/UoN-CS-Guide/"/>
<id>https://aaronos0.github.io/2020/09/27/UoN-CS-Guide/</id>
<published>2020-09-27T21:32:57.000Z</published>
<updated>2020-09-27T21:32:57.000Z</updated>
<content type="html"><![CDATA[<h1 id="1-Computer-Science-information-Guide"><a href="#1-Computer-Science-information-Guide" class="headerlink" title="1. Computer Science information Guide"></a>1. Computer Science information Guide</h1><span id="more"></span><body class="wiz-editor-body " data-wiz-document-type="common" style="opacity: 1;" ><div><span data-wiz-span="data-wiz-span" style="font-size: 1.5rem;">1. School of Computer Science</span></div><div><ul><li><a href="https://www.nottingham.ac.uk/computerscience/index.aspx"><font color="#0046f5">Home Page</font></a></li><li><a href="https://moodle.nottingham.ac.uk/"><font color="#0046f5">Teach System Moodle</font></a></li><li><a href="https://uniofnottm.sharepoint.com/sites/StudentModules"><font color="#0046f5">StudentModules SharePoint</font></a></li><li><a href="https://www.nottingham.ac.uk/pgstudy/course/taught/computer-science-or-computer-science-artificial-intelligence-msc" style=""><font color="#0f47ce">CS Msc Overview</font></a></li><li><a href="http://eprints.nottingham.ac.uk/view/subject_d/d=5FCS.html"><font color="#0f47ce">eDissertations</font></a></li><li><a href="http://www.cs.nott.ac.uk/~pszrq/PGT.html"><font color="#0f47ce">PGT-Q&A</font></a></li><li><a href="https://www.nottingham.ac.uk/it-services/software/software.aspx"><font color="#0f47ce">Normal Software</font></a></li><li><a href="https://www.nottingham.ac.uk/it-services/computers/virtual.aspx"><font color="#0f47ce">Virtual Desktop</font></a></li></ul></div><div><br></div><div><span data-wiz-span="data-wiz-span" style="font-size: 1.5rem;">2. Course(来自SharePoint)</span></div><div><span data-wiz-span="data-wiz-span" style="font-size:1.167rem;color:rgb(255, 0, 0)">A: Autumn ; </span><span style="color:rgb(111, 192, 55);font-size:1.167rem" data-wiz-span="data-wiz-span">S: Spring </span></div><div><span data-wiz-span="data-wiz-span" style="font-size: 1.25rem;">2.1. Without CS background</span></div><div class="wiz-table-container" style="position: relative; padding: 0px;" ><div class="wiz-table-body" ><table style="width: 1647px;"><tbody><tr style="height: 33px;"><td align="left" valign="middle" style="width: 111px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div>Code</div></td><td align="left" valign="middle" style="width: 140px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div>Module Name</div></td><td align="center" valign="middle" style="width: 69px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div>Credits</div></td><td align="left" valign="middle" style="width: 158px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div>Convenor</div></td><td align="left" valign="middle" style="width: 711px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div>Abstract </div></td><td align="left" valign="middle" style="width: 259px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div>Exam</div></td><td align="left" valign="middle" style="width: 198px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div>Advice</div></td></tr><tr style="height: 185px;"><td align="left" valign="middle" style="width: 111px;"><div><font color="#ff0000">A</font>COMP4008</div></td><td align="left" valign="middle" style="width: 140px;"><div>Programming</div></td><td align="center" valign="middle" style="width: 69px;"><div>20</div></td><td align="left" valign="middle" style="width: 158px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/thorsten.altenkirch"><font color="#0046f5">Thorsten Altenkirch </font></a></div><div><a href="https://www.nottingham.ac.uk/computerscience/people/i.triguerovelazquez"><font color="#0046f5">Isaac Triguero</font></a></div></td><td align="left" valign="middle" style="width: 711px;"><div>This module provides a comprehensive overview of the principles of programming, including such concepts as procedural logic, variables, flow control, input and output and the analysis and design of programs(<font color="#00b0f0">Python</font>). The emphasis in this course will be <font color="#00b0f0">game development</font> in a small group using Python.</div></td><td align="left" valign="middle" style="width: 259px;"><div><ul><li>2h Exam: 50%</li><li>4 programming exercises: 20%</li><li>Group project: 30%</li></ul></div></td><td align="left" valign="middle" style="width: 198px;"><div>1. Python基础课程</div><div>2. PyGame做项目</div></td></tr><tr><td align="left" valign="middle" style="width: 111px;"><div><span style="color: rgb(255, 0, 0);">A</span>COMP4035</div></td><td align="left" valign="middle" style="width: 140px;"><div>Systems and Networks</div></td><td align="center" valign="middle" style="width: 69px;"><div>20</div></td><td align="left" valign="middle" style="width: 158px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/gail.hopkins"><font color="#0046f5">Gail Hopkins</font></a></div></td><td align="left" valign="middle" style="width: 711px;"><div><font color="#00b0f0">Unix</font> is introduced in terms of the Unix file structure, Input and Output and the Command Line Interface that is used to manipulate these.</div><div>Computer communication is taught with respect to the Client-Server Architecture and applications that use this. <font color="#00b0f0">TCP/IP、IPv4\IPv6、LANs\WANs、Wifi、routers、VPN、Firewalls</font>.</div></td><td align="left" valign="middle" style="width: 259px;"><div><ul><li>2h Exam: 75%</li><li>2000 words coursework: 25%</li></ul></div></td><td align="left" valign="middle" style="width: 198px;"><div>1. Unix操作系统</div><div>2. 基本网络协议及C-S架构</div></td></tr><tr><td align="left" valign="middle" style="width: 111px;"><div><span style="color: rgb(255, 0, 0);">A</span>COMP4039</div></td><td align="left" valign="middle" style="width: 140px;"><div>Databases, Interfaces and Software Design Principles</div></td><td align="center" valign="middle" style="width: 69px;"><div>20</div></td><td align="left" valign="middle" style="width: 158px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/jeremie.clos"><font color="#0046f5">Jeremie Clos</font></a></div><div><a href="https://www.nottingham.ac.uk/computerscience/people/stuart.reeves"><font color="#0046f5">Stuart Reeves</font></a></div></td><td align="left" valign="middle" style="width: 711px;"><div>To give an understanding of the structure and workings of rational database systems and provide some insight into recent developments. To enable students to gain experience with design and implementation of database driven web interfaces. <font color="#00b0f0">Wed design && SQL</font>.</div></td><td align="left" valign="middle" style="width: 259px;"><ul><li>Exam: 40%</li><li>Coursework 1A: 10%</li><li>Coursework 1B: 10%</li><li>Coursework 2: 40%</li></ul></td><td align="left" valign="middle" style="width: 198px;"><div>1. SQL dev</div><div>2. Web dev</div></td></tr></tbody></table></div></div><div><span data-wiz-span="data-wiz-span" style="font-size: 1.167rem;"><br></span></div><div><span data-wiz-span="data-wiz-span" style="font-size: 1.25rem;">2.2. AI Module </span></div><div class="wiz-table-container" style="position: relative; padding: 0px;" ><div class="wiz-table-body" ><table style="width: 1651px;"><tbody><tr><td style="width: 108px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div><span style="">Code</span></div></td><td style="width: 137px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div><span style="">Module Name</span></div></td><td style="width: 72px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff" align="center"><div><span style="">Credits</span></div></td><td style="width: 146px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div><span style="">Convenor</span></div></td><td style="width: 680px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div><span style="">Abstract</span></div></td><td style="width: 284px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div><span style="">Exam</span></div></td><td style="width: 223px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div><span style="">Advice</span></div></td></tr><tr><td colspan="1" rowspan="1" style="width: 108px;"><div><span style="color: rgb(255, 0, 0);">A</span>COMP3008</div></td> <td colspan="1" rowspan="1" style="width: 137px;"><div>Knowledge Representation and Reasoning</div></td> <td colspan="1" rowspan="1" style="width: 72px;" align="center"><div>20</div></td> <td colspan="1" rowspan="1" style="width: 146px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/daniel.karapetyan"><font color="#0046f5">Daniel Karapetyan</font></a></div></td> <td colspan="1" rowspan="1" style="width: 680px;"><div>This course examines how knowledge can be represented symbolically and how it can be manipulated in an automated way by reasoning programs. Some of the topics you’ll cover include: first order logic; resolution; description logic; default reasoning; rule-based systems; belief networks; planning and reasoning about actions. </div></td> <td colspan="1" rowspan="1" style="width: 284px;"><div><ul><li>2h Exam: 75%</li><li>Coursework: 25%</li></ul></div></td> <td colspan="1" rowspan="1" style="width: 223px;"><div><a href="https://www.jiqizhixin.com/graph/technologies/1c2aec65-cc26-48f5-8c2d-ad3327512b28"><font color="#0046f5">知识表征</font></a><br></div></td></tr><tr><td colspan="1" rowspan="1" style="width: 108px;"><div><span style="color: rgb(255, 0, 0);">A</span>COMP3009</div></td> <td colspan="1" rowspan="1" style="width: 137px;"><div>Machine Learning</div></td> <td colspan="1" rowspan="1" style="width: 72px;" align="center"><div>20</div></td> <td colspan="1" rowspan="1" style="width: 146px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/michel.valstar"><font color="#0046f5">Michel Valstar</font></a></div></td> <td colspan="1" rowspan="1" style="width: 680px;"><div>Providing you with an introduction to machine learning, pattern recognition, and data mining techniques.You'll cover a range of topics including: machine learning foundations; pattern recognition foundations; artificial neural networks; deep learning; applications of machine learning; data mining techniques and evaluating hypotheses.</div></td> <td colspan="1" rowspan="1" style="width: 284px;"><div style=""><ul><li>2h Exam: 70%</li><li>Group Coursework: 30%</li></ul></div></td> <td colspan="1" rowspan="1" style="width: 223px;"><div>AI基础课</div></td></tr><tr><td colspan="1" rowspan="1" style="width: 108px;"><div><span style="color: rgb(255, 0, 0);">A</span>COMP4034</div></td> <td colspan="1" rowspan="1" style="width: 137px;"><div>Autonomous Robotic Systems</div></td> <td colspan="1" rowspan="1" style="width: 72px;" align="center"><div>20</div></td> <td colspan="1" rowspan="1" style="width: 146px;" class=""><div><a href="https://www.nottingham.ac.uk/computerscience/people/ayse.kucukyilmaz" style=""><font color="#004ff3">Ayse Kucukyilmaz</font></a></div></td> <td colspan="1" rowspan="1" style="width: 680px;"><div>A significant part of the module is laboratory-based, utilising physical robotic hardware to reinforce the theoretical principles covered. Cover a range of topics including basic behavioural control architectures, multi-source data aggregation, programming of multiple behaviours, capabilities and limitations of sensors and actuators, and filtering techniques.</div></td> <td colspan="1" rowspan="1" style="width: 284px;"><div><ul><li>2h Exam: 50%</li><li>Lab assignment and report: 50%</li></ul></div></td> <td colspan="1" rowspan="1" style="width: 223px;"><div>1. HCI智能机器人方向</div><div>2. 老师首次在UoN教学</div><div>3. 疫情情况主要使用模拟软件</div><div>4. Python和数学基础</div></td></tr><tr><td style="width: 108px;"><div><span style="color: rgb(255, 0, 0);">A</span><span style="">COMP4041</span></div></td><td style="width: 137px;"><div>Linear and Discrete Optimization</div></td><td style="width: 72px;" align="center"><div>20</div></td><td style="width: 146px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/dario.landasilva" style="text-decoration-skip-ink: none;"><font color="#0046f5">Dario Landa Silva</font></a></div></td><td style="width: 680px;"><div>To develop the skills for modelling a range of optimization problems in business and industry using mathematical models.To develop skills for identifying, analyzing, modelling and solving real-world linear and discrete optimization.The module covers topics such as linear programming, integer programming, combinatorial optimization, modelling and optimization software, and multi-objective optimization among others. </div></td><td style="width: 284px;"><div><ul><li>1.5h Exam: 50%</li><li>Solve a real-world scenario: 25%</li><li>Inclass Test: 25%</li></ul></div></td><td style="width: 223px;" class=""><div>1. 线性/离散优化,运筹学</div><div>2. 需要一定数学基础(线性代数)</div><div>3. 少量编程,偏重数学模型</div></td></tr><tr><td colspan="1" rowspan="1" style="width: 108px;"><div><span style="color: rgb(255, 0, 0);">A</span>COMP3074</div></td> <td colspan="1" rowspan="1" style="width: 137px;"><div>Human-AI Interaction</div></td> <td colspan="1" rowspan="1" style="width: 72px;" align="center"><div>20</div></td> <td colspan="1" rowspan="1" style="width: 146px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/joel.fischer"><font color="#0046f5">Joel Fischer</font></a></div></td> <td colspan="1" rowspan="1" style="width: 680px;"><div>This module is an introduction to the design of human-AI interaction to ensure the AI-driven systems we built are beneficial and useful to people. The module will cover practical design topics including methods and techniques such as <font color="#00b0f0">natural language processing</font> and <font color="#00b0f0">human-robot interaction</font>. </div></td> <td colspan="1" rowspan="1" style="width: 284px;"><div><ul><li>1.5h Exam: 60%</li><li>Coursework: 40%</li></ul></div></td> <td colspan="1" rowspan="1" style="width: 223px;"><div>HCI方向,涉及NLP</div></td></tr><tr><td style="width: 108px;"><div><font color="#6fc037">S</font>COMP3004</div></td><td style="width: 137px;"><div>Designing Intelligent Agents</div></td><td style="width: 72px;" align="center"><div>20</div></td><td style="width: 146px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/colin.johnson"><font color="#0046f5">Colin Johnson</font></a></div></td><td style="width: 680px;"><div>You'll be given a basic introduction to the analysis and design of intelligent agents, software systems which perceive their environment and act in that environment in pursuit of their goals. Cover topics including task environments, reactive, deliberative and hybrid architectures for individual agents, and architectures and coordination mechanisms for multi-agent systems.</div></td><td style="width: 284px;"><ul><li>2500 word report and code : 100%</li></ul></td><td style="width: 223px;"><div><a href="https://www.jiqizhixin.com/graph/technologies/3500fe33-55d4-450f-8076-25c2ef744e63"><font color="#0046f5">智能代理</font></a>,如: Siri </div></td></tr><tr><td style="width: 108px;"><div><span style="color: rgb(111, 192, 55);">S</span>COMP4002</div></td><td style="width: 137px;"><div>Games</div></td><td style="width: 72px;" align="center"><div>20</div></td><td style="width: 146px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/martin.flintham"><font color="#0046f5">Martin Flintham</font></a></div></td><td style="width: 680px;"><div>This module covers the history, development and state-of-the art in computer games and technological entertainment. Students will use appropriate software environments to individually develop a number of games to explore relevant theoretical design and practical implementation concepts.</div></td><td style="width: 284px;"><div><ul><li>1h Exam: 30%</li><li>2000 report: 30%</li><li>programming: 40%</li></ul></div></td><td style="width: 223px;"><div>HCI方向</div></td></tr><tr><td style="width: 108px;"><div><span style="color: rgb(111, 192, 55);">S</span>COMP4030</div></td><td style="width: 137px;"><div>Data Modelling and Analysis</div></td><td style="width: 72px;" align="center"><div>20</div></td><td style="width: 146px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/mercedes.torrestorres"><font color="#0046f5">Mercedes Torres Torres</font></a></div></td><td style="width: 680px;"><div>Topics covered include: basic statistics; types of data; data visualisation techniques; data modelling; data pre-processing methods including data imputation; forecasting methods; clustering and classification methods (decision trees, naīve bayes classifiers, k-nearest neighbours); data simulation and model interpretation techniques to aid decision support.</div></td><td style="width: 284px;"><div><ul><li>Lab submission: 25%</li><li>4000 words and code: 75%</li></ul></div></td><td style="width: 223px;"><div>DS方向,数据建模</div></td></tr><tr><td style="width: 108px;"><div><span style="color: rgb(111, 192, 55);">S</span>COMP4033</div></td><td style="width: 137px;"><div>Fuzzy Logic and Fuzzy Systems</div></td><td style="width: 72px;" align="center"><div>20</div></td><td style="width: 146px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/chao.chen"><font color="#0046f5">Chao Chen</font></a></div></td><td style="width: 680px;"><div>This module aims to provide a thorough understanding of fuzzy sets and systems from a theoretical and practical perspective. Topics commonly include: type-1 fuzzy sets, type-1 fuzzy logic systems, type-1 fuzzy set based applications, type-2 fuzzy sets, type-2 fuzzy logic systems, type-2 fuzzy set based applications. R language.</div></td><td style="width: 284px;"><div><ul><li>2h Exam: 75%</li><li>2000 words and code: 25%</li></ul></div></td><td style="width: 223px;"><div>模糊系统(感觉真模糊,哈哈哈)</div></td></tr><tr><td style="width: 108px;"><div><span style="color: rgb(111, 192, 55);">S</span>COMP4038</div></td><td style="width: 137px;"><div>Simulation and Optimization for Decision Support</div></td><td style="width: 72px;" align="center"><div>20</div></td><td style="width: 146px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/dario.landasilva" style="text-decoration-skip-ink: none;"><font color="#0046f5">Dario Landa Silva</font></a></div><div><a href="https://www.nottingham.ac.uk/computerscience/people/peer-olaf.siebers"><font color="#0046f5">Peer-Olaf Siebers</font></a></div></td><td style="width: 680px;"><div>This module offers insight into the applications of selected methods of decision support. The foundations for applying these methods are derived from Operations Research Simulation, Social Simulation, Data Science, Automated Scheduling, and Decision Analysis.</div></td><td style="width: 284px;"><div><ul><li>1h Exam: 25%</li><li>2500 words: 65%</li><li>Lab Exe: 10%</li></ul></div></td><td style="width: 223px;"><div>决策支持系统DDS,数据库管理系统相关</div></td></tr><tr><td style="width: 108px;"><div><span style="color: rgb(111, 192, 55);">S</span>COMP4103</div></td><td style="width: 137px;"><div>Big Data</div></td><td style="width: 72px;" align="center"><div>10</div></td><td style="width: 146px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/i.triguerovelazquez" style="text-decoration-skip-ink: none;"><font color="#0046f5">Isaac Triguero</font></a></div></td><td style="width: 680px;"><div>This includes the <font color="#00b0f0">MapReduce programming model</font>, as well as an overview of recent technologies (<font color="#00b0f0">Hadoop ecosystem</font>, and Apache Spark). Then, you will learn how to interact with the latest APIs of Apache Spark (RDDs, DataFrames and Datasets) to create distributed programs capable of dealing with big datasets (using Python and/or Scala). Finally, we will dive into the analytics part of the course, including data pre-processing approaches, distributed machine learning algorithms and data stream algorithms. To do so, you will use the Machine learning library of Apache Spark (<font color="#00b0f0">MLlib</font>) to understand how some machine learning algorithms (e.g. Decision Trees, Random Forests, k-means) can be deployed at a scale. </div></td><td style="width: 284px;"><div><ul><li>2h Exam: 50%</li><li>Group project: 50%</li></ul></div></td><td style="width: 223px;"><div>Hadoop生态、MapReduce</div></td></tr></tbody></table></div></div><div><span data-wiz-span="data-wiz-span" style="font-size: 1.167rem;"><br></span></div><div><span data-wiz-span="data-wiz-span" style="font-size: 1.25rem;">2.3. Other Module </span></div><div class="wiz-table-container" style="position: relative; padding: 0px;" ><div class="wiz-table-body" ><table style="width: 1731px;"><tbody><tr><td align="left" valign="middle" style="width: 107px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div><span style="">Code</span></div></td><td align="left" valign="middle" style="width: 135px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div><span style="">Module Name</span></div></td><td align="center" valign="middle" style="width: 70px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff" class=""><div><span style="">Credits</span></div></td><td align="left" valign="middle" style="width: 157px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div><span style="">Convenor</span></div></td><td align="left" valign="middle" style="width: 713px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div><span style="">Abstract</span></div></td><td align="left" valign="middle" style="width: 306px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff" class=""><div><span style="">Exam</span></div></td><td align="left" valign="middle" style="width: 242px; background-color: rgb(209, 230, 255);" data-background-color="#d1e6ff"><div><span style="">Advice</span></div></td></tr><tr><td align="left" valign="middle" style="width: 107px;"><div><span style="color: rgb(255, 0, 0);">A</span>COMP4019</div></td><td align="left" valign="middle" style="width: 135px;"><div>Advanced Algorithms and Data Structures</div></td><td align="center" valign="middle" style="width: 70px;" class=""><div>10</div></td><td align="left" valign="middle" style="width: 157px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/venanzio.capretta"><font color="#0046f5">Venanzio Capretta</font></a></div></td><td align="left" valign="middle" style="width: 713px;"><div>The topics covered may include: Binary Search Trees (Red-Black Trees), Dynamic Programming, Graph Algorithms (e.g. shortest path, maximum flows), Amortized Analysis, Priority Queues (Binary, Leftist and Fibonacci Heaps), String Algorithms (string matching, longest common subsequence). Among the special topics relevant to contemporary application, we may study: Public Key Cryptography (the RSA cryptosystem), the Page-rank algorithm (from Google search), Neural Networks and other data structures and algorithms in AI. <span style=""><font color="#00b0f0">Haskell</font></span></div></td><td align="left" valign="middle" style="width: 306px;"><ul><li>2h Exam: 100%</li></ul></td><td align="left" valign="middle" style="width: 242px;"><div>计算机高级基础:算法与数据结构</div></td></tr><tr><td align="left" valign="middle" style="width: 107px;"><div><span style="color: rgb(255, 0, 0);">A</span>COMP4040</div></td><td align="left" valign="middle" style="width: 135px;"><div>Project in Advanced Algorithms and Data Structures</div></td><td align="center" valign="middle" style="width: 70px;" class=""><div>10</div></td><td align="left" valign="middle" style="width: 157px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/venanzio.capretta" style="text-decoration-skip-ink: none;"><font color="#0046f5">Venanzio Capretta</font></a></div></td><td align="left" valign="middle" style="width: 713px;"><div>This project builds on the theory presented in COMP4019, and involves a self-guided study of selected advanced data structures and algorithms. The outcome of the project is an implementation of the algorithms and data structures with appropriate documentation. <span style=""><font color="#00b0f0">Haskell</font></span></div></td><td align="left" valign="middle" style="width: 306px;"><ul><li>Implementation Assignments: 100%</li></ul></td><td align="left" valign="middle" style="width: 242px;"><div><a href="http://www.duplavis.com/venanzio/advanced_algorithms/index.html"><font color="#0088f3">Haskell实现算法与数据结构设计</font></a></div></td></tr><tr><td align="left" valign="middle" style="width: 107px;"><div><span style="color: rgb(255, 0, 0);">A</span>COMP4032</div></td><td align="left" valign="middle" style="width: 135px;"><div>Advanced Computer Networks</div></td><td align="center" valign="middle" style="width: 70px;" class=""><div>20</div></td><td align="left" valign="middle" style="width: 157px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/milena.radenkovic"><font color="#0046f5">Milena Radenkovic</font></a></div></td><td align="left" valign="middle" style="width: 713px;"><div>This module will provide students with an advanced knowledge of computer communications networks, using examples from all-IP core telecommunications networks to illustrate aspects of transmission coding, error control, media access, internet protocols, routing, presentation coding, services and security. The module will provide an advanced knowledge of various routing and query protocols in: Ad Hoc Networks; Mobile Ad Hoc Networks (MANETs); Vehicular Ad Hoc Networks (VANETs); Disconnection/Disruption/Delay Tolerant Networks (DTNs);</div></td><td align="left" valign="middle" style="width: 306px;" class=""><div><ul><li>2h Exam: 50%</li><li>Report and code(10-20pages): 50% </li></ul></div></td><td align="left" valign="middle" style="width: 242px;"><div>1. 高级计算机网络,深度不浅</div><div>2. 老师很Professional</div></td></tr><tr><td align="left" valign="middle" style="width: 107px;"><div><span style="color: rgb(255, 0, 0);">A</span>COMP4075</div></td><td align="left" valign="middle" style="width: 135px;"><div>Real-world Functional Programming</div></td><td align="center" valign="middle" style="width: 70px;" class=""><div>10</div></td><td align="left" valign="middle" style="width: 157px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/henrik.nilsson"><font color="#0046f5">Henrik Nilsson</font></a></div></td><td align="left" valign="middle" style="width: 713px;"><div>This module introduces tools, techniques, and theory needed for programming real-world applications functionally, with a particular emphasis on the inherent benefits of functional programming and strong typing for reuse, maintenance, concurrency, distribution, and high availability. The medium of instruction is mainly <font color="#00b0f0">Haskell</font>.</div></td><td align="left" valign="middle" style="width: 306px;"><div><ul><li>1.5h Exam: 50%</li><li>Programming Tasks: 50%</li></ul></div></td><td align="left" valign="middle" style="width: 242px;"><div><span style="">Haskell, 你懂的</span></div></td></tr><tr><td align="left" valign="middle" style="width: 107px;"><div><span style="color: rgb(255, 0, 0);">A</span>COMP4095</div></td><td align="left" valign="middle" style="width: 135px;"><div>Real-world Functional Programming Project</div></td><td align="center" valign="middle" style="width: 70px;" class=""><div>10</div></td><td align="left" valign="middle" style="width: 157px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/henrik.nilsson" style="text-decoration-skip-ink: none;"><font color="#0046f5">Henrik Nilsson</font></a></div></td><td align="left" valign="middle" style="width: 713px;"><div>What is important is that the ideas underpinning the project is related to real-world functional programming. As a first step, each student need to define their project through a project pitch. The pitch must outline the project, explain why the project is in scope of G54RFP/COPM4075, state what languages or frameworks are going to be used, justify that the size of the project is appropriate for a 10 credit module (100 h of work), and state what the outcome of a successful project is.</div></td><td align="left" valign="middle" style="width: 306px;"><ul><li>5000 word report and code: 100%</li></ul></td><td align="left" valign="middle" style="width: 242px;"><div><span style="">COMP4075的</span>项目</div></td></tr><tr><td align="left" valign="middle" style="width: 107px;"><div><span style="color: rgb(111, 192, 55);">S</span>COMP3011</div></td><td align="left" valign="middle" style="width: 135px;"><div>Computer Graphics</div></td><td align="center" valign="middle" style="width: 70px;" class=""><div>20</div></td><td align="left" valign="middle" style="width: 157px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/jeremie.clos" style="text-decoration-skip-ink: none;"><font color="#0046f5">Jeremie Clos</font></a></div></td><td align="left" valign="middle" style="width: 713px;"><div>You will learn the principles of 3D computer graphics, focusing on modelling and viewing objects/scene in a three-dimensional (3D) world on the computer, rendering the objects/scene to give it realism, and projecting objects/scene onto 2D display in analogy to your taking a photo of a 3D world using a camera. OpenGL programming skills</div></td><td align="left" valign="middle" style="width: 306px;"><div><ul><li>Coursework 1/2: 15%</li><li>3D graphics project: 70%</li></ul></div></td><td align="left" valign="middle" style="width: 242px;"><div><span style="">OpenGL感兴趣的</span></div></td></tr><tr><td align="left" valign="middle" style="width: 107px;"><div><span style="color: rgb(111, 192, 55);">S</span>COMP4036</div></td><td align="left" valign="middle" style="width: 135px;"><div>Mixed Reality</div></td><td align="center" valign="middle" style="width: 70px;" class=""><div>20</div></td><td align="left" valign="middle" style="width: 157px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/steve.benford"><font color="#0046f5">Steven Benford </font></a></div><div><a href="https://www.nottingham.ac.uk/computerscience/people/paul.tennent"><font color="#0046f5">Paul Tennent</font></a></div></td><td align="left" valign="middle" style="width: 713px;"><div>You will gain knowledge and hands-on experience of design and development with key technologies along this continuum, including working with both ubiquitous computing based sensor systems and locative media. You will learn about the Human-Computer Interaction challenges that need to be considered when creating mixed reality applications along with strategies for addressing them, so as to create compelling and reliable user experiences.</div></td><td align="left" valign="middle" style="width: 306px;"><div><ul><li>report1 2000-word: 50%</li><li><span style="">report2 2000-word: 50%</span></li></ul></div></td><td align="left" valign="middle" style="width: 242px;"><div>HCI方向</div></td></tr><tr><td align="left" valign="middle" style="width: 107px;"><div><span style="color: rgb(111, 192, 55);">S</span>COMP3021</div></td><td align="left" valign="middle" style="width: 135px;"><div>Fundamentals of Information Visualisation</div></td><td align="center" valign="middle" style="width: 70px;" class=""><div>10</div></td><td align="left" valign="middle" style="width: 157px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/ke.zhou"><font color="#0046f5">Ke Zhou</font></a></div></td><td align="left" valign="middle" style="width: 713px;"><div>You will learn about the differences between scientific and creative approaches to constructing visualisations, and consider some important challenges such as the representation of ambiguous or time-based data. You will also learn about psychological theories that help explain how humans process information, and consider their relevance to the design of effective visualisations. </div></td><td align="left" valign="middle" style="width: 306px;"><div><ul><li>2h Exam: 75%</li><li>3000 words and code: 25%</li></ul></div></td><td align="left" valign="middle" style="width: 242px;"><div>数据可视化,HCI方向</div></td></tr><tr style="height: 33px;"><td align="left" valign="middle" style="width: 107px;"><div><span style="color: rgb(111, 192, 55);">S</span>COMP3022</div></td><td align="left" valign="middle" style="width: 135px;"><div>Information Visualisation Project</div></td><td align="center" valign="middle" style="width: 70px;" class=""><div>10</div></td><td align="left" valign="middle" style="width: 157px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/ke.zhou" style="text-decoration-skip-ink: none;"><font color="#0046f5">Ke Zhou</font></a></div></td><td align="left" valign="middle" style="width: 713px;"><div>You will gain experience in web-based technologies that enable the implementation of multi-layered and interactive information visualisations, supported through lab work that introduces specific features of these technologies.This module will require some challenging programming work, and assumes some basic knowledge of HTML, CSS and Javascript. </div></td><td align="left" valign="middle" style="width: 306px;"><div><ul><li>Presentation : 20%</li><li>Structured portfolio: 80%</li></ul></div></td><td align="left" valign="middle" style="width: 242px;"><div>需要一些设计和前端的知识</div></td></tr><tr><td style="width: 107px;"><div><span style="color: rgb(111, 192, 55);">S</span>COMP4074</div></td><td style="width: 135px;"><div>Programs, Proofs and Types</div></td><td style="width: 70px;" class="" align="center"><div>20</div></td><td style="width: 157px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/thorsten.altenkirch"><font color="#0046f5">Thorsten Altenkirch</font></a></div></td><td style="width: 713px;"><div>This module focuses on some of the fundamental mathematical concepts that underlie modern programming and programming languages emphasizing the role of types. We will use a dependently typed programming language / interactive proof system (e.g. Agda) to implement some concepts on a computer. Example topics include: basic lambda calculus; operational semantics; domain theory; types, propositions as types and formal verification.</div></td><td style="width: 306px;"><div><ul><li>2h Exam: 75%</li><li>courseworks: 25%</li></ul></div></td><td style="width: 242px;"><div>1. Agda,类Haskell</div><div><span style=""><font color="#0046f5">2. <a href="https://songkeys.github.io/posts/2020-course-sharing-ppt/">Programs, Proofs and Types</a></font></span></div></td></tr><tr><td style="width: 107px;"><div><span style="color: rgb(111, 192, 55);">S</span>COMP4101</div></td><td style="width: 135px;"><div>Malware Analysis</div></td><td style="width: 70px;" class="" align="center"><div>10</div></td><td style="width: 157px;"><div><a href="https://www.nottingham.ac.uk/computerscience/people/steven.bagley"><font color="#0046f5">Steven Bagley</font></a></div></td><td style="width: 713px;"><div>We will look at how to setup a safe environment in which to analyse Malware, as well as exploring both static and dynamic malware analysis. Although malware takes many forms, the focus of this course will primarily be on executable binaries, covering both object file formats, but also the use of tools such as debuggers, virtual machines, and disassemblers to explore them. Obfuscation and packing schemes will be discussed, along with various issues related to Windows internals.</div></td><td style="width: 306px;"><div><ul><li>1h Exam: 50%</li><li>Courseworks: 50%</li></ul></div></td><td style="width: 242px;" class=""><div>恶意软件分析</div></td></tr></tbody></table></div></div><div><br></div><div><br></div><div><span data-wiz-span="data-wiz-span" style="font-size: 1.5rem;">3. Course-Choosing Personal Advice</span></div><div>(1). 课程方向相近,相互联系或在大方向上互补</div><div><span style="">(2). 老师职称背景、近期论文方向、是否为某Lab Member,后期选导师的时候比较重要</span></div><div><span style=""><span style="">(3). 考试及最终评估形式</span><br></span></div><div><br></div></body></html><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><h1 id="1-Computer-Science-information-Guide"><a href="#1-Computer-Science-information-Guide" class="headerlink" title="1. Computer Science information Guide"></a>1. Computer Science information Guide</h1></summary>
<category term="UoN" scheme="https://aaronos0.github.io/categories/UoN/"/>
<category term="School of Computer Science" scheme="https://aaronos0.github.io/tags/School-of-Computer-Science/"/>
</entry>
<entry>
<title>Python - Theme 3 Advanced Character</title>
<link href="https://aaronos0.github.io/2020/09/27/Python-Theme-3-Advanced-Character/"/>
<id>https://aaronos0.github.io/2020/09/27/Python-Theme-3-Advanced-Character/</id>
<published>2020-09-27T15:27:18.000Z</published>
<updated>2020-09-27T15:27:18.000Z</updated>
<content type="html"><![CDATA[<h1 id="1-切片"><a href="#1-切片" class="headerlink" title="1. 切片"></a>1. 切片</h1><blockquote><p>Python提供了切片(Slice)操作符,常用来取一个list或者tuple的部分元素。有了切片,就可以用一行代码代替多行循环了。</p></blockquote><span id="more"></span><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span>L = <span class="built_in">list</span>(<span class="built_in">range</span>(<span class="number">10</span>))</span><br><span class="line"><span class="meta">>>> </span>L[<span class="number">0</span>:<span class="number">3</span>] <span class="comment">##按照元素的索引切片</span></span><br><span class="line">[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>]</span><br><span class="line"><span class="meta">>>> </span>L[:<span class="number">3</span>] <span class="comment">##从第0个元素开始,第3个结束</span></span><br><span class="line">[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>]</span><br><span class="line"><span class="meta">>>> </span>L[<span class="number">1</span>:<span class="number">3</span>] <span class="comment">##从第1个元素开始</span></span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>]</span><br><span class="line"><span class="meta">>>> </span>L[:-<span class="number">2</span>] <span class="comment">##倒数第2个元素结束</span></span><br><span class="line">[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>]</span><br><span class="line"><span class="meta">>>> </span>L[:<span class="number">5</span>] <span class="comment">##取前5个</span></span><br><span class="line">[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]</span><br><span class="line"><span class="meta">>>> </span>L[-<span class="number">5</span>:] <span class="comment">##取后5个</span></span><br><span class="line">[<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]</span><br><span class="line"><span class="meta">>>> </span>L[:<span class="number">7</span>:<span class="number">2</span>] <span class="comment">##第0~7元素,每隔2个</span></span><br><span class="line">[<span class="number">0</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>]</span><br><span class="line"><span class="meta">>>> </span>L[::<span class="number">2</span>] <span class="comment">##全部元素每隔2个</span></span><br><span class="line">[<span class="number">0</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">8</span>]</span><br><span class="line"><span class="meta">>>> </span>L[:] <span class="comment">##相当于复制了一遍list</span></span><br><span class="line">[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]</span><br><span class="line"><span class="meta">>>> </span>(<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>)[:<span class="number">3</span>] <span class="comment">##切片tuple</span></span><br><span class="line">(<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>)</span><br><span class="line"><span class="meta">>>> </span><span class="string">'ABCDEFG'</span>[::<span class="number">2</span>] <span class="comment">##字符串也可以看做一种list</span></span><br><span class="line"><span class="string">'ACEG'</span></span><br></pre></td></tr></table></figure><h1 id="2-迭代"><a href="#2-迭代" class="headerlink" title="2. 迭代"></a>2. 迭代</h1><p>迭代(Iteration),我们一般用for循环来遍历一个list或者tuple,这种遍历我们称为迭代。只要是可迭代对象即可迭代。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">##如何判断是否为可迭代对象</span></span><br><span class="line"><span class="meta">>>> </span><span class="keyword">from</span> collections <span class="keyword">import</span> Iterable</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>(<span class="string">'abc'</span>,Iterable) <span class="comment">##str是可迭代对象</span></span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>([<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>],Iterable) <span class="comment">##list是可迭代</span></span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>({<span class="string">'a'</span>:<span class="number">1</span>,<span class="string">'b'</span>:<span class="number">2</span>,<span class="string">'c'</span>:<span class="number">3</span>},Iterable) <span class="comment">##dict是可迭代</span></span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>(<span class="number">123</span>,Iterable) <span class="comment">##整数不可迭代</span></span><br><span class="line"><span class="literal">False</span></span><br></pre></td></tr></table></figure><h2 id="2-1-迭代list"><a href="#2-1-迭代list" class="headerlink" title="2.1. 迭代list"></a>2.1. 迭代list</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span>L = [<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>,<span class="string">'d'</span>,<span class="string">'e'</span>,<span class="string">'f'</span>]</span><br><span class="line"><span class="meta">>>> </span><span class="keyword">for</span> i <span class="keyword">in</span> L:</span><br><span class="line"><span class="meta">>>> </span> <span class="built_in">print</span>(i)</span><br><span class="line">a</span><br><span class="line">b</span><br><span class="line">c</span><br><span class="line">d</span><br><span class="line">e</span><br><span class="line">f</span><br><span class="line"></span><br><span class="line"><span class="comment">##实现带下标输出</span></span><br><span class="line"><span class="meta">>>> </span><span class="keyword">for</span> i,value <span class="keyword">in</span> <span class="built_in">enumerate</span>(L):</span><br><span class="line"><span class="meta">>>> </span> <span class="built_in">print</span>(i,value)</span><br><span class="line"><span class="number">0</span> a</span><br><span class="line"><span class="number">1</span> b</span><br><span class="line"><span class="number">2</span> c</span><br><span class="line"><span class="number">3</span> d</span><br><span class="line"><span class="number">4</span> e</span><br><span class="line"><span class="number">5</span> f</span><br></pre></td></tr></table></figure><h2 id="2-2-迭代dict"><a href="#2-2-迭代dict" class="headerlink" title="2.2. 迭代dict"></a>2.2. 迭代dict</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">d = {<span class="string">'a'</span>: <span class="number">1</span>, <span class="string">'b'</span>: <span class="number">2</span>, <span class="string">'c'</span>: <span class="number">3</span>}</span><br><span class="line"><span class="comment">##遍历key</span></span><br><span class="line"><span class="meta">>>> </span><span class="keyword">for</span> key <span class="keyword">in</span> d:</span><br><span class="line"><span class="meta">>>> </span> <span class="built_in">print</span>(key)</span><br><span class="line">a</span><br><span class="line">b</span><br><span class="line">c</span><br><span class="line"></span><br><span class="line"><span class="comment">##遍历value</span></span><br><span class="line"><span class="meta">>>> </span><span class="keyword">for</span> value <span class="keyword">in</span> d.values():</span><br><span class="line"><span class="meta">>>> </span> <span class="built_in">print</span>(value)</span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="comment">##同时迭代k,v</span></span><br><span class="line">>>><span class="keyword">for</span> k,v <span class="keyword">in</span> d.items():</span><br><span class="line"><span class="meta">>>> </span> <span class="built_in">print</span>(k,v)</span><br><span class="line">a <span class="number">1</span></span><br><span class="line">b <span class="number">2</span></span><br><span class="line">c <span class="number">3</span></span><br></pre></td></tr></table></figure><h2 id="2-3-迭代str"><a href="#2-3-迭代str" class="headerlink" title="2.3. 迭代str"></a>2.3. 迭代str</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">>>><span class="keyword">for</span> i <span class="keyword">in</span> <span class="string">'abcdef'</span>:</span><br><span class="line"><span class="meta">>>> </span> <span class="built_in">print</span>(i)</span><br><span class="line">a</span><br><span class="line">b</span><br><span class="line">c</span><br><span class="line">d</span><br><span class="line">e</span><br><span class="line">f</span><br></pre></td></tr></table></figure><h1 id="3-列表生成式"><a href="#3-列表生成式" class="headerlink" title="3. 列表生成式"></a>3. 列表生成式</h1><p>列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># x*x:要生成的元素写在前面; 后面直接跟for循环.</span></span><br><span class="line"><span class="meta">>>> </span>[x*x <span class="keyword">for</span> x <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>,<span class="number">11</span>)]</span><br><span class="line">[<span class="number">1</span>, <span class="number">4</span>, <span class="number">9</span>, <span class="number">16</span>, <span class="number">25</span>, <span class="number">36</span>, <span class="number">49</span>, <span class="number">64</span>, <span class="number">81</span>, <span class="number">100</span>]</span><br></pre></td></tr></table></figure><h2 id="3-1-For循环后面跟if判断"><a href="#3-1-For循环后面跟if判断" class="headerlink" title="3.1. For循环后面跟if判断"></a>3.1. For循环后面跟if判断</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># if在for循环之后不能带else,因为在这里代表筛选条件</span></span><br><span class="line"><span class="meta">>>> </span>[x*x <span class="keyword">for</span> x <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>,<span class="number">11</span>) <span class="keyword">if</span> x % <span class="number">2</span> == <span class="number">0</span>]</span><br><span class="line">[<span class="number">4</span>, <span class="number">16</span>, <span class="number">36</span>, <span class="number">64</span>, <span class="number">100</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># if在for循环之前必须带else,因为是表达式,必须根据x计算出一个结果</span></span><br><span class="line"><span class="meta">>>> </span>[x <span class="keyword">if</span> x % <span class="number">2</span> == <span class="number">0</span> <span class="keyword">else</span> -x <span class="keyword">for</span> x <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>,<span class="number">11</span>)]</span><br><span class="line">[-<span class="number">1</span>, <span class="number">2</span>, -<span class="number">3</span>, <span class="number">4</span>, -<span class="number">5</span>, <span class="number">6</span>, -<span class="number">7</span>, <span class="number">8</span>, -<span class="number">9</span>, <span class="number">10</span>]</span><br></pre></td></tr></table></figure><h2 id="3-2-两层for循环-很少用到三层及以上的循环"><a href="#3-2-两层for循环-很少用到三层及以上的循环" class="headerlink" title="3.2. 两层for循环; 很少用到三层及以上的循环"></a>3.2. 两层for循环; 很少用到三层及以上的循环</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 生成全排列</span></span><br><span class="line"><span class="meta">>>> </span>[x + y <span class="keyword">for</span> x <span class="keyword">in</span> <span class="string">"abc"</span> <span class="keyword">for</span> y <span class="keyword">in</span> <span class="string">"123"</span>]</span><br><span class="line">[<span class="string">'a1'</span>, <span class="string">'a2'</span>, <span class="string">'a3'</span>, <span class="string">'b1'</span>, <span class="string">'b2'</span>, <span class="string">'b3'</span>, <span class="string">'c1'</span>, <span class="string">'c2'</span>, <span class="string">'c3'</span>]</span><br></pre></td></tr></table></figure><h2 id="3-3-For循环可以同时使用两个变量"><a href="#3-3-For循环可以同时使用两个变量" class="headerlink" title="3.3. For循环可以同时使用两个变量"></a>3.3. For循环可以同时使用两个变量</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span>d = {<span class="string">'x'</span>: <span class="string">'1'</span>, <span class="string">'y'</span>: <span class="string">'2'</span>, <span class="string">'z'</span>: <span class="string">'3'</span>}</span><br><span class="line"><span class="meta">>>> </span>[k + <span class="string">'='</span> + v <span class="keyword">for</span> k, v <span class="keyword">in</span> d.items()]</span><br><span class="line">[<span class="string">'x=1'</span>, <span class="string">'y=2'</span>, <span class="string">'z=3'</span>]</span><br></pre></td></tr></table></figure><h1 id="4-生成器"><a href="#4-生成器" class="headerlink" title="4. 生成器"></a>4. 生成器</h1><p>受到内存的限制,创建列表的容量是有限的,而且如果仅用到列表前几个元素,那么就白白浪费了空间。生成器(generator),是储存了列表元素的算法,形成了一边循环一边计算的机制,从而节省了空间。</p><h2 id="4-1-方法一"><a href="#4-1-方法一" class="headerlink" title="4.1. 方法一"></a>4.1. 方法一</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 将列表生成式的[]改为()</span></span><br><span class="line"><span class="meta">>>> </span>L = (x*<span class="number">2</span> <span class="keyword">for</span> x <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">4</span>))</span><br><span class="line"><span class="meta">>>> </span>L</span><br><span class="line"><generator <span class="built_in">object</span> <genexpr> at <span class="number">0x109ac12d0</span>></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">next</span>(L) <span class="comment"># next()函数获得generator的下一个返回值</span></span><br><span class="line"><span class="number">0</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">next</span>(L)</span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="meta">>>> </span>L.__next__()</span><br><span class="line"><span class="number">4</span></span><br><span class="line"><span class="meta">>>> </span>L.__next__()</span><br><span class="line"><span class="number">6</span></span><br><span class="line"><span class="meta">>>> </span>L.__next__() <span class="comment"># 直到最后一个元素,抛出 StopIteration 错误</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line"> File <span class="string">"<input>"</span>, line <span class="number">1</span>, <span class="keyword">in</span> <module></span><br><span class="line">StopIteration</span><br><span class="line"></span><br><span class="line"><span class="comment"># 一般都使用for语句访问,next()只是用来说明用的</span></span><br><span class="line">L = (x*<span class="number">2</span> <span class="keyword">for</span> x <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">4</span>))</span><br><span class="line"><span class="meta">>>> </span><span class="keyword">for</span> i <span class="keyword">in</span> L:</span><br><span class="line"><span class="meta">... </span> <span class="built_in">print</span>(i)</span><br><span class="line"><span class="meta">... </span> </span><br><span class="line"><span class="number">0</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="number">4</span></span><br><span class="line"><span class="number">6</span></span><br></pre></td></tr></table></figure><h2 id="4-2-方法二-yield语句"><a href="#4-2-方法二-yield语句" class="headerlink" title="4.2. 方法二: yield语句"></a>4.2. 方法二: yield语句</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 不使用生成器, 则先生成List, 后打印List</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">gen_num</span>():</span></span><br><span class="line"> nums = []</span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">4</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'生成数据:'</span>, i)</span><br><span class="line"> nums.append(i)</span><br><span class="line"> <span class="keyword">return</span> nums</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">nums = gen_num()</span><br><span class="line"><span class="keyword">for</span> num <span class="keyword">in</span> nums:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'打印数据:'</span>, num)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 输出</span></span><br><span class="line">生成数据: <span class="number">0</span></span><br><span class="line">生成数据: <span class="number">1</span></span><br><span class="line">生成数据: <span class="number">2</span></span><br><span class="line">生成数据: <span class="number">3</span></span><br><span class="line">打印数据: <span class="number">0</span></span><br><span class="line">打印数据: <span class="number">1</span></span><br><span class="line">打印数据: <span class="number">2</span></span><br><span class="line">打印数据: <span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用生成器yield</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">gen_num</span>():</span></span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">4</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'生成数据:'</span>, i)</span><br><span class="line"> <span class="keyword">yield</span> i</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">nums = gen_num()</span><br><span class="line"><span class="keyword">for</span> num <span class="keyword">in</span> nums:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'打印数据:'</span>, num)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 输出 - 边生成边输出</span></span><br><span class="line">生成数据: <span class="number">0</span></span><br><span class="line">打印数据: <span class="number">0</span></span><br><span class="line">生成数据: <span class="number">1</span></span><br><span class="line">打印数据: <span class="number">1</span></span><br><span class="line">生成数据: <span class="number">2</span></span><br><span class="line">打印数据: <span class="number">2</span></span><br><span class="line">生成数据: <span class="number">3</span></span><br><span class="line">打印数据: <span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 杨辉三角 --<代码精妙> </span></span><br><span class="line"><span class="meta">>>> </span><span class="function"><span class="keyword">def</span> <span class="title">triangles</span>():</span></span><br><span class="line"><span class="meta">... </span> L = [<span class="number">1</span>]</span><br><span class="line"><span class="meta">... </span> <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line"><span class="meta">... </span> <span class="keyword">yield</span> L</span><br><span class="line"><span class="meta">... </span> L = [<span class="number">1</span>] + [L[i]+L[i+<span class="number">1</span>] <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(L)-<span class="number">1</span>)] + [<span class="number">1</span>]</span><br></pre></td></tr></table></figure><h1 id="5-迭代器"><a href="#5-迭代器" class="headerlink" title="5. 迭代器"></a>5. 迭代器</h1><h2 id="5-1-Iterable-对象-列表、字典、字符串、生成器"><a href="#5-1-Iterable-对象-列表、字典、字符串、生成器" class="headerlink" title="5.1. Iterable 对象(列表、字典、字符串、生成器)"></a>5.1. Iterable 对象(列表、字典、字符串、生成器)</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span><span class="keyword">from</span> collections.abc <span class="keyword">import</span> Iterable</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>([], Iterable)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>({}, Iterable)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>(<span class="string">'abc'</span>, Iterable)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>((x*<span class="number">2</span> <span class="keyword">for</span> x <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">6</span>)), Iterable)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>(<span class="number">10</span>, Iterable) <span class="comment"># 数字不可迭代</span></span><br><span class="line"><span class="literal">False</span></span><br></pre></td></tr></table></figure><h2 id="5-2-Iterator-迭代器-可以被next-函数调用并不断返回下一个值的对象"><a href="#5-2-Iterator-迭代器-可以被next-函数调用并不断返回下一个值的对象" class="headerlink" title="5.2. Iterator: 迭代器 - 可以被next()函数调用并不断返回下一个值的对象"></a>5.2. Iterator: 迭代器 - 可以被next()函数调用并不断返回下一个值的对象</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Iterator计算是惰性的,只有需要返回下一个数据时才计算。而且只能遍历一次。</span></span><br><span class="line"><span class="meta">>>> </span><span class="keyword">from</span> collections.abc <span class="keyword">import</span> Iterator</span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>((x*<span class="number">2</span> <span class="keyword">for</span> x <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">6</span>)), Iterator)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>([], Iterator)</span><br><span class="line"><span class="literal">False</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>({}, Iterator)</span><br><span class="line"><span class="literal">False</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>(<span class="string">'abc'</span>, Iterator)</span><br><span class="line"><span class="literal">False</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># iter()函数可以将 list, dict, str等变成 Iterator</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>(<span class="built_in">iter</span>([]), Iterator)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>(<span class="built_in">iter</span>({}), Iterator)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">>>> </span><span class="built_in">isinstance</span>(<span class="built_in">iter</span>(<span class="string">'abc'</span>), Iterator)</span><br><span class="line"><span class="literal">True</span></span><br></pre></td></tr></table></figure><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html"><h1 id="1-切片"><a href="#1-切片" class="headerlink" title="1. 切片"></a>1. 切片</h1><blockquote>
<p>Python提供了切片(Slice)操作符,常用来取一个list或者tuple的部分元素。有了切片,就可以用一行代码代替多行循环了。</p>
</blockquote></summary>
<category term="Computer Science" scheme="https://aaronos0.github.io/categories/Computer-Science/"/>
<category term="Python Learning" scheme="https://aaronos0.github.io/tags/Python-Learning/"/>
</entry>
</feed>