-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
2058 lines (1509 loc) · 160 KB
/
index.html
File metadata and controls
2058 lines (1509 loc) · 160 KB
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
<link rel="mask-icon" href="/images/logo.svg" color="#222">
<link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css" integrity="sha256-2H3fkXt6FEmrReK448mDVGKb3WW2ZZw35gI7vqHOE4Y=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
<script class="next-config" data-name="main" type="application/json">{"hostname":"link-bnds.github.io","root":"/","images":"/images","scheme":"Gemini","version":"8.7.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}}</script><script src="/js/config.js"></script>
<meta property="og:type" content="website">
<meta property="og:title" content="Link LZCのBlog">
<meta property="og:url" content="https://link-bnds.github.io/index.html">
<meta property="og:site_name" content="Link LZCのBlog">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="Link LZC">
<meta name="twitter:card" content="summary">
<link rel="canonical" href="https://link-bnds.github.io/">
<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":true,"isPost":false,"lang":"en","comments":"","permalink":"","path":"index.html","title":""}</script>
<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Link LZCのBlog</title>
<noscript>
<link rel="stylesheet" href="/css/noscript.css">
</noscript>
<!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --></head>
<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
<div class="headband"></div>
<main class="main">
<header class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-container">
<div class="site-nav-toggle">
<div class="toggle" aria-label="Toggle navigation bar" role="button">
<span class="toggle-line"></span>
<span class="toggle-line"></span>
<span class="toggle-line"></span>
</div>
</div>
<div class="site-meta">
<a href="/" class="brand" rel="start">
<i class="logo-line"></i>
<h1 class="site-title">Link LZCのBlog</h1>
<i class="logo-line"></i>
</a>
<p class="site-subtitle" itemprop="description">Accumulation</p>
</div>
<div class="site-nav-right">
<div class="toggle popup-trigger">
</div>
</div>
</div>
<nav class="site-nav">
<ul class="main-menu menu">
<li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
<li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>About</a></li>
<li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a></li>
<li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories</a></li>
<li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li>
<li class="menu-item menu-item-schedule"><a href="/schedule/" rel="section"><i class="fa fa-calendar fa-fw"></i>Schedule</a></li>
</ul>
</nav>
</div>
<div class="toggle sidebar-toggle" role="button">
<span class="toggle-line"></span>
<span class="toggle-line"></span>
<span class="toggle-line"></span>
</div>
<aside class="sidebar">
<div class="sidebar-inner sidebar-overview-active">
<ul class="sidebar-nav">
<li class="sidebar-nav-toc">
Table of Contents
</li>
<li class="sidebar-nav-overview">
Overview
</li>
</ul>
<div class="sidebar-panel-container">
<!--noindex-->
<div class="post-toc-wrap sidebar-panel">
</div>
<!--/noindex-->
<div class="site-overview-wrap sidebar-panel">
<div class="site-overview">
<div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
<img class="site-author-image" itemprop="image" alt="Link LZC"
src="/images/rin.png">
<p class="site-author-name" itemprop="name">Link LZC</p>
<div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
<nav class="site-state">
<div class="site-state-item site-state-posts">
<a href="/archives/">
<span class="site-state-item-count">68</span>
<span class="site-state-item-name">posts</span>
</a>
</div>
</nav>
</div>
<div class="links-of-author site-overview-item animated">
<span class="links-of-author-item">
<a href="https://github.com/Link-BNDS" title="GitHub → https://github.com/Link-BNDS" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
</span>
<span class="links-of-author-item">
<a href="https://space.bilibili.com/31118078" title="Bilibili → https://space.bilibili.com/31118078" rel="noopener" target="_blank">Bilibili</a>
</span>
<span class="links-of-author-item">
<a href="mailto:kpl12345yh@163.com" title="E-Mail → mailto:kpl12345yh@163.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
</span>
</div>
<div class="cc-license site-overview-item animated" itemprop="license">
<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="https://cdn.jsdelivr.net/npm/@creativecommons/vocabulary@2020.11.3/assets/license_badges/small/by_nc_sa.svg" alt="Creative Commons"></a>
</div>
</div>
</div>
</div>
<div class="back-to-top animated" role="button" aria-label="Back to top">
<i class="fa fa-arrow-up"></i>
<span>0%</span>
</div>
</div>
</aside>
<div class="sidebar-dimmer"></div>
</header>
<div class="reading-progress-bar"></div>
<noscript>
<div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>
<div class="main-inner index posts-expand">
<div class="post-block">
<article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
<link itemprop="mainEntityOfPage" href="https://link-bnds.github.io/hello-world/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/rin.png">
<meta itemprop="name" content="Link LZC">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Link LZCのBlog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/hello-world/" class="post-title-link" itemprop="url">Hello World</a>
</h2>
<div class="post-meta-container">
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2023-02-18 22:03:52" itemprop="dateCreated datePublished" datetime="2023-02-18T22:03:52+08:00">2023-02-18</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">Edited on</span>
<time title="Modified: 2021-08-22 12:49:36" itemprop="dateModified" datetime="2021-08-22T12:49:36+08:00">2021-08-22</time>
</span>
</div>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>hello world</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
</div>
<div class="post-block">
<article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
<link itemprop="mainEntityOfPage" href="https://link-bnds.github.io/APCSA-Practice/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/rin.png">
<meta itemprop="name" content="Link LZC">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Link LZCのBlog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/APCSA-Practice/" class="post-title-link" itemprop="url">APCSA_Practice</a>
</h2>
<div class="post-meta-container">
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2022-08-25 21:44:09 / Modified: 21:57:39" itemprop="dateCreated datePublished" datetime="2022-08-25T21:44:09+08:00">2022-08-25</time>
</span>
</div>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>We developed an cross-platform, integrated, and graphical Java app
based on JavaFX for BNDSer to practice AP CSA FRQs.</p>
<p>You can download the release at <a
target="_blank" rel="noopener" href="https://github.com/Link-BNDS/CSA_Practice_APP/releases/tag/AP"
class="uri">https://github.com/Link-BNDS/CSA_Practice_APP/releases/tag/AP</a>
. You can also find the src at repo <a
target="_blank" rel="noopener" href="https://github.com/Link-BNDS/CSA_Practice_APP"
class="uri">https://github.com/Link-BNDS/CSA_Practice_APP</a>.</p>
<p>The description can also be found at that repo.</p>
<p>Thanks for your support.</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
</div>
<div class="post-block">
<article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
<link itemprop="mainEntityOfPage" href="https://link-bnds.github.io/Logistic-Regression/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/rin.png">
<meta itemprop="name" content="Link LZC">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Link LZCのBlog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/Logistic-Regression/" class="post-title-link" itemprop="url">Logistic_Regression</a>
</h2>
<div class="post-meta-container">
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2022-07-28 23:28:23 / Modified: 23:44:51" itemprop="dateCreated datePublished" datetime="2022-07-28T23:28:23+08:00">2022-07-28</time>
</span>
</div>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="前言">1. 前言</h3>
<p>在Coursera上吴恩达的Machine Learning中,Logistic
Regression作为初学者们接触到的第二个模型,其本身是非常重要的。但是在课程当中其损失函数的梯度公式则被一笔带过。而很多人都对其为什么与线性回归的损失函数梯度是一样的表示好奇,因此,我特地从头推导了这一式子。</p>
<h3 id="逻辑斯蒂回归损失函数梯度推导">2.
逻辑斯蒂回归损失函数梯度推导</h3>
<h4 id="introduction">2.1. Introduction</h4>
<p>The "Machine Learning" course on Coursera is a one of the most
popular course over all of MOOC. This course is famous for its
simplified teaching step and other bright spots. Logistics regression,
as the most important model for beginner, lacks appropriate detailed
derivation of formula. In this blog, I am going to expound this section
step by step.</p>
<h4 id="logistic-regression">2.2. Logistic Regression</h4>
<p>I think most of you have already got familiar with logistic
regression.</p>
<h4 id="cost-function">2.3. Cost Function</h4>
<p>As we all know, the cost function of the logistic regression is
modified from the one from linear regression model. <span
class="math display">\[
Cost\left( h_{\theta}\left( x \right) ,y \right) =\left\{
\begin{aligned}
-\log \left( h_{\theta}\left( x \right) \right)
\,\, if\,\,y&=1\\
-\log \left( 1-h_{\theta}\left( x \right) \right) \,\,
if\,\,y&=0\\
\end{aligned} \right.
\]</span></p>
<p>To simplify this function, we can write it as</p>
<p><span class="math display">\[
J\left( \theta \right) =\frac{1}{2m}\sum_{i=1}^m{Cost\left(
h_{\theta}\left( x^{\left( i \right)} \right) ,y^{\left( i \right)}
\right)}
\]</span></p>
<p>which</p>
<p><span class="math display">\[
Cost\left( h_{\theta}\left( x^{\left( i \right)} \right) ,y^{\left( i
\right)} \right) =-y^{\left( i \right)}\log \left( h_{\theta}\left( x
\right) \right) -\left( 1-y^{\left( i \right)} \right) \log \left(
1-h_{\theta}\left( x^{\left( i \right)} \right) \right)
\]</span></p>
<h4 id="gradient-of-cost-function">2.4. Gradient of Cost Function</h4>
<p>And next, is the part I want to expound. Andrew, in this MOOC, just
briefly introduce that the gradient of the new cost function is the same
as the one for linear regression. There is no explicit derivation
here.</p>
<p>For cost function <span class="math inline">\(J\)</span>, we can
write it as</p>
<p><span class="math display">\[\begin{equation}
\begin{aligned}
J\left( \theta \right) &=-\frac{1}{m}\left[ \sum_{i=1}^m{-y^{\left(
i \right)}\log \left( h_{\theta}\left( x \right) \right) -\left(
1-y^{\left( i \right)} \right) \log \left( 1-h_{\theta}\left( x^{\left(
i \right)} \right) \right)} \right]
\\
\frac{\partial}{\partial \theta _j}J\left( \theta \right)
&=\frac{\partial}{\partial \theta _j}\left[ -\frac{1}{m}\left[
\sum_{i=1}^m{-y^{\left( i \right)}\log \left( h_{\theta}\left( x \right)
\right) -\left( 1-y^{\left( i \right)} \right) \log \left(
1-h_{\theta}\left( x^{\left( i \right)} \right) \right)} \right] \right]
\\
&=-\frac{1}{m}\left[ \sum_{i=1}^m{\left( y^{\left( i
\right)}\frac{1}{h_{\theta}\left( x^{\left( i \right)} \right)}\cdot
\frac{\partial}{\partial \theta _j}h_{\theta}\left( x^{\left( i \right)}
\right) +\left( 1-y^{\left( i \right)} \right) \cdot
\frac{1}{1-h_{\theta}\left( x^{\left( i \right)} \right)}\cdot
\frac{\partial}{\partial \theta _j}\left( -h_{\theta}\left( x^{\left( i
\right)} \right) \right) \right)} \right]
\\
&=-\frac{1}{m}\left[ \sum_{i=1}^m{\left( y^{\left( i
\right)}\frac{1}{h_{\theta}\left( x^{\left( i \right)} \right)}-\left(
1-y^{\left( i \right)} \right) \cdot \frac{1}{1-h_{\theta}\left(
x^{\left( i \right)} \right)} \right) \cdot \frac{\partial}{\partial
\theta _j}\left( h_{\theta}\left( x^{\left( i \right)} \right) \right)}
\right]
\\
&=-\frac{1}{m}\left[ \sum_{i=1}^m{\left( y^{\left( i
\right)}\frac{1}{g\left( \theta ^Tx \right)}-\left( 1-y^{\left( i
\right)} \right) \cdot \frac{1}{1-g\left( \theta ^Tx \right)} \right)
\cdot \frac{\partial}{\partial \theta _j}\left( g\left( \theta ^Tx
\right) \right)} \right]
\end{aligned}
\end{equation}\]</span></p>
<p>In logistic regression, we use the logistic function as our decision
function. Therefore, (Mentioned: <span class="math inline">\(T\)</span>
refers to transpose)</p>
<p><span class="math display">\[\begin{equation}
\begin{aligned}
\frac{\partial}{\partial \theta _j}g\left( \theta ^Tx \right)
&=\frac{\partial}{\partial \theta _j}\cdot \frac{1}{1+e^{-\theta
^Tx}}
\\
&=\frac{\partial}{\partial \theta _j}\left( 1+e^{-\theta ^Tx}
\right) ^{-1}
\\
&=-\left( 1+e^{-\theta ^Tx} \right) ^{-2}\cdot e^{e^{-\theta
^Tx}}\cdot x
\\
&=-\frac{e^{e^{-\theta ^Tx}}\cdot -x}{\left( 1+e^{e^{-\theta ^Tx}}
\right) ^2}
\end{aligned}
\end{equation}\]</span></p>
<p>We set <span class="math inline">\(k=e^{-\theta ^Tx}\)</span>, plug
it in,</p>
<p><span class="math display">\[\begin{equation}
\begin{aligned}
&=\frac{k}{\left( 1+k \right) ^2}\cdot x
\\
&=\left( \frac{1}{1+k}\cdot \frac{1+k-1}{1+k} \right) \cdot x
\\
&=\left[ \frac{1}{1+k}\cdot \left( 1-\frac{1}{1+k} \right) \right]
\cdot x
\end{aligned}
\end{equation}\]</span></p>
<p>Bring it back, we can see we construct the logistic function
itself,</p>
<p><span class="math display">\[\begin{equation}
\begin{aligned}
&=\left[ \frac{1}{1+e^{-\theta ^Tx}}\cdot \left(
1-\frac{1}{1+e^{-\theta ^Tx}} \right) \right] \cdot x
\end{aligned}
\end{equation}\]</span></p>
<p>Thus, we can use the logistic function to simplify the eqution,</p>
<p><span class="math display">\[\begin{equation}
\begin{aligned}
&=\left[ \frac{1}{1+e^{-\theta ^Tx}}\cdot \left(
1-\frac{1}{1+e^{-\theta ^Tx}} \right) \right] \cdot x
\\
&=g\left( \theta ^Tx \right) \cdot \left( 1-g\left( \theta ^Tx
\right) \right) \cdot x
\end{aligned}
\end{equation}\]</span></p>
<p>We plug back this portion to the gradient of cost function (1),</p>
<p><span class="math display">\[\begin{equation}
\begin{aligned}
\frac{\partial}{\partial \theta _j}J\left( \theta \right)
&=-\frac{1}{m}\left[ \sum_{i=1}^m{\left( y^{\left( i \right)}\cdot
\frac{1}{g\left( \theta ^Tx \right)}-\left( 1-y^{\left( i \right)}
\right) \cdot \frac{1}{1-g\left( \theta ^Tx \right)} \right)}\cdot
g\left( \theta ^Tx \right) \cdot \left( 1-g\left( \theta ^Tx \right)
\right) \cdot x^{\left( i \right)} \right]
\\
&=-\frac{1}{m}\left[ \sum_{i=1}^m{\left[ y^{\left( i \right)}\cdot
\left( 1-g\left( \theta ^Tx \right) \right) \cdot x^{\left( i
\right)}-\left( 1-y^{\left( i \right)} \right) \cdot g\left( \theta ^Tx
\right) \cdot x^{\left( i \right)} \right]} \right]
\\
&=-\frac{1}{m}\sum_{i=1}^m{\left[ y^{\left( i \right)}\cdot
x^{\left( i \right)}-y^{\left( i \right)}\cdot g\left( \theta ^Tx
\right) \cdot x^{\left( i \right)}-g\left( \theta ^Tx \right) \cdot
x^{\left( i \right)}+y^{\left( i \right)}\cdot g\left( \theta ^Tx
\right) \cdot x^{\left( i \right)} \right]}
\\
&\mathrm{because}-y^{\left( i \right)}\cdot g\left( \theta ^Tx
\right) \cdot x^{\left( i \right)}\,\,\mathrm{offsets} +y^{\left( i
\right)}\cdot g\left( \theta ^Tx \right) \cdot x^{\left( i \right)}
\\
&=-\frac{1}{m}\sum_{i=1}^m{\left[ y^{\left( i \right)}\cdot
x^{\left( i \right)}-g\left( \theta ^Tx \right) \cdot x^{\left( i
\right)} \right]}
\\
&=-\frac{1}{m}\sum_{i=1}^m{\left[ \left[ y^{\left( i
\right)}-g\left( \theta ^Tx \right) \right] \cdot x^{\left( i \right)}
\right]}
\\
&=\frac{1}{m}\sum_{i=1}^m{\left[ \left[ h_{\theta}\left( x^{\left( i
\right)} \right) -y^{\left( i \right)} \right] \cdot x^{\left( i
\right)} \right]}
\end{aligned}
\end{equation}\]</span></p>
<p>which is the same as linear regression's</p>
<p>Q.E.D.</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
</div>
<div class="post-block">
<article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
<link itemprop="mainEntityOfPage" href="https://link-bnds.github.io/Stokes-Theorem/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/rin.png">
<meta itemprop="name" content="Link LZC">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Link LZCのBlog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/Stokes-Theorem/" class="post-title-link" itemprop="url">Stokes_Theorem</a>
</h2>
<div class="post-meta-container">
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2022-07-20 15:20:42 / Modified: 19:04:41" itemprop="dateCreated datePublished" datetime="2022-07-20T15:20:42+08:00">2022-07-20</time>
</span>
</div>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="introduction">Introduction</h3>
<p>In my second semester of High School Junior year, when we were
studying vector calculus <span class="citation"
data-cites="Calculus">@Calculus</span> 3, our teacher gave us an
assignment: proof of Stokes formula. CALCULUS EARLY TRANSCENDENTALS
EIGHTH EDITION JAMES STEWART (our Calculus3 textbook) only has a special
form of stokes' theorem proof. Thus, I looked up the prood at old
Chinese Text Book. It is a pity that our high school calculus education
with American calculus education are quite different compared to
Chinese's. I searched on zhihu, Bilibili and other public platforms, but
failed to find any suitable results. Luckily, I finally figure it out
when I went back home during the epidemic.</p>
<h3 id="stokes-theorem">Stoke's Theorem</h3>
<p>For surface S in <span class="math inline">\(R^3\)</span> space is an
oriented surface in <span class="math inline">\(x,y,z\)</span>
coordinate with boundary <span class="math inline">\(\partial
S\)</span>. Let <span class="math inline">\(R\)</span> be abounded, open
region in <span class="math inline">\(s, t\)</span> plane with smooth
boundary <span class="math inline">\(\partial R\)</span> . Suppose that
<span class="math inline">\(F\)</span> is a continuously differentiable
vector field. <span class="math inline">\(\vec{r}\)</span> is a smooth
parametrization that maps <span class="math inline">\(R\)</span> to
<span class="math inline">\(S\)</span>, and <span
class="math inline">\(\partial R\)</span> to <span
class="math inline">\(\partial S\)</span>. Then</p>
<p><span class="math display">\[
\iint_S{curl\,\,\vec{F}\,\,·\,\,d\vec{S}}=\iint_S{\left( \nabla \times
\vec{F} \right) \,\,·\,\,\vec{n}\,\,dS}=\int_{\partial
S}{\vec{F}\,\,·\,\,\vec{T}\,\,ds}=\,\,\int_{\partial
S}{\vec{F}\,\,·\,\,d\vec{r}}
\]</span></p>
<figure>
<img src="/img/stks.png" alt="stks" />
<figcaption aria-hidden="true">stks</figcaption>
</figure>
<h3 id="proof">Proof</h3>
<p>Let</p>
<p><span class="math display">\[
\vec{r}\left( s,t \right) =\left[ \begin{array}{c}
x\left( s,t \right)\\
y\left( s,t \right)\\
z\left( s,t \right)\\
\end{array} \right] \,\,: R\rightarrow S.
\]</span></p>
<p>Then.</p>
<p><span class="math display">\[
d\vec{r}\mid_{\partial R}^{}\,\,=\left[ \begin{matrix}
\frac{\partial x}{\partial s}& \frac{\partial x}{\partial
t}\\
\frac{\partial y}{\partial s}& \frac{\partial y}{\partial
t}\\
\frac{\partial z}{\partial s}& \frac{\partial z}{\partial
t}\\
\end{matrix} \right] \left[ \begin{array}{c}
ds\\
dt\\
\end{array} \right] \,\,=\left[ \begin{array}{c}
\frac{\partial x}{\partial s}ds+\frac{\partial x}{\partial t}dt\\
\frac{\partial y}{\partial s}ds+\frac{\partial y}{\partial t}dt\\
\frac{\partial z}{\partial s}ds+\frac{\partial z}{\partial t}dt\\
\end{array} \right] \,\,=\left[ \begin{array}{c}
\frac{\partial x}{\partial s}\\
\frac{\partial y}{\partial s}\\
\frac{\partial z}{\partial s}\\
\end{array} \right] ds+\left[ \begin{array}{c}
\frac{\partial x}{\partial t}\\
\frac{\partial y}{\partial t}\\
\frac{\partial z}{\partial t}\\
\end{array} \right] dt\,\,=\frac{\partial \vec{r}}{\partial
s}ds+\frac{\partial \vec{r}}{\partial t}dt
\]</span></p>
<p>Hence.</p>
<p><span class="math display">\[
\int_{\partial S}{\vec{F}·}d\vec{r}=\int_{\partial S}{\left(
\vec{F}·\frac{\partial \vec{r}}{\partial s}ds+\vec{F}·\frac{\partial
\vec{r}}{\partial s} \right)}
\]</span></p>
<p>We define a 2-dimensional vector field <span
class="math inline">\(G=(G1,G2)\)</span> on the s,t, plane by</p>
<p><span class="math display">\[
G_1=\vec{F}\,\,·\,\,\frac{\partial \vec{r}}{\partial
s}\,\, and\,\, G_2=\vec{F}\,\,·\,\,\frac{\partial \vec{r}}{\partial t}
\]</span></p>
<p>Therefore, we put G into the original line integral</p>
<p><span class="math display">\[
\int_{\partial S}{\vec{F}\,\,·\,\,d\vec{r}=}\int_{\partial R}{\left(
G_1ds+G_2dt \right) =\int_R{\begin{array}{c}
\left( \frac{\partial G_2}{\partial s}-\frac{\partial G_1}{\partial
t} \right) dsdt\,\,,\\
\end{array}}\,\,}
\]</span></p>
<p><span class="math display">\[
\int_{\partial S}{\vec{F}\,\,·\,\,d\vec{r}=}\int_{\partial R}{\left(
G_1ds+G_2dt \right) \,\,}
\]</span></p>
<p>On the other hand.</p>
<p><span class="math display">\[
\iint_S{curl\,\,\vec{F}\,\,·\,\,d\vec{S}\,\,=\,\,\iint_R{curl\,\,\vec{F}\mid_{\vec{r}}^{}·\frac{\partial
\vec{r}}{\partial s}\times \frac{\partial \vec{r}}{\partial t}dsdt}}
\]</span></p>
<p>We expand it, get</p>
<p><span class="math display">\[
curl\,\,\vec{F}\mid_{\vec{r}}^{}\cdot \frac{\partial \vec{r}}{\partial
s}\times \frac{\partial \vec{r}}{\partial t}\,\,=\,\,\left|
\begin{matrix}
\frac{\partial F_3}{\partial y}-\frac{\partial F_2}{\partial
z}& \frac{\partial F_1}{\partial z}-\frac{\partial
F_3}{\partial x}& \frac{\partial F_2}{\partial
x}-\frac{\partial F_1}{\partial y}\\
\frac{\partial x}{\partial s}& \frac{\partial y}{\partial
s}& \frac{\partial z}{\partial s}\\
\frac{\partial x}{\partial t}& \frac{\partial y}{\partial
t}& \frac{\partial z}{\partial t}\\
\end{matrix} \right|
\]</span></p>
<p><span class="math display">\[
=\frac{\partial \vec{F}}{\partial s}·\frac{\partial \vec{r}}{\partial
t}-\frac{\partial \vec{F}}{\partial t}·\frac{\partial \vec{r}}{\partial
s}\,\,=\frac{\partial G_2}{\partial s}-\frac{\partial G_1}{\partial t}
\]</span></p>
<p>Hence.</p>
<p><span class="math display">\[
\int_R{curl\,\,F\mid_{\vec{r}}^{}\cdot \frac{\partial \vec{r}}{\partial
s}}\times \frac{\partial \vec{r}}{\partial
t}\,\,dsdt\,\,=\,\,\int_R{\begin{array}{c}
\left( \frac{\partial G_2}{\partial s}-\frac{\partial G_1}{\partial
t} \right) dsdt\,\,\\
\end{array}}
\]</span></p>
<p>For the line integral part,we have</p>
<p><span class="math display">\[
\int_{\partial S}{\vec{F}\,\,·\,\,d\vec{r}=}\int_{\partial
R}{\begin{array}{c}
\left( G_1ds+G_2dt \right) \,\,\\
\end{array}}
\]</span></p>
<p>Use Green's Theorem, we know</p>
<p><span class="math display">\[
\int_{\partial R}{\begin{array}{c}
\left( G_1ds+G_2dt \right)\\
\end{array}}=\int_R{\begin{array}{c}
\left( \frac{\partial G_2}{\partial s}-\frac{\partial G_1}{\partial
t} \right) dsdt\,\,,\\
\end{array}}
\]</span></p>
<p>So, we can conclude that</p>
<p><span class="math display">\[
\int_{\partial S}{\vec{F}·}d\vec{r}=\int_R{\begin{array}{c}
\left( \frac{\partial G_2}{\partial s}-\frac{\partial G_1}{\partial
t} \right) dsdt=\int_R{curl\,\,F\mid_{\vec{r}}^{}\cdot \frac{\partial
\vec{r}}{\partial s}}\times \frac{\partial \vec{r}}{\partial t}\\
\end{array}}dsdt=\iint_S{curl\,\,F\,\,·\,\,d\vec{S}}
\]</span></p>
<p>Thus, we finished our proof of Stokes' Theorem.</p>
<!--noindex-->
<div class="post-button">
<a class="btn" href="/Stokes-Theorem/#more" rel="contents">
Read more »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
</div>
<div class="post-block">
<article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
<link itemprop="mainEntityOfPage" href="https://link-bnds.github.io/ML-ex1/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/rin.png">
<meta itemprop="name" content="Link LZC">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Link LZCのBlog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/ML-ex1/" class="post-title-link" itemprop="url">ML_ex1</a>
</h2>
<div class="post-meta-container">
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2022-03-18 14:50:54" itemprop="dateCreated datePublished" datetime="2022-03-18T14:50:54+08:00">2022-03-18</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">Edited on</span>
<time title="Modified: 2022-07-20 17:26:37" itemprop="dateModified" datetime="2022-07-20T17:26:37+08:00">2022-07-20</time>
</span>
</div>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="machine-learning-by-andrew-ng">Machine Learning By Andrew
Ng</h3>
<h4 id="content">Content</h4>
<ul>
<li>Introduction</li>
<li>Potential Submit Problem</li>
<li>ex1.m</li>
<li>computerCost.m</li>
<li>gradientDescent.m</li>
<li>ex1_multi.m</li>
<li>featureNormalize.m</li>
<li>computeCostMulti.m</li>
<li>gradientDescentMulti.m</li>
<li>normalEqn.m</li>
</ul>
<h4 id="introduction">Introduction</h4>
<p>I have listened for so many times that the course: Machine Learning
by Andrew Ng is one of the best introductory course for machine
learning. Now, I have finished the first two week's classed and pass the
first programming project. I truly fathom out the deliberate design of
this course. The passion and meticulous designed project impress me as
well. It is so particular that I feel like someone is teaching me
hand-by-hand. Coursera uses a kind of judging method which is not like
the common OJ that you upload your program and they run it and test it
with their test data. Coursera uses an aggregate local submit module
that can do all the stuff. You just need to generate a token that linked
to your coursera account so that it can upload your progress. It is so
cooooool!!!!!! <img src="/img/ML/ex1/f1.png" alt="f1" /> <img
src="/img/ML/ex1/f2.png" alt="f2" /> ##### Potential Submit Problem Once
I tried to submit the program, it says <img src="/img/ML/ex1/d1.png"
alt="d1" /> According to
https://blog.csdn.net/qq_44498043/article/details/105904715 and
https://blog.csdn.net/weixin_45923568/article/details/104193579 , I
found out that the error is caused by the vulnerable grammar that it
used before. And in the latest version of matlab, matlab banned those
kinds of grammar in order to increase the stability. So all we need is
to go to the coursera website and download the new version of submit
module.</p>
<h4 id="ex1.m">ex1.m</h4>
<pre class="line-numbers language-matlab" data-language="matlab"><code class="language-matlab"><span class="token comment">%% Initialization</span>
clear <span class="token punctuation">;</span> close all<span class="token punctuation">;</span> clc
<span class="token comment">%% ==================== Part 1: Basic Function ====================</span>
<span class="token comment">% Complete warmUpExercise.m</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'Running warmUpExercise ... \n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'5x5 Identity Matrix: \n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">warmUpExercise</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'Program paused. Press enter to continue.\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">pause</span><span class="token punctuation">;</span>
<span class="token comment">%% ======================= Part 2: Plotting =======================</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'Plotting Data ...\n'</span><span class="token punctuation">)</span>
data <span class="token operator">=</span> <span class="token function">load</span><span class="token punctuation">(</span><span class="token string">'ex1data1.txt'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
X <span class="token operator">=</span> <span class="token function">data</span><span class="token punctuation">(</span><span class="token operator">:</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> y <span class="token operator">=</span> <span class="token function">data</span><span class="token punctuation">(</span><span class="token operator">:</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
m <span class="token operator">=</span> <span class="token function">length</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">% number of training examples</span>
<span class="token comment">% Plot Data</span>
<span class="token comment">% Note: You have to complete the code in plotData.m</span>
<span class="token function">plotData</span><span class="token punctuation">(</span>X<span class="token punctuation">,</span> y<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'Program paused. Press enter to continue.\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">pause</span><span class="token punctuation">;</span>
<span class="token comment">%% =================== Part 3: Cost and Gradient descent ===================</span>
X <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token function">ones</span><span class="token punctuation">(</span>m<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">data</span><span class="token punctuation">(</span><span class="token operator">:</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">% Add a column of ones to x</span>
theta <span class="token operator">=</span> <span class="token function">zeros</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">% initialize fitting parameters</span>
<span class="token comment">% Some gradient descent settings</span>
iterations <span class="token operator">=</span> <span class="token number">1500</span><span class="token punctuation">;</span>
alpha <span class="token operator">=</span> <span class="token number">0.01</span><span class="token punctuation">;</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'\nTesting the cost function ...\n'</span><span class="token punctuation">)</span>
<span class="token comment">% compute and display initial cost</span>
J <span class="token operator">=</span> <span class="token function">computeCost</span><span class="token punctuation">(</span>X<span class="token punctuation">,</span> y<span class="token punctuation">,</span> theta<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'With theta = [0 ; 0]\nCost computed = %f\n'</span><span class="token punctuation">,</span> J<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'Expected cost value (approx) 32.07\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">% further testing of the cost function</span>
J <span class="token operator">=</span> <span class="token function">computeCost</span><span class="token punctuation">(</span>X<span class="token punctuation">,</span> y<span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span> <span class="token punctuation">;</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'\nWith theta = [-1 ; 2]\nCost computed = %f\n'</span><span class="token punctuation">,</span> J<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'Expected cost value (approx) 54.24\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'Program paused. Press enter to continue.\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">pause</span><span class="token punctuation">;</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'\nRunning Gradient Descent ...\n'</span><span class="token punctuation">)</span>
<span class="token comment">% run gradient descent</span>
theta <span class="token operator">=</span> <span class="token function">gradientDescent</span><span class="token punctuation">(</span>X<span class="token punctuation">,</span> y<span class="token punctuation">,</span> theta<span class="token punctuation">,</span> alpha<span class="token punctuation">,</span> iterations<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">% print theta to screen</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'Theta found by gradient descent:\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'%f\n'</span><span class="token punctuation">,</span> theta<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'Expected theta values (approx)\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">' -3.6303\n 1.1664\n\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">% Plot the linear fit</span>
hold on<span class="token punctuation">;</span> <span class="token comment">% keep previous plot visible</span>
<span class="token function">plot</span><span class="token punctuation">(</span><span class="token function">X</span><span class="token punctuation">(</span><span class="token operator">:</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span> X<span class="token operator">*</span>theta<span class="token punctuation">,</span> <span class="token string">'-'</span><span class="token punctuation">)</span>
<span class="token function">legend</span><span class="token punctuation">(</span><span class="token string">'Training data'</span><span class="token punctuation">,</span> <span class="token string">'Linear regression'</span><span class="token punctuation">)</span>
hold off <span class="token comment">% don't overlay any more plots on this figure</span>
<span class="token comment">% Predict values for population sizes of 35,000 and 70,000</span>
predict1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3.5</span><span class="token punctuation">]</span> <span class="token operator">*</span>theta<span class="token punctuation">;</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'For population = 35,000, we predict a profit of %f\n'</span><span class="token punctuation">,</span><span class="token punctuation">...</span>
predict1<span class="token operator">*</span><span class="token number">10000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
predict2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span> <span class="token operator">*</span> theta<span class="token punctuation">;</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'For population = 70,000, we predict a profit of %f\n'</span><span class="token punctuation">,</span><span class="token punctuation">...</span>
predict2<span class="token operator">*</span><span class="token number">10000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'Program paused. Press enter to continue.\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">pause</span><span class="token punctuation">;</span>
<span class="token comment">%% ============= Part 4: Visualizing J(theta_0, theta_1) =============</span>
<span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token string">'Visualizing J(theta_0, theta_1) ...\n'</span><span class="token punctuation">)</span>
<span class="token comment">% Grid over which we will calculate J</span>
theta0_vals <span class="token operator">=</span> <span class="token function">linspace</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
theta1_vals <span class="token operator">=</span> <span class="token function">linspace</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">% initialize J_vals to a matrix of 0's</span>
J_vals <span class="token operator">=</span> <span class="token function">zeros</span><span class="token punctuation">(</span><span class="token function">length</span><span class="token punctuation">(</span>theta0_vals<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">length</span><span class="token punctuation">(</span>theta1_vals<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">% Fill out J_vals</span>
<span class="token keyword">for</span> <span class="token number">i</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token operator">:</span><span class="token function">length</span><span class="token punctuation">(</span>theta0_vals<span class="token punctuation">)</span>
<span class="token keyword">for</span> <span class="token number">j</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token operator">:</span><span class="token function">length</span><span class="token punctuation">(</span>theta1_vals<span class="token punctuation">)</span>
t <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token function">theta0_vals</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">theta1_vals</span><span class="token punctuation">(</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token function">J_vals</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token function">computeCost</span><span class="token punctuation">(</span>X<span class="token punctuation">,</span> y<span class="token punctuation">,</span> t<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">end</span>
<span class="token keyword">end</span>
<span class="token comment">% Because of the way meshgrids work in the surf command, we need to</span>
<span class="token comment">% transpose J_vals before calling surf, or else the axes will be flipped</span>
J_vals <span class="token operator">=</span> J_vals<span class="token operator">'</span><span class="token punctuation">;</span>
<span class="token comment">% Surface plot</span>
figure<span class="token punctuation">;</span>
<span class="token function">surf</span><span class="token punctuation">(</span>theta0_vals<span class="token punctuation">,</span> theta1_vals<span class="token punctuation">,</span> J_vals<span class="token punctuation">)</span>
<span class="token function">xlabel</span><span class="token punctuation">(</span><span class="token string">'\theta_0'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">ylabel</span><span class="token punctuation">(</span><span class="token string">'\theta_1'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">% Contour plot</span>
figure<span class="token punctuation">;</span>
<span class="token comment">% Plot J_vals as 15 contours spaced logarithmically between 0.01 and 100</span>
<span class="token function">contour</span><span class="token punctuation">(</span>theta0_vals<span class="token punctuation">,</span> theta1_vals<span class="token punctuation">,</span> J_vals<span class="token punctuation">,</span> <span class="token function">logspace</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token function">xlabel</span><span class="token punctuation">(</span><span class="token string">'\theta_0'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">ylabel</span><span class="token punctuation">(</span><span class="token string">'\theta_1'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
hold on<span class="token punctuation">;</span>
<span class="token function">plot</span><span class="token punctuation">(</span><span class="token function">theta</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">theta</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'rx'</span><span class="token punctuation">,</span> <span class="token string">'MarkerSize'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token string">'LineWidth'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h5 id="computercost.m">computerCost.m</h5>
<pre class="line-numbers language-matlab" data-language="matlab"><code class="language-matlab"><span class="token keyword">function</span> J <span class="token operator">=</span> <span class="token function">computeCost</span><span class="token punctuation">(</span>X<span class="token punctuation">,</span> y<span class="token punctuation">,</span> theta<span class="token punctuation">)</span>
<span class="token comment">%COMPUTECOST Compute cost for linear regression</span>
<span class="token comment">% J = COMPUTECOST(X, y, theta) computes the cost of using theta as the</span>
<span class="token comment">% parameter for linear regression to fit the data points in X and y</span>
<span class="token comment">% Initialize some useful values</span>
m <span class="token operator">=</span> <span class="token function">length</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">% number of training examples</span>
<span class="token comment">% You need to return the following variables correctly </span>