diff --git a/doc/pub/week35/html/._week35-bs000.html b/doc/pub/week35/html/._week35-bs000.html index a463d20ef..3b671f811 100644 --- a/doc/pub/week35/html/._week35-bs000.html +++ b/doc/pub/week35/html/._week35-bs000.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -410,7 +403,7 @@ MathJax.Hub.Config({
  • 9
  • 10
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs001.html b/doc/pub/week35/html/._week35-bs001.html index 9ae4b4464..978ea6de4 100644 --- a/doc/pub/week35/html/._week35-bs001.html +++ b/doc/pub/week35/html/._week35-bs001.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -383,6 +376,8 @@ MathJax.Hub.Config({
    1. See lecture notes for week 35 at https://compphysics.github.io/MachineLearning/doc/web/course.html
    2. Goodfellow, Bengio and Courville, Deep Learning, chapter 2 on linear algebra and sections 3.1-3.10 on elements of statistics (background)
    3. +
    4. Raschka et al on preprocessing of data, relevant for exercise 3 this week, see chapter 4.
    5. +
    6. For exercise 1 of week 35, the book by A. Aldo Faisal, Cheng Soon Ong, and Marc Peter Deisenroth on the Mathematics of Machine Learning, may be very relevant. In particular chapter 5 at URL"https://mml-book.github.io/" (section 5.5 on derivatives) is very useful for exercise 1 this coming week.

    @@ -400,7 +395,7 @@ MathJax.Hub.Config({

  • 10
  • 11
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs002.html b/doc/pub/week35/html/._week35-bs002.html index 315de77ed..9acec0922 100644 --- a/doc/pub/week35/html/._week35-bs002.html +++ b/doc/pub/week35/html/._week35-bs002.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,7 +360,7 @@ MathJax.Hub.Config({

     

     

     

    -

    Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week

    +

    For exercise sessions: Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week

    We need first a reminder from last week about linear regression.

    @@ -404,7 +397,7 @@ Similarly, Mehta et al
  • 11
  • 12
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs003.html b/doc/pub/week35/html/._week35-bs003.html index effb25bdb..cad3da412 100644 --- a/doc/pub/week35/html/._week35-bs003.html +++ b/doc/pub/week35/html/._week35-bs003.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -428,7 +421,7 @@ values \( \tilde{y}_i \), namely the so-called cost/loss function.
  • 12
  • 13
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs004.html b/doc/pub/week35/html/._week35-bs004.html index 6effe0190..f5283d363 100644 --- a/doc/pub/week35/html/._week35-bs004.html +++ b/doc/pub/week35/html/._week35-bs004.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -410,7 +403,7 @@ $$
  • 13
  • 14
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs005.html b/doc/pub/week35/html/._week35-bs005.html index 1f8405dda..586c696eb 100644 --- a/doc/pub/week35/html/._week35-bs005.html +++ b/doc/pub/week35/html/._week35-bs005.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -433,7 +426,7 @@ $$
  • 14
  • 15
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs006.html b/doc/pub/week35/html/._week35-bs006.html index 5ee85f944..5d6d78ad0 100644 --- a/doc/pub/week35/html/._week35-bs006.html +++ b/doc/pub/week35/html/._week35-bs006.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -429,7 +422,7 @@ allow for the usage of direct linear algebra methods such as LU decomposi
  • 15
  • 16
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs007.html b/doc/pub/week35/html/._week35-bs007.html index 1e9ce4e00..b7188b249 100644 --- a/doc/pub/week35/html/._week35-bs007.html +++ b/doc/pub/week35/html/._week35-bs007.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -416,7 +409,7 @@ $$
  • 16
  • 17
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs008.html b/doc/pub/week35/html/._week35-bs008.html index 8c2caec88..1cbb749fa 100644 --- a/doc/pub/week35/html/._week35-bs008.html +++ b/doc/pub/week35/html/._week35-bs008.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -414,7 +407,7 @@ vector is differentiable.
  • 17
  • 18
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs009.html b/doc/pub/week35/html/._week35-bs009.html index 68f1c6901..35e64b1fa 100644 --- a/doc/pub/week35/html/._week35-bs009.html +++ b/doc/pub/week35/html/._week35-bs009.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -413,7 +406,7 @@ $$
  • 18
  • 19
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs010.html b/doc/pub/week35/html/._week35-bs010.html index 689eeb7e4..a62fea7b4 100644 --- a/doc/pub/week35/html/._week35-bs010.html +++ b/doc/pub/week35/html/._week35-bs010.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -424,7 +417,7 @@ $$
  • 19
  • 20
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs011.html b/doc/pub/week35/html/._week35-bs011.html index 8db887866..fea66bdc4 100644 --- a/doc/pub/week35/html/._week35-bs011.html +++ b/doc/pub/week35/html/._week35-bs011.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -426,7 +419,7 @@ $$
  • 20
  • 21
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs012.html b/doc/pub/week35/html/._week35-bs012.html index bc697a8d3..9fc728584 100644 --- a/doc/pub/week35/html/._week35-bs012.html +++ b/doc/pub/week35/html/._week35-bs012.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -427,7 +420,7 @@ $$
  • 21
  • 22
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs013.html b/doc/pub/week35/html/._week35-bs013.html index 1d23712ec..0fef0efa4 100644 --- a/doc/pub/week35/html/._week35-bs013.html +++ b/doc/pub/week35/html/._week35-bs013.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -439,7 +432,7 @@ $$
  • 22
  • 23
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs014.html b/doc/pub/week35/html/._week35-bs014.html index 5af7ad834..3e251c1f6 100644 --- a/doc/pub/week35/html/._week35-bs014.html +++ b/doc/pub/week35/html/._week35-bs014.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -408,7 +401,7 @@ $$
  • 23
  • 24
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs015.html b/doc/pub/week35/html/._week35-bs015.html index 1e6072630..e614da5ad 100644 --- a/doc/pub/week35/html/._week35-bs015.html +++ b/doc/pub/week35/html/._week35-bs015.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -422,7 +415,7 @@ problem.
  • 24
  • 25
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs016.html b/doc/pub/week35/html/._week35-bs016.html index 25ce3586d..9da65cdaa 100644 --- a/doc/pub/week35/html/._week35-bs016.html +++ b/doc/pub/week35/html/._week35-bs016.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -417,7 +410,7 @@ $$
  • 25
  • 26
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs017.html b/doc/pub/week35/html/._week35-bs017.html index c71bca0ba..e84d3c4d9 100644 --- a/doc/pub/week35/html/._week35-bs017.html +++ b/doc/pub/week35/html/._week35-bs017.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -406,7 +399,7 @@ $$
  • 26
  • 27
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs018.html b/doc/pub/week35/html/._week35-bs018.html index 4e50357d4..2d92f459d 100644 --- a/doc/pub/week35/html/._week35-bs018.html +++ b/doc/pub/week35/html/._week35-bs018.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -459,7 +452,7 @@ ytildenp = np.<
  • 27
  • 28
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs019.html b/doc/pub/week35/html/._week35-bs019.html index ca5cca869..6b138e20e 100644 --- a/doc/pub/week35/html/._week35-bs019.html +++ b/doc/pub/week35/html/._week35-bs019.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -500,7 +493,7 @@ Since we are not using Scikit-Learn here we can define our own \( R2 \) f
  • 28
  • 29
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs020.html b/doc/pub/week35/html/._week35-bs020.html index 8ff2faa2b..4dc4044e9 100644 --- a/doc/pub/week35/html/._week35-bs020.html +++ b/doc/pub/week35/html/._week35-bs020.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -413,7 +406,7 @@ but now splitting the data into a training set and a test set.
  • 29
  • 30
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs021.html b/doc/pub/week35/html/._week35-bs021.html index f8a265d40..1e94b90da 100644 --- a/doc/pub/week35/html/._week35-bs021.html +++ b/doc/pub/week35/html/._week35-bs021.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -457,7 +450,7 @@ ypredict = X_test 30
  • 31
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs022.html b/doc/pub/week35/html/._week35-bs022.html index 5e9c33046..d26b59621 100644 --- a/doc/pub/week35/html/._week35-bs022.html +++ b/doc/pub/week35/html/._week35-bs022.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -435,7 +428,7 @@ normally recommend using the latter functionality.
  • 31
  • 32
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs023.html b/doc/pub/week35/html/._week35-bs023.html index 006420cef..be5e111e5 100644 --- a/doc/pub/week35/html/._week35-bs023.html +++ b/doc/pub/week35/html/._week35-bs023.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -422,7 +415,7 @@ visualization.
  • 32
  • 33
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs024.html b/doc/pub/week35/html/._week35-bs024.html index 4db24cb5d..5c76e2186 100644 --- a/doc/pub/week35/html/._week35-bs024.html +++ b/doc/pub/week35/html/._week35-bs024.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -417,7 +410,7 @@ the features in a way to avoid such outlier values.
  • 33
  • 34
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs025.html b/doc/pub/week35/html/._week35-bs025.html index bb7962cc1..efe604346 100644 --- a/doc/pub/week35/html/._week35-bs025.html +++ b/doc/pub/week35/html/._week35-bs025.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -405,7 +398,7 @@ ensures that all features are exactly between \( 0 \) and \( 1 \). The
  • 34
  • 35
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs026.html b/doc/pub/week35/html/._week35-bs026.html index 4b85872e7..8a8d3546e 100644 --- a/doc/pub/week35/html/._week35-bs026.html +++ b/doc/pub/week35/html/._week35-bs026.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -419,7 +412,7 @@ techniques.
  • 35
  • 36
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs027.html b/doc/pub/week35/html/._week35-bs027.html index 09f71ff06..e7da5f800 100644 --- a/doc/pub/week35/html/._week35-bs027.html +++ b/doc/pub/week35/html/._week35-bs027.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -406,7 +399,7 @@ This ensures that each feature has zero mean and unit standard deviation. For d
  • 36
  • 37
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs028.html b/doc/pub/week35/html/._week35-bs028.html index 7b8f178a4..ecc29be05 100644 --- a/doc/pub/week35/html/._week35-bs028.html +++ b/doc/pub/week35/html/._week35-bs028.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -447,7 +440,7 @@ display(XPandas-Xscaled)
  • 37
  • 38
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs029.html b/doc/pub/week35/html/._week35-bs029.html index d848edb1d..2b41810cd 100644 --- a/doc/pub/week35/html/._week35-bs029.html +++ b/doc/pub/week35/html/._week35-bs029.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -406,7 +399,7 @@ $$
  • 38
  • 39
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs030.html b/doc/pub/week35/html/._week35-bs030.html index 567d1e5be..ebbb25cb1 100644 --- a/doc/pub/week35/html/._week35-bs030.html +++ b/doc/pub/week35/html/._week35-bs030.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -489,7 +482,7 @@ plt.show()
  • 39
  • 40
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs031.html b/doc/pub/week35/html/._week35-bs031.html index 479b6b551..3ee214377 100644 --- a/doc/pub/week35/html/._week35-bs031.html +++ b/doc/pub/week35/html/._week35-bs031.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -509,7 +502,7 @@ clf = skl.40
  • 41
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs032.html b/doc/pub/week35/html/._week35-bs032.html index eeee149e4..a0aa82370 100644 --- a/doc/pub/week35/html/._week35-bs032.html +++ b/doc/pub/week35/html/._week35-bs032.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -415,7 +408,7 @@ from the library Scikit-Learn (when not shrinking \( \beta_0 \)) for the
  • 41
  • 42
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs033.html b/doc/pub/week35/html/._week35-bs033.html index 93d8dbb9e..fb9d553e1 100644 --- a/doc/pub/week35/html/._week35-bs033.html +++ b/doc/pub/week35/html/._week35-bs033.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -423,7 +416,7 @@ This can clearly lead to problems in evaluating the cost/loss functions.
  • 42
  • 43
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs034.html b/doc/pub/week35/html/._week35-bs034.html index b6ae5b20c..22fc7e5b1 100644 --- a/doc/pub/week35/html/._week35-bs034.html +++ b/doc/pub/week35/html/._week35-bs034.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -435,7 +428,7 @@ y_pred = y_pred 43
  • 44
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs035.html b/doc/pub/week35/html/._week35-bs035.html index f731d14a6..f10422079 100644 --- a/doc/pub/week35/html/._week35-bs035.html +++ b/doc/pub/week35/html/._week35-bs035.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -427,7 +420,7 @@ $$
  • 44
  • 45
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs036.html b/doc/pub/week35/html/._week35-bs036.html index affc4ed09..715733cbf 100644 --- a/doc/pub/week35/html/._week35-bs036.html +++ b/doc/pub/week35/html/._week35-bs036.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -432,7 +425,7 @@ $$
  • 45
  • 46
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs037.html b/doc/pub/week35/html/._week35-bs037.html index 24f809a94..f39627a49 100644 --- a/doc/pub/week35/html/._week35-bs037.html +++ b/doc/pub/week35/html/._week35-bs037.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -411,7 +404,7 @@ $$
  • 46
  • 47
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs038.html b/doc/pub/week35/html/._week35-bs038.html index d92e38989..9fec3eeeb 100644 --- a/doc/pub/week35/html/._week35-bs038.html +++ b/doc/pub/week35/html/._week35-bs038.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -543,7 +536,7 @@ regression next week.
  • 47
  • 48
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs039.html b/doc/pub/week35/html/._week35-bs039.html index 4aca76378..82e4f122a 100644 --- a/doc/pub/week35/html/._week35-bs039.html +++ b/doc/pub/week35/html/._week35-bs039.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -366,33 +359,9 @@ MathJax.Hub.Config({

     

     

     

    - -

    The Boston housing data example

    + +

    Material for lecture Monday, August 26

    -

    The Boston housing -data set was originally a part of UCI Machine Learning Repository -and has been removed now. The data set is now included in Scikit-Learn's -library. There are 506 samples and 13 feature (predictor) variables -in this data set. The objective is to predict the value of prices of -the house using the features (predictors) listed here. -

    - -

    The features/predictors are

    -
      -
    1. CRIM: Per capita crime rate by town
    2. -
    3. ZN: Proportion of residential land zoned for lots over 25000 square feet
    4. -
    5. INDUS: Proportion of non-retail business acres per town
    6. -
    7. CHAS: Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)
    8. -
    9. NOX: Nitric oxide concentration (parts per 10 million)
    10. -
    11. RM: Average number of rooms per dwelling
    12. -
    13. AGE: Proportion of owner-occupied units built prior to 1940
    14. -
    15. DIS: Weighted distances to five Boston employment centers
    16. -
    17. RAD: Index of accessibility to radial highways
    18. -
    19. TAX: Full-value property tax rate per USD10000
    20. -
    21. B: \( 1000(Bk - 0.63)^2 \), where \( Bk \) is the proportion of [people of African American descent] by town
    22. -
    23. LSTAT: Percentage of lower status of the population
    24. -
    25. MEDV: Median value of owner-occupied homes in USD 1000s
    26. -

      @@ -418,7 +387,7 @@ the house using the features (predictors) listed here.
    • 48
    • 49
    • ...
    • -
    • 74
    • +
    • 72
    • »
    diff --git a/doc/pub/week35/html/._week35-bs040.html b/doc/pub/week35/html/._week35-bs040.html index 79084da05..8619d07cd 100644 --- a/doc/pub/week35/html/._week35-bs040.html +++ b/doc/pub/week35/html/._week35-bs040.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,346 +360,37 @@ MathJax.Hub.Config({

     

     

     

    -

    Housing data, the code

    -

    We start by importing the libraries

    +

    Mathematical Interpretation of Ordinary Least Squares

    - -
    -
    -
    -
    -
    -
    import numpy as np
    -import matplotlib.pyplot as plt 
    +

    What is presented here is a mathematical analysis of various regression algorithms (ordinary least squares, Ridge and Lasso Regression). The analysis is based on an important algorithm in linear algebra, the so-called Singular Value Decomposition (SVD).

    -import pandas as pd -import seaborn as sns -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +

    We have shown that in ordinary least squares the optimal parameters \( \beta \) are given by

    -

    and load the Boston Housing DataSet from Scikit-Learn

    +$$ +\hat{\boldsymbol{\beta}} = \left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}. +$$ +

    The hat over \( \boldsymbol{\beta} \) means we have the optimal parameters after minimization of the cost function.

    - -
    -
    -
    -
    -
    -
    from sklearn.datasets import load_boston
    +

    This means that our best model is defined as

    -boston_dataset = load_boston() +$$ +\tilde{\boldsymbol{y}}=\boldsymbol{X}\hat{\boldsymbol{\beta}} = \boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}. +$$ -# boston_dataset is a dictionary -# let's check what it contains -boston_dataset.keys() -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +

    We now define a matrix

    +$$ +\boldsymbol{A}=\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T. +$$ -

    Then we invoke Pandas

    - - -
    -
    -
    -
    -
    -
    boston = pd.DataFrame(boston_dataset.data, columns=boston_dataset.feature_names)
    -boston.head()
    -boston['MEDV'] = boston_dataset.target
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    and preprocess the data

    - - -
    -
    -
    -
    -
    -
    # check for missing values in all the columns
    -boston.isnull().sum()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    We can then visualize the data

    - - -
    -
    -
    -
    -
    -
    # set the size of the figure
    -sns.set(rc={'figure.figsize':(11.7,8.27)})
    -
    -# plot a histogram showing the distribution of the target values
    -sns.distplot(boston['MEDV'], bins=30)
    -plt.show()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    It is now useful to look at the correlation matrix

    - - -
    -
    -
    -
    -
    -
    # compute the pair wise correlation for all columns  
    -correlation_matrix = boston.corr().round(2)
    -# use the heatmap function from seaborn to plot the correlation matrix
    -# annot = True to print the values inside the square
    -sns.heatmap(data=correlation_matrix, annot=True)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    From the above coorelation plot we can see that MEDV is strongly correlated to LSTAT and RM. We see also that RAD and TAX are stronly correlated, but we don't include this in our features together to avoid multi-colinearity

    - - - -
    -
    -
    -
    -
    -
    plt.figure(figsize=(20, 5))
    -
    -features = ['LSTAT', 'RM']
    -target = boston['MEDV']
    -
    -for i, col in enumerate(features):
    -    plt.subplot(1, len(features) , i+1)
    -    x = boston[col]
    -    y = target
    -    plt.scatter(x, y, marker='o')
    -    plt.title(col)
    -    plt.xlabel(col)
    -    plt.ylabel('MEDV')
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Now we start training our model

    - - -
    -
    -
    -
    -
    -
    X = pd.DataFrame(np.c_[boston['LSTAT'], boston['RM']], columns = ['LSTAT','RM'])
    -Y = boston['MEDV']
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    We split the data into training and test sets

    - - - -
    -
    -
    -
    -
    -
    from sklearn.model_selection import train_test_split
    -
    -# splits the training and test data set in 80% : 20%
    -# assign random_state to any value.This ensures consistency.
    -X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state=5)
    -print(X_train.shape)
    -print(X_test.shape)
    -print(Y_train.shape)
    -print(Y_test.shape)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Then we use the linear regression functionality from Scikit-Learn

    - - -
    -
    -
    -
    -
    -
    from sklearn.linear_model import LinearRegression
    -from sklearn.metrics import mean_squared_error, r2_score
    -
    -lin_model = LinearRegression()
    -lin_model.fit(X_train, Y_train)
    -
    -# model evaluation for training set
    -
    -y_train_predict = lin_model.predict(X_train)
    -rmse = (np.sqrt(mean_squared_error(Y_train, y_train_predict)))
    -r2 = r2_score(Y_train, y_train_predict)
    -
    -print("The model performance for training set")
    -print("--------------------------------------")
    -print('RMSE is {}'.format(rmse))
    -print('R2 score is {}'.format(r2))
    -print("\n")
    -
    -# model evaluation for testing set
    -
    -y_test_predict = lin_model.predict(X_test)
    -# root mean square error of the model
    -rmse = (np.sqrt(mean_squared_error(Y_test, y_test_predict)))
    -
    -# r-squared score of the model
    -r2 = r2_score(Y_test, y_test_predict)
    -
    -print("The model performance for testing set")
    -print("--------------------------------------")
    -print('RMSE is {}'.format(rmse))
    -print('R2 score is {}'.format(r2))
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -
    -
    -
    -
    -
    -
    # plotting the y_test vs y_pred
    -# ideally should have been a straight line
    -plt.scatter(Y_test, y_test_predict)
    -plt.show()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +

    We can rewrite

    +$$ +\tilde{\boldsymbol{y}}=\boldsymbol{X}\hat{\boldsymbol{\beta}} = \boldsymbol{A}\boldsymbol{y}. +$$ +

    The matrix \( \boldsymbol{A} \) has the important property that \( \boldsymbol{A}^2=\boldsymbol{A} \). This is the definition of a projection matrix. +We can then interpret our optimal model \( \tilde{\boldsymbol{y}} \) as being represented by an orthogonal projection of \( \boldsymbol{y} \) onto a space defined by the column vectors of \( \boldsymbol{X} \). In our case here the matrix \( \boldsymbol{A} \) is a square matrix. If it is a general rectangular matrix we have an oblique projection matrix. +

    @@ -733,7 +417,7 @@ plt.show()

  • 49
  • 50
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs041.html b/doc/pub/week35/html/._week35-bs041.html index eb592e42f..bb6dcbe54 100644 --- a/doc/pub/week35/html/._week35-bs041.html +++ b/doc/pub/week35/html/._week35-bs041.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,7 +360,14 @@ MathJax.Hub.Config({

     

     

     

    -

    Material for lecture Monday, August 26

    +

    Residual Error

    + +

    We have defined the residual error as

    +$$ +\boldsymbol{\epsilon}=\boldsymbol{y}-\tilde{\boldsymbol{y}}=\left[\boldsymbol{I}-\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\right]\boldsymbol{y}. +$$ + +

    The residual errors are then the projections of \( \boldsymbol{y} \) onto the orthogonal component of the space defined by the column vectors of \( \boldsymbol{X} \).

    @@ -394,7 +394,7 @@ MathJax.Hub.Config({

  • 50
  • 51
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs042.html b/doc/pub/week35/html/._week35-bs042.html index 725bc4e94..33ad58458 100644 --- a/doc/pub/week35/html/._week35-bs042.html +++ b/doc/pub/week35/html/._week35-bs042.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,37 +360,25 @@ MathJax.Hub.Config({

     

     

     

    -

    Mathematical Interpretation of Ordinary Least Squares

    +

    Simple case

    -

    What is presented here is a mathematical analysis of various regression algorithms (ordinary least squares, Ridge and Lasso Regression). The analysis is based on an important algorithm in linear algebra, the so-called Singular Value Decomposition (SVD).

    - -

    We have shown that in ordinary least squares the optimal parameters \( \beta \) are given by

    +

    If the matrix \( \boldsymbol{X} \) is an orthogonal (or unitary in case of complex values) matrix, we have

    $$ -\hat{\boldsymbol{\beta}} = \left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}. +\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{X}\boldsymbol{X}^T = \boldsymbol{I}. $$ -

    The hat over \( \boldsymbol{\beta} \) means we have the optimal parameters after minimization of the cost function.

    - -

    This means that our best model is defined as

    - +

    In this case the matrix \( \boldsymbol{A} \) becomes

    $$ -\tilde{\boldsymbol{y}}=\boldsymbol{X}\hat{\boldsymbol{\beta}} = \boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}. +\boldsymbol{A}=\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T)=\boldsymbol{I}, $$ -

    We now define a matrix

    +

    and we have the obvious case

    $$ -\boldsymbol{A}=\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T. +\boldsymbol{\epsilon}=\boldsymbol{y}-\tilde{\boldsymbol{y}}=0. $$ -

    We can rewrite

    -$$ -\tilde{\boldsymbol{y}}=\boldsymbol{X}\hat{\boldsymbol{\beta}} = \boldsymbol{A}\boldsymbol{y}. -$$ - -

    The matrix \( \boldsymbol{A} \) has the important property that \( \boldsymbol{A}^2=\boldsymbol{A} \). This is the definition of a projection matrix. -We can then interpret our optimal model \( \tilde{\boldsymbol{y}} \) as being represented by an orthogonal projection of \( \boldsymbol{y} \) onto a space defined by the column vectors of \( \boldsymbol{X} \). In our case here the matrix \( \boldsymbol{A} \) is a square matrix. If it is a general rectangular matrix we have an oblique projection matrix. -

    +

    This serves also as a useful test of our codes.

    @@ -424,7 +405,7 @@ We can then interpret our optimal model \( \tilde{\boldsymbol{y}} \) as being re

  • 51
  • 52
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs043.html b/doc/pub/week35/html/._week35-bs043.html index c76d0b67d..9e163c18e 100644 --- a/doc/pub/week35/html/._week35-bs043.html +++ b/doc/pub/week35/html/._week35-bs043.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,14 +360,49 @@ MathJax.Hub.Config({

     

     

     

    -

    Residual Error

    +

    The singular value decomposition

    -

    We have defined the residual error as

    -$$ -\boldsymbol{\epsilon}=\boldsymbol{y}-\tilde{\boldsymbol{y}}=\left[\boldsymbol{I}-\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\right]\boldsymbol{y}. -$$ +
    +
    + + +

    The examples we have looked at so far are cases where we normally can +invert the matrix \( \boldsymbol{X}^T\boldsymbol{X} \). Using a polynomial expansion where we fit of various functions leads to +row vectors of the design matrix which are essentially orthogonal due +to the polynomial character of our model. Obtaining the inverse of the +design matrix is then often done via a so-called LU, QR or Cholesky +decomposition. +

    + +

    As we will also see in the first project, +this may +however not the be case in general and a standard matrix inversion +algorithm based on say LU, QR or Cholesky decomposition may lead to singularities. We will see examples of this below. +

    + +

    There is however a way to circumvent this problem and also +gain some insights about the ordinary least squares approach, and +later shrinkage methods like Ridge and Lasso regressions. +

    + +

    This is given by the Singular Value Decomposition (SVD) algorithm, +perhaps the most powerful linear algebra algorithm. The SVD provides +a numerically stable matrix decomposition that is used in a large +swath oc applications and the decomposition is always stable +numerically. +

    + +

    In machine learning it plays a central role in dealing with for +example design matrices that may be near singular or singular. +Furthermore, as we will see here, the singular values can be related +to the covariance matrix (and thereby the correlation matrix) and in +turn the variance of a given quantity. It plays also an important role +in the principal component analysis where high-dimensional data can be +reduced to the statistically relevant features. +

    +
    +
    -

    The residual errors are then the projections of \( \boldsymbol{y} \) onto the orthogonal component of the space defined by the column vectors of \( \boldsymbol{X} \).

    @@ -401,7 +429,7 @@ $$

  • 52
  • 53
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs044.html b/doc/pub/week35/html/._week35-bs044.html index dd56442ff..01e064f50 100644 --- a/doc/pub/week35/html/._week35-bs044.html +++ b/doc/pub/week35/html/._week35-bs044.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,25 +360,54 @@ MathJax.Hub.Config({

     

     

     

    -

    Simple case

    - -

    If the matrix \( \boldsymbol{X} \) is an orthogonal (or unitary in case of complex values) matrix, we have

    +

    Linear Regression Problems

    +

    One of the typical problems we encounter with linear regression, in particular +when the matrix \( \boldsymbol{X} \) (our so-called design matrix) is high-dimensional, +are problems with near singular or singular matrices. The column vectors of \( \boldsymbol{X} \) +may be linearly dependent, normally referred to as super-collinearity. +This means that the matrix may be rank deficient and it is basically impossible to +to model the data using linear regression. As an example, consider the matrix +

    $$ -\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{X}\boldsymbol{X}^T = \boldsymbol{I}. +\begin{align*} +\mathbf{X} & = \left[ +\begin{array}{rrr} +1 & -1 & 2 +\\ +1 & 0 & 1 +\\ +1 & 2 & -1 +\\ +1 & 1 & 0 +\end{array} \right] +\end{align*} $$ -

    In this case the matrix \( \boldsymbol{A} \) becomes

    +

    The columns of \( \boldsymbol{X} \) are linearly dependent. We see this easily since the +the first column is the row-wise sum of the other two columns. The rank (more correct, +the column rank) of a matrix is the dimension of the space spanned by the +column vectors. Hence, the rank of \( \mathbf{X} \) is equal to the number +of linearly independent columns. In this particular case the matrix has rank 2. +

    + +

    Super-collinearity of an \( (n \times p) \)-dimensional design matrix \( \mathbf{X} \) implies +that the inverse of the matrix \( \boldsymbol{X}^T\boldsymbol{X} \) (the matrix we need to invert to solve the linear regression equations) is non-invertible. If we have a square matrix that does not have an inverse, we say this matrix singular. The example here demonstrates this +

    $$ -\boldsymbol{A}=\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T)=\boldsymbol{I}, +\begin{align*} +\boldsymbol{X} & = \left[ +\begin{array}{rr} +1 & -1 +\\ +1 & -1 +\end{array} \right]. +\end{align*} $$ -

    and we have the obvious case

    -$$ -\boldsymbol{\epsilon}=\boldsymbol{y}-\tilde{\boldsymbol{y}}=0. -$$ - -

    This serves also as a useful test of our codes.

    +

    We see easily that \( \mbox{det}(\boldsymbol{X}) = x_{11} x_{22} - x_{12} x_{21} = 1 \times (-1) - 1 \times (-1) = 0 \). Hence, \( \mathbf{X} \) is singular and its inverse is undefined. +This is equivalent to saying that the matrix \( \boldsymbol{X} \) has at least an eigenvalue which is zero. +

    @@ -412,7 +434,7 @@ $$

  • 53
  • 54
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs045.html b/doc/pub/week35/html/._week35-bs045.html index 427d19ab5..c9d29b265 100644 --- a/doc/pub/week35/html/._week35-bs045.html +++ b/doc/pub/week35/html/._week35-bs045.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,49 +360,29 @@ MathJax.Hub.Config({

     

     

     

    -

    The singular value decomposition

    +

    Fixing the singularity

    -
    -
    - +

    If our design matrix \( \boldsymbol{X} \) which enters the linear regression problem

    +$$ +\begin{align} +\boldsymbol{\beta} & = (\boldsymbol{X}^{T} \boldsymbol{X})^{-1} \boldsymbol{X}^{T} \boldsymbol{y}, +\tag{1} +\end{align} +$$ -

    The examples we have looked at so far are cases where we normally can -invert the matrix \( \boldsymbol{X}^T\boldsymbol{X} \). Using a polynomial expansion where we fit of various functions leads to -row vectors of the design matrix which are essentially orthogonal due -to the polynomial character of our model. Obtaining the inverse of the -design matrix is then often done via a so-called LU, QR or Cholesky -decomposition. +

    has linearly dependent column vectors, we will not be able to compute the inverse +of \( \boldsymbol{X}^T\boldsymbol{X} \) and we cannot find the parameters (estimators) \( \beta_i \). +The estimators are only well-defined if \( (\boldsymbol{X}^{T}\boldsymbol{X})^{-1} \) exits. +This is more likely to happen when the matrix \( \boldsymbol{X} \) is high-dimensional. In this case it is likely to encounter a situation where +the regression parameters \( \beta_i \) cannot be estimated.

    -

    As we will also see in the first project, -this may -however not the be case in general and a standard matrix inversion -algorithm based on say LU, QR or Cholesky decomposition may lead to singularities. We will see examples of this below. -

    - -

    There is however a way to circumvent this problem and also -gain some insights about the ordinary least squares approach, and -later shrinkage methods like Ridge and Lasso regressions. -

    - -

    This is given by the Singular Value Decomposition (SVD) algorithm, -perhaps the most powerful linear algebra algorithm. The SVD provides -a numerically stable matrix decomposition that is used in a large -swath oc applications and the decomposition is always stable -numerically. -

    - -

    In machine learning it plays a central role in dealing with for -example design matrices that may be near singular or singular. -Furthermore, as we will see here, the singular values can be related -to the covariance matrix (and thereby the correlation matrix) and in -turn the variance of a given quantity. It plays also an important role -in the principal component analysis where high-dimensional data can be -reduced to the statistically relevant features. -

    -
    -
    +

    A cheap ad hoc approach is simply to add a small diagonal component to the matrix to invert, that is we change

    +$$ +\boldsymbol{X}^{T} \boldsymbol{X} \rightarrow \boldsymbol{X}^{T} \boldsymbol{X}+\lambda \boldsymbol{I}, +$$ +

    where \( \boldsymbol{I} \) is the identity matrix. When we discuss Ridge regression this is actually what we end up evaluating. The parameter \( \lambda \) is called a hyperparameter. More about this later.

    @@ -436,7 +409,7 @@ reduced to the statistically relevant features.

  • 54
  • 55
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs046.html b/doc/pub/week35/html/._week35-bs046.html index 73141d530..b473f8311 100644 --- a/doc/pub/week35/html/._week35-bs046.html +++ b/doc/pub/week35/html/._week35-bs046.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,53 +360,40 @@ MathJax.Hub.Config({

     

     

     

    -

    Linear Regression Problems

    +

    Basic math of the SVD

    -

    One of the typical problems we encounter with linear regression, in particular -when the matrix \( \boldsymbol{X} \) (our so-called design matrix) is high-dimensional, -are problems with near singular or singular matrices. The column vectors of \( \boldsymbol{X} \) -may be linearly dependent, normally referred to as super-collinearity. -This means that the matrix may be rank deficient and it is basically impossible to -to model the data using linear regression. As an example, consider the matrix -

    -$$ -\begin{align*} -\mathbf{X} & = \left[ -\begin{array}{rrr} -1 & -1 & 2 -\\ -1 & 0 & 1 -\\ -1 & 2 & -1 -\\ -1 & 1 & 0 -\end{array} \right] -\end{align*} -$$ - -

    The columns of \( \boldsymbol{X} \) are linearly dependent. We see this easily since the -the first column is the row-wise sum of the other two columns. The rank (more correct, -the column rank) of a matrix is the dimension of the space spanned by the -column vectors. Hence, the rank of \( \mathbf{X} \) is equal to the number -of linearly independent columns. In this particular case the matrix has rank 2. +

    From standard linear algebra we know that a square matrix \( \boldsymbol{X} \) can be diagonalized if and only it is +a so-called normal matrix, that is if \( \boldsymbol{X}\in {\mathbb{R}}^{n\times n} \) +we have \( \boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{X}^T\boldsymbol{X} \) or if \( \boldsymbol{X}\in {\mathbb{C}}^{n\times n} \) we have \( \boldsymbol{X}\boldsymbol{X}^{\dagger}=\boldsymbol{X}^{\dagger}\boldsymbol{X} \). +The matrix has then a set of eigenpairs

    -

    Super-collinearity of an \( (n \times p) \)-dimensional design matrix \( \mathbf{X} \) implies -that the inverse of the matrix \( \boldsymbol{X}^T\boldsymbol{X} \) (the matrix we need to invert to solve the linear regression equations) is non-invertible. If we have a square matrix that does not have an inverse, we say this matrix singular. The example here demonstrates this -

    $$ -\begin{align*} -\boldsymbol{X} & = \left[ -\begin{array}{rr} -1 & -1 -\\ -1 & -1 -\end{array} \right]. -\end{align*} +(\lambda_1,\boldsymbol{u}_1),\dots, (\lambda_n,\boldsymbol{u}_n), $$ -

    We see easily that \( \mbox{det}(\boldsymbol{X}) = x_{11} x_{22} - x_{12} x_{21} = 1 \times (-1) - 1 \times (-1) = 0 \). Hence, \( \mathbf{X} \) is singular and its inverse is undefined. -This is equivalent to saying that the matrix \( \boldsymbol{X} \) has at least an eigenvalue which is zero. +

    and the eigenvalues are given by the diagonal matrix

    +$$ +\boldsymbol{\Sigma}=\mathrm{Diag}(\lambda_1, \dots,\lambda_n). +$$ + +

    The matrix \( \boldsymbol{X} \) can be written in terms of an orthogonal/unitary transformation \( \boldsymbol{U} \)

    +$$ +\boldsymbol{X} = \boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T, +$$ + +

    with \( \boldsymbol{U}\boldsymbol{U}^T=\boldsymbol{I} \) or \( \boldsymbol{U}\boldsymbol{U}^{\dagger}=\boldsymbol{I} \).

    + +

    Not all square matrices are diagonalizable. A matrix like the one discussed above

    +$$ +\boldsymbol{X} = \begin{bmatrix} +1& -1 \\ +1& -1\\ +\end{bmatrix} +$$ + +

    is not diagonalizable, it is a so-called defective matrix. It is easy to see that the condition +\( \boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{X}^T\boldsymbol{X} \) is not fulfilled.

    @@ -441,7 +421,7 @@ This is equivalent to saying that the matrix \( \boldsymbol{X} \) has at least a

  • 55
  • 56
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs047.html b/doc/pub/week35/html/._week35-bs047.html index e1935f89f..61d693ebe 100644 --- a/doc/pub/week35/html/._week35-bs047.html +++ b/doc/pub/week35/html/._week35-bs047.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,29 +360,52 @@ MathJax.Hub.Config({

     

     

     

    -

    Fixing the singularity

    +

    The SVD, a Fantastic Algorithm

    -

    If our design matrix \( \boldsymbol{X} \) which enters the linear regression problem

    -$$ -\begin{align} -\boldsymbol{\beta} & = (\boldsymbol{X}^{T} \boldsymbol{X})^{-1} \boldsymbol{X}^{T} \boldsymbol{y}, -\tag{1} -\end{align} -$$ - -

    has linearly dependent column vectors, we will not be able to compute the inverse -of \( \boldsymbol{X}^T\boldsymbol{X} \) and we cannot find the parameters (estimators) \( \beta_i \). -The estimators are only well-defined if \( (\boldsymbol{X}^{T}\boldsymbol{X})^{-1} \) exits. -This is more likely to happen when the matrix \( \boldsymbol{X} \) is high-dimensional. In this case it is likely to encounter a situation where -the regression parameters \( \beta_i \) cannot be estimated. +

    However, and this is the strength of the SVD algorithm, any general +matrix \( \boldsymbol{X} \) can be decomposed in terms of a diagonal matrix and +two orthogonal/unitary matrices. The Singular Value Decompostion +(SVD) theorem +states that a general \( m\times n \) matrix \( \boldsymbol{X} \) can be written in +terms of a diagonal matrix \( \boldsymbol{\Sigma} \) of dimensionality \( m\times n \) +and two orthognal matrices \( \boldsymbol{U} \) and \( \boldsymbol{V} \), where the first has +dimensionality \( m \times m \) and the last dimensionality \( n\times n \). +We have then

    -

    A cheap ad hoc approach is simply to add a small diagonal component to the matrix to invert, that is we change

    -$$ -\boldsymbol{X}^{T} \boldsymbol{X} \rightarrow \boldsymbol{X}^{T} \boldsymbol{X}+\lambda \boldsymbol{I}, +$$ +\boldsymbol{X} = \boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T $$ -

    where \( \boldsymbol{I} \) is the identity matrix. When we discuss Ridge regression this is actually what we end up evaluating. The parameter \( \lambda \) is called a hyperparameter. More about this later.

    +

    As an example, the above defective matrix can be decomposed as

    + +$$ +\boldsymbol{X} = \frac{1}{\sqrt{2}}\begin{bmatrix} 1& 1 \\ 1& -1\\ \end{bmatrix} \begin{bmatrix} 2& 0 \\ 0& 0\\ \end{bmatrix} \frac{1}{\sqrt{2}}\begin{bmatrix} 1& -1 \\ 1& 1\\ \end{bmatrix}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T, +$$ + +

    with eigenvalues \( \sigma_1=2 \) and \( \sigma_2=0 \). +The SVD exits always! +

    + +

    The SVD +decomposition (singular values) gives eigenvalues +\( \sigma_i\geq\sigma_{i+1} \) for all \( i \) and for dimensions larger than \( i=p \), the +eigenvalues (singular values) are zero. +

    + +

    In the general case, where our design matrix \( \boldsymbol{X} \) has dimension +\( n\times p \), the matrix is thus decomposed into an \( n\times n \) +orthogonal matrix \( \boldsymbol{U} \), a \( p\times p \) orthogonal matrix \( \boldsymbol{V} \) +and a diagonal matrix \( \boldsymbol{\Sigma} \) with \( r=\mathrm{min}(n,p) \) +singular values \( \sigma_i\geq 0 \) on the main diagonal and zeros filling +the rest of the matrix. There are at most \( p \) singular values +assuming that \( n > p \). In our regression examples for the nuclear +masses and the equation of state this is indeed the case, while for +the Ising model we have \( p > n \). These are often cases that lead to +near singular or singular matrices. +

    + +

    The columns of \( \boldsymbol{U} \) are called the left singular vectors while the columns of \( \boldsymbol{V} \) are the right singular vectors.

    @@ -416,7 +432,7 @@ $$

  • 56
  • 57
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs048.html b/doc/pub/week35/html/._week35-bs048.html index 14dc0869c..8908d83be 100644 --- a/doc/pub/week35/html/._week35-bs048.html +++ b/doc/pub/week35/html/._week35-bs048.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,40 +360,26 @@ MathJax.Hub.Config({

     

     

     

    -

    Basic math of the SVD

    +

    Economy-size SVD

    -

    From standard linear algebra we know that a square matrix \( \boldsymbol{X} \) can be diagonalized if and only it is -a so-called normal matrix, that is if \( \boldsymbol{X}\in {\mathbb{R}}^{n\times n} \) -we have \( \boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{X}^T\boldsymbol{X} \) or if \( \boldsymbol{X}\in {\mathbb{C}}^{n\times n} \) we have \( \boldsymbol{X}\boldsymbol{X}^{\dagger}=\boldsymbol{X}^{\dagger}\boldsymbol{X} \). -The matrix has then a set of eigenpairs +

    If we assume that \( n > p \), then our matrix \( \boldsymbol{U} \) has dimension \( n +\times n \). The last \( n-p \) columns of \( \boldsymbol{U} \) become however +irrelevant in our calculations since they are multiplied with the +zeros in \( \boldsymbol{\Sigma} \).

    -$$ -(\lambda_1,\boldsymbol{u}_1),\dots, (\lambda_n,\boldsymbol{u}_n), -$$ +

    The economy-size decomposition removes extra rows or columns of zeros +from the diagonal matrix of singular values, \( \boldsymbol{\Sigma} \), along with the columns +in either \( \boldsymbol{U} \) or \( \boldsymbol{V} \) that multiply those zeros in the expression. +Removing these zeros and columns can improve execution time +and reduce storage requirements without compromising the accuracy of +the decomposition. +

    -

    and the eigenvalues are given by the diagonal matrix

    -$$ -\boldsymbol{\Sigma}=\mathrm{Diag}(\lambda_1, \dots,\lambda_n). -$$ - -

    The matrix \( \boldsymbol{X} \) can be written in terms of an orthogonal/unitary transformation \( \boldsymbol{U} \)

    -$$ -\boldsymbol{X} = \boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T, -$$ - -

    with \( \boldsymbol{U}\boldsymbol{U}^T=\boldsymbol{I} \) or \( \boldsymbol{U}\boldsymbol{U}^{\dagger}=\boldsymbol{I} \).

    - -

    Not all square matrices are diagonalizable. A matrix like the one discussed above

    -$$ -\boldsymbol{X} = \begin{bmatrix} -1& -1 \\ -1& -1\\ -\end{bmatrix} -$$ - -

    is not diagonalizable, it is a so-called defective matrix. It is easy to see that the condition -\( \boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{X}^T\boldsymbol{X} \) is not fulfilled. +

    If \( n > p \), we keep only the first \( p \) columns of \( \boldsymbol{U} \) and \( \boldsymbol{\Sigma} \) has dimension \( p\times p \). +If \( p > n \), then only the first \( n \) columns of \( \boldsymbol{V} \) are computed and \( \boldsymbol{\Sigma} \) has dimension \( n\times n \). +The \( n=p \) case is obvious, we retain the full SVD. +In general the economy-size SVD leads to less FLOPS and still conserving the desired accuracy.

    @@ -428,7 +407,7 @@ $$

  • 57
  • 58
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs049.html b/doc/pub/week35/html/._week35-bs049.html index e0273c51f..2f1c7df67 100644 --- a/doc/pub/week35/html/._week35-bs049.html +++ b/doc/pub/week35/html/._week35-bs049.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,53 +360,69 @@ MathJax.Hub.Config({

     

     

     

    -

    The SVD, a Fantastic Algorithm

    +

    Codes for the SVD

    -

    However, and this is the strength of the SVD algorithm, any general -matrix \( \boldsymbol{X} \) can be decomposed in terms of a diagonal matrix and -two orthogonal/unitary matrices. The Singular Value Decompostion -(SVD) theorem -states that a general \( m\times n \) matrix \( \boldsymbol{X} \) can be written in -terms of a diagonal matrix \( \boldsymbol{\Sigma} \) of dimensionality \( m\times n \) -and two orthognal matrices \( \boldsymbol{U} \) and \( \boldsymbol{V} \), where the first has -dimensionality \( m \times m \) and the last dimensionality \( n\times n \). -We have then + + +

    +
    +
    +
    +
    +
    import numpy as np
    +# SVD inversion
    +def SVD(A):
    +    ''' Takes as input a numpy matrix A and returns inv(A) based on singular value decomposition (SVD).
    +    SVD is numerically more stable than the inversion algorithms provided by
    +    numpy and scipy.linalg at the cost of being slower.
    +    '''
    +    U, S, VT = np.linalg.svd(A,full_matrices=True)
    +    print('test U')
    +    print( (np.transpose(U) @ U - U @np.transpose(U)))
    +    print('test VT')
    +    print( (np.transpose(VT) @ VT - VT @np.transpose(VT)))
    +    print(U)
    +    print(S)
    +    print(VT)
    +
    +    D = np.zeros((len(U),len(VT)))
    +    for i in range(0,len(VT)):
    +        D[i,i]=S[i]
    +    return U @ D @ VT
    +
    +
    +X = np.array([ [1.0,-1.0], [1.0,-1.0]])
    +#X = np.array([[1, 2], [3, 4], [5, 6]])
    +
    +print(X)
    +C = SVD(X)
    +# Print the difference between the original matrix and the SVD one
    +print(C-X)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    The matrix \( \boldsymbol{X} \) has columns that are linearly dependent. The first +column is the row-wise sum of the other two columns. The rank of a +matrix (the column rank) is the dimension of space spanned by the +column vectors. The rank of the matrix is the number of linearly +independent columns, in this case just \( 2 \). We see this from the +singular values when running the above code. Running the standard +inversion algorithm for matrix inversion with \( \boldsymbol{X}^T\boldsymbol{X} \) results +in the program terminating due to a singular matrix.

    -$$ -\boldsymbol{X} = \boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T -$$ - -

    As an example, the above defective matrix can be decomposed as

    - -$$ -\boldsymbol{X} = \frac{1}{\sqrt{2}}\begin{bmatrix} 1& 1 \\ 1& -1\\ \end{bmatrix} \begin{bmatrix} 2& 0 \\ 0& 0\\ \end{bmatrix} \frac{1}{\sqrt{2}}\begin{bmatrix} 1& -1 \\ 1& 1\\ \end{bmatrix}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T, -$$ - -

    with eigenvalues \( \sigma_1=2 \) and \( \sigma_2=0 \). -The SVD exits always! -

    - -

    The SVD -decomposition (singular values) gives eigenvalues -\( \sigma_i\geq\sigma_{i+1} \) for all \( i \) and for dimensions larger than \( i=p \), the -eigenvalues (singular values) are zero. -

    - -

    In the general case, where our design matrix \( \boldsymbol{X} \) has dimension -\( n\times p \), the matrix is thus decomposed into an \( n\times n \) -orthogonal matrix \( \boldsymbol{U} \), a \( p\times p \) orthogonal matrix \( \boldsymbol{V} \) -and a diagonal matrix \( \boldsymbol{\Sigma} \) with \( r=\mathrm{min}(n,p) \) -singular values \( \sigma_i\geq 0 \) on the main diagonal and zeros filling -the rest of the matrix. There are at most \( p \) singular values -assuming that \( n > p \). In our regression examples for the nuclear -masses and the equation of state this is indeed the case, while for -the Ising model we have \( p > n \). These are often cases that lead to -near singular or singular matrices. -

    - -

    The columns of \( \boldsymbol{U} \) are called the left singular vectors while the columns of \( \boldsymbol{V} \) are the right singular vectors.

    -

      @@ -439,7 +448,7 @@ near singular or singular matrices.
    • 58
    • 59
    • ...
    • -
    • 74
    • +
    • 72
    • »
    diff --git a/doc/pub/week35/html/._week35-bs050.html b/doc/pub/week35/html/._week35-bs050.html index 973ba3d53..3a252eb47 100644 --- a/doc/pub/week35/html/._week35-bs050.html +++ b/doc/pub/week35/html/._week35-bs050.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,26 +360,22 @@ MathJax.Hub.Config({

     

     

     

    -

    Economy-size SVD

    +

    Note about SVD Calculations

    -

    If we assume that \( n > p \), then our matrix \( \boldsymbol{U} \) has dimension \( n -\times n \). The last \( n-p \) columns of \( \boldsymbol{U} \) become however -irrelevant in our calculations since they are multiplied with the -zeros in \( \boldsymbol{\Sigma} \). +

    The \( U \), \( S \), and \( V \) matrices returned from the svd() function +cannot be multiplied directly.

    -

    The economy-size decomposition removes extra rows or columns of zeros -from the diagonal matrix of singular values, \( \boldsymbol{\Sigma} \), along with the columns -in either \( \boldsymbol{U} \) or \( \boldsymbol{V} \) that multiply those zeros in the expression. -Removing these zeros and columns can improve execution time -and reduce storage requirements without compromising the accuracy of -the decomposition. +

    As you can see from the code, the \( S \) vector must be converted into a +diagonal matrix. This may cause a problem as the size of the matrices +do not fit the rules of matrix multiplication, where the number of +columns in a matrix must match the number of rows in the subsequent +matrix.

    -

    If \( n > p \), we keep only the first \( p \) columns of \( \boldsymbol{U} \) and \( \boldsymbol{\Sigma} \) has dimension \( p\times p \). -If \( p > n \), then only the first \( n \) columns of \( \boldsymbol{V} \) are computed and \( \boldsymbol{\Sigma} \) has dimension \( n\times n \). -The \( n=p \) case is obvious, we retain the full SVD. -In general the economy-size SVD leads to less FLOPS and still conserving the desired accuracy. +

    If you wish to include the zero singular values, you will need to +resize the matrices and set up a diagonal matrix as done in the above +example

    @@ -414,7 +403,7 @@ In general the economy-size SVD leads to less FLOPS and still conserving the des

  • 59
  • 60
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs051.html b/doc/pub/week35/html/._week35-bs051.html index cb5e50c89..7567d7363 100644 --- a/doc/pub/week35/html/._week35-bs051.html +++ b/doc/pub/week35/html/._week35-bs051.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,68 +360,38 @@ MathJax.Hub.Config({

     

     

     

    -

    Codes for the SVD

    +

    Mathematics of the SVD and implications

    +

    Let us take a closer look at the mathematics of the SVD and the various implications for machine learning studies.

    - -
    -
    -
    -
    -
    -
    import numpy as np
    -# SVD inversion
    -def SVD(A):
    -    ''' Takes as input a numpy matrix A and returns inv(A) based on singular value decomposition (SVD).
    -    SVD is numerically more stable than the inversion algorithms provided by
    -    numpy and scipy.linalg at the cost of being slower.
    -    '''
    -    U, S, VT = np.linalg.svd(A,full_matrices=True)
    -    print('test U')
    -    print( (np.transpose(U) @ U - U @np.transpose(U)))
    -    print('test VT')
    -    print( (np.transpose(VT) @ VT - VT @np.transpose(VT)))
    -    print(U)
    -    print(S)
    -    print(VT)
    +

    Our starting point is our design matrix \( \boldsymbol{X} \) of dimension \( n\times p \)

    +$$ +\boldsymbol{X}=\begin{bmatrix} +x_{0,0} & x_{0,1} & x_{0,2}& \dots & \dots x_{0,p-1}\\ +x_{1,0} & x_{1,1} & x_{1,2}& \dots & \dots x_{1,p-1}\\ +x_{2,0} & x_{2,1} & x_{2,2}& \dots & \dots x_{2,p-1}\\ +\dots & \dots & \dots & \dots \dots & \dots \\ +x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \dots & \dots x_{n-2,p-1}\\ +x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \dots & \dots x_{n-1,p-1}\\ +\end{bmatrix}. +$$ - D = np.zeros((len(U),len(VT))) - for i in range(0,len(VT)): - D[i,i]=S[i] - return U @ D @ VT +

    We can SVD decompose our matrix as

    +$$ +\boldsymbol{X}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T, +$$ +

    where \( \boldsymbol{U} \) is an orthogonal matrix of dimension \( n\times n \), meaning that \( \boldsymbol{U}\boldsymbol{U}^T=\boldsymbol{U}^T\boldsymbol{U}=\boldsymbol{I}_n \). Here \( \boldsymbol{I}_n \) is the unit matrix of dimension \( n \times n \).

    -X = np.array([ [1.0,-1.0], [1.0,-1.0]]) -#X = np.array([[1, 2], [3, 4], [5, 6]]) +

    Similarly, \( \boldsymbol{V} \) is an orthogonal matrix of dimension \( p\times p \), meaning that \( \boldsymbol{V}\boldsymbol{V}^T=\boldsymbol{V}^T\boldsymbol{V}=\boldsymbol{I}_p \). Here \( \boldsymbol{I}_n \) is the unit matrix of dimension \( p \times p \).

    -print(X) -C = SVD(X) -# Print the difference between the original matrix and the SVD one -print(C-X) -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +

    Finally \( \boldsymbol{\Sigma} \) contains the singular values \( \sigma_i \). This matrix has dimension \( n\times p \) and the singular values \( \sigma_i \) are all positive. The non-zero values are ordered in descending order, that is

    -

    The matrix \( \boldsymbol{X} \) has columns that are linearly dependent. The first -column is the row-wise sum of the other two columns. The rank of a -matrix (the column rank) is the dimension of space spanned by the -column vectors. The rank of the matrix is the number of linearly -independent columns, in this case just \( 2 \). We see this from the -singular values when running the above code. Running the standard -inversion algorithm for matrix inversion with \( \boldsymbol{X}^T\boldsymbol{X} \) results -in the program terminating due to a singular matrix. -

    +$$ +\sigma_0 > \sigma_1 > \sigma_2 > \dots > \sigma_{p-1} > 0. +$$ + +

    All values beyond \( p-1 \) are all zero.

    @@ -455,7 +418,7 @@ in the program terminating due to a singular matrix.

  • 60
  • 61
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs052.html b/doc/pub/week35/html/._week35-bs052.html index 6cde61b45..1123583c0 100644 --- a/doc/pub/week35/html/._week35-bs052.html +++ b/doc/pub/week35/html/._week35-bs052.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,22 +360,61 @@ MathJax.Hub.Config({

     

     

     

    -

    Note about SVD Calculations

    +

    Example Matrix

    -

    The \( U \), \( S \), and \( V \) matrices returned from the svd() function -cannot be multiplied directly. -

    +

    As an example, consider the following \( 3\times 2 \) example for the matrix \( \boldsymbol{\Sigma} \)

    -

    As you can see from the code, the \( S \) vector must be converted into a -diagonal matrix. This may cause a problem as the size of the matrices -do not fit the rules of matrix multiplication, where the number of -columns in a matrix must match the number of rows in the subsequent -matrix. -

    +$$ +\boldsymbol{\Sigma}= +\begin{bmatrix} +2& 0 \\ +0 & 1 \\ +0 & 0 \\ +\end{bmatrix} +$$ -

    If you wish to include the zero singular values, you will need to -resize the matrices and set up a diagonal matrix as done in the above -example +

    The singular values are \( \sigma_0=2 \) and \( \sigma_1=1 \). It is common to rewrite the matrix \( \boldsymbol{\Sigma} \) as

    + +$$ +\boldsymbol{\Sigma}= +\begin{bmatrix} +\boldsymbol{\tilde{\Sigma}}\\ +\boldsymbol{0}\\ +\end{bmatrix}, +$$ + +

    where

    +$$ +\boldsymbol{\tilde{\Sigma}}= +\begin{bmatrix} +2& 0 \\ +0 & 1 \\ +\end{bmatrix}, +$$ + +

    contains only the singular values. Note also (and we will use this below) that

    + +$$ +\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}= +\begin{bmatrix} +4& 0 \\ +0 & 1 \\ +\end{bmatrix}, +$$ + +

    which is a \( 2\times 2 \) matrix while

    +$$ +\boldsymbol{\Sigma}\boldsymbol{\Sigma}^T= +\begin{bmatrix} +4& 0 & 0\\ +0 & 1 & 0\\ +0 & 0 & 0\\ +\end{bmatrix}, +$$ + +

    is a \( 3\times 3 \) matrix. The last row and column of this last matrix +contain only zeros. This will have important consequences for our SVD +decomposition of the design matrix.

    @@ -410,7 +442,7 @@ example

  • 61
  • 62
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs053.html b/doc/pub/week35/html/._week35-bs053.html index 27aae33ce..4734db895 100644 --- a/doc/pub/week35/html/._week35-bs053.html +++ b/doc/pub/week35/html/._week35-bs053.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,38 +360,48 @@ MathJax.Hub.Config({

     

     

     

    -

    Mathematics of the SVD and implications

    +

    Setting up the Matrix to be inverted

    -

    Let us take a closer look at the mathematics of the SVD and the various implications for machine learning studies.

    - -

    Our starting point is our design matrix \( \boldsymbol{X} \) of dimension \( n\times p \)

    -$$ -\boldsymbol{X}=\begin{bmatrix} -x_{0,0} & x_{0,1} & x_{0,2}& \dots & \dots x_{0,p-1}\\ -x_{1,0} & x_{1,1} & x_{1,2}& \dots & \dots x_{1,p-1}\\ -x_{2,0} & x_{2,1} & x_{2,2}& \dots & \dots x_{2,p-1}\\ -\dots & \dots & \dots & \dots \dots & \dots \\ -x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \dots & \dots x_{n-2,p-1}\\ -x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \dots & \dots x_{n-1,p-1}\\ -\end{bmatrix}. -$$ - -

    We can SVD decompose our matrix as

    -$$ -\boldsymbol{X}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T, -$$ - -

    where \( \boldsymbol{U} \) is an orthogonal matrix of dimension \( n\times n \), meaning that \( \boldsymbol{U}\boldsymbol{U}^T=\boldsymbol{U}^T\boldsymbol{U}=\boldsymbol{I}_n \). Here \( \boldsymbol{I}_n \) is the unit matrix of dimension \( n \times n \).

    - -

    Similarly, \( \boldsymbol{V} \) is an orthogonal matrix of dimension \( p\times p \), meaning that \( \boldsymbol{V}\boldsymbol{V}^T=\boldsymbol{V}^T\boldsymbol{V}=\boldsymbol{I}_p \). Here \( \boldsymbol{I}_n \) is the unit matrix of dimension \( p \times p \).

    - -

    Finally \( \boldsymbol{\Sigma} \) contains the singular values \( \sigma_i \). This matrix has dimension \( n\times p \) and the singular values \( \sigma_i \) are all positive. The non-zero values are ordered in descending order, that is

    +

    The matrix that may cause problems for us is \( \boldsymbol{X}^T\boldsymbol{X} \). Using the SVD we can rewrite this matrix as

    $$ -\sigma_0 > \sigma_1 > \sigma_2 > \dots > \sigma_{p-1} > 0. +\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T, $$ -

    All values beyond \( p-1 \) are all zero.

    +

    and using the orthogonality of the matrix \( \boldsymbol{U} \) we have

    + +$$ +\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{V}^T. +$$ + +

    We define \( \boldsymbol{\Sigma}^T\boldsymbol{\Sigma}=\tilde{\boldsymbol{\Sigma}}^2 \) which is a diagonal matrix containing only the singular values squared. It has dimensionality \( p \times p \).

    + +

    We can now insert the result for the matrix \( \boldsymbol{X}^T\boldsymbol{X} \) into our equation for ordinary least squares where

    + +$$ +\tilde{y}_{\mathrm{OLS}}=\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}, +$$ + +

    and using our SVD decomposition of \( \boldsymbol{X} \) we have

    + +$$ +\tilde{y}_{\mathrm{OLS}}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T\left(\boldsymbol{V}\tilde{\boldsymbol{\Sigma}}^{2}(\boldsymbol{V}^T\right)^{-1}\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{y}, +$$ + +

    which gives us, using the orthogonality of the matrix \( \boldsymbol{V} \),

    + +$$ +\tilde{y}_{\mathrm{OLS}}=\boldsymbol{U}\boldsymbol{U}^T\boldsymbol{y}=\sum_{i=0}^{p-1}\boldsymbol{u}_i\boldsymbol{u}^T_i\boldsymbol{y}, +$$ + +

    It means that the ordinary least square model (with the optimal +parameters) \( \boldsymbol{\tilde{y}} \), corresponds to an orthogonal +transformation of the output (or target) vector \( \boldsymbol{y} \) by the +vectors of the matrix \( \boldsymbol{U} \). Note that the summation ends at +\( p-1 \), that is \( \boldsymbol{\tilde{y}}\ne \boldsymbol{y} \). We can thus not use the +orthogonality relation for the matrix \( \boldsymbol{U} \). This can already be +when we multiply the matrices \( \boldsymbol{\Sigma}^T\boldsymbol{U}^T \). +

    @@ -425,7 +428,7 @@ $$

  • 62
  • 63
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs054.html b/doc/pub/week35/html/._week35-bs054.html index 0a1c9c5c6..19c7bcce6 100644 --- a/doc/pub/week35/html/._week35-bs054.html +++ b/doc/pub/week35/html/._week35-bs054.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,61 +360,52 @@ MathJax.Hub.Config({

     

     

     

    -

    Example Matrix

    - -

    As an example, consider the following \( 3\times 2 \) example for the matrix \( \boldsymbol{\Sigma} \)

    +

    Further properties (important for our analyses later)

    +

    Let us study again \( \boldsymbol{X}^T\boldsymbol{X} \) in terms of our SVD,

    $$ -\boldsymbol{\Sigma}= -\begin{bmatrix} -2& 0 \\ -0 & 1 \\ -0 & 0 \\ -\end{bmatrix} +\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{V}^T. $$ -

    The singular values are \( \sigma_0=2 \) and \( \sigma_1=1 \). It is common to rewrite the matrix \( \boldsymbol{\Sigma} \) as

    - +

    If we now multiply from the right with \( \boldsymbol{V} \) (using the orthogonality of \( \boldsymbol{V} \)) we get

    $$ -\boldsymbol{\Sigma}= -\begin{bmatrix} -\boldsymbol{\tilde{\Sigma}}\\ -\boldsymbol{0}\\ -\end{bmatrix}, +\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{V}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}. $$ -

    where

    +

    This means the vectors \( \boldsymbol{v}_i \) of the orthogonal matrix \( \boldsymbol{V} \) are the eigenvectors of the matrix \( \boldsymbol{X}^T\boldsymbol{X} \) +with eigenvalues given by the singular values squared, that is +

    $$ -\boldsymbol{\tilde{\Sigma}}= -\begin{bmatrix} -2& 0 \\ -0 & 1 \\ -\end{bmatrix}, +\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{v}_i=\boldsymbol{v}_i\sigma_i^2. $$ -

    contains only the singular values. Note also (and we will use this below) that

    - +

    Similarly, if we use the SVD decomposition for the matrix \( \boldsymbol{X}\boldsymbol{X}^T \), we have

    $$ -\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}= -\begin{bmatrix} -4& 0 \\ -0 & 1 \\ -\end{bmatrix}, +\boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{\Sigma}^T\boldsymbol{U}^T. $$ -

    which is a \( 2\times 2 \) matrix while

    +

    If we now multiply from the right with \( \boldsymbol{U} \) (using the orthogonality of \( \boldsymbol{U} \)) we get

    $$ -\boldsymbol{\Sigma}\boldsymbol{\Sigma}^T= -\begin{bmatrix} -4& 0 & 0\\ -0 & 1 & 0\\ -0 & 0 & 0\\ -\end{bmatrix}, +\left(\boldsymbol{X}\boldsymbol{X}^T\right)\boldsymbol{U}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{\Sigma}^T. $$ -

    is a \( 3\times 3 \) matrix. The last row and column of this last matrix -contain only zeros. This will have important consequences for our SVD -decomposition of the design matrix. +

    This means the vectors \( \boldsymbol{u}_i \) of the orthogonal matrix \( \boldsymbol{U} \) are the eigenvectors of the matrix \( \boldsymbol{X}\boldsymbol{X}^T \) +with eigenvalues given by the singular values squared, that is +

    +$$ +\left(\boldsymbol{X}\boldsymbol{X}^T\right)\boldsymbol{u}_i=\boldsymbol{u}_i\sigma_i^2. +$$ + +

    Important note: we have defined our design matrix \( \boldsymbol{X} \) to be an +\( n\times p \) matrix. In most supervised learning cases we have that \( n +\ge p \), and quite often we have \( n >> p \). For linear algebra based methods like ordinary least squares or Ridge regression, this leads to a matrix \( \boldsymbol{X}^T\boldsymbol{X} \) which is small and thereby easier to handle from a computational point of view (in terms of number of floating point operations). +

    + +

    In our lectures, the number of columns will +always refer to the number of features in our data set, while the +number of rows represents the number of data inputs. Note that in +other texts you may find the opposite notation. This has consequences +for the definition of for example the covariance matrix and its relation to the SVD.

    @@ -449,7 +433,7 @@ decomposition of the design matrix.

  • 63
  • 64
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs055.html b/doc/pub/week35/html/._week35-bs055.html index b1f700239..e42c6c51e 100644 --- a/doc/pub/week35/html/._week35-bs055.html +++ b/doc/pub/week35/html/._week35-bs055.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,47 +360,31 @@ MathJax.Hub.Config({

     

     

     

    -

    Setting up the Matrix to be inverted

    +

    Meet the Covariance Matrix

    -

    The matrix that may cause problems for us is \( \boldsymbol{X}^T\boldsymbol{X} \). Using the SVD we can rewrite this matrix as

    +

    Before we move on to a discussion of Ridge and Lasso regression, we want to show an important example of the above.

    + +

    We have already noted that the matrix \( \boldsymbol{X}^T\boldsymbol{X} \) in ordinary +least squares is proportional to the second derivative of the cost +function, that is we have +

    $$ -\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T, +\frac{\partial^2 C(\boldsymbol{\beta})}{\partial \boldsymbol{\beta}\partial \boldsymbol{\beta}^T} =\frac{2}{n}\boldsymbol{X}^T\boldsymbol{X}. $$ -

    and using the orthogonality of the matrix \( \boldsymbol{U} \) we have

    +

    This quantity defines was what is called the Hessian matrix (the second derivative of a function we want to optimize).

    + +

    The Hessian matrix plays an important role and is defined in this course as

    $$ -\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{V}^T. +\boldsymbol{H}=\boldsymbol{X}^T\boldsymbol{X}. $$ -

    We define \( \boldsymbol{\Sigma}^T\boldsymbol{\Sigma}=\tilde{\boldsymbol{\Sigma}}^2 \) which is a diagonal matrix containing only the singular values squared. It has dimensionality \( p \times p \).

    - -

    We can now insert the result for the matrix \( \boldsymbol{X}^T\boldsymbol{X} \) into our equation for ordinary least squares where

    - -$$ -\tilde{y}_{\mathrm{OLS}}=\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}, -$$ - -

    and using our SVD decomposition of \( \boldsymbol{X} \) we have

    - -$$ -\tilde{y}_{\mathrm{OLS}}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T\left(\boldsymbol{V}\tilde{\boldsymbol{\Sigma}}^{2}(\boldsymbol{V}^T\right)^{-1}\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{y}, -$$ - -

    which gives us, using the orthogonality of the matrix \( \boldsymbol{V} \),

    - -$$ -\tilde{y}_{\mathrm{OLS}}=\boldsymbol{U}\boldsymbol{U}^T\boldsymbol{y}=\sum_{i=0}^{p-1}\boldsymbol{u}_i\boldsymbol{u}^T_i\boldsymbol{y}, -$$ - -

    It means that the ordinary least square model (with the optimal -parameters) \( \boldsymbol{\tilde{y}} \), corresponds to an orthogonal -transformation of the output (or target) vector \( \boldsymbol{y} \) by the -vectors of the matrix \( \boldsymbol{U} \). Note that the summation ends at -\( p-1 \), that is \( \boldsymbol{\tilde{y}}\ne \boldsymbol{y} \). We can thus not use the -orthogonality relation for the matrix \( \boldsymbol{U} \). This can already be -when we multiply the matrices \( \boldsymbol{\Sigma}^T\boldsymbol{U}^T \). +

    The Hessian matrix for ordinary least squares is also proportional to +the covariance matrix. This means also that we can use the SVD to find +the eigenvalues of the covariance matrix and the Hessian matrix in +terms of the singular values. Let us develop these arguments, as they will play an important role in our machine learning studies.

    @@ -435,7 +412,7 @@ when we multiply the matrices \( \boldsymbol{\Sigma}^T\boldsymbol{U}^T \).

  • 64
  • 65
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs056.html b/doc/pub/week35/html/._week35-bs056.html index a365db225..9a995f789 100644 --- a/doc/pub/week35/html/._week35-bs056.html +++ b/doc/pub/week35/html/._week35-bs056.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,52 +360,46 @@ MathJax.Hub.Config({

     

     

     

    -

    Further properties (important for our analyses later)

    +

    Introducing the Covariance and Correlation functions

    -

    Let us study again \( \boldsymbol{X}^T\boldsymbol{X} \) in terms of our SVD,

    -$$ -\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{V}^T. -$$ - -

    If we now multiply from the right with \( \boldsymbol{V} \) (using the orthogonality of \( \boldsymbol{V} \)) we get

    -$$ -\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{V}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}. -$$ - -

    This means the vectors \( \boldsymbol{v}_i \) of the orthogonal matrix \( \boldsymbol{V} \) are the eigenvectors of the matrix \( \boldsymbol{X}^T\boldsymbol{X} \) -with eigenvalues given by the singular values squared, that is -

    -$$ -\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{v}_i=\boldsymbol{v}_i\sigma_i^2. -$$ - -

    Similarly, if we use the SVD decomposition for the matrix \( \boldsymbol{X}\boldsymbol{X}^T \), we have

    -$$ -\boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{\Sigma}^T\boldsymbol{U}^T. -$$ - -

    If we now multiply from the right with \( \boldsymbol{U} \) (using the orthogonality of \( \boldsymbol{U} \)) we get

    -$$ -\left(\boldsymbol{X}\boldsymbol{X}^T\right)\boldsymbol{U}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{\Sigma}^T. -$$ - -

    This means the vectors \( \boldsymbol{u}_i \) of the orthogonal matrix \( \boldsymbol{U} \) are the eigenvectors of the matrix \( \boldsymbol{X}\boldsymbol{X}^T \) -with eigenvalues given by the singular values squared, that is -

    -$$ -\left(\boldsymbol{X}\boldsymbol{X}^T\right)\boldsymbol{u}_i=\boldsymbol{u}_i\sigma_i^2. -$$ - -

    Important note: we have defined our design matrix \( \boldsymbol{X} \) to be an -\( n\times p \) matrix. In most supervised learning cases we have that \( n -\ge p \), and quite often we have \( n >> p \). For linear algebra based methods like ordinary least squares or Ridge regression, this leads to a matrix \( \boldsymbol{X}^T\boldsymbol{X} \) which is small and thereby easier to handle from a computational point of view (in terms of number of floating point operations). +

    Before we discuss the link between for example Ridge regression and the singular value decomposition, we need to remind ourselves about +the definition of the covariance and the correlation function. These are quantities that play a central role in machine learning methods.

    -

    In our lectures, the number of columns will -always refer to the number of features in our data set, while the -number of rows represents the number of data inputs. Note that in -other texts you may find the opposite notation. This has consequences -for the definition of for example the covariance matrix and its relation to the SVD. +

    Suppose we have defined two vectors +\( \hat{x} \) and \( \hat{y} \) with \( n \) elements each. The covariance matrix \( \boldsymbol{C} \) is defined as +

    +$$ +\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{cov}[\boldsymbol{x},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\ + \mathrm{cov}[\boldsymbol{y},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{y},\boldsymbol{y}] \\ + \end{bmatrix}, +$$ + +

    where for example

    +$$ +\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] =\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})(y_i- \overline{y}). +$$ + +

    With this definition and recalling that the variance is defined as

    +$$ +\mathrm{var}[\boldsymbol{x}]=\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})^2, +$$ + +

    we can rewrite the covariance matrix as

    +$$ +\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{var}[\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\ + \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] & \mathrm{var}[\boldsymbol{y}] \\ + \end{bmatrix}. +$$ + +

    Note: we have used \( 1/n \) in the above definitions of the sample variance and covariance. We assume then that we can calculate the exact mean value. +What you will find in essentially all statistics texts are equations +with a factor \( 1/(n-1) \). This is called Bessel's correction. This +method corrects the bias in the estimation of the population variance +and covariance. It also partially corrects the bias in the estimation +of the population standard deviation. If you use a library like +Scikit-Learn or nunmpy's function to calculate the covariance, this +quantity will be computed with a factor \( 1/(n-1) \).

    @@ -440,7 +427,7 @@ for the definition of for example the covariance matrix and its relation to the

  • 65
  • 66
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs057.html b/doc/pub/week35/html/._week35-bs057.html index ed497b411..aa0b78f33 100644 --- a/doc/pub/week35/html/._week35-bs057.html +++ b/doc/pub/week35/html/._week35-bs057.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,33 +360,33 @@ MathJax.Hub.Config({

     

     

     

    -

    Meet the Covariance Matrix

    +

    Covariance and Correlation Matrix

    -

    Before we move on to a discussion of Ridge and Lasso regression, we want to show an important example of the above.

    - -

    We have already noted that the matrix \( \boldsymbol{X}^T\boldsymbol{X} \) in ordinary -least squares is proportional to the second derivative of the cost -function, that is we have +

    The covariance takes values between zero and infinity and may thus +lead to problems with loss of numerical precision for particularly +large values. It is common to scale the covariance matrix by +introducing instead the correlation matrix defined via the so-called +correlation function

    $$ -\frac{\partial^2 C(\boldsymbol{\beta})}{\partial \boldsymbol{\beta}\partial \boldsymbol{\beta}^T} =\frac{2}{n}\boldsymbol{X}^T\boldsymbol{X}. +\mathrm{corr}[\boldsymbol{x},\boldsymbol{y}]=\frac{\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}]}{\sqrt{\mathrm{var}[\boldsymbol{x}] \mathrm{var}[\boldsymbol{y}]}}. $$ -

    This quantity defines was what is called the Hessian matrix (the second derivative of a function we want to optimize).

    - -

    The Hessian matrix plays an important role and is defined in this course as

    - -$$ -\boldsymbol{H}=\boldsymbol{X}^T\boldsymbol{X}. -$$ - -

    The Hessian matrix for ordinary least squares is also proportional to -the covariance matrix. This means also that we can use the SVD to find -the eigenvalues of the covariance matrix and the Hessian matrix in -terms of the singular values. Let us develop these arguments, as they will play an important role in our machine learning studies. +

    The correlation function is then given by values \( \mathrm{corr}[\boldsymbol{x},\boldsymbol{y}] +\in [-1,1] \). This avoids eventual problems with too large values. We +can then define the correlation matrix for the two vectors \( \boldsymbol{x} \) +and \( \boldsymbol{y} \) as

    +$$ +\boldsymbol{K}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} 1 & \mathrm{corr}[\boldsymbol{x},\boldsymbol{y}] \\ + \mathrm{corr}[\boldsymbol{y},\boldsymbol{x}] & 1 \\ + \end{bmatrix}, +$$ + +

    In the above example this is the function we constructed using pandas.

    +

      @@ -419,7 +412,7 @@ terms of the singular values. Let us develop these arguments, as they will pla
    • 66
    • 67
    • ...
    • -
    • 74
    • +
    • 72
    • »
    diff --git a/doc/pub/week35/html/._week35-bs058.html b/doc/pub/week35/html/._week35-bs058.html index 2a0b61850..226a56a10 100644 --- a/doc/pub/week35/html/._week35-bs058.html +++ b/doc/pub/week35/html/._week35-bs058.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,48 +360,66 @@ MathJax.Hub.Config({

     

     

     

    -

    Introducing the Covariance and Correlation functions

    +

    Correlation Function and Design/Feature Matrix

    -

    Before we discuss the link between for example Ridge regression and the singular value decomposition, we need to remind ourselves about -the definition of the covariance and the correlation function. These are quantities that play a central role in machine learning methods. +

    In our derivation of the various regression algorithms like Ordinary Least Squares or Ridge regression +we defined the design/feature matrix \( \boldsymbol{X} \) as

    -

    Suppose we have defined two vectors -\( \hat{x} \) and \( \hat{y} \) with \( n \) elements each. The covariance matrix \( \boldsymbol{C} \) is defined as +$$ +\boldsymbol{X}=\begin{bmatrix} +x_{0,0} & x_{0,1} & x_{0,2}& \dots & \dots x_{0,p-1}\\ +x_{1,0} & x_{1,1} & x_{1,2}& \dots & \dots x_{1,p-1}\\ +x_{2,0} & x_{2,1} & x_{2,2}& \dots & \dots x_{2,p-1}\\ +\dots & \dots & \dots & \dots \dots & \dots \\ +x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \dots & \dots x_{n-2,p-1}\\ +x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \dots & \dots x_{n-1,p-1}\\ +\end{bmatrix}, +$$ + +

    with \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \), with the predictors/features \( p \) refering to the column numbers and the +entries \( n \) being the row elements. +We can rewrite the design/feature matrix in terms of its column vectors as

    $$ -\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{cov}[\boldsymbol{x},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\ - \mathrm{cov}[\boldsymbol{y},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{y},\boldsymbol{y}] \\ - \end{bmatrix}, +\boldsymbol{X}=\begin{bmatrix} \boldsymbol{x}_0 & \boldsymbol{x}_1 & \boldsymbol{x}_2 & \dots & \dots & \boldsymbol{x}_{p-1}\end{bmatrix}, $$ -

    where for example

    +

    with a given vector

    $$ -\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] =\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})(y_i- \overline{y}). +\boldsymbol{x}_i^T = \begin{bmatrix}x_{0,i} & x_{1,i} & x_{2,i}& \dots & \dots x_{n-1,i}\end{bmatrix}. $$ -

    With this definition and recalling that the variance is defined as

    -$$ -\mathrm{var}[\boldsymbol{x}]=\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})^2, -$$ - -

    we can rewrite the covariance matrix as

    -$$ -\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{var}[\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\ - \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] & \mathrm{var}[\boldsymbol{y}] \\ - \end{bmatrix}. -$$ - -

    Note: we have used \( 1/n \) in the above definitions of the sample variance and covariance. We assume then that we can calculate the exact mean value. -What you will find in essentially all statistics texts are equations -with a factor \( 1/(n-1) \). This is called Bessel's correction. This -method corrects the bias in the estimation of the population variance -and covariance. It also partially corrects the bias in the estimation -of the population standard deviation. If you use a library like -Scikit-Learn or nunmpy's function to calculate the covariance, this -quantity will be computed with a factor \( 1/(n-1) \). +

    With these definitions, we can now rewrite our \( 2\times 2 \) +correlation/covariance matrix in terms of a moe general design/feature +matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \). This leads to a \( p\times p \) +covariance matrix for the vectors \( \boldsymbol{x}_i \) with \( i=0,1,\dots,p-1 \)

    +$$ +\boldsymbol{C}[\boldsymbol{x}] = \begin{bmatrix} +\mathrm{var}[\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_{p-1}]\\ +\mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_0] & \mathrm{var}[\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_{p-1}]\\ +\mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_1] & \mathrm{var}[\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_{p-1}]\\ +\dots & \dots & \dots & \dots & \dots & \dots \\ +\dots & \dots & \dots & \dots & \dots & \dots \\ +\mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_{2}] & \dots & \dots & \mathrm{var}[\boldsymbol{x}_{p-1}]\\ +\end{bmatrix}, +$$ + +

    and the correlation matrix

    +$$ +\boldsymbol{K}[\boldsymbol{x}] = \begin{bmatrix} +1 & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_1] & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_2] & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_{p-1}]\\ +\mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_0] & 1 & \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_2] & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_{p-1}]\\ +\mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_0] & \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_1] & 1 & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_{p-1}]\\ +\dots & \dots & \dots & \dots & \dots & \dots \\ +\dots & \dots & \dots & \dots & \dots & \dots \\ +\mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_1] & \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_{2}] & \dots & \dots & 1\\ +\end{bmatrix}, +$$ + +

      @@ -434,7 +445,7 @@ quantity will be computed with a factor \( 1/(n-1) \).
    • 67
    • 68
    • ...
    • -
    • 74
    • +
    • 72
    • »
    diff --git a/doc/pub/week35/html/._week35-bs059.html b/doc/pub/week35/html/._week35-bs059.html index e22920fc3..c471e93ce 100644 --- a/doc/pub/week35/html/._week35-bs059.html +++ b/doc/pub/week35/html/._week35-bs059.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,32 +360,61 @@ MathJax.Hub.Config({

     

     

     

    -

    Covariance and Correlation Matrix

    +

    Covariance Matrix Examples

    -

    The covariance takes values between zero and infinity and may thus -lead to problems with loss of numerical precision for particularly -large values. It is common to scale the covariance matrix by -introducing instead the correlation matrix defined via the so-called -correlation function +

    The Numpy function np.cov calculates the covariance elements using +the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have +the exact mean values. The following simple function uses the +np.vstack function which takes each vector of dimension \( 1\times n \) +and produces a \( 2\times n \) matrix \( \boldsymbol{W} \)

    +

    Note that this assumes you have the features as the rows, and the inputs as columns, that is

    $$ -\mathrm{corr}[\boldsymbol{x},\boldsymbol{y}]=\frac{\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}]}{\sqrt{\mathrm{var}[\boldsymbol{x}] \mathrm{var}[\boldsymbol{y}]}}. -$$ - -

    The correlation function is then given by values \( \mathrm{corr}[\boldsymbol{x},\boldsymbol{y}] -\in [-1,1] \). This avoids eventual problems with too large values. We -can then define the correlation matrix for the two vectors \( \boldsymbol{x} \) -and \( \boldsymbol{y} \) as -

    - -$$ -\boldsymbol{K}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} 1 & \mathrm{corr}[\boldsymbol{x},\boldsymbol{y}] \\ - \mathrm{corr}[\boldsymbol{y},\boldsymbol{x}] & 1 \\ +\boldsymbol{W} = \begin{bmatrix} x_0 & x_1 & x_2 & \dots & x_{n-2} & x_{n-1} \\ + y_0 & y_1 & y_2 & \dots & y_{n-2} & y_{n-1} \\ \end{bmatrix}, $$ -

    In the above example this is the function we constructed using pandas.

    +

    which in turn is converted into into the \( 2\times 2 \) covariance matrix +\( \boldsymbol{C} \) via the Numpy function np.cov(). We note that we can also calculate +the mean value of each set of samples \( \boldsymbol{x} \) etc using the Numpy +function np.mean(x). We can also extract the eigenvalues of the +covariance matrix through the np.linalg.eig() function. +

    + + + +
    +
    +
    +
    +
    +
    # Importing various packages
    +import numpy as np
    +n = 100
    +x = np.random.normal(size=n)
    +print(np.mean(x))
    +y = 4+3*x+np.random.normal(size=n)
    +print(np.mean(y))
    +W = np.vstack((x, y))
    +C = np.cov(W)
    +print(C)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +

    @@ -419,7 +441,7 @@ $$

  • 68
  • 69
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs060.html b/doc/pub/week35/html/._week35-bs060.html index b0ea1ab31..588934e48 100644 --- a/doc/pub/week35/html/._week35-bs060.html +++ b/doc/pub/week35/html/._week35-bs060.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,65 +360,64 @@ MathJax.Hub.Config({

     

     

     

    -

    Correlation Function and Design/Feature Matrix

    +

    Correlation Matrix

    -

    In our derivation of the various regression algorithms like Ordinary Least Squares or Ridge regression -we defined the design/feature matrix \( \boldsymbol{X} \) as +

    The previous example can be converted into the correlation matrix by +simply scaling the matrix elements with the variances. We should also +subtract the mean values for each column. This leads to the following +code which sets up the correlations matrix for the previous example in +a more brute force way. Here we scale the mean values for each column of the design matrix, calculate the relevant mean values and variances and then finally set up the \( 2\times 2 \) correlation matrix (since we have only two vectors).

    -$$ -\boldsymbol{X}=\begin{bmatrix} -x_{0,0} & x_{0,1} & x_{0,2}& \dots & \dots x_{0,p-1}\\ -x_{1,0} & x_{1,1} & x_{1,2}& \dots & \dots x_{1,p-1}\\ -x_{2,0} & x_{2,1} & x_{2,2}& \dots & \dots x_{2,p-1}\\ -\dots & \dots & \dots & \dots \dots & \dots \\ -x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \dots & \dots x_{n-2,p-1}\\ -x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \dots & \dots x_{n-1,p-1}\\ -\end{bmatrix}, -$$ -

    with \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \), with the predictors/features \( p \) refering to the column numbers and the -entries \( n \) being the row elements. -We can rewrite the design/feature matrix in terms of its column vectors as -

    -$$ -\boldsymbol{X}=\begin{bmatrix} \boldsymbol{x}_0 & \boldsymbol{x}_1 & \boldsymbol{x}_2 & \dots & \dots & \boldsymbol{x}_{p-1}\end{bmatrix}, -$$ + +
    +
    +
    +
    +
    +
    import numpy as np
    +n = 100
    +# define two vectors                                                                                           
    +x = np.random.random(size=n)
    +y = 4+3*x+np.random.normal(size=n)
    +#scaling the x and y vectors                                                                                   
    +x = x - np.mean(x)
    +y = y - np.mean(y)
    +variance_x = np.sum(x@x)/n
    +variance_y = np.sum(y@y)/n
    +print(variance_x)
    +print(variance_y)
    +cov_xy = np.sum(x@y)/n
    +cov_xx = np.sum(x@x)/n
    +cov_yy = np.sum(y@y)/n
    +C = np.zeros((2,2))
    +C[0,0]= cov_xx/variance_x
    +C[1,1]= cov_yy/variance_y
    +C[0,1]= cov_xy/np.sqrt(variance_y*variance_x)
    +C[1,0]= C[0,1]
    +print(C)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    -

    with a given vector

    -$$ -\boldsymbol{x}_i^T = \begin{bmatrix}x_{0,i} & x_{1,i} & x_{2,i}& \dots & \dots x_{n-1,i}\end{bmatrix}. -$$ - -

    With these definitions, we can now rewrite our \( 2\times 2 \) -correlation/covariance matrix in terms of a moe general design/feature -matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \). This leads to a \( p\times p \) -covariance matrix for the vectors \( \boldsymbol{x}_i \) with \( i=0,1,\dots,p-1 \) +

    We see that the matrix elements along the diagonal are one as they +should be and that the matrix is symmetric. Furthermore, diagonalizing +this matrix we easily see that it is a positive definite matrix.

    -$$ -\boldsymbol{C}[\boldsymbol{x}] = \begin{bmatrix} -\mathrm{var}[\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_{p-1}]\\ -\mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_0] & \mathrm{var}[\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_{p-1}]\\ -\mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_1] & \mathrm{var}[\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_{p-1}]\\ -\dots & \dots & \dots & \dots & \dots & \dots \\ -\dots & \dots & \dots & \dots & \dots & \dots \\ -\mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_{2}] & \dots & \dots & \mathrm{var}[\boldsymbol{x}_{p-1}]\\ -\end{bmatrix}, -$$ - -

    and the correlation matrix

    -$$ -\boldsymbol{K}[\boldsymbol{x}] = \begin{bmatrix} -1 & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_1] & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_2] & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_{p-1}]\\ -\mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_0] & 1 & \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_2] & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_{p-1}]\\ -\mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_0] & \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_1] & 1 & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_{p-1}]\\ -\dots & \dots & \dots & \dots & \dots & \dots \\ -\dots & \dots & \dots & \dots & \dots & \dots \\ -\mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_1] & \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_{2}] & \dots & \dots & 1\\ -\end{bmatrix}, -$$ - +

    The above procedure with numpy can be made more compact if we use pandas.

    @@ -452,7 +444,7 @@ $$

  • 69
  • 70
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs061.html b/doc/pub/week35/html/._week35-bs061.html index 8c73e8cd0..1cdf88f6d 100644 --- a/doc/pub/week35/html/._week35-bs061.html +++ b/doc/pub/week35/html/._week35-bs061.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,29 +360,9 @@ MathJax.Hub.Config({

     

     

     

    -

    Covariance Matrix Examples

    - -

    The Numpy function np.cov calculates the covariance elements using -the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have -the exact mean values. The following simple function uses the -np.vstack function which takes each vector of dimension \( 1\times n \) -and produces a \( 2\times n \) matrix \( \boldsymbol{W} \) -

    - -

    Note that this assumes you have the features as the rows, and the inputs as columns, that is

    -$$ -\boldsymbol{W} = \begin{bmatrix} x_0 & x_1 & x_2 & \dots & x_{n-2} & x_{n-1} \\ - y_0 & y_1 & y_2 & \dots & y_{n-2} & y_{n-1} \\ - \end{bmatrix}, -$$ - -

    which in turn is converted into into the \( 2\times 2 \) covariance matrix -\( \boldsymbol{C} \) via the Numpy function np.cov(). We note that we can also calculate -the mean value of each set of samples \( \boldsymbol{x} \) etc using the Numpy -function np.mean(x). We can also extract the eigenvalues of the -covariance matrix through the np.linalg.eig() function. -

    +

    Correlation Matrix with Pandas

    +

    We whow here how we can set up the correlation matrix using pandas, as done in this simple code

    @@ -397,16 +370,20 @@ covariance matrix through the np.linalg.eig() function.
    -
    # Importing various packages
    -import numpy as np
    -n = 100
    +  
    import numpy as np
    +import pandas as pd
    +n = 10
     x = np.random.normal(size=n)
    -print(np.mean(x))
    +x = x - np.mean(x)
     y = 4+3*x+np.random.normal(size=n)
    -print(np.mean(y))
    -W = np.vstack((x, y))
    -C = np.cov(W)
    -print(C)
    +y = y - np.mean(y)
    +# Note that we transpose the matrix in order to stay with our ordering n x p
    +X = (np.vstack((x, y))).T
    +print(X)
    +Xpd = pd.DataFrame(X)
    +print(Xpd)
    +correlation_matrix = Xpd.corr()
    +print(correlation_matrix)
     
    @@ -422,6 +399,7 @@ C = np.c
    +

    We expand this model to the Franke function discussed above.

    @@ -448,7 +426,7 @@ C = np.c

  • 70
  • 71
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs062.html b/doc/pub/week35/html/._week35-bs062.html index d2957ece0..be0d99b5c 100644 --- a/doc/pub/week35/html/._week35-bs062.html +++ b/doc/pub/week35/html/._week35-bs062.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,14 +360,7 @@ MathJax.Hub.Config({

     

     

     

    -

    Correlation Matrix

    - -

    The previous example can be converted into the correlation matrix by -simply scaling the matrix elements with the variances. We should also -subtract the mean values for each column. This leads to the following -code which sets up the correlations matrix for the previous example in -a more brute force way. Here we scale the mean values for each column of the design matrix, calculate the relevant mean values and variances and then finally set up the \( 2\times 2 \) correlation matrix (since we have only two vectors). -

    +

    Correlation Matrix with Pandas and the Franke function

    @@ -383,27 +369,49 @@ a more brute force way. Here we scale the mean values for each column of the des
    -
    import numpy as np
    -n = 100
    -# define two vectors                                                                                           
    -x = np.random.random(size=n)
    -y = 4+3*x+np.random.normal(size=n)
    -#scaling the x and y vectors                                                                                   
    -x = x - np.mean(x)
    -y = y - np.mean(y)
    -variance_x = np.sum(x@x)/n
    -variance_y = np.sum(y@y)/n
    -print(variance_x)
    -print(variance_y)
    -cov_xy = np.sum(x@y)/n
    -cov_xx = np.sum(x@x)/n
    -cov_yy = np.sum(y@y)/n
    -C = np.zeros((2,2))
    -C[0,0]= cov_xx/variance_x
    -C[1,1]= cov_yy/variance_y
    -C[0,1]= cov_xy/np.sqrt(variance_y*variance_x)
    -C[1,0]= C[0,1]
    -print(C)
    +  
    # Common imports
    +import numpy as np
    +import pandas as pd
    +
    +
    +def FrankeFunction(x,y):
    +	term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
    +	term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
    +	term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
    +	term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
    +	return term1 + term2 + term3 + term4
    +
    +
    +def create_X(x, y, n ):
    +	if len(x.shape) > 1:
    +		x = np.ravel(x)
    +		y = np.ravel(y)
    +
    +	N = len(x)
    +	l = int((n+1)*(n+2)/2)		# Number of elements in beta
    +	X = np.ones((N,l))
    +
    +	for i in range(1,n+1):
    +		q = int((i)*(i+1)/2)
    +		for k in range(i+1):
    +			X[:,q+k] = (x**(i-k))*(y**k)
    +
    +	return X
    +
    +
    +# Making meshgrid of datapoints and compute Franke's function
    +n = 4
    +N = 100
    +x = np.sort(np.random.uniform(0, 1, N))
    +y = np.sort(np.random.uniform(0, 1, N))
    +z = FrankeFunction(x, y)
    +X = create_X(x, y, n=n)    
    +
    +Xpd = pd.DataFrame(X)
    +# subtract the mean values and set up the covariance matrix
    +Xpd = Xpd - Xpd.mean()
    +covariance_matrix = Xpd.cov()
    +print(covariance_matrix)
     
    @@ -419,12 +427,16 @@ C[1,0]
    -

    We see that the matrix elements along the diagonal are one as they -should be and that the matrix is symmetric. Furthermore, diagonalizing -this matrix we easily see that it is a positive definite matrix. +

    We note here that the covariance is zero for the first rows and +columns since all matrix elements in the design matrix were set to one +(we are fitting the function in terms of a polynomial of degree \( n \)).

    -

    The above procedure with numpy can be made more compact if we use pandas.

    +

    This means that the variance for these elements will be zero and will +cause problems when we set up the correlation matrix. We can simply +drop these elements and construct a correlation +matrix without these elements. +

    @@ -450,8 +462,6 @@ this matrix we easily see that it is a positive definite matrix.

  • 70
  • 71
  • 72
  • -
  • ...
  • -
  • 74
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs063.html b/doc/pub/week35/html/._week35-bs063.html index e8608f59c..eb0fd246f 100644 --- a/doc/pub/week35/html/._week35-bs063.html +++ b/doc/pub/week35/html/._week35-bs063.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,46 +360,41 @@ MathJax.Hub.Config({

     

     

     

    -

    Correlation Matrix with Pandas

    +

    Rewriting the Covariance and/or Correlation Matrix

    -

    We whow here how we can set up the correlation matrix using pandas, as done in this simple code

    +

    We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix \( \boldsymbol{X} \) as

    +$$ +\boldsymbol{C}[\boldsymbol{x}] = \frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}= \mathbb{E}[\boldsymbol{X}^T\boldsymbol{X}]. +$$ - -
    -
    -
    -
    -
    -
    import numpy as np
    -import pandas as pd
    -n = 10
    -x = np.random.normal(size=n)
    -x = x - np.mean(x)
    -y = 4+3*x+np.random.normal(size=n)
    -y = y - np.mean(y)
    -# Note that we transpose the matrix in order to stay with our ordering n x p
    -X = (np.vstack((x, y))).T
    -print(X)
    -Xpd = pd.DataFrame(X)
    -print(Xpd)
    -correlation_matrix = Xpd.corr()
    -print(correlation_matrix)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +

    To see this let us simply look at a design matrix \( \boldsymbol{X}\in {\mathbb{R}}^{2\times 2} \)

    +$$ +\boldsymbol{X}=\begin{bmatrix} +x_{00} & x_{01}\\ +x_{10} & x_{11}\\ +\end{bmatrix}=\begin{bmatrix} +\boldsymbol{x}_{0} & \boldsymbol{x}_{1}\\ +\end{bmatrix}. +$$ -

    We expand this model to the Franke function discussed above.

    +

    If we then compute the expectation value (note the \( 1/n \) factor instead of \( 1/(n-1) \))

    +$$ +\mathbb{E}[\boldsymbol{X}^T\boldsymbol{X}] = \frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}=\frac{1}{n}\begin{bmatrix} +x_{00}^2+x_{10}^2 & x_{00}x_{01}+x_{10}x_{11}\\ +x_{01}x_{00}+x_{11}x_{10} & x_{01}^2+x_{11}^2\\ +\end{bmatrix}, +$$ + +

    which is just

    +$$ +\boldsymbol{C}[\boldsymbol{x}_0,\boldsymbol{x}_1] = \boldsymbol{C}[\boldsymbol{x}]=\begin{bmatrix} \mathrm{var}[\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_1] \\ + \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_0] & \mathrm{var}[\boldsymbol{x}_1] \\ + \end{bmatrix}, +$$ + +

    where we wrote $$\boldsymbol{C}[\boldsymbol{x}_0,\boldsymbol{x}_1] = \boldsymbol{C}[\boldsymbol{x}]$$ to indicate that this is the covariance of the vectors \( \boldsymbol{x} \) of the design/feature matrix \( \boldsymbol{X} \).

    + +

    It is easy to generalize this to a matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \).

    @@ -431,9 +419,6 @@ correlation_matrix = Xpd70

  • 71
  • 72
  • -
  • 73
  • -
  • ...
  • -
  • 74
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs064.html b/doc/pub/week35/html/._week35-bs064.html index 273c59b84..9e8c7c50e 100644 --- a/doc/pub/week35/html/._week35-bs064.html +++ b/doc/pub/week35/html/._week35-bs064.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,83 +360,39 @@ MathJax.Hub.Config({

     

     

     

    -

    Correlation Matrix with Pandas and the Franke function

    +

    Linking with the SVD

    +

    We saw earlier that

    +$$ +\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{V}^T. +$$ - -
    -
    -
    -
    -
    -
    # Common imports
    -import numpy as np
    -import pandas as pd
    +

    Since the matrices here have dimension \( p\times p \), with \( p \) corresponding to the singular values, we defined earlier the matrix

    +$$ +\boldsymbol{\Sigma}^T\boldsymbol{\Sigma} = \begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0}\\ \end{bmatrix}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} \\ \boldsymbol{0}\\ \end{bmatrix}, +$$ +

    where the tilde-matrix \( \tilde{\boldsymbol{\Sigma}} \) is a matrix of dimension \( p\times p \) containing only the singular values \( \sigma_i \), that is

    -def FrankeFunction(x,y): - term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2)) - term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1)) - term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2)) - term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2) - return term1 + term2 + term3 + term4 +$$ +\tilde{\boldsymbol{\Sigma}}=\begin{bmatrix} \sigma_0 & 0 & 0 & \dots & 0 & 0 \\ + 0 & \sigma_1 & 0 & \dots & 0 & 0 \\ + 0 & 0 & \sigma_2 & \dots & 0 & 0 \\ + 0 & 0 & 0 & \dots & \sigma_{p-2} & 0 \\ + 0 & 0 & 0 & \dots & 0 & \sigma_{p-1} \\ +\end{bmatrix}, +$$ +

    meaning we can write

    +$$ +\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\tilde{\boldsymbol{\Sigma}}^2\boldsymbol{V}^T. +$$ -def create_X(x, y, n ): - if len(x.shape) > 1: - x = np.ravel(x) - y = np.ravel(y) +

    Multiplying from the right with \( \boldsymbol{V} \) (using the orthogonality of \( \boldsymbol{V} \)) we get

    +$$ +\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{V}=\boldsymbol{V}\tilde{\boldsymbol{\Sigma}}^2. +$$ - N = len(x) - l = int((n+1)*(n+2)/2) # Number of elements in beta - X = np.ones((N,l)) - - for i in range(1,n+1): - q = int((i)*(i+1)/2) - for k in range(i+1): - X[:,q+k] = (x**(i-k))*(y**k) - - return X - - -# Making meshgrid of datapoints and compute Franke's function -n = 4 -N = 100 -x = np.sort(np.random.uniform(0, 1, N)) -y = np.sort(np.random.uniform(0, 1, N)) -z = FrankeFunction(x, y) -X = create_X(x, y, n=n) - -Xpd = pd.DataFrame(X) -# subtract the mean values and set up the covariance matrix -Xpd = Xpd - Xpd.mean() -covariance_matrix = Xpd.cov() -print(covariance_matrix) -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    We note here that the covariance is zero for the first rows and -columns since all matrix elements in the design matrix were set to one -(we are fitting the function in terms of a polynomial of degree \( n \)). -

    - -

    This means that the variance for these elements will be zero and will -cause problems when we set up the correlation matrix. We can simply -drop these elements and construct a correlation -matrix without these elements. -

    @@ -467,8 +416,6 @@ matrix without these elements.

  • 70
  • 71
  • 72
  • -
  • 73
  • -
  • 74
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs065.html b/doc/pub/week35/html/._week35-bs065.html index 400760e90..9ceef4a9e 100644 --- a/doc/pub/week35/html/._week35-bs065.html +++ b/doc/pub/week35/html/._week35-bs065.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,41 +360,45 @@ MathJax.Hub.Config({

     

     

     

    -

    Rewriting the Covariance and/or Correlation Matrix

    +

    What does it mean?

    + +

    This means the vectors \( \boldsymbol{v}_i \) of the orthogonal matrix \( \boldsymbol{V} \) +are the eigenvectors of the matrix \( \boldsymbol{X}^T\boldsymbol{X} \) with eigenvalues +given by the singular values squared, that is +

    -

    We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix \( \boldsymbol{X} \) as

    $$ -\boldsymbol{C}[\boldsymbol{x}] = \frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}= \mathbb{E}[\boldsymbol{X}^T\boldsymbol{X}]. +\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{v}_i=\boldsymbol{v}_i\sigma_i^2. $$ -

    To see this let us simply look at a design matrix \( \boldsymbol{X}\in {\mathbb{R}}^{2\times 2} \)

    +

    In other words, each non-zero singular value of \( \boldsymbol{X} \) is a positive +square root of an eigenvalue of \( \boldsymbol{X}^T\boldsymbol{X} \). It means also that +the columns of \( \boldsymbol{V} \) are the eigenvectors of +\( \boldsymbol{X}^T\boldsymbol{X} \). Since we have ordered the singular values of +\( \boldsymbol{X} \) in a descending order, it means that the column vectors +\( \boldsymbol{v}_i \) are hierarchically ordered by how much correlation they +encode from the columns of \( \boldsymbol{X} \). +

    + +

    Note that these are also the eigenvectors and eigenvalues of the +Hessian matrix. Note also that the Hessian matrix we are discussing here is from a cost function defined by the mean squared error only. +

    + +

    If we now recall the definition of the covariance matrix (not using +Bessel's correction) we have +

    + $$ -\boldsymbol{X}=\begin{bmatrix} -x_{00} & x_{01}\\ -x_{10} & x_{11}\\ -\end{bmatrix}=\begin{bmatrix} -\boldsymbol{x}_{0} & \boldsymbol{x}_{1}\\ -\end{bmatrix}. +\boldsymbol{C}[\boldsymbol{X}]=\frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}, $$ -

    If we then compute the expectation value (note the \( 1/n \) factor instead of \( 1/(n-1) \))

    -$$ -\mathbb{E}[\boldsymbol{X}^T\boldsymbol{X}] = \frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}=\frac{1}{n}\begin{bmatrix} -x_{00}^2+x_{10}^2 & x_{00}x_{01}+x_{10}x_{11}\\ -x_{01}x_{00}+x_{11}x_{10} & x_{01}^2+x_{11}^2\\ -\end{bmatrix}, -$$ - -

    which is just

    -$$ -\boldsymbol{C}[\boldsymbol{x}_0,\boldsymbol{x}_1] = \boldsymbol{C}[\boldsymbol{x}]=\begin{bmatrix} \mathrm{var}[\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_1] \\ - \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_0] & \mathrm{var}[\boldsymbol{x}_1] \\ - \end{bmatrix}, -$$ - -

    where we wrote $$\boldsymbol{C}[\boldsymbol{x}_0,\boldsymbol{x}_1] = \boldsymbol{C}[\boldsymbol{x}]$$ to indicate that this is the covariance of the vectors \( \boldsymbol{x} \) of the design/feature matrix \( \boldsymbol{X} \).

    - -

    It is easy to generalize this to a matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \).

    +

    meaning that every squared non-singular value of \( \boldsymbol{X} \) divided by \( n \) ( +the number of samples) are the eigenvalues of the covariance +matrix. Every singular value of \( \boldsymbol{X} \) is thus a positive square +root of an eigenvalue of \( \boldsymbol{X}^T\boldsymbol{X} \). If the matrix \( \boldsymbol{X} \) is +self-adjoint, the singular values of \( \boldsymbol{X} \) are equal to the +absolute value of the eigenvalues of \( \boldsymbol{X} \). +

    @@ -424,8 +421,6 @@ $$

  • 70
  • 71
  • 72
  • -
  • 73
  • -
  • 74
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs066.html b/doc/pub/week35/html/._week35-bs066.html index 3ac5abb57..56f321783 100644 --- a/doc/pub/week35/html/._week35-bs066.html +++ b/doc/pub/week35/html/._week35-bs066.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,39 +360,39 @@ MathJax.Hub.Config({

     

     

     

    -

    Linking with the SVD

    +

    And finally \( \boldsymbol{X}\boldsymbol{X}^T \)

    -

    We saw earlier that

    -$$ -\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{V}^T. -$$ - -

    Since the matrices here have dimension \( p\times p \), with \( p \) corresponding to the singular values, we defined earlier the matrix

    -$$ -\boldsymbol{\Sigma}^T\boldsymbol{\Sigma} = \begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0}\\ \end{bmatrix}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} \\ \boldsymbol{0}\\ \end{bmatrix}, -$$ - -

    where the tilde-matrix \( \tilde{\boldsymbol{\Sigma}} \) is a matrix of dimension \( p\times p \) containing only the singular values \( \sigma_i \), that is

    +

    For \( \boldsymbol{X}\boldsymbol{X}^T \) we found

    $$ -\tilde{\boldsymbol{\Sigma}}=\begin{bmatrix} \sigma_0 & 0 & 0 & \dots & 0 & 0 \\ - 0 & \sigma_1 & 0 & \dots & 0 & 0 \\ - 0 & 0 & \sigma_2 & \dots & 0 & 0 \\ - 0 & 0 & 0 & \dots & \sigma_{p-2} & 0 \\ - 0 & 0 & 0 & \dots & 0 & \sigma_{p-1} \\ -\end{bmatrix}, +\boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T=\boldsymbol{U}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{U}^T. $$ -

    meaning we can write

    +

    Since the matrices here have dimension \( n\times n \), we have

    $$ -\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\tilde{\boldsymbol{\Sigma}}^2\boldsymbol{V}^T. +\boldsymbol{\Sigma}\boldsymbol{\Sigma}^T = \begin{bmatrix} \tilde{\boldsymbol{\Sigma}} \\ \boldsymbol{0}\\ \end{bmatrix}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} \boldsymbol{0}\\ \end{bmatrix}=\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0} \\ \boldsymbol{0} & \boldsymbol{0}\\ \end{bmatrix}, $$ -

    Multiplying from the right with \( \boldsymbol{V} \) (using the orthogonality of \( \boldsymbol{V} \)) we get

    +

    leading to

    $$ -\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{V}=\boldsymbol{V}\tilde{\boldsymbol{\Sigma}}^2. +\boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{U}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0} \\ \boldsymbol{0} & \boldsymbol{0}\\ \end{bmatrix}\boldsymbol{U}^T. $$ +

    Multiplying with \( \boldsymbol{U} \) from the right gives us the eigenvalue problem

    +$$ +(\boldsymbol{X}\boldsymbol{X}^T)\boldsymbol{U}=\boldsymbol{U}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0} \\ \boldsymbol{0} & \boldsymbol{0}\\ \end{bmatrix}. +$$ + +

    It means that the eigenvalues of \( \boldsymbol{X}\boldsymbol{X}^T \) are again given by +the non-zero singular values plus now a series of zeros. The column +vectors of \( \boldsymbol{U} \) are the eigenvectors of \( \boldsymbol{X}\boldsymbol{X}^T \) and +measure how much correlations are contained in the rows of \( \boldsymbol{X} \). +

    + +

    Since we will mainly be interested in the correlations among the features +of our data (the columns of \( \boldsymbol{X} \), the quantity of interest for us are the non-zero singular +values and the column vectors of \( \boldsymbol{V} \). +

    @@ -421,8 +414,6 @@ $$

  • 70
  • 71
  • 72
  • -
  • 73
  • -
  • 74
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs067.html b/doc/pub/week35/html/._week35-bs067.html index 4479b7afc..822327f6a 100644 --- a/doc/pub/week35/html/._week35-bs067.html +++ b/doc/pub/week35/html/._week35-bs067.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,45 +360,59 @@ MathJax.Hub.Config({

     

     

     

    -

    What does it mean?

    +

    Ridge and LASSO Regression

    -

    This means the vectors \( \boldsymbol{v}_i \) of the orthogonal matrix \( \boldsymbol{V} \) -are the eigenvectors of the matrix \( \boldsymbol{X}^T\boldsymbol{X} \) with eigenvalues -given by the singular values squared, that is +

    Let us remind ourselves about the expression for the standard Mean Squared Error (MSE) which we used to define our cost function and the equations for the ordinary least squares (OLS) method, that is +our optimization problem is +

    +$$ +{\displaystyle \min_{\boldsymbol{\beta}\in {\mathbb{R}}^{p}}}\frac{1}{n}\left\{\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)^T\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)\right\}. +$$ + +

    or we can state it as

    +$$ +{\displaystyle \min_{\boldsymbol{\beta}\in +{\mathbb{R}}^{p}}}\frac{1}{n}\sum_{i=0}^{n-1}\left(y_i-\tilde{y}_i\right)^2=\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2, +$$ + +

    where we have used the definition of a norm-2 vector, that is

    +$$ +\vert\vert \boldsymbol{x}\vert\vert_2 = \sqrt{\sum_i x_i^2}. +$$ + +

    By minimizing the above equation with respect to the parameters +\( \boldsymbol{\beta} \) we could then obtain an analytical expression for the +parameters \( \boldsymbol{\beta} \). We can add a regularization parameter \( \lambda \) by +defining a new cost function to be optimized, that is

    $$ -\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{v}_i=\boldsymbol{v}_i\sigma_i^2. +{\displaystyle \min_{\boldsymbol{\beta}\in +{\mathbb{R}}^{p}}}\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_2^2 $$ -

    In other words, each non-zero singular value of \( \boldsymbol{X} \) is a positive -square root of an eigenvalue of \( \boldsymbol{X}^T\boldsymbol{X} \). It means also that -the columns of \( \boldsymbol{V} \) are the eigenvectors of -\( \boldsymbol{X}^T\boldsymbol{X} \). Since we have ordered the singular values of -\( \boldsymbol{X} \) in a descending order, it means that the column vectors -\( \boldsymbol{v}_i \) are hierarchically ordered by how much correlation they -encode from the columns of \( \boldsymbol{X} \). -

    - -

    Note that these are also the eigenvectors and eigenvalues of the -Hessian matrix. Note also that the Hessian matrix we are discussing here is from a cost function defined by the mean squared error only. -

    - -

    If we now recall the definition of the covariance matrix (not using -Bessel's correction) we have +

    which leads to the Ridge regression minimization problem where we +require that \( \vert\vert \boldsymbol{\beta}\vert\vert_2^2\le t \), where \( t \) is +a finite number larger than zero. By defining

    $$ -\boldsymbol{C}[\boldsymbol{X}]=\frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}, +C(\boldsymbol{X},\boldsymbol{\beta})=\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_1, +$$ + +

    we have a new optimization equation

    +$$ +{\displaystyle \min_{\boldsymbol{\beta}\in +{\mathbb{R}}^{p}}}\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_1 +$$ + +

    which leads to Lasso regression. Lasso stands for least absolute shrinkage and selection operator.

    + +

    Here we have defined the norm-1 as

    +$$ +\vert\vert \boldsymbol{x}\vert\vert_1 = \sum_i \vert x_i\vert. $$ -

    meaning that every squared non-singular value of \( \boldsymbol{X} \) divided by \( n \) ( -the number of samples) are the eigenvalues of the covariance -matrix. Every singular value of \( \boldsymbol{X} \) is thus a positive square -root of an eigenvalue of \( \boldsymbol{X}^T\boldsymbol{X} \). If the matrix \( \boldsymbol{X} \) is -self-adjoint, the singular values of \( \boldsymbol{X} \) are equal to the -absolute value of the eigenvalues of \( \boldsymbol{X} \). -

    @@ -426,8 +433,6 @@ absolute value of the eigenvalues of \( \boldsymbol{X} \).

  • 70
  • 71
  • 72
  • -
  • 73
  • -
  • 74
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs068.html b/doc/pub/week35/html/._week35-bs068.html index c129f466f..bfd54c0a3 100644 --- a/doc/pub/week35/html/._week35-bs068.html +++ b/doc/pub/week35/html/._week35-bs068.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,39 +360,67 @@ MathJax.Hub.Config({

     

     

     

    -

    And finally \( \boldsymbol{X}\boldsymbol{X}^T \)

    +

    Deriving the Ridge Regression Equations

    -

    For \( \boldsymbol{X}\boldsymbol{X}^T \) we found

    +

    Using the matrix-vector expression for Ridge regression and dropping the parameter \( 1/n \) in front of the standard means squared error equation, we have

    $$ -\boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T=\boldsymbol{U}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{U}^T. +C(\boldsymbol{X},\boldsymbol{\beta})=\left\{(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})^T(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\right\}+\lambda\boldsymbol{\beta}^T\boldsymbol{\beta}, $$ -

    Since the matrices here have dimension \( n\times n \), we have

    +

    and +taking the derivatives with respect to \( \boldsymbol{\beta} \) we obtain then +a slightly modified matrix inversion problem which for finite values +of \( \lambda \) does not suffer from singularity problems. We obtain +the optimal parameters +

    $$ -\boldsymbol{\Sigma}\boldsymbol{\Sigma}^T = \begin{bmatrix} \tilde{\boldsymbol{\Sigma}} \\ \boldsymbol{0}\\ \end{bmatrix}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} \boldsymbol{0}\\ \end{bmatrix}=\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0} \\ \boldsymbol{0} & \boldsymbol{0}\\ \end{bmatrix}, +\hat{\boldsymbol{\beta}}_{\mathrm{Ridge}} = \left(\boldsymbol{X}^T\boldsymbol{X}+\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}, $$ -

    leading to

    +

    with \( \boldsymbol{I} \) being a \( p\times p \) identity matrix with the constraint that

    + $$ -\boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{U}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0} \\ \boldsymbol{0} & \boldsymbol{0}\\ \end{bmatrix}\boldsymbol{U}^T. +\sum_{i=0}^{p-1} \beta_i^2 \leq t, $$ -

    Multiplying with \( \boldsymbol{U} \) from the right gives us the eigenvalue problem

    +

    with \( t \) a finite positive number.

    + +

    If we keep the \( 1/n \) factor, the equation for the optimal \( \beta \) changes to

    $$ -(\boldsymbol{X}\boldsymbol{X}^T)\boldsymbol{U}=\boldsymbol{U}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0} \\ \boldsymbol{0} & \boldsymbol{0}\\ \end{bmatrix}. +\hat{\boldsymbol{\beta}}_{\mathrm{Ridge}} = \left(\boldsymbol{X}^T\boldsymbol{X}+n\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}. $$ -

    It means that the eigenvalues of \( \boldsymbol{X}\boldsymbol{X}^T \) are again given by -the non-zero singular values plus now a series of zeros. The column -vectors of \( \boldsymbol{U} \) are the eigenvectors of \( \boldsymbol{X}\boldsymbol{X}^T \) and -measure how much correlations are contained in the rows of \( \boldsymbol{X} \). +

    In many textbooks the \( 1/n \) term is often omitted. Note that a library like Scikit-Learn does not include the \( 1/n \) factor in the setup of the cost function.

    + +

    When we compare this with the ordinary least squares result we have

    +$$ +\hat{\boldsymbol{\beta}}_{\mathrm{OLS}} = \left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}, +$$ + +

    which can lead to singular matrices. However, with the SVD, we can always compute the inverse of the matrix \( \boldsymbol{X}^T\boldsymbol{X} \).

    + +

    We see that Ridge regression is nothing but the standard OLS with a +modified diagonal term added to \( \boldsymbol{X}^T\boldsymbol{X} \). The consequences, in +particular for our discussion of the bias-variance tradeoff are rather +interesting. We will see that for specific values of \( \lambda \), we may +even reduce the variance of the optimal parameters \( \boldsymbol{\beta} \). These topics and other related ones, will be discussed after the more linear algebra oriented analysis here.

    -

    Since we will mainly be interested in the correlations among the features -of our data (the columns of \( \boldsymbol{X} \), the quantity of interest for us are the non-zero singular -values and the column vectors of \( \boldsymbol{V} \). +

    Using our insights about the SVD of the design matrix \( \boldsymbol{X} \) +We have already analyzed the OLS solutions in terms of the eigenvectors (the columns) of the right singular value matrix \( \boldsymbol{U} \) as

    +$$ +\tilde{\boldsymbol{y}}_{\mathrm{OLS}}=\boldsymbol{X}\boldsymbol{\beta} =\boldsymbol{U}\boldsymbol{U}^T\boldsymbol{y}. +$$ + +

    For Ridge regression this becomes

    + +$$ +\tilde{\boldsymbol{y}}_{\mathrm{Ridge}}=\boldsymbol{X}\boldsymbol{\beta}_{\mathrm{Ridge}} = \boldsymbol{U\Sigma V^T}\left(\boldsymbol{V}\boldsymbol{\Sigma}^2\boldsymbol{V}^T+\lambda\boldsymbol{I} \right)^{-1}(\boldsymbol{U\Sigma V^T})^T\boldsymbol{y}=\sum_{j=0}^{p-1}\boldsymbol{u}_j\boldsymbol{u}_j^T\frac{\sigma_j^2}{\sigma_j^2+\lambda}\boldsymbol{y}, +$$ + +

    with the vectors \( \boldsymbol{u}_j \) being the columns of \( \boldsymbol{U} \) from the SVD of the matrix \( \boldsymbol{X} \).

    @@ -419,8 +440,6 @@ values and the column vectors of \( \boldsymbol{V} \).

  • 70
  • 71
  • 72
  • -
  • 73
  • -
  • 74
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs069.html b/doc/pub/week35/html/._week35-bs069.html index cb6bde732..8ccceef93 100644 --- a/doc/pub/week35/html/._week35-bs069.html +++ b/doc/pub/week35/html/._week35-bs069.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,59 +360,22 @@ MathJax.Hub.Config({

     

     

     

    -

    Ridge and LASSO Regression

    +

    Interpreting the Ridge results

    + +

    Since \( \lambda \geq 0 \), it means that compared to OLS, we have

    -

    Let us remind ourselves about the expression for the standard Mean Squared Error (MSE) which we used to define our cost function and the equations for the ordinary least squares (OLS) method, that is -our optimization problem is -

    $$ -{\displaystyle \min_{\boldsymbol{\beta}\in {\mathbb{R}}^{p}}}\frac{1}{n}\left\{\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)^T\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)\right\}. +\frac{\sigma_j^2}{\sigma_j^2+\lambda} \leq 1. $$ -

    or we can state it as

    -$$ -{\displaystyle \min_{\boldsymbol{\beta}\in -{\mathbb{R}}^{p}}}\frac{1}{n}\sum_{i=0}^{n-1}\left(y_i-\tilde{y}_i\right)^2=\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2, -$$ - -

    where we have used the definition of a norm-2 vector, that is

    -$$ -\vert\vert \boldsymbol{x}\vert\vert_2 = \sqrt{\sum_i x_i^2}. -$$ - -

    By minimizing the above equation with respect to the parameters -\( \boldsymbol{\beta} \) we could then obtain an analytical expression for the -parameters \( \boldsymbol{\beta} \). We can add a regularization parameter \( \lambda \) by -defining a new cost function to be optimized, that is +

    Ridge regression finds the coordinates of \( \boldsymbol{y} \) with respect to the +orthonormal basis \( \boldsymbol{U} \), it then shrinks the coordinates by +\( \frac{\sigma_j^2}{\sigma_j^2+\lambda} \). Recall that the SVD has +eigenvalues ordered in a descending way, that is \( \sigma_i \geq +\sigma_{i+1} \).

    -$$ -{\displaystyle \min_{\boldsymbol{\beta}\in -{\mathbb{R}}^{p}}}\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_2^2 -$$ - -

    which leads to the Ridge regression minimization problem where we -require that \( \vert\vert \boldsymbol{\beta}\vert\vert_2^2\le t \), where \( t \) is -a finite number larger than zero. By defining -

    - -$$ -C(\boldsymbol{X},\boldsymbol{\beta})=\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_1, -$$ - -

    we have a new optimization equation

    -$$ -{\displaystyle \min_{\boldsymbol{\beta}\in -{\mathbb{R}}^{p}}}\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_1 -$$ - -

    which leads to Lasso regression. Lasso stands for least absolute shrinkage and selection operator.

    - -

    Here we have defined the norm-1 as

    -$$ -\vert\vert \boldsymbol{x}\vert\vert_1 = \sum_i \vert x_i\vert. -$$ - +

    For small eigenvalues \( \sigma_i \) it means that their contributions become less important, a fact which can be used to reduce the number of degrees of freedom. More about this when we have covered the material on a statistical interpretation of various linear regression methods.

    @@ -438,8 +394,6 @@ $$

  • 70
  • 71
  • 72
  • -
  • 73
  • -
  • 74
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs070.html b/doc/pub/week35/html/._week35-bs070.html index 477c6c391..a407ca564 100644 --- a/doc/pub/week35/html/._week35-bs070.html +++ b/doc/pub/week35/html/._week35-bs070.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,67 +360,35 @@ MathJax.Hub.Config({

     

     

     

    -

    Deriving the Ridge Regression Equations

    +

    More interpretations

    -

    Using the matrix-vector expression for Ridge regression and dropping the parameter \( 1/n \) in front of the standard means squared error equation, we have

    +

    For the sake of simplicity, let us assume that the design matrix is orthonormal, that is

    $$ -C(\boldsymbol{X},\boldsymbol{\beta})=\left\{(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})^T(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\right\}+\lambda\boldsymbol{\beta}^T\boldsymbol{\beta}, +\boldsymbol{X}^T\boldsymbol{X}=(\boldsymbol{X}^T\boldsymbol{X})^{-1} =\boldsymbol{I}. $$ -

    and -taking the derivatives with respect to \( \boldsymbol{\beta} \) we obtain then -a slightly modified matrix inversion problem which for finite values -of \( \lambda \) does not suffer from singularity problems. We obtain -the optimal parameters -

    +

    In this case the standard OLS results in

    $$ -\hat{\boldsymbol{\beta}}_{\mathrm{Ridge}} = \left(\boldsymbol{X}^T\boldsymbol{X}+\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}, +\boldsymbol{\beta}^{\mathrm{OLS}} = \boldsymbol{X}^T\boldsymbol{y}=\sum_{i=0}^{n-1}\boldsymbol{u}_i\boldsymbol{u}_i^T\boldsymbol{y}, $$ -

    with \( \boldsymbol{I} \) being a \( p\times p \) identity matrix with the constraint that

    +

    and

    $$ -\sum_{i=0}^{p-1} \beta_i^2 \leq t, +\boldsymbol{\beta}^{\mathrm{Ridge}} = \left(\boldsymbol{I}+\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}=\left(1+\lambda\right)^{-1}\boldsymbol{\beta}^{\mathrm{OLS}}, $$ -

    with \( t \) a finite positive number.

    - -

    If we keep the \( 1/n \) factor, the equation for the optimal \( \beta \) changes to

    -$$ -\hat{\boldsymbol{\beta}}_{\mathrm{Ridge}} = \left(\boldsymbol{X}^T\boldsymbol{X}+n\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}. -$$ - -

    In many textbooks the \( 1/n \) term is often omitted. Note that a library like Scikit-Learn does not include the \( 1/n \) factor in the setup of the cost function.

    - -

    When we compare this with the ordinary least squares result we have

    -$$ -\hat{\boldsymbol{\beta}}_{\mathrm{OLS}} = \left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}, -$$ - -

    which can lead to singular matrices. However, with the SVD, we can always compute the inverse of the matrix \( \boldsymbol{X}^T\boldsymbol{X} \).

    - -

    We see that Ridge regression is nothing but the standard OLS with a -modified diagonal term added to \( \boldsymbol{X}^T\boldsymbol{X} \). The consequences, in -particular for our discussion of the bias-variance tradeoff are rather -interesting. We will see that for specific values of \( \lambda \), we may -even reduce the variance of the optimal parameters \( \boldsymbol{\beta} \). These topics and other related ones, will be discussed after the more linear algebra oriented analysis here. +

    that is the Ridge estimator scales the OLS estimator by the inverse of a factor \( 1+\lambda \), and +the Ridge estimator converges to zero when the hyperparameter goes to +infinity.

    -

    Using our insights about the SVD of the design matrix \( \boldsymbol{X} \) -We have already analyzed the OLS solutions in terms of the eigenvectors (the columns) of the right singular value matrix \( \boldsymbol{U} \) as +

    We will come back to more interpreations after we have gone through some of the statistical analysis part.

    + +

    For more discussions of Ridge and Lasso regression, Wessel van Wieringen's article is highly recommended. +Similarly, Mehta et al's article is also recommended.

    -$$ -\tilde{\boldsymbol{y}}_{\mathrm{OLS}}=\boldsymbol{X}\boldsymbol{\beta} =\boldsymbol{U}\boldsymbol{U}^T\boldsymbol{y}. -$$ - -

    For Ridge regression this becomes

    - -$$ -\tilde{\boldsymbol{y}}_{\mathrm{Ridge}}=\boldsymbol{X}\boldsymbol{\beta}_{\mathrm{Ridge}} = \boldsymbol{U\Sigma V^T}\left(\boldsymbol{V}\boldsymbol{\Sigma}^2\boldsymbol{V}^T+\lambda\boldsymbol{I} \right)^{-1}(\boldsymbol{U\Sigma V^T})^T\boldsymbol{y}=\sum_{j=0}^{p-1}\boldsymbol{u}_j\boldsymbol{u}_j^T\frac{\sigma_j^2}{\sigma_j^2+\lambda}\boldsymbol{y}, -$$ - -

    with the vectors \( \boldsymbol{u}_j \) being the columns of \( \boldsymbol{U} \) from the SVD of the matrix \( \boldsymbol{X} \).

    @@ -445,8 +406,6 @@ $$

  • 70
  • 71
  • 72
  • -
  • 73
  • -
  • 74
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs071.html b/doc/pub/week35/html/._week35-bs071.html index 010763ae4..c2b0cfed1 100644 --- a/doc/pub/week35/html/._week35-bs071.html +++ b/doc/pub/week35/html/._week35-bs071.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -367,22 +360,36 @@ MathJax.Hub.Config({

     

     

     

    -

    Interpreting the Ridge results

    +

    Deriving the Lasso Regression Equations

    -

    Since \( \lambda \geq 0 \), it means that compared to OLS, we have

    +

    Using the matrix-vector expression for Lasso regression, we have the following cost function

    $$ -\frac{\sigma_j^2}{\sigma_j^2+\lambda} \leq 1. +C(\boldsymbol{X},\boldsymbol{\beta})=\frac{1}{n}\left\{(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})^T(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\right\}+\lambda\vert\vert\boldsymbol{\beta}\vert\vert_1, $$ -

    Ridge regression finds the coordinates of \( \boldsymbol{y} \) with respect to the -orthonormal basis \( \boldsymbol{U} \), it then shrinks the coordinates by -\( \frac{\sigma_j^2}{\sigma_j^2+\lambda} \). Recall that the SVD has -eigenvalues ordered in a descending way, that is \( \sigma_i \geq -\sigma_{i+1} \). -

    +

    Taking the derivative with respect to \( \boldsymbol{\beta} \) and recalling that the derivative of the absolute value is (we drop the boldfaced vector symbol for simplicty)

    +$$ +\frac{d \vert \beta\vert}{d \beta}=\mathrm{sgn}(\beta)=\left\{\begin{array}{cc} 1 & \beta > 0 \\-1 & \beta < 0, \end{array}\right. +$$ -

    For small eigenvalues \( \sigma_i \) it means that their contributions become less important, a fact which can be used to reduce the number of degrees of freedom. More about this when we have covered the material on a statistical interpretation of various linear regression methods.

    +

    we have that the derivative of the cost function is

    + +$$ +\frac{\partial C(\boldsymbol{X},\boldsymbol{\beta})}{\partial \boldsymbol{\beta}}=-\frac{2}{n}\boldsymbol{X}^T(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})+\lambda sgn(\boldsymbol{\beta})=0, +$$ + +

    and reordering we have

    +$$ +\boldsymbol{X}^T\boldsymbol{X}\boldsymbol{\beta}+\frac{n}{2}\lambda sgn(\boldsymbol{\beta})=2\boldsymbol{X}^T\boldsymbol{y}. +$$ + +

    We can redefine \( \lambda \) to absorb the constant \( n/2 \) and we rewrite the last equation as

    +$$ +\boldsymbol{X}^T\boldsymbol{X}\boldsymbol{\beta}+\lambda sgn(\boldsymbol{\beta})=2\boldsymbol{X}^T\boldsymbol{y}. +$$ + +

    This equation does not lead to a nice analytical equation as in either Ridge regression or ordinary least squares. This equation can however be solved by using standard convex optimization algorithms using for example the Python package CVXOPT. We will discuss this later.

    @@ -399,9 +406,6 @@ eigenvalues ordered in a descending way, that is \( \sigma_i \geq

  • 70
  • 71
  • 72
  • -
  • 73
  • -
  • 74
  • -
  • »
  • diff --git a/doc/pub/week35/html/week35-bs.html b/doc/pub/week35/html/week35-bs.html index a463d20ef..3b671f811 100644 --- a/doc/pub/week35/html/week35-bs.html +++ b/doc/pub/week35/html/week35-bs.html @@ -38,11 +38,11 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -144,11 +144,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -284,7 +279,7 @@ MathJax.Hub.Config({ @@ -410,7 +403,7 @@ MathJax.Hub.Config({
  • 9
  • 10
  • ...
  • -
  • 74
  • +
  • 72
  • »
  • diff --git a/doc/pub/week35/html/week35-reveal.html b/doc/pub/week35/html/week35-reveal.html index f070cf033..77435f387 100644 --- a/doc/pub/week35/html/week35-reveal.html +++ b/doc/pub/week35/html/week35-reveal.html @@ -212,11 +212,13 @@ MathJax.Hub.Config({

    1. See lecture notes for week 35 at https://compphysics.github.io/MachineLearning/doc/web/course.html
    2. Goodfellow, Bengio and Courville, Deep Learning, chapter 2 on linear algebra and sections 3.1-3.10 on elements of statistics (background)
    3. +

    4. Raschka et al on preprocessing of data, relevant for exercise 3 this week, see chapter 4.
    5. +

    6. For exercise 1 of week 35, the book by A. Aldo Faisal, Cheng Soon Ong, and Marc Peter Deisenroth on the Mathematics of Machine Learning, may be very relevant. In particular chapter 5 at URL"https://mml-book.github.io/" (section 5.5 on derivatives) is very useful for exercise 1 this coming week.
    -

    Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week

    +

    For exercise sessions: Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week

    We need first a reminder from last week about linear regression.

    @@ -1917,377 +1919,6 @@ regression next week.

    -
    -

    The Boston housing data example

    - -

    The Boston housing -data set was originally a part of UCI Machine Learning Repository -and has been removed now. The data set is now included in Scikit-Learn's -library. There are 506 samples and 13 feature (predictor) variables -in this data set. The objective is to predict the value of prices of -the house using the features (predictors) listed here. -

    - -

    The features/predictors are

    -
      -

    1. CRIM: Per capita crime rate by town
    2. -

    3. ZN: Proportion of residential land zoned for lots over 25000 square feet
    4. -

    5. INDUS: Proportion of non-retail business acres per town
    6. -

    7. CHAS: Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)
    8. -

    9. NOX: Nitric oxide concentration (parts per 10 million)
    10. -

    11. RM: Average number of rooms per dwelling
    12. -

    13. AGE: Proportion of owner-occupied units built prior to 1940
    14. -

    15. DIS: Weighted distances to five Boston employment centers
    16. -

    17. RAD: Index of accessibility to radial highways
    18. -

    19. TAX: Full-value property tax rate per USD10000
    20. -

    21. B: \( 1000(Bk - 0.63)^2 \), where \( Bk \) is the proportion of [people of African American descent] by town
    22. -

    23. LSTAT: Percentage of lower status of the population
    24. -

    25. MEDV: Median value of owner-occupied homes in USD 1000s
    26. -
    -
    - -
    -

    Housing data, the code

    -

    We start by importing the libraries

    - - -
    -
    -
    -
    -
    -
    import numpy as np
    -import matplotlib.pyplot as plt 
    -
    -import pandas as pd  
    -import seaborn as sns 
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    and load the Boston Housing DataSet from Scikit-Learn

    - - - -
    -
    -
    -
    -
    -
    from sklearn.datasets import load_boston
    -
    -boston_dataset = load_boston()
    -
    -# boston_dataset is a dictionary
    -# let's check what it contains
    -boston_dataset.keys()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Then we invoke Pandas

    - - -
    -
    -
    -
    -
    -
    boston = pd.DataFrame(boston_dataset.data, columns=boston_dataset.feature_names)
    -boston.head()
    -boston['MEDV'] = boston_dataset.target
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    and preprocess the data

    - - -
    -
    -
    -
    -
    -
    # check for missing values in all the columns
    -boston.isnull().sum()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    We can then visualize the data

    - - -
    -
    -
    -
    -
    -
    # set the size of the figure
    -sns.set(rc={'figure.figsize':(11.7,8.27)})
    -
    -# plot a histogram showing the distribution of the target values
    -sns.distplot(boston['MEDV'], bins=30)
    -plt.show()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    It is now useful to look at the correlation matrix

    - - -
    -
    -
    -
    -
    -
    # compute the pair wise correlation for all columns  
    -correlation_matrix = boston.corr().round(2)
    -# use the heatmap function from seaborn to plot the correlation matrix
    -# annot = True to print the values inside the square
    -sns.heatmap(data=correlation_matrix, annot=True)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    From the above coorelation plot we can see that MEDV is strongly correlated to LSTAT and RM. We see also that RAD and TAX are stronly correlated, but we don't include this in our features together to avoid multi-colinearity

    - - - -
    -
    -
    -
    -
    -
    plt.figure(figsize=(20, 5))
    -
    -features = ['LSTAT', 'RM']
    -target = boston['MEDV']
    -
    -for i, col in enumerate(features):
    -    plt.subplot(1, len(features) , i+1)
    -    x = boston[col]
    -    y = target
    -    plt.scatter(x, y, marker='o')
    -    plt.title(col)
    -    plt.xlabel(col)
    -    plt.ylabel('MEDV')
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Now we start training our model

    - - -
    -
    -
    -
    -
    -
    X = pd.DataFrame(np.c_[boston['LSTAT'], boston['RM']], columns = ['LSTAT','RM'])
    -Y = boston['MEDV']
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    We split the data into training and test sets

    - - - -
    -
    -
    -
    -
    -
    from sklearn.model_selection import train_test_split
    -
    -# splits the training and test data set in 80% : 20%
    -# assign random_state to any value.This ensures consistency.
    -X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state=5)
    -print(X_train.shape)
    -print(X_test.shape)
    -print(Y_train.shape)
    -print(Y_test.shape)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Then we use the linear regression functionality from Scikit-Learn

    - - -
    -
    -
    -
    -
    -
    from sklearn.linear_model import LinearRegression
    -from sklearn.metrics import mean_squared_error, r2_score
    -
    -lin_model = LinearRegression()
    -lin_model.fit(X_train, Y_train)
    -
    -# model evaluation for training set
    -
    -y_train_predict = lin_model.predict(X_train)
    -rmse = (np.sqrt(mean_squared_error(Y_train, y_train_predict)))
    -r2 = r2_score(Y_train, y_train_predict)
    -
    -print("The model performance for training set")
    -print("--------------------------------------")
    -print('RMSE is {}'.format(rmse))
    -print('R2 score is {}'.format(r2))
    -print("\n")
    -
    -# model evaluation for testing set
    -
    -y_test_predict = lin_model.predict(X_test)
    -# root mean square error of the model
    -rmse = (np.sqrt(mean_squared_error(Y_test, y_test_predict)))
    -
    -# r-squared score of the model
    -r2 = r2_score(Y_test, y_test_predict)
    -
    -print("The model performance for testing set")
    -print("--------------------------------------")
    -print('RMSE is {}'.format(rmse))
    -print('R2 score is {}'.format(r2))
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -
    -
    -
    -
    -
    -
    # plotting the y_test vs y_pred
    -# ideally should have been a straight line
    -plt.scatter(Y_test, y_test_predict)
    -plt.show()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -

    Material for lecture Monday, August 26

    diff --git a/doc/pub/week35/html/week35-solarized.html b/doc/pub/week35/html/week35-solarized.html index 49a3f8aba..3e2cd5d1a 100644 --- a/doc/pub/week35/html/week35-solarized.html +++ b/doc/pub/week35/html/week35-solarized.html @@ -65,11 +65,11 @@ div.toc p,a { {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -171,11 +171,6 @@ div.toc p,a { 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -333,9 +328,11 @@ MathJax.Hub.Config({
    1. See lecture notes for week 35 at https://compphysics.github.io/MachineLearning/doc/web/course.html
    2. Goodfellow, Bengio and Courville, Deep Learning, chapter 2 on linear algebra and sections 3.1-3.10 on elements of statistics (background)
    3. +
    4. Raschka et al on preprocessing of data, relevant for exercise 3 this week, see chapter 4.
    5. +
    6. For exercise 1 of week 35, the book by A. Aldo Faisal, Cheng Soon Ong, and Marc Peter Deisenroth on the Mathematics of Machine Learning, may be very relevant. In particular chapter 5 at URL"https://mml-book.github.io/" (section 5.5 on derivatives) is very useful for exercise 1 this coming week.










    -

    Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week

    +

    For exercise sessions: Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week

    We need first a reminder from last week about linear regression.

    @@ -1882,375 +1879,6 @@ intercept. This becomes more important when we discuss Ridge and Lasso regression next week.

    - -

    The Boston housing data example

    - -

    The Boston housing -data set was originally a part of UCI Machine Learning Repository -and has been removed now. The data set is now included in Scikit-Learn's -library. There are 506 samples and 13 feature (predictor) variables -in this data set. The objective is to predict the value of prices of -the house using the features (predictors) listed here. -

    - -

    The features/predictors are

    -
      -
    1. CRIM: Per capita crime rate by town
    2. -
    3. ZN: Proportion of residential land zoned for lots over 25000 square feet
    4. -
    5. INDUS: Proportion of non-retail business acres per town
    6. -
    7. CHAS: Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)
    8. -
    9. NOX: Nitric oxide concentration (parts per 10 million)
    10. -
    11. RM: Average number of rooms per dwelling
    12. -
    13. AGE: Proportion of owner-occupied units built prior to 1940
    14. -
    15. DIS: Weighted distances to five Boston employment centers
    16. -
    17. RAD: Index of accessibility to radial highways
    18. -
    19. TAX: Full-value property tax rate per USD10000
    20. -
    21. B: \( 1000(Bk - 0.63)^2 \), where \( Bk \) is the proportion of [people of African American descent] by town
    22. -
    23. LSTAT: Percentage of lower status of the population
    24. -
    25. MEDV: Median value of owner-occupied homes in USD 1000s
    26. -
    -









    -

    Housing data, the code

    -

    We start by importing the libraries

    - - -
    -
    -
    -
    -
    -
    import numpy as np
    -import matplotlib.pyplot as plt 
    -
    -import pandas as pd  
    -import seaborn as sns 
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    and load the Boston Housing DataSet from Scikit-Learn

    - - - -
    -
    -
    -
    -
    -
    from sklearn.datasets import load_boston
    -
    -boston_dataset = load_boston()
    -
    -# boston_dataset is a dictionary
    -# let's check what it contains
    -boston_dataset.keys()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Then we invoke Pandas

    - - -
    -
    -
    -
    -
    -
    boston = pd.DataFrame(boston_dataset.data, columns=boston_dataset.feature_names)
    -boston.head()
    -boston['MEDV'] = boston_dataset.target
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    and preprocess the data

    - - -
    -
    -
    -
    -
    -
    # check for missing values in all the columns
    -boston.isnull().sum()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    We can then visualize the data

    - - -
    -
    -
    -
    -
    -
    # set the size of the figure
    -sns.set(rc={'figure.figsize':(11.7,8.27)})
    -
    -# plot a histogram showing the distribution of the target values
    -sns.distplot(boston['MEDV'], bins=30)
    -plt.show()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    It is now useful to look at the correlation matrix

    - - -
    -
    -
    -
    -
    -
    # compute the pair wise correlation for all columns  
    -correlation_matrix = boston.corr().round(2)
    -# use the heatmap function from seaborn to plot the correlation matrix
    -# annot = True to print the values inside the square
    -sns.heatmap(data=correlation_matrix, annot=True)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    From the above coorelation plot we can see that MEDV is strongly correlated to LSTAT and RM. We see also that RAD and TAX are stronly correlated, but we don't include this in our features together to avoid multi-colinearity

    - - - -
    -
    -
    -
    -
    -
    plt.figure(figsize=(20, 5))
    -
    -features = ['LSTAT', 'RM']
    -target = boston['MEDV']
    -
    -for i, col in enumerate(features):
    -    plt.subplot(1, len(features) , i+1)
    -    x = boston[col]
    -    y = target
    -    plt.scatter(x, y, marker='o')
    -    plt.title(col)
    -    plt.xlabel(col)
    -    plt.ylabel('MEDV')
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Now we start training our model

    - - -
    -
    -
    -
    -
    -
    X = pd.DataFrame(np.c_[boston['LSTAT'], boston['RM']], columns = ['LSTAT','RM'])
    -Y = boston['MEDV']
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    We split the data into training and test sets

    - - - -
    -
    -
    -
    -
    -
    from sklearn.model_selection import train_test_split
    -
    -# splits the training and test data set in 80% : 20%
    -# assign random_state to any value.This ensures consistency.
    -X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state=5)
    -print(X_train.shape)
    -print(X_test.shape)
    -print(Y_train.shape)
    -print(Y_test.shape)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Then we use the linear regression functionality from Scikit-Learn

    - - -
    -
    -
    -
    -
    -
    from sklearn.linear_model import LinearRegression
    -from sklearn.metrics import mean_squared_error, r2_score
    -
    -lin_model = LinearRegression()
    -lin_model.fit(X_train, Y_train)
    -
    -# model evaluation for training set
    -
    -y_train_predict = lin_model.predict(X_train)
    -rmse = (np.sqrt(mean_squared_error(Y_train, y_train_predict)))
    -r2 = r2_score(Y_train, y_train_predict)
    -
    -print("The model performance for training set")
    -print("--------------------------------------")
    -print('RMSE is {}'.format(rmse))
    -print('R2 score is {}'.format(r2))
    -print("\n")
    -
    -# model evaluation for testing set
    -
    -y_test_predict = lin_model.predict(X_test)
    -# root mean square error of the model
    -rmse = (np.sqrt(mean_squared_error(Y_test, y_test_predict)))
    -
    -# r-squared score of the model
    -r2 = r2_score(Y_test, y_test_predict)
    -
    -print("The model performance for testing set")
    -print("--------------------------------------")
    -print('RMSE is {}'.format(rmse))
    -print('R2 score is {}'.format(r2))
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -
    -
    -
    -
    -
    -
    # plotting the y_test vs y_pred
    -# ideally should have been a straight line
    -plt.scatter(Y_test, y_test_predict)
    -plt.show()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -









    Material for lecture Monday, August 26

    diff --git a/doc/pub/week35/html/week35.html b/doc/pub/week35/html/week35.html index 5e08c4f45..f4dc35ba5 100644 --- a/doc/pub/week35/html/week35.html +++ b/doc/pub/week35/html/week35.html @@ -142,11 +142,11 @@ div.toc p,a { {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Why Linear Regression (aka Ordinary Least Squares and family), ' - 'repeat from last week', + ('For exercise sessions: Why Linear Regression (aka Ordinary ' + 'Least Squares and family), repeat from last week', 2, None, - 'why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), + 'for-exercise-sessions-why-linear-regression-aka-ordinary-least-squares-and-family-repeat-from-last-week'), ('The equations for ordinary least squares', 2, None, @@ -248,11 +248,6 @@ div.toc p,a { 2, None, 'linear-regression-code-intercept-handling-first'), - ('The Boston housing data example', - 2, - None, - 'the-boston-housing-data-example'), - ('Housing data, the code', 2, None, 'housing-data-the-code'), ('Material for lecture Monday, August 26', 2, None, @@ -410,9 +405,11 @@ MathJax.Hub.Config({
    1. See lecture notes for week 35 at https://compphysics.github.io/MachineLearning/doc/web/course.html
    2. Goodfellow, Bengio and Courville, Deep Learning, chapter 2 on linear algebra and sections 3.1-3.10 on elements of statistics (background)
    3. +
    4. Raschka et al on preprocessing of data, relevant for exercise 3 this week, see chapter 4.
    5. +
    6. For exercise 1 of week 35, the book by A. Aldo Faisal, Cheng Soon Ong, and Marc Peter Deisenroth on the Mathematics of Machine Learning, may be very relevant. In particular chapter 5 at URL"https://mml-book.github.io/" (section 5.5 on derivatives) is very useful for exercise 1 this coming week.










    -

    Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week

    +

    For exercise sessions: Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week

    We need first a reminder from last week about linear regression.

    @@ -1959,375 +1956,6 @@ intercept. This becomes more important when we discuss Ridge and Lasso regression next week.

    - -

    The Boston housing data example

    - -

    The Boston housing -data set was originally a part of UCI Machine Learning Repository -and has been removed now. The data set is now included in Scikit-Learn's -library. There are 506 samples and 13 feature (predictor) variables -in this data set. The objective is to predict the value of prices of -the house using the features (predictors) listed here. -

    - -

    The features/predictors are

    -
      -
    1. CRIM: Per capita crime rate by town
    2. -
    3. ZN: Proportion of residential land zoned for lots over 25000 square feet
    4. -
    5. INDUS: Proportion of non-retail business acres per town
    6. -
    7. CHAS: Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)
    8. -
    9. NOX: Nitric oxide concentration (parts per 10 million)
    10. -
    11. RM: Average number of rooms per dwelling
    12. -
    13. AGE: Proportion of owner-occupied units built prior to 1940
    14. -
    15. DIS: Weighted distances to five Boston employment centers
    16. -
    17. RAD: Index of accessibility to radial highways
    18. -
    19. TAX: Full-value property tax rate per USD10000
    20. -
    21. B: \( 1000(Bk - 0.63)^2 \), where \( Bk \) is the proportion of [people of African American descent] by town
    22. -
    23. LSTAT: Percentage of lower status of the population
    24. -
    25. MEDV: Median value of owner-occupied homes in USD 1000s
    26. -
    -









    -

    Housing data, the code

    -

    We start by importing the libraries

    - - -
    -
    -
    -
    -
    -
    import numpy as np
    -import matplotlib.pyplot as plt 
    -
    -import pandas as pd  
    -import seaborn as sns 
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    and load the Boston Housing DataSet from Scikit-Learn

    - - - -
    -
    -
    -
    -
    -
    from sklearn.datasets import load_boston
    -
    -boston_dataset = load_boston()
    -
    -# boston_dataset is a dictionary
    -# let's check what it contains
    -boston_dataset.keys()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Then we invoke Pandas

    - - -
    -
    -
    -
    -
    -
    boston = pd.DataFrame(boston_dataset.data, columns=boston_dataset.feature_names)
    -boston.head()
    -boston['MEDV'] = boston_dataset.target
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    and preprocess the data

    - - -
    -
    -
    -
    -
    -
    # check for missing values in all the columns
    -boston.isnull().sum()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    We can then visualize the data

    - - -
    -
    -
    -
    -
    -
    # set the size of the figure
    -sns.set(rc={'figure.figsize':(11.7,8.27)})
    -
    -# plot a histogram showing the distribution of the target values
    -sns.distplot(boston['MEDV'], bins=30)
    -plt.show()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    It is now useful to look at the correlation matrix

    - - -
    -
    -
    -
    -
    -
    # compute the pair wise correlation for all columns  
    -correlation_matrix = boston.corr().round(2)
    -# use the heatmap function from seaborn to plot the correlation matrix
    -# annot = True to print the values inside the square
    -sns.heatmap(data=correlation_matrix, annot=True)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    From the above coorelation plot we can see that MEDV is strongly correlated to LSTAT and RM. We see also that RAD and TAX are stronly correlated, but we don't include this in our features together to avoid multi-colinearity

    - - - -
    -
    -
    -
    -
    -
    plt.figure(figsize=(20, 5))
    -
    -features = ['LSTAT', 'RM']
    -target = boston['MEDV']
    -
    -for i, col in enumerate(features):
    -    plt.subplot(1, len(features) , i+1)
    -    x = boston[col]
    -    y = target
    -    plt.scatter(x, y, marker='o')
    -    plt.title(col)
    -    plt.xlabel(col)
    -    plt.ylabel('MEDV')
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Now we start training our model

    - - -
    -
    -
    -
    -
    -
    X = pd.DataFrame(np.c_[boston['LSTAT'], boston['RM']], columns = ['LSTAT','RM'])
    -Y = boston['MEDV']
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    We split the data into training and test sets

    - - - -
    -
    -
    -
    -
    -
    from sklearn.model_selection import train_test_split
    -
    -# splits the training and test data set in 80% : 20%
    -# assign random_state to any value.This ensures consistency.
    -X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state=5)
    -print(X_train.shape)
    -print(X_test.shape)
    -print(Y_train.shape)
    -print(Y_test.shape)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Then we use the linear regression functionality from Scikit-Learn

    - - -
    -
    -
    -
    -
    -
    from sklearn.linear_model import LinearRegression
    -from sklearn.metrics import mean_squared_error, r2_score
    -
    -lin_model = LinearRegression()
    -lin_model.fit(X_train, Y_train)
    -
    -# model evaluation for training set
    -
    -y_train_predict = lin_model.predict(X_train)
    -rmse = (np.sqrt(mean_squared_error(Y_train, y_train_predict)))
    -r2 = r2_score(Y_train, y_train_predict)
    -
    -print("The model performance for training set")
    -print("--------------------------------------")
    -print('RMSE is {}'.format(rmse))
    -print('R2 score is {}'.format(r2))
    -print("\n")
    -
    -# model evaluation for testing set
    -
    -y_test_predict = lin_model.predict(X_test)
    -# root mean square error of the model
    -rmse = (np.sqrt(mean_squared_error(Y_test, y_test_predict)))
    -
    -# r-squared score of the model
    -r2 = r2_score(Y_test, y_test_predict)
    -
    -print("The model performance for testing set")
    -print("--------------------------------------")
    -print('RMSE is {}'.format(rmse))
    -print('R2 score is {}'.format(r2))
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -
    -
    -
    -
    -
    -
    # plotting the y_test vs y_pred
    -# ideally should have been a straight line
    -plt.scatter(Y_test, y_test_predict)
    -plt.show()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -









    Material for lecture Monday, August 26

    diff --git a/doc/pub/week35/ipynb/ipynb-week35-src.tar.gz b/doc/pub/week35/ipynb/ipynb-week35-src.tar.gz index 2dc558ade..bdd604887 100644 Binary files a/doc/pub/week35/ipynb/ipynb-week35-src.tar.gz and b/doc/pub/week35/ipynb/ipynb-week35-src.tar.gz differ diff --git a/doc/pub/week35/ipynb/week35.ipynb b/doc/pub/week35/ipynb/week35.ipynb index c4592d520..05a4d73da 100644 --- a/doc/pub/week35/ipynb/week35.ipynb +++ b/doc/pub/week35/ipynb/week35.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "f85075af", + "id": "dcd490ff", "metadata": { "editable": true }, @@ -14,7 +14,7 @@ }, { "cell_type": "markdown", - "id": "939494c1", + "id": "385618a8", "metadata": { "editable": true }, @@ -27,7 +27,7 @@ }, { "cell_type": "markdown", - "id": "1841e9a3", + "id": "a9261fd6", "metadata": { "editable": true }, @@ -49,7 +49,7 @@ }, { "cell_type": "markdown", - "id": "ac619fc2", + "id": "df4590c7", "metadata": { "editable": true }, @@ -58,17 +58,21 @@ "\n", "1. See lecture notes for week 35 at \n", "\n", - "2. Goodfellow, Bengio and Courville, Deep Learning, chapter 2 on linear algebra and sections 3.1-3.10 on elements of statistics (background)" + "2. Goodfellow, Bengio and Courville, Deep Learning, chapter 2 on linear algebra and sections 3.1-3.10 on elements of statistics (background)\n", + "\n", + "3. Raschka et al on preprocessing of data, relevant for exercise 3 this week, see chapter 4.\n", + "\n", + "4. For exercise 1 of week 35, the book by A. Aldo Faisal, Cheng Soon Ong, and Marc Peter Deisenroth on the Mathematics of Machine Learning, may be very relevant. In particular chapter 5 at URL\"https://mml-book.github.io/\" (section 5.5 on derivatives) is very useful for exercise 1 this coming week." ] }, { "cell_type": "markdown", - "id": "5face6ed", + "id": "5eef6e31", "metadata": { "editable": true }, "source": [ - "## Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week\n", + "## For exercise sessions: Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week\n", "\n", "We need first a reminder from last week about linear regression. \n", "\n", @@ -97,7 +101,7 @@ }, { "cell_type": "markdown", - "id": "a4bedcd2", + "id": "0e6da68b", "metadata": { "editable": true }, @@ -113,7 +117,7 @@ }, { "cell_type": "markdown", - "id": "2da8382c", + "id": "9963a6d7", "metadata": { "editable": true }, @@ -125,7 +129,7 @@ }, { "cell_type": "markdown", - "id": "694a7daa", + "id": "010ae04e", "metadata": { "editable": true }, @@ -135,7 +139,7 @@ }, { "cell_type": "markdown", - "id": "c297e859", + "id": "301aec9d", "metadata": { "editable": true }, @@ -147,7 +151,7 @@ }, { "cell_type": "markdown", - "id": "8f31f1bc", + "id": "a250feef", "metadata": { "editable": true }, @@ -168,7 +172,7 @@ }, { "cell_type": "markdown", - "id": "89c4d341", + "id": "369f3465", "metadata": { "editable": true }, @@ -180,7 +184,7 @@ }, { "cell_type": "markdown", - "id": "fdfbd2dc", + "id": "fa2a164f", "metadata": { "editable": true }, @@ -193,7 +197,7 @@ }, { "cell_type": "markdown", - "id": "0fc448b3", + "id": "80949d9b", "metadata": { "editable": true }, @@ -205,7 +209,7 @@ }, { "cell_type": "markdown", - "id": "a36d87aa", + "id": "906135a3", "metadata": { "editable": true }, @@ -217,7 +221,7 @@ }, { "cell_type": "markdown", - "id": "06a898cf", + "id": "e061663e", "metadata": { "editable": true }, @@ -227,7 +231,7 @@ }, { "cell_type": "markdown", - "id": "ef60c2a4", + "id": "52af4f07", "metadata": { "editable": true }, @@ -239,7 +243,7 @@ }, { "cell_type": "markdown", - "id": "dde18c66", + "id": "53507b07", "metadata": { "editable": true }, @@ -252,7 +256,7 @@ }, { "cell_type": "markdown", - "id": "c9b14acf", + "id": "6c3379fc", "metadata": { "editable": true }, @@ -264,7 +268,7 @@ }, { "cell_type": "markdown", - "id": "5fb51043", + "id": "270eeb89", "metadata": { "editable": true }, @@ -274,7 +278,7 @@ }, { "cell_type": "markdown", - "id": "2504595a", + "id": "ee12e558", "metadata": { "editable": true }, @@ -286,7 +290,7 @@ }, { "cell_type": "markdown", - "id": "5ab3d119", + "id": "1e436d36", "metadata": { "editable": true }, @@ -298,7 +302,7 @@ }, { "cell_type": "markdown", - "id": "c3a354b9", + "id": "ef716711", "metadata": { "editable": true }, @@ -309,7 +313,7 @@ }, { "cell_type": "markdown", - "id": "8db46121", + "id": "4dee6a54", "metadata": { "editable": true }, @@ -321,7 +325,7 @@ }, { "cell_type": "markdown", - "id": "2cc230b6", + "id": "2c8d557b", "metadata": { "editable": true }, @@ -340,7 +344,7 @@ }, { "cell_type": "markdown", - "id": "749ea58e", + "id": "4a48602b", "metadata": { "editable": true }, @@ -353,7 +357,7 @@ }, { "cell_type": "markdown", - "id": "5b2211ed", + "id": "7e4a4d44", "metadata": { "editable": true }, @@ -363,7 +367,7 @@ }, { "cell_type": "markdown", - "id": "96ebc1d8", + "id": "840a673a", "metadata": { "editable": true }, @@ -375,7 +379,7 @@ }, { "cell_type": "markdown", - "id": "b250395c", + "id": "b08500e5", "metadata": { "editable": true }, @@ -385,7 +389,7 @@ }, { "cell_type": "markdown", - "id": "0cc078f3", + "id": "b1a78dd8", "metadata": { "editable": true }, @@ -397,7 +401,7 @@ }, { "cell_type": "markdown", - "id": "8ac380eb", + "id": "6067910a", "metadata": { "editable": true }, @@ -407,7 +411,7 @@ }, { "cell_type": "markdown", - "id": "a85f0ac4", + "id": "c016fa3e", "metadata": { "editable": true }, @@ -419,7 +423,7 @@ }, { "cell_type": "markdown", - "id": "b4f3a163", + "id": "c191006f", "metadata": { "editable": true }, @@ -430,7 +434,7 @@ }, { "cell_type": "markdown", - "id": "fb45d63b", + "id": "60abcbe1", "metadata": { "editable": true }, @@ -442,7 +446,7 @@ }, { "cell_type": "markdown", - "id": "f51b94e4", + "id": "994bf571", "metadata": { "editable": true }, @@ -452,7 +456,7 @@ }, { "cell_type": "markdown", - "id": "e18ef62b", + "id": "f6f9f55a", "metadata": { "editable": true }, @@ -464,7 +468,7 @@ }, { "cell_type": "markdown", - "id": "6ea60395", + "id": "4bc3eca7", "metadata": { "editable": true }, @@ -474,7 +478,7 @@ }, { "cell_type": "markdown", - "id": "480124d7", + "id": "6c7176ed", "metadata": { "editable": true }, @@ -486,7 +490,7 @@ }, { "cell_type": "markdown", - "id": "afdab5a4", + "id": "057dea72", "metadata": { "editable": true }, @@ -506,7 +510,7 @@ }, { "cell_type": "markdown", - "id": "613d2423", + "id": "e75cebb7", "metadata": { "editable": true }, @@ -533,7 +537,7 @@ }, { "cell_type": "markdown", - "id": "b4de8264", + "id": "bf3426be", "metadata": { "editable": true }, @@ -545,7 +549,7 @@ }, { "cell_type": "markdown", - "id": "898b9674", + "id": "a374fc10", "metadata": { "editable": true }, @@ -557,7 +561,7 @@ }, { "cell_type": "markdown", - "id": "d4f7eadc", + "id": "ae52f9e8", "metadata": { "editable": true }, @@ -573,7 +577,7 @@ }, { "cell_type": "markdown", - "id": "4dd2d7b4", + "id": "2edec47f", "metadata": { "editable": true }, @@ -591,7 +595,7 @@ }, { "cell_type": "markdown", - "id": "16cef5b9", + "id": "59ecbe48", "metadata": { "editable": true }, @@ -603,7 +607,7 @@ }, { "cell_type": "markdown", - "id": "61a98293", + "id": "64022a11", "metadata": { "editable": true }, @@ -615,7 +619,7 @@ }, { "cell_type": "markdown", - "id": "e19477b5", + "id": "73f71475", "metadata": { "editable": true }, @@ -626,7 +630,7 @@ }, { "cell_type": "markdown", - "id": "8a594d22", + "id": "80a56df8", "metadata": { "editable": true }, @@ -638,7 +642,7 @@ }, { "cell_type": "markdown", - "id": "bd8763d3", + "id": "7bbc831e", "metadata": { "editable": true }, @@ -648,7 +652,7 @@ }, { "cell_type": "markdown", - "id": "f7901ccd", + "id": "a735b108", "metadata": { "editable": true }, @@ -660,7 +664,7 @@ }, { "cell_type": "markdown", - "id": "1fd01bfe", + "id": "2d85ed6f", "metadata": { "editable": true }, @@ -674,7 +678,7 @@ }, { "cell_type": "markdown", - "id": "77bc8280", + "id": "bead98c0", "metadata": { "editable": true }, @@ -686,7 +690,7 @@ }, { "cell_type": "markdown", - "id": "47429058", + "id": "17ed60cb", "metadata": { "editable": true }, @@ -698,7 +702,7 @@ }, { "cell_type": "markdown", - "id": "d1e259fd", + "id": "ce704db6", "metadata": { "editable": true }, @@ -710,7 +714,7 @@ }, { "cell_type": "markdown", - "id": "2f71787b", + "id": "cb26cc69", "metadata": { "editable": true }, @@ -720,7 +724,7 @@ }, { "cell_type": "markdown", - "id": "f0f77618", + "id": "4e8736ec", "metadata": { "editable": true }, @@ -732,7 +736,7 @@ }, { "cell_type": "markdown", - "id": "c8b319b6", + "id": "932671e7", "metadata": { "editable": true }, @@ -744,7 +748,7 @@ }, { "cell_type": "markdown", - "id": "37da9b7b", + "id": "f86f1941", "metadata": { "editable": true }, @@ -756,7 +760,7 @@ }, { "cell_type": "markdown", - "id": "63b41a6d", + "id": "4c4faf02", "metadata": { "editable": true }, @@ -770,7 +774,7 @@ }, { "cell_type": "markdown", - "id": "32d0c2e9", + "id": "c87ff9ca", "metadata": { "editable": true }, @@ -782,7 +786,7 @@ }, { "cell_type": "markdown", - "id": "ff376a49", + "id": "4baa19a0", "metadata": { "editable": true }, @@ -794,7 +798,7 @@ }, { "cell_type": "markdown", - "id": "08b581b9", + "id": "3c44de1a", "metadata": { "editable": true }, @@ -806,7 +810,7 @@ }, { "cell_type": "markdown", - "id": "1de7bd67", + "id": "74a54ecc", "metadata": { "editable": true }, @@ -816,7 +820,7 @@ }, { "cell_type": "markdown", - "id": "86c372c0", + "id": "d7779cf9", "metadata": { "editable": true }, @@ -828,7 +832,7 @@ }, { "cell_type": "markdown", - "id": "df812770", + "id": "7b05aa6b", "metadata": { "editable": true }, @@ -838,7 +842,7 @@ }, { "cell_type": "markdown", - "id": "421aa557", + "id": "22f13742", "metadata": { "editable": true }, @@ -850,7 +854,7 @@ }, { "cell_type": "markdown", - "id": "f776d3f1", + "id": "1e01a281", "metadata": { "editable": true }, @@ -860,7 +864,7 @@ }, { "cell_type": "markdown", - "id": "937ec64e", + "id": "d5786be4", "metadata": { "editable": true }, @@ -872,7 +876,7 @@ }, { "cell_type": "markdown", - "id": "b406071e", + "id": "99751432", "metadata": { "editable": true }, @@ -884,7 +888,7 @@ }, { "cell_type": "markdown", - "id": "20b59dcc", + "id": "ed499fdb", "metadata": { "editable": true }, @@ -896,7 +900,7 @@ }, { "cell_type": "markdown", - "id": "3f4da8c9", + "id": "04eb0e64", "metadata": { "editable": true }, @@ -911,7 +915,7 @@ }, { "cell_type": "markdown", - "id": "fc31daff", + "id": "9a095b81", "metadata": { "editable": true }, @@ -923,7 +927,7 @@ }, { "cell_type": "markdown", - "id": "4f3161da", + "id": "9cad1773", "metadata": { "editable": true }, @@ -933,7 +937,7 @@ }, { "cell_type": "markdown", - "id": "fb6d5bf5", + "id": "153095ad", "metadata": { "editable": true }, @@ -945,7 +949,7 @@ }, { "cell_type": "markdown", - "id": "a4416ea7", + "id": "a96e3aed", "metadata": { "editable": true }, @@ -955,7 +959,7 @@ }, { "cell_type": "markdown", - "id": "c128591b", + "id": "9b327c0d", "metadata": { "editable": true }, @@ -967,7 +971,7 @@ }, { "cell_type": "markdown", - "id": "cfceb095", + "id": "1c4ebcd3", "metadata": { "editable": true }, @@ -977,7 +981,7 @@ }, { "cell_type": "markdown", - "id": "0ae0950d", + "id": "a4e72821", "metadata": { "editable": true }, @@ -989,7 +993,7 @@ }, { "cell_type": "markdown", - "id": "5680fe7e", + "id": "2f0cbfc3", "metadata": { "editable": true }, @@ -1001,7 +1005,7 @@ }, { "cell_type": "markdown", - "id": "ca756664", + "id": "3539b330", "metadata": { "editable": true }, @@ -1013,7 +1017,7 @@ }, { "cell_type": "markdown", - "id": "45d22349", + "id": "6103ca86", "metadata": { "editable": true }, @@ -1023,7 +1027,7 @@ }, { "cell_type": "markdown", - "id": "d867b974", + "id": "129e20f4", "metadata": { "editable": true }, @@ -1035,7 +1039,7 @@ }, { "cell_type": "markdown", - "id": "332da975", + "id": "4afd59e4", "metadata": { "editable": true }, @@ -1048,7 +1052,7 @@ }, { "cell_type": "markdown", - "id": "0b252c32", + "id": "0c3972c6", "metadata": { "editable": true }, @@ -1060,7 +1064,7 @@ }, { "cell_type": "markdown", - "id": "64407311", + "id": "85eb3f2a", "metadata": { "editable": true }, @@ -1070,7 +1074,7 @@ }, { "cell_type": "markdown", - "id": "f6c7b927", + "id": "f4763736", "metadata": { "editable": true }, @@ -1082,7 +1086,7 @@ }, { "cell_type": "markdown", - "id": "c3c7c8ce", + "id": "2bf61e46", "metadata": { "editable": true }, @@ -1092,7 +1096,7 @@ }, { "cell_type": "markdown", - "id": "4f68d9f9", + "id": "f0e93ad2", "metadata": { "editable": true }, @@ -1104,7 +1108,7 @@ }, { "cell_type": "markdown", - "id": "286d8ccb", + "id": "f4b840ad", "metadata": { "editable": true }, @@ -1114,7 +1118,7 @@ }, { "cell_type": "markdown", - "id": "40526192", + "id": "a3222221", "metadata": { "editable": true }, @@ -1126,7 +1130,7 @@ }, { "cell_type": "markdown", - "id": "5f11c4f4", + "id": "1daa041f", "metadata": { "editable": true }, @@ -1136,7 +1140,7 @@ }, { "cell_type": "markdown", - "id": "aecfda11", + "id": "de1fe884", "metadata": { "editable": true }, @@ -1148,7 +1152,7 @@ }, { "cell_type": "markdown", - "id": "56fa7802", + "id": "2887338a", "metadata": { "editable": true }, @@ -1158,7 +1162,7 @@ }, { "cell_type": "markdown", - "id": "75c39020", + "id": "e4d885bc", "metadata": { "editable": true }, @@ -1170,7 +1174,7 @@ }, { "cell_type": "markdown", - "id": "b46b0ae6", + "id": "3119b338", "metadata": { "editable": true }, @@ -1182,7 +1186,7 @@ }, { "cell_type": "markdown", - "id": "d958c295", + "id": "22d9f137", "metadata": { "editable": true }, @@ -1194,7 +1198,7 @@ }, { "cell_type": "markdown", - "id": "4517ac9f", + "id": "157dd7bd", "metadata": { "editable": true }, @@ -1206,7 +1210,7 @@ }, { "cell_type": "markdown", - "id": "a98a0918", + "id": "9e31097e", "metadata": { "editable": true }, @@ -1218,7 +1222,7 @@ }, { "cell_type": "markdown", - "id": "d0737f5e", + "id": "f2091b64", "metadata": { "editable": true }, @@ -1234,7 +1238,7 @@ }, { "cell_type": "markdown", - "id": "db23b821", + "id": "61fb0157", "metadata": { "editable": true }, @@ -1246,7 +1250,7 @@ }, { "cell_type": "markdown", - "id": "f1fd1c63", + "id": "261c9d0a", "metadata": { "editable": true }, @@ -1256,7 +1260,7 @@ }, { "cell_type": "markdown", - "id": "7237d50f", + "id": "1539cfc1", "metadata": { "editable": true }, @@ -1268,7 +1272,7 @@ }, { "cell_type": "markdown", - "id": "f9f489c2", + "id": "0e420502", "metadata": { "editable": true }, @@ -1286,7 +1290,7 @@ }, { "cell_type": "markdown", - "id": "715d96e6", + "id": "c02bc997", "metadata": { "editable": true }, @@ -1298,7 +1302,7 @@ }, { "cell_type": "markdown", - "id": "9aeab761", + "id": "8adb8612", "metadata": { "editable": true }, @@ -1310,7 +1314,7 @@ }, { "cell_type": "markdown", - "id": "9e588548", + "id": "0ae28de6", "metadata": { "editable": true }, @@ -1320,7 +1324,7 @@ }, { "cell_type": "markdown", - "id": "4a3b6f9a", + "id": "d560a0b3", "metadata": { "editable": true }, @@ -1332,7 +1336,7 @@ }, { "cell_type": "markdown", - "id": "17c15215", + "id": "cc2025d5", "metadata": { "editable": true }, @@ -1342,7 +1346,7 @@ }, { "cell_type": "markdown", - "id": "830f09b8", + "id": "d583e016", "metadata": { "editable": true }, @@ -1354,7 +1358,7 @@ }, { "cell_type": "markdown", - "id": "43ef7e57", + "id": "a9bc2ecf", "metadata": { "editable": true }, @@ -1364,7 +1368,7 @@ }, { "cell_type": "markdown", - "id": "c6192ea8", + "id": "1ef3c28e", "metadata": { "editable": true }, @@ -1378,7 +1382,7 @@ }, { "cell_type": "markdown", - "id": "c898ab39", + "id": "650230f1", "metadata": { "editable": true }, @@ -1390,7 +1394,7 @@ }, { "cell_type": "markdown", - "id": "7c506ac3", + "id": "c05bcc7a", "metadata": { "editable": true }, @@ -1401,7 +1405,7 @@ }, { "cell_type": "markdown", - "id": "9abe3265", + "id": "14250185", "metadata": { "editable": true }, @@ -1414,7 +1418,7 @@ { "cell_type": "code", "execution_count": 1, - "id": "1fb7cef3", + "id": "be1f278b", "metadata": { "collapsed": false, "editable": true @@ -1441,7 +1445,7 @@ }, { "cell_type": "markdown", - "id": "a1f370e1", + "id": "6c77ec9f", "metadata": { "editable": true }, @@ -1452,7 +1456,7 @@ { "cell_type": "code", "execution_count": 2, - "id": "a8ef0c63", + "id": "d4735874", "metadata": { "collapsed": false, "editable": true @@ -1465,7 +1469,7 @@ }, { "cell_type": "markdown", - "id": "5ad74ecc", + "id": "04d6c28c", "metadata": { "editable": true }, @@ -1479,7 +1483,7 @@ { "cell_type": "code", "execution_count": 3, - "id": "8bf50c11", + "id": "4d56a494", "metadata": { "collapsed": false, "editable": true @@ -1492,7 +1496,7 @@ }, { "cell_type": "markdown", - "id": "6a2f5501", + "id": "e22d165b", "metadata": { "editable": true }, @@ -1503,7 +1507,7 @@ { "cell_type": "code", "execution_count": 4, - "id": "c74b6cca", + "id": "477278b6", "metadata": { "collapsed": false, "editable": true @@ -1515,7 +1519,7 @@ }, { "cell_type": "markdown", - "id": "356ab966", + "id": "afb15e6b", "metadata": { "editable": true }, @@ -1526,7 +1530,7 @@ { "cell_type": "code", "execution_count": 5, - "id": "bb40ec37", + "id": "10865d1b", "metadata": { "collapsed": false, "editable": true @@ -1542,7 +1546,7 @@ }, { "cell_type": "markdown", - "id": "c6736d9d", + "id": "5690f819", "metadata": { "editable": true }, @@ -1553,7 +1557,7 @@ { "cell_type": "code", "execution_count": 6, - "id": "3c1632ab", + "id": "788af57c", "metadata": { "collapsed": false, "editable": true @@ -1567,7 +1571,7 @@ }, { "cell_type": "markdown", - "id": "b4bae24e", + "id": "e532b2ae", "metadata": { "editable": true }, @@ -1588,7 +1592,7 @@ }, { "cell_type": "markdown", - "id": "9ba46e33", + "id": "7c60b095", "metadata": { "editable": true }, @@ -1599,7 +1603,7 @@ { "cell_type": "code", "execution_count": 7, - "id": "e937a2ea", + "id": "645f6491", "metadata": { "collapsed": false, "editable": true @@ -1652,7 +1656,7 @@ }, { "cell_type": "markdown", - "id": "77e59503", + "id": "6fb0e831", "metadata": { "editable": true }, @@ -1663,7 +1667,7 @@ { "cell_type": "code", "execution_count": 8, - "id": "326a509c", + "id": "2844abea", "metadata": { "collapsed": false, "editable": true @@ -1688,7 +1692,7 @@ }, { "cell_type": "markdown", - "id": "3efd11b9", + "id": "d8d9e293", "metadata": { "editable": true }, @@ -1700,7 +1704,7 @@ }, { "cell_type": "markdown", - "id": "3497a997", + "id": "5120f87d", "metadata": { "editable": true }, @@ -1729,7 +1733,7 @@ }, { "cell_type": "markdown", - "id": "b38214c7", + "id": "8abaf6d3", "metadata": { "editable": true }, @@ -1754,7 +1758,7 @@ }, { "cell_type": "markdown", - "id": "f4869adf", + "id": "47cfeb98", "metadata": { "editable": true }, @@ -1774,7 +1778,7 @@ }, { "cell_type": "markdown", - "id": "aacb7439", + "id": "f8c95276", "metadata": { "editable": true }, @@ -1801,7 +1805,7 @@ }, { "cell_type": "markdown", - "id": "e2671db3", + "id": "3395f9d8", "metadata": { "editable": true }, @@ -1814,7 +1818,7 @@ }, { "cell_type": "markdown", - "id": "d2a7de1b", + "id": "873be209", "metadata": { "editable": true }, @@ -1826,7 +1830,7 @@ }, { "cell_type": "markdown", - "id": "0008b167", + "id": "a2ca2dff", "metadata": { "editable": true }, @@ -1837,7 +1841,7 @@ }, { "cell_type": "markdown", - "id": "f482cbf6", + "id": "2a6106b6", "metadata": { "editable": true }, @@ -1853,7 +1857,7 @@ { "cell_type": "code", "execution_count": 9, - "id": "8858e7f5", + "id": "422f6c5d", "metadata": { "collapsed": false, "editable": true @@ -1887,7 +1891,7 @@ }, { "cell_type": "markdown", - "id": "6151e773", + "id": "1737d628", "metadata": { "editable": true }, @@ -1897,7 +1901,7 @@ }, { "cell_type": "markdown", - "id": "905c33bc", + "id": "008e6446", "metadata": { "editable": true }, @@ -1912,7 +1916,7 @@ }, { "cell_type": "markdown", - "id": "55d9897f", + "id": "dc100165", "metadata": { "editable": true }, @@ -1924,7 +1928,7 @@ }, { "cell_type": "markdown", - "id": "0255cf64", + "id": "251fba51", "metadata": { "editable": true }, @@ -1934,7 +1938,7 @@ }, { "cell_type": "markdown", - "id": "4e9bfa24", + "id": "aaa3597b", "metadata": { "editable": true }, @@ -1950,7 +1954,7 @@ { "cell_type": "code", "execution_count": 10, - "id": "16abe96d", + "id": "29034b76", "metadata": { "collapsed": false, "editable": true @@ -1967,7 +1971,7 @@ }, { "cell_type": "markdown", - "id": "99d2d0bb", + "id": "de9bb925", "metadata": { "editable": true }, @@ -1980,7 +1984,7 @@ { "cell_type": "code", "execution_count": 11, - "id": "69c8da60", + "id": "7edbfe51", "metadata": { "collapsed": false, "editable": true @@ -2027,7 +2031,7 @@ }, { "cell_type": "markdown", - "id": "499b086e", + "id": "484f8a18", "metadata": { "editable": true }, @@ -2038,7 +2042,7 @@ { "cell_type": "code", "execution_count": 12, - "id": "a329b56c", + "id": "35d143aa", "metadata": { "collapsed": false, "editable": true @@ -2141,7 +2145,7 @@ }, { "cell_type": "markdown", - "id": "4862279e", + "id": "6932e0a1", "metadata": { "editable": true }, @@ -2169,7 +2173,7 @@ }, { "cell_type": "markdown", - "id": "89458a53", + "id": "ed1256b9", "metadata": { "editable": true }, @@ -2204,7 +2208,7 @@ }, { "cell_type": "markdown", - "id": "75beecd7", + "id": "882eb389", "metadata": { "editable": true }, @@ -2219,7 +2223,7 @@ { "cell_type": "code", "execution_count": 13, - "id": "1c0b765e", + "id": "0e76d155", "metadata": { "collapsed": false, "editable": true @@ -2244,7 +2248,7 @@ }, { "cell_type": "markdown", - "id": "eef3487f", + "id": "e603a97d", "metadata": { "editable": true }, @@ -2260,7 +2264,7 @@ }, { "cell_type": "markdown", - "id": "3a42e892", + "id": "dffd46a8", "metadata": { "editable": true }, @@ -2272,7 +2276,7 @@ }, { "cell_type": "markdown", - "id": "f3b87a35", + "id": "22494ff2", "metadata": { "editable": true }, @@ -2287,7 +2291,7 @@ }, { "cell_type": "markdown", - "id": "fb075b8f", + "id": "29fb4371", "metadata": { "editable": true }, @@ -2299,7 +2303,7 @@ }, { "cell_type": "markdown", - "id": "fd37a00d", + "id": "fbba75a6", "metadata": { "editable": true }, @@ -2309,7 +2313,7 @@ }, { "cell_type": "markdown", - "id": "f5799d20", + "id": "b82fc31f", "metadata": { "editable": true }, @@ -2321,7 +2325,7 @@ }, { "cell_type": "markdown", - "id": "15452b23", + "id": "6271c174", "metadata": { "editable": true }, @@ -2331,7 +2335,7 @@ }, { "cell_type": "markdown", - "id": "3062f943", + "id": "6f11b3d3", "metadata": { "editable": true }, @@ -2343,7 +2347,7 @@ }, { "cell_type": "markdown", - "id": "1d6eba03", + "id": "72c26f93", "metadata": { "editable": true }, @@ -2356,7 +2360,7 @@ }, { "cell_type": "markdown", - "id": "2f80679a", + "id": "68d83e28", "metadata": { "editable": true }, @@ -2368,7 +2372,7 @@ }, { "cell_type": "markdown", - "id": "36efe233", + "id": "7cc1789b", "metadata": { "editable": true }, @@ -2378,7 +2382,7 @@ }, { "cell_type": "markdown", - "id": "9761a179", + "id": "091380ea", "metadata": { "editable": true }, @@ -2390,7 +2394,7 @@ }, { "cell_type": "markdown", - "id": "b31014be", + "id": "4db55db7", "metadata": { "editable": true }, @@ -2400,7 +2404,7 @@ }, { "cell_type": "markdown", - "id": "122ecc6f", + "id": "8fd22f24", "metadata": { "editable": true }, @@ -2412,7 +2416,7 @@ }, { "cell_type": "markdown", - "id": "d11463f0", + "id": "545c7397", "metadata": { "editable": true }, @@ -2422,7 +2426,7 @@ }, { "cell_type": "markdown", - "id": "b3a7f515", + "id": "d58bae30", "metadata": { "editable": true }, @@ -2434,7 +2438,7 @@ }, { "cell_type": "markdown", - "id": "9c62b501", + "id": "06431be0", "metadata": { "editable": true }, @@ -2444,7 +2448,7 @@ }, { "cell_type": "markdown", - "id": "7cfd9cb6", + "id": "7728bc49", "metadata": { "editable": true }, @@ -2456,7 +2460,7 @@ }, { "cell_type": "markdown", - "id": "2f296f4d", + "id": "7df19dd2", "metadata": { "editable": true }, @@ -2466,7 +2470,7 @@ }, { "cell_type": "markdown", - "id": "efb50761", + "id": "c4505bee", "metadata": { "editable": true }, @@ -2478,7 +2482,7 @@ }, { "cell_type": "markdown", - "id": "24f72185", + "id": "bedd545f", "metadata": { "editable": true }, @@ -2488,7 +2492,7 @@ }, { "cell_type": "markdown", - "id": "d5013b57", + "id": "6cbc0a1f", "metadata": { "editable": true }, @@ -2500,7 +2504,7 @@ }, { "cell_type": "markdown", - "id": "2f6ff059", + "id": "fd73402c", "metadata": { "editable": true }, @@ -2512,7 +2516,7 @@ }, { "cell_type": "markdown", - "id": "d47b38fb", + "id": "060b6f4f", "metadata": { "editable": true }, @@ -2524,7 +2528,7 @@ }, { "cell_type": "markdown", - "id": "36a8d4a7", + "id": "0db8f249", "metadata": { "editable": true }, @@ -2537,7 +2541,7 @@ }, { "cell_type": "markdown", - "id": "63c75044", + "id": "411a6461", "metadata": { "editable": true }, @@ -2549,7 +2553,7 @@ }, { "cell_type": "markdown", - "id": "bb70e597", + "id": "38dfa3b9", "metadata": { "editable": true }, @@ -2559,7 +2563,7 @@ }, { "cell_type": "markdown", - "id": "457f8252", + "id": "165a70e3", "metadata": { "editable": true }, @@ -2573,7 +2577,7 @@ { "cell_type": "code", "execution_count": 14, - "id": "d74c8d3e", + "id": "59e9ba53", "metadata": { "collapsed": false, "editable": true @@ -2670,7 +2674,7 @@ }, { "cell_type": "markdown", - "id": "3427026e", + "id": "03d4e28b", "metadata": { "editable": true }, @@ -2691,7 +2695,7 @@ }, { "cell_type": "markdown", - "id": "1661a515", + "id": "ee233b8d", "metadata": { "editable": true }, @@ -2703,7 +2707,7 @@ }, { "cell_type": "markdown", - "id": "116383ca", + "id": "8fa76e18", "metadata": { "editable": true }, @@ -2713,7 +2717,7 @@ }, { "cell_type": "markdown", - "id": "f328ed8e", + "id": "dcc88ef6", "metadata": { "editable": true }, @@ -2725,7 +2729,7 @@ }, { "cell_type": "markdown", - "id": "70a1c41a", + "id": "6f241f45", "metadata": { "editable": true }, @@ -2735,7 +2739,7 @@ }, { "cell_type": "markdown", - "id": "e54c04b5", + "id": "e7fb14df", "metadata": { "editable": true }, @@ -2747,7 +2751,7 @@ }, { "cell_type": "markdown", - "id": "8d6e489d", + "id": "7b4dcc6a", "metadata": { "editable": true }, @@ -2763,371 +2767,7 @@ }, { "cell_type": "markdown", - "id": "945db817", - "metadata": { - "editable": true - }, - "source": [ - "## The Boston housing data example\n", - "\n", - "The Boston housing \n", - "data set was originally a part of UCI Machine Learning Repository\n", - "and has been removed now. The data set is now included in **Scikit-Learn**'s \n", - "library. There are 506 samples and 13 feature (predictor) variables\n", - "in this data set. The objective is to predict the value of prices of\n", - "the house using the features (predictors) listed here.\n", - "\n", - "The features/predictors are\n", - "1. CRIM: Per capita crime rate by town\n", - "\n", - "2. ZN: Proportion of residential land zoned for lots over 25000 square feet\n", - "\n", - "3. INDUS: Proportion of non-retail business acres per town\n", - "\n", - "4. CHAS: Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)\n", - "\n", - "5. NOX: Nitric oxide concentration (parts per 10 million)\n", - "\n", - "6. RM: Average number of rooms per dwelling\n", - "\n", - "7. AGE: Proportion of owner-occupied units built prior to 1940\n", - "\n", - "8. DIS: Weighted distances to five Boston employment centers\n", - "\n", - "9. RAD: Index of accessibility to radial highways\n", - "\n", - "10. TAX: Full-value property tax rate per USD10000\n", - "\n", - "11. B: $1000(Bk - 0.63)^2$, where $Bk$ is the proportion of [people of African American descent] by town\n", - "\n", - "12. LSTAT: Percentage of lower status of the population\n", - "\n", - "13. MEDV: Median value of owner-occupied homes in USD 1000s" - ] - }, - { - "cell_type": "markdown", - "id": "77e5c2f0", - "metadata": { - "editable": true - }, - "source": [ - "## Housing data, the code\n", - "We start by importing the libraries" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "id": "69f38362", - "metadata": { - "collapsed": false, - "editable": true - }, - "outputs": [], - "source": [ - "import numpy as np\n", - "import matplotlib.pyplot as plt \n", - "\n", - "import pandas as pd \n", - "import seaborn as sns" - ] - }, - { - "cell_type": "markdown", - "id": "65c9c381", - "metadata": { - "editable": true - }, - "source": [ - "and load the Boston Housing DataSet from **Scikit-Learn**" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "id": "12b2e5d1", - "metadata": { - "collapsed": false, - "editable": true - }, - "outputs": [], - "source": [ - "from sklearn.datasets import load_boston\n", - "\n", - "boston_dataset = load_boston()\n", - "\n", - "# boston_dataset is a dictionary\n", - "# let's check what it contains\n", - "boston_dataset.keys()" - ] - }, - { - "cell_type": "markdown", - "id": "2f67bd5c", - "metadata": { - "editable": true - }, - "source": [ - "Then we invoke Pandas" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "id": "0798fc31", - "metadata": { - "collapsed": false, - "editable": true - }, - "outputs": [], - "source": [ - "boston = pd.DataFrame(boston_dataset.data, columns=boston_dataset.feature_names)\n", - "boston.head()\n", - "boston['MEDV'] = boston_dataset.target" - ] - }, - { - "cell_type": "markdown", - "id": "011f85eb", - "metadata": { - "editable": true - }, - "source": [ - "and preprocess the data" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "id": "4e88e6d9", - "metadata": { - "collapsed": false, - "editable": true - }, - "outputs": [], - "source": [ - "# check for missing values in all the columns\n", - "boston.isnull().sum()" - ] - }, - { - "cell_type": "markdown", - "id": "d13bfff7", - "metadata": { - "editable": true - }, - "source": [ - "We can then visualize the data" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "id": "acfac152", - "metadata": { - "collapsed": false, - "editable": true - }, - "outputs": [], - "source": [ - "# set the size of the figure\n", - "sns.set(rc={'figure.figsize':(11.7,8.27)})\n", - "\n", - "# plot a histogram showing the distribution of the target values\n", - "sns.distplot(boston['MEDV'], bins=30)\n", - "plt.show()" - ] - }, - { - "cell_type": "markdown", - "id": "a8a65330", - "metadata": { - "editable": true - }, - "source": [ - "It is now useful to look at the correlation matrix" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "id": "ba430138", - "metadata": { - "collapsed": false, - "editable": true - }, - "outputs": [], - "source": [ - "# compute the pair wise correlation for all columns \n", - "correlation_matrix = boston.corr().round(2)\n", - "# use the heatmap function from seaborn to plot the correlation matrix\n", - "# annot = True to print the values inside the square\n", - "sns.heatmap(data=correlation_matrix, annot=True)" - ] - }, - { - "cell_type": "markdown", - "id": "9dc31721", - "metadata": { - "editable": true - }, - "source": [ - "From the above coorelation plot we can see that **MEDV** is strongly correlated to **LSTAT** and **RM**. We see also that **RAD** and **TAX** are stronly correlated, but we don't include this in our features together to avoid multi-colinearity" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "id": "0fba74de", - "metadata": { - "collapsed": false, - "editable": true - }, - "outputs": [], - "source": [ - "plt.figure(figsize=(20, 5))\n", - "\n", - "features = ['LSTAT', 'RM']\n", - "target = boston['MEDV']\n", - "\n", - "for i, col in enumerate(features):\n", - " plt.subplot(1, len(features) , i+1)\n", - " x = boston[col]\n", - " y = target\n", - " plt.scatter(x, y, marker='o')\n", - " plt.title(col)\n", - " plt.xlabel(col)\n", - " plt.ylabel('MEDV')" - ] - }, - { - "cell_type": "markdown", - "id": "534b849e", - "metadata": { - "editable": true - }, - "source": [ - "Now we start training our model" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "id": "1857b4d1", - "metadata": { - "collapsed": false, - "editable": true - }, - "outputs": [], - "source": [ - "X = pd.DataFrame(np.c_[boston['LSTAT'], boston['RM']], columns = ['LSTAT','RM'])\n", - "Y = boston['MEDV']" - ] - }, - { - "cell_type": "markdown", - "id": "76a73676", - "metadata": { - "editable": true - }, - "source": [ - "We split the data into training and test sets" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "id": "bff38dc8", - "metadata": { - "collapsed": false, - "editable": true - }, - "outputs": [], - "source": [ - "from sklearn.model_selection import train_test_split\n", - "\n", - "# splits the training and test data set in 80% : 20%\n", - "# assign random_state to any value.This ensures consistency.\n", - "X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state=5)\n", - "print(X_train.shape)\n", - "print(X_test.shape)\n", - "print(Y_train.shape)\n", - "print(Y_test.shape)" - ] - }, - { - "cell_type": "markdown", - "id": "0a2f8089", - "metadata": { - "editable": true - }, - "source": [ - "Then we use the linear regression functionality from **Scikit-Learn**" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "id": "1e496c22", - "metadata": { - "collapsed": false, - "editable": true - }, - "outputs": [], - "source": [ - "from sklearn.linear_model import LinearRegression\n", - "from sklearn.metrics import mean_squared_error, r2_score\n", - "\n", - "lin_model = LinearRegression()\n", - "lin_model.fit(X_train, Y_train)\n", - "\n", - "# model evaluation for training set\n", - "\n", - "y_train_predict = lin_model.predict(X_train)\n", - "rmse = (np.sqrt(mean_squared_error(Y_train, y_train_predict)))\n", - "r2 = r2_score(Y_train, y_train_predict)\n", - "\n", - "print(\"The model performance for training set\")\n", - "print(\"--------------------------------------\")\n", - "print('RMSE is {}'.format(rmse))\n", - "print('R2 score is {}'.format(r2))\n", - "print(\"\\n\")\n", - "\n", - "# model evaluation for testing set\n", - "\n", - "y_test_predict = lin_model.predict(X_test)\n", - "# root mean square error of the model\n", - "rmse = (np.sqrt(mean_squared_error(Y_test, y_test_predict)))\n", - "\n", - "# r-squared score of the model\n", - "r2 = r2_score(Y_test, y_test_predict)\n", - "\n", - "print(\"The model performance for testing set\")\n", - "print(\"--------------------------------------\")\n", - "print('RMSE is {}'.format(rmse))\n", - "print('R2 score is {}'.format(r2))" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "id": "b29656fe", - "metadata": { - "collapsed": false, - "editable": true - }, - "outputs": [], - "source": [ - "# plotting the y_test vs y_pred\n", - "# ideally should have been a straight line\n", - "plt.scatter(Y_test, y_test_predict)\n", - "plt.show()" - ] - }, - { - "cell_type": "markdown", - "id": "fe09bc44", + "id": "9e481f5c", "metadata": { "editable": true }, @@ -3137,7 +2777,7 @@ }, { "cell_type": "markdown", - "id": "d3382169", + "id": "b6782032", "metadata": { "editable": true }, @@ -3151,7 +2791,7 @@ }, { "cell_type": "markdown", - "id": "3857daba", + "id": "d94ea73b", "metadata": { "editable": true }, @@ -3163,7 +2803,7 @@ }, { "cell_type": "markdown", - "id": "76261d37", + "id": "e4d69482", "metadata": { "editable": true }, @@ -3175,7 +2815,7 @@ }, { "cell_type": "markdown", - "id": "40e205e6", + "id": "dddce5e5", "metadata": { "editable": true }, @@ -3187,7 +2827,7 @@ }, { "cell_type": "markdown", - "id": "6c52cdb6", + "id": "74844e1d", "metadata": { "editable": true }, @@ -3197,7 +2837,7 @@ }, { "cell_type": "markdown", - "id": "02d60cf7", + "id": "b6cddc2d", "metadata": { "editable": true }, @@ -3209,7 +2849,7 @@ }, { "cell_type": "markdown", - "id": "534ed2bb", + "id": "7fd48c4a", "metadata": { "editable": true }, @@ -3219,7 +2859,7 @@ }, { "cell_type": "markdown", - "id": "1b452694", + "id": "27ca0f25", "metadata": { "editable": true }, @@ -3231,7 +2871,7 @@ }, { "cell_type": "markdown", - "id": "c2d89ae1", + "id": "55bf04ea", "metadata": { "editable": true }, @@ -3242,7 +2882,7 @@ }, { "cell_type": "markdown", - "id": "e4250c05", + "id": "a4d2071c", "metadata": { "editable": true }, @@ -3254,7 +2894,7 @@ }, { "cell_type": "markdown", - "id": "cb3fece3", + "id": "59aa956f", "metadata": { "editable": true }, @@ -3266,7 +2906,7 @@ }, { "cell_type": "markdown", - "id": "5b00cb80", + "id": "4f37a377", "metadata": { "editable": true }, @@ -3276,7 +2916,7 @@ }, { "cell_type": "markdown", - "id": "88bc2754", + "id": "490ba49f", "metadata": { "editable": true }, @@ -3288,7 +2928,7 @@ }, { "cell_type": "markdown", - "id": "36f54905", + "id": "06a14bf5", "metadata": { "editable": true }, @@ -3300,7 +2940,7 @@ }, { "cell_type": "markdown", - "id": "d9053b73", + "id": "6fe4ffd7", "metadata": { "editable": true }, @@ -3310,7 +2950,7 @@ }, { "cell_type": "markdown", - "id": "c48d7e54", + "id": "b8c77ea6", "metadata": { "editable": true }, @@ -3322,7 +2962,7 @@ }, { "cell_type": "markdown", - "id": "46a000e5", + "id": "68370821", "metadata": { "editable": true }, @@ -3332,7 +2972,7 @@ }, { "cell_type": "markdown", - "id": "52d75bcb", + "id": "e36fd2b1", "metadata": { "editable": true }, @@ -3344,7 +2984,7 @@ }, { "cell_type": "markdown", - "id": "4195b03f", + "id": "05ec86e2", "metadata": { "editable": true }, @@ -3354,7 +2994,7 @@ }, { "cell_type": "markdown", - "id": "925e6cdd", + "id": "b68bc782", "metadata": { "editable": true }, @@ -3394,7 +3034,7 @@ }, { "cell_type": "markdown", - "id": "51f2ff8a", + "id": "c61f8ea3", "metadata": { "editable": true }, @@ -3411,7 +3051,7 @@ }, { "cell_type": "markdown", - "id": "6918bf17", + "id": "6fd095ee", "metadata": { "editable": true }, @@ -3434,7 +3074,7 @@ }, { "cell_type": "markdown", - "id": "3d2f592b", + "id": "7e3c3846", "metadata": { "editable": true }, @@ -3451,7 +3091,7 @@ }, { "cell_type": "markdown", - "id": "41b65d18", + "id": "02dd7893", "metadata": { "editable": true }, @@ -3470,7 +3110,7 @@ }, { "cell_type": "markdown", - "id": "1a461569", + "id": "008d289a", "metadata": { "editable": true }, @@ -3481,7 +3121,7 @@ }, { "cell_type": "markdown", - "id": "be605228", + "id": "d1678ef8", "metadata": { "editable": true }, @@ -3493,7 +3133,7 @@ }, { "cell_type": "markdown", - "id": "f17cec6d", + "id": "8a3f242a", "metadata": { "editable": true }, @@ -3511,7 +3151,7 @@ }, { "cell_type": "markdown", - "id": "9b9d5ddf", + "id": "3a895dbd", "metadata": { "editable": true }, @@ -3527,7 +3167,7 @@ }, { "cell_type": "markdown", - "id": "932bffa3", + "id": "44478a61", "metadata": { "editable": true }, @@ -3539,7 +3179,7 @@ }, { "cell_type": "markdown", - "id": "c1f00aee", + "id": "cc4f54d5", "metadata": { "editable": true }, @@ -3549,7 +3189,7 @@ }, { "cell_type": "markdown", - "id": "3604c99a", + "id": "d012b4b2", "metadata": { "editable": true }, @@ -3564,7 +3204,7 @@ }, { "cell_type": "markdown", - "id": "2e80cf21", + "id": "d6c31e34", "metadata": { "editable": true }, @@ -3576,7 +3216,7 @@ }, { "cell_type": "markdown", - "id": "a32f751b", + "id": "5593d8db", "metadata": { "editable": true }, @@ -3586,7 +3226,7 @@ }, { "cell_type": "markdown", - "id": "a8a1ea95", + "id": "815a5930", "metadata": { "editable": true }, @@ -3598,7 +3238,7 @@ }, { "cell_type": "markdown", - "id": "e9f08d58", + "id": "dbc418ac", "metadata": { "editable": true }, @@ -3608,7 +3248,7 @@ }, { "cell_type": "markdown", - "id": "b31aa210", + "id": "581256e4", "metadata": { "editable": true }, @@ -3620,7 +3260,7 @@ }, { "cell_type": "markdown", - "id": "04ca3686", + "id": "5a2cc1df", "metadata": { "editable": true }, @@ -3632,7 +3272,7 @@ }, { "cell_type": "markdown", - "id": "2744d555", + "id": "a81a54b1", "metadata": { "editable": true }, @@ -3647,7 +3287,7 @@ }, { "cell_type": "markdown", - "id": "b4dbc730", + "id": "9d2202e0", "metadata": { "editable": true }, @@ -3658,7 +3298,7 @@ }, { "cell_type": "markdown", - "id": "6a7e0d8d", + "id": "a0991d90", "metadata": { "editable": true }, @@ -3678,7 +3318,7 @@ }, { "cell_type": "markdown", - "id": "b5589818", + "id": "b4527e2b", "metadata": { "editable": true }, @@ -3690,7 +3330,7 @@ }, { "cell_type": "markdown", - "id": "ba92a58d", + "id": "ff44389a", "metadata": { "editable": true }, @@ -3700,7 +3340,7 @@ }, { "cell_type": "markdown", - "id": "3e42d32f", + "id": "5ac1645e", "metadata": { "editable": true }, @@ -3712,7 +3352,7 @@ }, { "cell_type": "markdown", - "id": "5871fc56", + "id": "a0890d70", "metadata": { "editable": true }, @@ -3741,7 +3381,7 @@ }, { "cell_type": "markdown", - "id": "2c40a587", + "id": "ff37a4d8", "metadata": { "editable": true }, @@ -3768,7 +3408,7 @@ }, { "cell_type": "markdown", - "id": "86bb27e5", + "id": "58bbcac9", "metadata": { "editable": true }, @@ -3778,8 +3418,8 @@ }, { "cell_type": "code", - "execution_count": 26, - "id": "7d723026", + "execution_count": 15, + "id": "3762157d", "metadata": { "collapsed": false, "editable": true @@ -3819,7 +3459,7 @@ }, { "cell_type": "markdown", - "id": "6ba20814", + "id": "9d01d3ac", "metadata": { "editable": true }, @@ -3836,7 +3476,7 @@ }, { "cell_type": "markdown", - "id": "f0ba9fdf", + "id": "62246539", "metadata": { "editable": true }, @@ -3859,7 +3499,7 @@ }, { "cell_type": "markdown", - "id": "05305864", + "id": "288c8b2e", "metadata": { "editable": true }, @@ -3873,7 +3513,7 @@ }, { "cell_type": "markdown", - "id": "ba19ecc6", + "id": "21070985", "metadata": { "editable": true }, @@ -3892,7 +3532,7 @@ }, { "cell_type": "markdown", - "id": "78fe6a71", + "id": "07eba868", "metadata": { "editable": true }, @@ -3902,7 +3542,7 @@ }, { "cell_type": "markdown", - "id": "eded4aaf", + "id": "68393979", "metadata": { "editable": true }, @@ -3914,7 +3554,7 @@ }, { "cell_type": "markdown", - "id": "986966fe", + "id": "d26fce24", "metadata": { "editable": true }, @@ -3928,7 +3568,7 @@ }, { "cell_type": "markdown", - "id": "c89f302c", + "id": "6bfcbc43", "metadata": { "editable": true }, @@ -3940,7 +3580,7 @@ }, { "cell_type": "markdown", - "id": "25a8285b", + "id": "f6cbe2c5", "metadata": { "editable": true }, @@ -3950,7 +3590,7 @@ }, { "cell_type": "markdown", - "id": "6cfac001", + "id": "1c421fc7", "metadata": { "editable": true }, @@ -3962,7 +3602,7 @@ }, { "cell_type": "markdown", - "id": "4578655e", + "id": "b53cd551", "metadata": { "editable": true }, @@ -3979,7 +3619,7 @@ }, { "cell_type": "markdown", - "id": "f363ae2e", + "id": "af27031a", "metadata": { "editable": true }, @@ -3989,7 +3629,7 @@ }, { "cell_type": "markdown", - "id": "e652538d", + "id": "6ce918d1", "metadata": { "editable": true }, @@ -4005,7 +3645,7 @@ }, { "cell_type": "markdown", - "id": "14e808f9", + "id": "50fb661f", "metadata": { "editable": true }, @@ -4015,7 +3655,7 @@ }, { "cell_type": "markdown", - "id": "270b06ed", + "id": "83f0bf41", "metadata": { "editable": true }, @@ -4031,7 +3671,7 @@ }, { "cell_type": "markdown", - "id": "81f802a2", + "id": "b2a2a893", "metadata": { "editable": true }, @@ -4041,7 +3681,7 @@ }, { "cell_type": "markdown", - "id": "201509ff", + "id": "14fa40d7", "metadata": { "editable": true }, @@ -4057,7 +3697,7 @@ }, { "cell_type": "markdown", - "id": "2e1c2673", + "id": "8f3d77c0", "metadata": { "editable": true }, @@ -4067,7 +3707,7 @@ }, { "cell_type": "markdown", - "id": "81f46769", + "id": "7536a405", "metadata": { "editable": true }, @@ -4084,7 +3724,7 @@ }, { "cell_type": "markdown", - "id": "67f178c5", + "id": "ee7e4cae", "metadata": { "editable": true }, @@ -4096,7 +3736,7 @@ }, { "cell_type": "markdown", - "id": "76c523eb", + "id": "dfcd0c38", "metadata": { "editable": true }, @@ -4108,7 +3748,7 @@ }, { "cell_type": "markdown", - "id": "0424921e", + "id": "fb1412d5", "metadata": { "editable": true }, @@ -4120,7 +3760,7 @@ }, { "cell_type": "markdown", - "id": "0745a41b", + "id": "b62112f7", "metadata": { "editable": true }, @@ -4130,7 +3770,7 @@ }, { "cell_type": "markdown", - "id": "b33fe91d", + "id": "364d925d", "metadata": { "editable": true }, @@ -4142,7 +3782,7 @@ }, { "cell_type": "markdown", - "id": "372dd691", + "id": "d87c572d", "metadata": { "editable": true }, @@ -4154,7 +3794,7 @@ }, { "cell_type": "markdown", - "id": "2bae5fff", + "id": "7a3a94aa", "metadata": { "editable": true }, @@ -4166,7 +3806,7 @@ }, { "cell_type": "markdown", - "id": "7df9ac42", + "id": "1a8472d3", "metadata": { "editable": true }, @@ -4176,7 +3816,7 @@ }, { "cell_type": "markdown", - "id": "33af8e3c", + "id": "4883d1ad", "metadata": { "editable": true }, @@ -4188,7 +3828,7 @@ }, { "cell_type": "markdown", - "id": "bc1796c4", + "id": "2692221f", "metadata": { "editable": true }, @@ -4198,7 +3838,7 @@ }, { "cell_type": "markdown", - "id": "adedaeb9", + "id": "5072a59c", "metadata": { "editable": true }, @@ -4210,7 +3850,7 @@ }, { "cell_type": "markdown", - "id": "823997bb", + "id": "9be9c1c1", "metadata": { "editable": true }, @@ -4226,7 +3866,7 @@ }, { "cell_type": "markdown", - "id": "63c5096e", + "id": "077ee510", "metadata": { "editable": true }, @@ -4238,7 +3878,7 @@ }, { "cell_type": "markdown", - "id": "d646164d", + "id": "8c630fbf", "metadata": { "editable": true }, @@ -4250,7 +3890,7 @@ }, { "cell_type": "markdown", - "id": "566adf44", + "id": "fc1ead71", "metadata": { "editable": true }, @@ -4260,7 +3900,7 @@ }, { "cell_type": "markdown", - "id": "41683df3", + "id": "4235481b", "metadata": { "editable": true }, @@ -4272,7 +3912,7 @@ }, { "cell_type": "markdown", - "id": "fefe96c7", + "id": "7222533e", "metadata": { "editable": true }, @@ -4283,7 +3923,7 @@ }, { "cell_type": "markdown", - "id": "376366a5", + "id": "513827f4", "metadata": { "editable": true }, @@ -4295,7 +3935,7 @@ }, { "cell_type": "markdown", - "id": "9a44dcea", + "id": "4d95be05", "metadata": { "editable": true }, @@ -4305,7 +3945,7 @@ }, { "cell_type": "markdown", - "id": "87f3a3ae", + "id": "fb1150ba", "metadata": { "editable": true }, @@ -4317,7 +3957,7 @@ }, { "cell_type": "markdown", - "id": "834dec2b", + "id": "3315a72e", "metadata": { "editable": true }, @@ -4327,7 +3967,7 @@ }, { "cell_type": "markdown", - "id": "955aff0d", + "id": "2746b8bc", "metadata": { "editable": true }, @@ -4339,7 +3979,7 @@ }, { "cell_type": "markdown", - "id": "f2cd6822", + "id": "b7425b50", "metadata": { "editable": true }, @@ -4350,7 +3990,7 @@ }, { "cell_type": "markdown", - "id": "bcc925fe", + "id": "4f807804", "metadata": { "editable": true }, @@ -4362,7 +4002,7 @@ }, { "cell_type": "markdown", - "id": "7d6c020e", + "id": "305f3427", "metadata": { "editable": true }, @@ -4380,7 +4020,7 @@ }, { "cell_type": "markdown", - "id": "91aab4f3", + "id": "c3422ea2", "metadata": { "editable": true }, @@ -4396,7 +4036,7 @@ }, { "cell_type": "markdown", - "id": "69c51950", + "id": "3775b045", "metadata": { "editable": true }, @@ -4408,7 +4048,7 @@ }, { "cell_type": "markdown", - "id": "b3d1753d", + "id": "3dd4f592", "metadata": { "editable": true }, @@ -4420,7 +4060,7 @@ }, { "cell_type": "markdown", - "id": "422a93cb", + "id": "ba660de3", "metadata": { "editable": true }, @@ -4432,7 +4072,7 @@ }, { "cell_type": "markdown", - "id": "e91fa3f9", + "id": "74fb4ec5", "metadata": { "editable": true }, @@ -4445,7 +4085,7 @@ }, { "cell_type": "markdown", - "id": "38d7243c", + "id": "a9c8d2c0", "metadata": { "editable": true }, @@ -4461,7 +4101,7 @@ }, { "cell_type": "markdown", - "id": "cf05427b", + "id": "fb203ace", "metadata": { "editable": true }, @@ -4475,7 +4115,7 @@ }, { "cell_type": "markdown", - "id": "669535d8", + "id": "8ab67c81", "metadata": { "editable": true }, @@ -4485,7 +4125,7 @@ }, { "cell_type": "markdown", - "id": "2d3f40e3", + "id": "f0ad9206", "metadata": { "editable": true }, @@ -4497,7 +4137,7 @@ }, { "cell_type": "markdown", - "id": "5a362e08", + "id": "3283fb3d", "metadata": { "editable": true }, @@ -4507,7 +4147,7 @@ }, { "cell_type": "markdown", - "id": "c6b7a147", + "id": "4bbd919f", "metadata": { "editable": true }, @@ -4519,7 +4159,7 @@ }, { "cell_type": "markdown", - "id": "d5057a56", + "id": "a6b10453", "metadata": { "editable": true }, @@ -4529,7 +4169,7 @@ }, { "cell_type": "markdown", - "id": "12933123", + "id": "b50ac67a", "metadata": { "editable": true }, @@ -4543,7 +4183,7 @@ }, { "cell_type": "markdown", - "id": "092207da", + "id": "d61e54e1", "metadata": { "editable": true }, @@ -4560,7 +4200,7 @@ }, { "cell_type": "markdown", - "id": "5a750215", + "id": "7a7ba311", "metadata": { "editable": true }, @@ -4576,7 +4216,7 @@ }, { "cell_type": "markdown", - "id": "441fe7f9", + "id": "e09eb880", "metadata": { "editable": true }, @@ -4588,7 +4228,7 @@ }, { "cell_type": "markdown", - "id": "ab03ac1e", + "id": "0f350d63", "metadata": { "editable": true }, @@ -4601,7 +4241,7 @@ }, { "cell_type": "markdown", - "id": "2c57e4b5", + "id": "1a8a8212", "metadata": { "editable": true }, @@ -4615,7 +4255,7 @@ }, { "cell_type": "markdown", - "id": "32b50f70", + "id": "c3026695", "metadata": { "editable": true }, @@ -4625,7 +4265,7 @@ }, { "cell_type": "markdown", - "id": "036448a4", + "id": "7c0237b7", "metadata": { "editable": true }, @@ -4638,7 +4278,7 @@ }, { "cell_type": "markdown", - "id": "c257faa8", + "id": "e7560854", "metadata": { "editable": true }, @@ -4657,7 +4297,7 @@ }, { "cell_type": "markdown", - "id": "1c9039ba", + "id": "f7bbecd4", "metadata": { "editable": true }, @@ -4669,7 +4309,7 @@ }, { "cell_type": "markdown", - "id": "0d3084a8", + "id": "8d9a4b52", "metadata": { "editable": true }, @@ -4681,7 +4321,7 @@ }, { "cell_type": "markdown", - "id": "67f574a8", + "id": "563665dd", "metadata": { "editable": true }, @@ -4691,7 +4331,7 @@ }, { "cell_type": "markdown", - "id": "eda6ee7a", + "id": "89454c32", "metadata": { "editable": true }, @@ -4703,7 +4343,7 @@ }, { "cell_type": "markdown", - "id": "4b6d4734", + "id": "1e871501", "metadata": { "editable": true }, @@ -4716,7 +4356,7 @@ }, { "cell_type": "markdown", - "id": "e257d49b", + "id": "499807ac", "metadata": { "editable": true }, @@ -4735,7 +4375,7 @@ }, { "cell_type": "markdown", - "id": "695b0f79", + "id": "74d0c0f2", "metadata": { "editable": true }, @@ -4745,7 +4385,7 @@ }, { "cell_type": "markdown", - "id": "f1d9028d", + "id": "cd589c1a", "metadata": { "editable": true }, @@ -4764,7 +4404,7 @@ }, { "cell_type": "markdown", - "id": "35bdcc95", + "id": "895e2a26", "metadata": { "editable": true }, @@ -4782,7 +4422,7 @@ }, { "cell_type": "markdown", - "id": "90cd1cea", + "id": "652ef993", "metadata": { "editable": true }, @@ -4796,7 +4436,7 @@ }, { "cell_type": "markdown", - "id": "53cde0d1", + "id": "032645aa", "metadata": { "editable": true }, @@ -4810,8 +4450,8 @@ }, { "cell_type": "code", - "execution_count": 27, - "id": "8a51d6f1", + "execution_count": 16, + "id": "3da55bd4", "metadata": { "collapsed": false, "editable": true @@ -4832,7 +4472,7 @@ }, { "cell_type": "markdown", - "id": "94b8b04d", + "id": "f3126b75", "metadata": { "editable": true }, @@ -4848,8 +4488,8 @@ }, { "cell_type": "code", - "execution_count": 28, - "id": "47392744", + "execution_count": 17, + "id": "1f290cec", "metadata": { "collapsed": false, "editable": true @@ -4881,7 +4521,7 @@ }, { "cell_type": "markdown", - "id": "33c0acc4", + "id": "4e6fb3a4", "metadata": { "editable": true }, @@ -4895,7 +4535,7 @@ }, { "cell_type": "markdown", - "id": "1b6103d0", + "id": "ecaa0231", "metadata": { "editable": true }, @@ -4907,8 +4547,8 @@ }, { "cell_type": "code", - "execution_count": 29, - "id": "bdfca206", + "execution_count": 18, + "id": "6563b3d9", "metadata": { "collapsed": false, "editable": true @@ -4933,7 +4573,7 @@ }, { "cell_type": "markdown", - "id": "b693ff15", + "id": "74841742", "metadata": { "editable": true }, @@ -4943,7 +4583,7 @@ }, { "cell_type": "markdown", - "id": "cacec5d1", + "id": "f55591a3", "metadata": { "editable": true }, @@ -4953,8 +4593,8 @@ }, { "cell_type": "code", - "execution_count": 30, - "id": "723177c1", + "execution_count": 19, + "id": "97451707", "metadata": { "collapsed": false, "editable": true @@ -5008,7 +4648,7 @@ }, { "cell_type": "markdown", - "id": "33b23bba", + "id": "6336656d", "metadata": { "editable": true }, @@ -5025,7 +4665,7 @@ }, { "cell_type": "markdown", - "id": "270fa62f", + "id": "0270b1d9", "metadata": { "editable": true }, @@ -5037,7 +4677,7 @@ }, { "cell_type": "markdown", - "id": "7974fb9e", + "id": "1ae64f83", "metadata": { "editable": true }, @@ -5049,7 +4689,7 @@ }, { "cell_type": "markdown", - "id": "139157fe", + "id": "2107ea40", "metadata": { "editable": true }, @@ -5059,7 +4699,7 @@ }, { "cell_type": "markdown", - "id": "0b37296d", + "id": "6a15504b", "metadata": { "editable": true }, @@ -5076,7 +4716,7 @@ }, { "cell_type": "markdown", - "id": "ad077374", + "id": "126c2eb3", "metadata": { "editable": true }, @@ -5086,7 +4726,7 @@ }, { "cell_type": "markdown", - "id": "d50ad735", + "id": "29ec770d", "metadata": { "editable": true }, @@ -5101,7 +4741,7 @@ }, { "cell_type": "markdown", - "id": "209e3d90", + "id": "a447cee9", "metadata": { "editable": true }, @@ -5111,7 +4751,7 @@ }, { "cell_type": "markdown", - "id": "50b76f78", + "id": "c92c270b", "metadata": { "editable": true }, @@ -5125,7 +4765,7 @@ }, { "cell_type": "markdown", - "id": "d059450a", + "id": "505d1ebf", "metadata": { "editable": true }, @@ -5137,7 +4777,7 @@ }, { "cell_type": "markdown", - "id": "6386e031", + "id": "89f6c701", "metadata": { "editable": true }, @@ -5149,7 +4789,7 @@ }, { "cell_type": "markdown", - "id": "f3e508b8", + "id": "38394640", "metadata": { "editable": true }, @@ -5161,7 +4801,7 @@ }, { "cell_type": "markdown", - "id": "cc3e754f", + "id": "db517ed1", "metadata": { "editable": true }, @@ -5171,7 +4811,7 @@ }, { "cell_type": "markdown", - "id": "9c7c81ad", + "id": "83d2b9db", "metadata": { "editable": true }, @@ -5183,7 +4823,7 @@ }, { "cell_type": "markdown", - "id": "281db6b9", + "id": "30f5763a", "metadata": { "editable": true }, @@ -5193,7 +4833,7 @@ }, { "cell_type": "markdown", - "id": "b115f208", + "id": "c62e872d", "metadata": { "editable": true }, @@ -5210,7 +4850,7 @@ }, { "cell_type": "markdown", - "id": "251348ff", + "id": "bcf91015", "metadata": { "editable": true }, @@ -5220,7 +4860,7 @@ }, { "cell_type": "markdown", - "id": "5e8999a8", + "id": "71e23263", "metadata": { "editable": true }, @@ -5232,7 +4872,7 @@ }, { "cell_type": "markdown", - "id": "baa41b1f", + "id": "d47a7f94", "metadata": { "editable": true }, @@ -5242,7 +4882,7 @@ }, { "cell_type": "markdown", - "id": "edc49a9c", + "id": "5f566839", "metadata": { "editable": true }, @@ -5254,7 +4894,7 @@ }, { "cell_type": "markdown", - "id": "aa468374", + "id": "8a9cac2d", "metadata": { "editable": true }, @@ -5268,7 +4908,7 @@ }, { "cell_type": "markdown", - "id": "fe80818f", + "id": "44c5e88d", "metadata": { "editable": true }, @@ -5280,7 +4920,7 @@ }, { "cell_type": "markdown", - "id": "1b07632d", + "id": "5c85f4a9", "metadata": { "editable": true }, @@ -5302,7 +4942,7 @@ }, { "cell_type": "markdown", - "id": "c5200e7a", + "id": "f6bdb4e0", "metadata": { "editable": true }, @@ -5314,7 +4954,7 @@ }, { "cell_type": "markdown", - "id": "0241bc7f", + "id": "3ff7bf25", "metadata": { "editable": true }, @@ -5329,7 +4969,7 @@ }, { "cell_type": "markdown", - "id": "a1c425c2", + "id": "5de2f572", "metadata": { "editable": true }, @@ -5341,7 +4981,7 @@ }, { "cell_type": "markdown", - "id": "dd5e8a32", + "id": "890ab675", "metadata": { "editable": true }, @@ -5353,7 +4993,7 @@ }, { "cell_type": "markdown", - "id": "e6571925", + "id": "ada4ab47", "metadata": { "editable": true }, @@ -5363,7 +5003,7 @@ }, { "cell_type": "markdown", - "id": "e7dbd70e", + "id": "b3030612", "metadata": { "editable": true }, @@ -5375,7 +5015,7 @@ }, { "cell_type": "markdown", - "id": "1466e8ae", + "id": "7d14cf92", "metadata": { "editable": true }, @@ -5385,7 +5025,7 @@ }, { "cell_type": "markdown", - "id": "d7f724ef", + "id": "478e7859", "metadata": { "editable": true }, @@ -5397,7 +5037,7 @@ }, { "cell_type": "markdown", - "id": "820829f8", + "id": "71faa986", "metadata": { "editable": true }, @@ -5407,7 +5047,7 @@ }, { "cell_type": "markdown", - "id": "2dd2f701", + "id": "b49d9644", "metadata": { "editable": true }, @@ -5419,7 +5059,7 @@ }, { "cell_type": "markdown", - "id": "1df74a76", + "id": "75d87d0a", "metadata": { "editable": true }, @@ -5436,7 +5076,7 @@ }, { "cell_type": "markdown", - "id": "8ef1c4f6", + "id": "5ba19317", "metadata": { "editable": true }, @@ -5449,7 +5089,7 @@ }, { "cell_type": "markdown", - "id": "d6e4c1f4", + "id": "b788ee7c", "metadata": { "editable": true }, @@ -5461,7 +5101,7 @@ }, { "cell_type": "markdown", - "id": "0ddd588d", + "id": "58c6e8e9", "metadata": { "editable": true }, @@ -5471,7 +5111,7 @@ }, { "cell_type": "markdown", - "id": "42f48807", + "id": "c4d1982b", "metadata": { "editable": true }, @@ -5484,7 +5124,7 @@ }, { "cell_type": "markdown", - "id": "829c3c2e", + "id": "73af0556", "metadata": { "editable": true }, @@ -5494,7 +5134,7 @@ }, { "cell_type": "markdown", - "id": "2a3748ae", + "id": "af846cba", "metadata": { "editable": true }, @@ -5506,7 +5146,7 @@ }, { "cell_type": "markdown", - "id": "6bcf3ebf", + "id": "b8013512", "metadata": { "editable": true }, @@ -5519,7 +5159,7 @@ }, { "cell_type": "markdown", - "id": "a7b8985f", + "id": "62986a9f", "metadata": { "editable": true }, @@ -5532,7 +5172,7 @@ }, { "cell_type": "markdown", - "id": "a5edb980", + "id": "5f524eb9", "metadata": { "editable": true }, @@ -5544,7 +5184,7 @@ }, { "cell_type": "markdown", - "id": "47925d0e", + "id": "8395a321", "metadata": { "editable": true }, @@ -5556,7 +5196,7 @@ }, { "cell_type": "markdown", - "id": "287a10ae", + "id": "a7538c53", "metadata": { "editable": true }, @@ -5566,7 +5206,7 @@ }, { "cell_type": "markdown", - "id": "0e81023f", + "id": "906c7328", "metadata": { "editable": true }, @@ -5579,7 +5219,7 @@ }, { "cell_type": "markdown", - "id": "e7615ac4", + "id": "a306b720", "metadata": { "editable": true }, @@ -5591,7 +5231,7 @@ }, { "cell_type": "markdown", - "id": "8214a390", + "id": "acbdd651", "metadata": { "editable": true }, @@ -5603,7 +5243,7 @@ }, { "cell_type": "markdown", - "id": "154da46f", + "id": "b0804db3", "metadata": { "editable": true }, @@ -5615,7 +5255,7 @@ }, { "cell_type": "markdown", - "id": "b62e6cfe", + "id": "856ab9d0", "metadata": { "editable": true }, @@ -5627,7 +5267,7 @@ }, { "cell_type": "markdown", - "id": "2d4462c8", + "id": "43f54a70", "metadata": { "editable": true }, @@ -5641,7 +5281,7 @@ }, { "cell_type": "markdown", - "id": "e9e4c483", + "id": "63da6aed", "metadata": { "editable": true }, @@ -5653,7 +5293,7 @@ }, { "cell_type": "markdown", - "id": "35403527", + "id": "98f98587", "metadata": { "editable": true }, @@ -5663,7 +5303,7 @@ }, { "cell_type": "markdown", - "id": "6b4f2461", + "id": "f068d670", "metadata": { "editable": true }, @@ -5675,7 +5315,7 @@ }, { "cell_type": "markdown", - "id": "e41db532", + "id": "a0acad84", "metadata": { "editable": true }, @@ -5687,7 +5327,7 @@ }, { "cell_type": "markdown", - "id": "6fbf9ebc", + "id": "83e04a36", "metadata": { "editable": true }, @@ -5699,7 +5339,7 @@ }, { "cell_type": "markdown", - "id": "79798101", + "id": "d0a3bc94", "metadata": { "editable": true }, @@ -5711,7 +5351,7 @@ }, { "cell_type": "markdown", - "id": "5155ed23", + "id": "7dc331ce", "metadata": { "editable": true }, @@ -5723,7 +5363,7 @@ }, { "cell_type": "markdown", - "id": "26857a03", + "id": "7b482417", "metadata": { "editable": true }, @@ -5742,7 +5382,7 @@ }, { "cell_type": "markdown", - "id": "5b73b124", + "id": "5410e9a0", "metadata": { "editable": true }, @@ -5754,7 +5394,7 @@ }, { "cell_type": "markdown", - "id": "d3fbbcea", + "id": "6face8fc", "metadata": { "editable": true }, @@ -5764,7 +5404,7 @@ }, { "cell_type": "markdown", - "id": "df2c5b38", + "id": "8ea80093", "metadata": { "editable": true }, @@ -5776,7 +5416,7 @@ }, { "cell_type": "markdown", - "id": "b3c2ae09", + "id": "8cf06863", "metadata": { "editable": true }, @@ -5786,7 +5426,7 @@ }, { "cell_type": "markdown", - "id": "0093c22b", + "id": "bc27c492", "metadata": { "editable": true }, @@ -5798,7 +5438,7 @@ }, { "cell_type": "markdown", - "id": "ce264b0f", + "id": "24763b7f", "metadata": { "editable": true }, @@ -5810,7 +5450,7 @@ }, { "cell_type": "markdown", - "id": "43d539b4", + "id": "1017238e", "metadata": { "editable": true }, @@ -5826,7 +5466,7 @@ }, { "cell_type": "markdown", - "id": "4c91ef19", + "id": "70fadf69", "metadata": { "editable": true }, @@ -5838,7 +5478,7 @@ }, { "cell_type": "markdown", - "id": "d5f365d5", + "id": "5c355331", "metadata": { "editable": true }, @@ -5850,7 +5490,7 @@ }, { "cell_type": "markdown", - "id": "3a283860", + "id": "983900f5", "metadata": { "editable": true }, @@ -5860,7 +5500,7 @@ }, { "cell_type": "markdown", - "id": "b7bd9d23", + "id": "0c8e05df", "metadata": { "editable": true }, @@ -5872,7 +5512,7 @@ }, { "cell_type": "markdown", - "id": "b04077ea", + "id": "67472aec", "metadata": { "editable": true }, @@ -5882,7 +5522,7 @@ }, { "cell_type": "markdown", - "id": "6b466931", + "id": "9aabe2d8", "metadata": { "editable": true }, @@ -5894,7 +5534,7 @@ }, { "cell_type": "markdown", - "id": "21350df8", + "id": "cdf138fb", "metadata": { "editable": true }, @@ -5911,7 +5551,7 @@ }, { "cell_type": "markdown", - "id": "6d69b078", + "id": "d52a2ceb", "metadata": { "editable": true }, @@ -5923,7 +5563,7 @@ }, { "cell_type": "markdown", - "id": "16f84dae", + "id": "f2dff249", "metadata": { "editable": true }, @@ -5935,7 +5575,7 @@ }, { "cell_type": "markdown", - "id": "3d86d4c0", + "id": "09adf91c", "metadata": { "editable": true }, @@ -5945,7 +5585,7 @@ }, { "cell_type": "markdown", - "id": "196fbf87", + "id": "347a4405", "metadata": { "editable": true }, @@ -5957,7 +5597,7 @@ }, { "cell_type": "markdown", - "id": "003336eb", + "id": "a7baf373", "metadata": { "editable": true }, @@ -5967,7 +5607,7 @@ }, { "cell_type": "markdown", - "id": "3bca8e80", + "id": "005a3830", "metadata": { "editable": true }, @@ -5979,7 +5619,7 @@ }, { "cell_type": "markdown", - "id": "fc570f8a", + "id": "3a2185dc", "metadata": { "editable": true }, @@ -5989,7 +5629,7 @@ }, { "cell_type": "markdown", - "id": "99e5bedb", + "id": "c7162e5c", "metadata": { "editable": true }, @@ -6001,7 +5641,7 @@ }, { "cell_type": "markdown", - "id": "736c1f1e", + "id": "b47a8381", "metadata": { "editable": true }, @@ -6011,7 +5651,7 @@ }, { "cell_type": "markdown", - "id": "c1cc149d", + "id": "6ec69d77", "metadata": { "editable": true }, @@ -6023,7 +5663,7 @@ }, { "cell_type": "markdown", - "id": "0e7699e1", + "id": "dcb8df3d", "metadata": { "editable": true }, diff --git a/doc/src/week35/week35.do.txt b/doc/src/week35/week35.do.txt index 81ab65d62..3e0d42358 100644 --- a/doc/src/week35/week35.do.txt +++ b/doc/src/week35/week35.do.txt @@ -18,16 +18,15 @@ o Monday: Ridge and Lasso regression and Singular Value Decomposition o See lecture notes for week 35 at URL:"https://compphysics.github.io/MachineLearning/doc/web/course.html" o Goodfellow, Bengio and Courville, Deep Learning, chapter 2 on linear algebra and sections 3.1-3.10 on elements of statistics (background) - - - + o Raschka et al on preprocessing of data, relevant for exercise 3 this week, see chapter 4. + o For exercise 1 of week 35, the book by A. Aldo Faisal, Cheng Soon Ong, and Marc Peter Deisenroth on the Mathematics of Machine Learning, may be very relevant. In particular chapter 5 at URL"https://mml-book.github.io/" (section 5.5 on derivatives) is very useful for exercise 1 this coming week. !split -===== Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week ===== +===== For exercise sessions: Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week ===== We need first a reminder from last week about linear regression. @@ -1376,159 +1375,6 @@ regression next week. -!split -===== The Boston housing data example ===== - -The Boston housing -data set was originally a part of UCI Machine Learning Repository -and has been removed now. The data set is now included in _Scikit-Learn_'s -library. There are 506 samples and 13 feature (predictor) variables -in this data set. The objective is to predict the value of prices of -the house using the features (predictors) listed here. - -The features/predictors are - o CRIM: Per capita crime rate by town - o ZN: Proportion of residential land zoned for lots over 25000 square feet - o INDUS: Proportion of non-retail business acres per town - o CHAS: Charles River dummy variable (= 1 if tract bounds river; 0 otherwise) - o NOX: Nitric oxide concentration (parts per 10 million) - o RM: Average number of rooms per dwelling - o AGE: Proportion of owner-occupied units built prior to 1940 - o DIS: Weighted distances to five Boston employment centers - o RAD: Index of accessibility to radial highways - o TAX: Full-value property tax rate per USD10000 - o B: $1000(Bk - 0.63)^2$, where $Bk$ is the proportion of [people of African American descent] by town - o LSTAT: Percentage of lower status of the population - o MEDV: Median value of owner-occupied homes in USD 1000s - -!split -===== Housing data, the code ===== -We start by importing the libraries -!bc pycod -import numpy as np -import matplotlib.pyplot as plt - -import pandas as pd -import seaborn as sns -!ec -and load the Boston Housing DataSet from _Scikit-Learn_ - - -!bc pycod -from sklearn.datasets import load_boston - -boston_dataset = load_boston() - -# boston_dataset is a dictionary -# let's check what it contains -boston_dataset.keys() -!ec -Then we invoke Pandas -!bc pycod -boston = pd.DataFrame(boston_dataset.data, columns=boston_dataset.feature_names) -boston.head() -boston['MEDV'] = boston_dataset.target -!ec -and preprocess the data -!bc pycod -# check for missing values in all the columns -boston.isnull().sum() -!ec -We can then visualize the data -!bc pycod -# set the size of the figure -sns.set(rc={'figure.figsize':(11.7,8.27)}) - -# plot a histogram showing the distribution of the target values -sns.distplot(boston['MEDV'], bins=30) -plt.show() -!ec - -It is now useful to look at the correlation matrix -!bc pycod -# compute the pair wise correlation for all columns -correlation_matrix = boston.corr().round(2) -# use the heatmap function from seaborn to plot the correlation matrix -# annot = True to print the values inside the square -sns.heatmap(data=correlation_matrix, annot=True) -!ec -From the above coorelation plot we can see that _MEDV_ is strongly correlated to _LSTAT_ and _RM_. We see also that _RAD_ and _TAX_ are stronly correlated, but we don't include this in our features together to avoid multi-colinearity - -!bc pycod -plt.figure(figsize=(20, 5)) - -features = ['LSTAT', 'RM'] -target = boston['MEDV'] - -for i, col in enumerate(features): - plt.subplot(1, len(features) , i+1) - x = boston[col] - y = target - plt.scatter(x, y, marker='o') - plt.title(col) - plt.xlabel(col) - plt.ylabel('MEDV') -!ec -Now we start training our model -!bc pycod -X = pd.DataFrame(np.c_[boston['LSTAT'], boston['RM']], columns = ['LSTAT','RM']) -Y = boston['MEDV'] -!ec -We split the data into training and test sets - -!bc pycod -from sklearn.model_selection import train_test_split - -# splits the training and test data set in 80% : 20% -# assign random_state to any value.This ensures consistency. -X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state=5) -print(X_train.shape) -print(X_test.shape) -print(Y_train.shape) -print(Y_test.shape) -!ec -Then we use the linear regression functionality from _Scikit-Learn_ -!bc pycod -from sklearn.linear_model import LinearRegression -from sklearn.metrics import mean_squared_error, r2_score - -lin_model = LinearRegression() -lin_model.fit(X_train, Y_train) - -# model evaluation for training set - -y_train_predict = lin_model.predict(X_train) -rmse = (np.sqrt(mean_squared_error(Y_train, y_train_predict))) -r2 = r2_score(Y_train, y_train_predict) - -print("The model performance for training set") -print("--------------------------------------") -print('RMSE is {}'.format(rmse)) -print('R2 score is {}'.format(r2)) -print("\n") - -# model evaluation for testing set - -y_test_predict = lin_model.predict(X_test) -# root mean square error of the model -rmse = (np.sqrt(mean_squared_error(Y_test, y_test_predict))) - -# r-squared score of the model -r2 = r2_score(Y_test, y_test_predict) - -print("The model performance for testing set") -print("--------------------------------------") -print('RMSE is {}'.format(rmse)) -print('R2 score is {}'.format(r2)) -!ec - -!bc pycod -# plotting the y_test vs y_pred -# ideally should have been a straight line -plt.scatter(Y_test, y_test_predict) -plt.show() -!ec -