430 lines
28 KiB
HTML
430 lines
28 KiB
HTML
<!--
|
|
HTML file automatically generated from DocOnce source
|
|
(https://github.com/doconce/doconce/)
|
|
doconce format html week38.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week38-bs --no_mako
|
|
-->
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<meta name="description" content="Data Analysis and Machine Learning: Logistic Regression and Optimization">
|
|
<title>Data Analysis and Machine Learning: Logistic Regression and Optimization</title>
|
|
<!-- Bootstrap style: bootstrap -->
|
|
<!-- doconce format html week38.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week38-bs --no_mako -->
|
|
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
|
<!-- not necessary
|
|
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
|
|
-->
|
|
<style type="text/css">
|
|
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
|
|
.dropdown-menu {
|
|
height: auto;
|
|
max-height: 400px;
|
|
overflow-x: hidden;
|
|
}
|
|
/* Adds an invisible element before each target to offset for the navigation
|
|
bar */
|
|
.anchor::before {
|
|
content:"";
|
|
display:block;
|
|
height:50px; /* fixed header height for style bootstrap */
|
|
margin:-50px 0 0; /* negative fixed header height */
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<!-- tocinfo
|
|
{'highest level': 2,
|
|
'sections': [('Plans for week 38', 2, None, 'plans-for-week-38'),
|
|
('Ridge and LASSO Regression, reminder',
|
|
2,
|
|
None,
|
|
'ridge-and-lasso-regression-reminder'),
|
|
('Various steps in cross-validation',
|
|
2,
|
|
None,
|
|
'various-steps-in-cross-validation'),
|
|
('How to set up the cross-validation for Ridge and/or Lasso',
|
|
2,
|
|
None,
|
|
'how-to-set-up-the-cross-validation-for-ridge-and-or-lasso'),
|
|
('Cross-validation in brief',
|
|
2,
|
|
None,
|
|
'cross-validation-in-brief'),
|
|
('Code Example for Cross-validation and $k$-fold '
|
|
'Cross-validation',
|
|
2,
|
|
None,
|
|
'code-example-for-cross-validation-and-k-fold-cross-validation'),
|
|
('Logistic Regression', 2, None, 'logistic-regression'),
|
|
('Classification problems', 2, None, 'classification-problems'),
|
|
('Optimization and Deep learning',
|
|
2,
|
|
None,
|
|
'optimization-and-deep-learning'),
|
|
('Basics', 2, None, 'basics'),
|
|
('Linear classifier', 2, None, 'linear-classifier'),
|
|
('Some selected properties', 2, None, 'some-selected-properties'),
|
|
('Simple example', 2, None, 'simple-example'),
|
|
('Plotting the mean value for each group',
|
|
2,
|
|
None,
|
|
'plotting-the-mean-value-for-each-group'),
|
|
('The logistic function', 2, None, 'the-logistic-function'),
|
|
('Examples of likelihood functions used in logistic regression '
|
|
'and nueral networks',
|
|
2,
|
|
None,
|
|
'examples-of-likelihood-functions-used-in-logistic-regression-and-nueral-networks'),
|
|
('Two parameters', 2, None, 'two-parameters'),
|
|
('Maximum likelihood', 2, None, 'maximum-likelihood'),
|
|
('The cost function rewritten',
|
|
2,
|
|
None,
|
|
'the-cost-function-rewritten'),
|
|
('Minimizing the cross entropy',
|
|
2,
|
|
None,
|
|
'minimizing-the-cross-entropy'),
|
|
('A more compact expression',
|
|
2,
|
|
None,
|
|
'a-more-compact-expression'),
|
|
('Extending to more predictors',
|
|
2,
|
|
None,
|
|
'extending-to-more-predictors'),
|
|
('Including more classes', 2, None, 'including-more-classes'),
|
|
('More classes', 2, None, 'more-classes'),
|
|
('Friday September 23', 2, None, 'friday-september-23'),
|
|
('Searching for Optimal Regularization Parameters $\\lambda$',
|
|
2,
|
|
None,
|
|
'searching-for-optimal-regularization-parameters-lambda'),
|
|
('Grid Search', 2, None, 'grid-search'),
|
|
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
|
|
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
|
|
('Using the correlation matrix',
|
|
2,
|
|
None,
|
|
'using-the-correlation-matrix'),
|
|
('Discussing the correlation data',
|
|
2,
|
|
None,
|
|
'discussing-the-correlation-data'),
|
|
('Other measures in classification studies: Cancer Data again',
|
|
2,
|
|
None,
|
|
'other-measures-in-classification-studies-cancer-data-again'),
|
|
('Optimization, the central part of any Machine Learning '
|
|
'algortithm',
|
|
2,
|
|
None,
|
|
'optimization-the-central-part-of-any-machine-learning-algortithm'),
|
|
('Revisiting our Logistic Regression case',
|
|
2,
|
|
None,
|
|
'revisiting-our-logistic-regression-case'),
|
|
('The equations to solve', 2, None, 'the-equations-to-solve'),
|
|
("Solving using Newton-Raphson's method",
|
|
2,
|
|
None,
|
|
'solving-using-newton-raphson-s-method'),
|
|
("Brief reminder on Newton-Raphson's method",
|
|
2,
|
|
None,
|
|
'brief-reminder-on-newton-raphson-s-method'),
|
|
('The equations', 2, None, 'the-equations'),
|
|
('Simple geometric interpretation',
|
|
2,
|
|
None,
|
|
'simple-geometric-interpretation'),
|
|
('Extending to more than one variable',
|
|
2,
|
|
None,
|
|
'extending-to-more-than-one-variable'),
|
|
('Steepest descent', 2, None, 'steepest-descent'),
|
|
('More on Steepest descent', 2, None, 'more-on-steepest-descent'),
|
|
('The ideal', 2, None, 'the-ideal'),
|
|
('The sensitiveness of the gradient descent',
|
|
2,
|
|
None,
|
|
'the-sensitiveness-of-the-gradient-descent'),
|
|
('Convex functions', 2, None, 'convex-functions'),
|
|
('Convex function', 2, None, 'convex-function'),
|
|
('Conditions on convex functions',
|
|
2,
|
|
None,
|
|
'conditions-on-convex-functions'),
|
|
('More on convex functions', 2, None, 'more-on-convex-functions'),
|
|
('Some simple problems', 2, None, 'some-simple-problems'),
|
|
('Revisiting our first homework',
|
|
2,
|
|
None,
|
|
'revisiting-our-first-homework'),
|
|
('Gradient descent example', 2, None, 'gradient-descent-example'),
|
|
('The derivative of the cost/loss function',
|
|
2,
|
|
None,
|
|
'the-derivative-of-the-cost-loss-function'),
|
|
('The Hessian matrix', 2, None, 'the-hessian-matrix'),
|
|
('Simple program', 2, None, 'simple-program'),
|
|
('Gradient Descent Example', 2, None, 'gradient-descent-example'),
|
|
('And a corresponding example using _scikit-learn_',
|
|
2,
|
|
None,
|
|
'and-a-corresponding-example-using-scikit-learn'),
|
|
('Gradient descent and Ridge',
|
|
2,
|
|
None,
|
|
'gradient-descent-and-ridge'),
|
|
('The Hessian matrix for Ridge Regression',
|
|
2,
|
|
None,
|
|
'the-hessian-matrix-for-ridge-regression'),
|
|
('Program example for gradient descent with Ridge Regression',
|
|
2,
|
|
None,
|
|
'program-example-for-gradient-descent-with-ridge-regression'),
|
|
('Using gradient descent methods, limitations',
|
|
2,
|
|
None,
|
|
'using-gradient-descent-methods-limitations'),
|
|
('Challenge yourself this weekend',
|
|
2,
|
|
None,
|
|
'challenge-yourself-this-weekend')]}
|
|
end of tocinfo -->
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<script type="text/x-mathjax-config">
|
|
MathJax.Hub.Config({
|
|
TeX: {
|
|
equationNumbers: { autoNumber: "none" },
|
|
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
|
|
}
|
|
});
|
|
</script>
|
|
<script type="text/javascript" async
|
|
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
|
|
</script>
|
|
|
|
|
|
<!-- Bootstrap navigation bar -->
|
|
<div class="navbar navbar-default navbar-fixed-top">
|
|
<div class="navbar-header">
|
|
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
|
|
<span class="icon-bar"></span>
|
|
<span class="icon-bar"></span>
|
|
<span class="icon-bar"></span>
|
|
</button>
|
|
<a class="navbar-brand" href="week38-bs.html">Data Analysis and Machine Learning: Logistic Regression and Optimization</a>
|
|
</div>
|
|
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
|
<ul class="nav navbar-nav navbar-right">
|
|
<li class="dropdown">
|
|
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
|
<ul class="dropdown-menu">
|
|
<!-- navigation toc: --> <li><a href="._week38-bs001.html#plans-for-week-38" style="font-size: 80%;">Plans for week 38</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs002.html#ridge-and-lasso-regression-reminder" style="font-size: 80%;">Ridge and LASSO Regression, reminder</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs003.html#various-steps-in-cross-validation" style="font-size: 80%;">Various steps in cross-validation</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs004.html#how-to-set-up-the-cross-validation-for-ridge-and-or-lasso" style="font-size: 80%;">How to set up the cross-validation for Ridge and/or Lasso</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs005.html#cross-validation-in-brief" style="font-size: 80%;">Cross-validation in brief</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs006.html#code-example-for-cross-validation-and-k-fold-cross-validation" style="font-size: 80%;">Code Example for Cross-validation and \( k \)-fold Cross-validation</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs007.html#logistic-regression" style="font-size: 80%;">Logistic Regression</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs008.html#classification-problems" style="font-size: 80%;">Classification problems</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs009.html#optimization-and-deep-learning" style="font-size: 80%;">Optimization and Deep learning</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs010.html#basics" style="font-size: 80%;">Basics</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs011.html#linear-classifier" style="font-size: 80%;">Linear classifier</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs012.html#some-selected-properties" style="font-size: 80%;">Some selected properties</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs013.html#simple-example" style="font-size: 80%;">Simple example</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs014.html#plotting-the-mean-value-for-each-group" style="font-size: 80%;">Plotting the mean value for each group</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs015.html#the-logistic-function" style="font-size: 80%;">The logistic function</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs016.html#examples-of-likelihood-functions-used-in-logistic-regression-and-nueral-networks" style="font-size: 80%;">Examples of likelihood functions used in logistic regression and nueral networks</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs017.html#two-parameters" style="font-size: 80%;">Two parameters</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs018.html#maximum-likelihood" style="font-size: 80%;">Maximum likelihood</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs019.html#the-cost-function-rewritten" style="font-size: 80%;">The cost function rewritten</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs020.html#minimizing-the-cross-entropy" style="font-size: 80%;">Minimizing the cross entropy</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs021.html#a-more-compact-expression" style="font-size: 80%;">A more compact expression</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs022.html#extending-to-more-predictors" style="font-size: 80%;">Extending to more predictors</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs023.html#including-more-classes" style="font-size: 80%;">Including more classes</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs024.html#more-classes" style="font-size: 80%;">More classes</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs025.html#friday-september-23" style="font-size: 80%;">Friday September 23</a></li>
|
|
<!-- navigation toc: --> <li><a href="#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;">Searching for Optimal Regularization Parameters \( \lambda \)</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs027.html#grid-search" style="font-size: 80%;">Grid Search</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs028.html#randomized-grid-search" style="font-size: 80%;">Randomized Grid Search</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs029.html#wisconsin-cancer-data" style="font-size: 80%;">Wisconsin Cancer Data</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs030.html#using-the-correlation-matrix" style="font-size: 80%;">Using the correlation matrix</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs031.html#discussing-the-correlation-data" style="font-size: 80%;">Discussing the correlation data</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs032.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;">Other measures in classification studies: Cancer Data again</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs033.html#optimization-the-central-part-of-any-machine-learning-algortithm" style="font-size: 80%;">Optimization, the central part of any Machine Learning algortithm</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs034.html#revisiting-our-logistic-regression-case" style="font-size: 80%;">Revisiting our Logistic Regression case</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs035.html#the-equations-to-solve" style="font-size: 80%;">The equations to solve</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs036.html#solving-using-newton-raphson-s-method" style="font-size: 80%;">Solving using Newton-Raphson's method</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs037.html#brief-reminder-on-newton-raphson-s-method" style="font-size: 80%;">Brief reminder on Newton-Raphson's method</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs038.html#the-equations" style="font-size: 80%;">The equations</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs039.html#simple-geometric-interpretation" style="font-size: 80%;">Simple geometric interpretation</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs040.html#extending-to-more-than-one-variable" style="font-size: 80%;">Extending to more than one variable</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs041.html#steepest-descent" style="font-size: 80%;">Steepest descent</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs042.html#more-on-steepest-descent" style="font-size: 80%;">More on Steepest descent</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs043.html#the-ideal" style="font-size: 80%;">The ideal</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs044.html#the-sensitiveness-of-the-gradient-descent" style="font-size: 80%;">The sensitiveness of the gradient descent</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs045.html#convex-functions" style="font-size: 80%;">Convex functions</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs046.html#convex-function" style="font-size: 80%;">Convex function</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs047.html#conditions-on-convex-functions" style="font-size: 80%;">Conditions on convex functions</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs048.html#more-on-convex-functions" style="font-size: 80%;">More on convex functions</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs049.html#some-simple-problems" style="font-size: 80%;">Some simple problems</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs050.html#revisiting-our-first-homework" style="font-size: 80%;">Revisiting our first homework</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs055.html#gradient-descent-example" style="font-size: 80%;">Gradient descent example</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs052.html#the-derivative-of-the-cost-loss-function" style="font-size: 80%;">The derivative of the cost/loss function</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs053.html#the-hessian-matrix" style="font-size: 80%;">The Hessian matrix</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs054.html#simple-program" style="font-size: 80%;">Simple program</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs055.html#gradient-descent-example" style="font-size: 80%;">Gradient Descent Example</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs056.html#and-a-corresponding-example-using-scikit-learn" style="font-size: 80%;">And a corresponding example using <b>scikit-learn</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs057.html#gradient-descent-and-ridge" style="font-size: 80%;">Gradient descent and Ridge</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs058.html#the-hessian-matrix-for-ridge-regression" style="font-size: 80%;">The Hessian matrix for Ridge Regression</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs059.html#program-example-for-gradient-descent-with-ridge-regression" style="font-size: 80%;">Program example for gradient descent with Ridge Regression</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs060.html#using-gradient-descent-methods-limitations" style="font-size: 80%;">Using gradient descent methods, limitations</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week38-bs061.html#challenge-yourself-this-weekend" style="font-size: 80%;">Challenge yourself this weekend</a></li>
|
|
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div> <!-- end of navigation bar -->
|
|
<div class="container">
|
|
<p> </p><p> </p><p> </p> <!-- add vertical space -->
|
|
<a name="part0026"></a>
|
|
<!-- !split -->
|
|
<h2 id="searching-for-optimal-regularization-parameters-lambda" class="anchor">Searching for Optimal Regularization Parameters \( \lambda \) </h2>
|
|
|
|
<p>In project 1, when using Ridge and Lasso regression, we end up
|
|
searching for the optimal parameter \( \lambda \) which minimizes our
|
|
selected scores (MSE or \( R2 \) values for example). The brute force
|
|
approach, as discussed in the code here for Ridge regression, consists
|
|
in evaluating the MSE as function of different \( \lambda \) values.
|
|
Based on these calculations, one tries then to determine the value of the hyperparameter \( \lambda \)
|
|
which results in optimal scores (for example the smallest MSE or an \( R2=1 \)).
|
|
</p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
<div class="cell border-box-sizing code_cell rendered">
|
|
<div class="input">
|
|
<div class="inner_cell">
|
|
<div class="input_area">
|
|
<div class="highlight" style="background: #f8f8f8">
|
|
<pre style="line-height: 125%;"><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
|
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
|
|
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
|
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
|
|
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn</span> <span style="color: #008000; font-weight: bold">import</span> linear_model
|
|
|
|
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">MSE</span>(y_data,y_model):
|
|
n <span style="color: #666666">=</span> np<span style="color: #666666">.</span>size(y_model)
|
|
<span style="color: #008000; font-weight: bold">return</span> np<span style="color: #666666">.</span>sum((y_data<span style="color: #666666">-</span>y_model)<span style="color: #666666">**2</span>)<span style="color: #666666">/</span>n
|
|
<span style="color: #408080; font-style: italic"># A seed just to ensure that the random numbers are the same for every run.</span>
|
|
<span style="color: #408080; font-style: italic"># Useful for eventual debugging.</span>
|
|
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">2021</span>)
|
|
|
|
n <span style="color: #666666">=</span> <span style="color: #666666">100</span>
|
|
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(n)
|
|
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>x<span style="color: #666666">**2</span>) <span style="color: #666666">+</span> <span style="color: #666666">1.5</span> <span style="color: #666666">*</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>(x<span style="color: #666666">-2</span>)<span style="color: #666666">**2</span>)<span style="color: #666666">+</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(n)
|
|
|
|
Maxpolydegree <span style="color: #666666">=</span> <span style="color: #666666">5</span>
|
|
X <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((n,Maxpolydegree<span style="color: #666666">-1</span>))
|
|
|
|
<span style="color: #008000; font-weight: bold">for</span> degree <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">1</span>,Maxpolydegree): <span style="color: #408080; font-style: italic">#No intercept column</span>
|
|
X[:,degree<span style="color: #666666">-1</span>] <span style="color: #666666">=</span> x<span style="color: #666666">**</span>(degree)
|
|
|
|
<span style="color: #408080; font-style: italic"># We split the data in test and training data</span>
|
|
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(X, y, test_size<span style="color: #666666">=0.2</span>)
|
|
|
|
<span style="color: #408080; font-style: italic"># Decide which values of lambda to use</span>
|
|
nlambdas <span style="color: #666666">=</span> <span style="color: #666666">500</span>
|
|
MSERidgePredict <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(nlambdas)
|
|
lambdas <span style="color: #666666">=</span> np<span style="color: #666666">.</span>logspace(<span style="color: #666666">-4</span>, <span style="color: #666666">2</span>, nlambdas)
|
|
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(nlambdas):
|
|
lmb <span style="color: #666666">=</span> lambdas[i]
|
|
RegRidge <span style="color: #666666">=</span> linear_model<span style="color: #666666">.</span>Ridge(lmb)
|
|
RegRidge<span style="color: #666666">.</span>fit(X_train,y_train)
|
|
ypredictRidge <span style="color: #666666">=</span> RegRidge<span style="color: #666666">.</span>predict(X_test)
|
|
MSERidgePredict[i] <span style="color: #666666">=</span> MSE(y_test,ypredictRidge)
|
|
|
|
<span style="color: #408080; font-style: italic"># Now plot the results</span>
|
|
plt<span style="color: #666666">.</span>figure()
|
|
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSERidgePredict, <span style="color: #BA2121">'g--'</span>, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE SL Ridge Test'</span>)
|
|
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">'log10(lambda)'</span>)
|
|
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">'MSE'</span>)
|
|
plt<span style="color: #666666">.</span>legend()
|
|
plt<span style="color: #666666">.</span>show()
|
|
</pre>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="output_wrapper">
|
|
<div class="output">
|
|
<div class="output_area">
|
|
<div class="output_subarea output_stream output_stdout output_text">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<p>Here we have performed a rather data greedy calculation as function of the regularization parameter \( \lambda \). There is no resampling here. The latter can easily be added by employing the function <b>RidgeCV</b> instead of just calling the <b>Ridge</b> function. For <b>RidgeCV</b> we need to pass the array of \( \lambda \) values.
|
|
By inspecting the figure we can in turn determine which is the optimal regularization parameter.
|
|
This becomes however less functional in the long run.
|
|
</p>
|
|
|
|
<p>
|
|
<!-- navigation buttons at the bottom of the page -->
|
|
<ul class="pagination">
|
|
<li><a href="._week38-bs025.html">«</a></li>
|
|
<li><a href="._week38-bs000.html">1</a></li>
|
|
<li><a href="">...</a></li>
|
|
<li><a href="._week38-bs018.html">19</a></li>
|
|
<li><a href="._week38-bs019.html">20</a></li>
|
|
<li><a href="._week38-bs020.html">21</a></li>
|
|
<li><a href="._week38-bs021.html">22</a></li>
|
|
<li><a href="._week38-bs022.html">23</a></li>
|
|
<li><a href="._week38-bs023.html">24</a></li>
|
|
<li><a href="._week38-bs024.html">25</a></li>
|
|
<li><a href="._week38-bs025.html">26</a></li>
|
|
<li class="active"><a href="._week38-bs026.html">27</a></li>
|
|
<li><a href="._week38-bs027.html">28</a></li>
|
|
<li><a href="._week38-bs028.html">29</a></li>
|
|
<li><a href="._week38-bs029.html">30</a></li>
|
|
<li><a href="._week38-bs030.html">31</a></li>
|
|
<li><a href="._week38-bs031.html">32</a></li>
|
|
<li><a href="._week38-bs032.html">33</a></li>
|
|
<li><a href="._week38-bs033.html">34</a></li>
|
|
<li><a href="._week38-bs034.html">35</a></li>
|
|
<li><a href="._week38-bs035.html">36</a></li>
|
|
<li><a href="">...</a></li>
|
|
<li><a href="._week38-bs061.html">62</a></li>
|
|
<li><a href="._week38-bs027.html">»</a></li>
|
|
</ul>
|
|
<!-- ------------------- end of main content --------------- -->
|
|
</div> <!-- end container -->
|
|
<!-- include javascript, jQuery *first* -->
|
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
|
|
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
|
|
<!-- Bootstrap footer
|
|
<footer>
|
|
<a href="https://..."><img width="250" align=right src="https://..."></a>
|
|
</footer>
|
|
-->
|
|
<center style="font-size:80%">
|
|
<!-- copyright only on the titlepage -->
|
|
</center>
|
|
</body>
|
|
</html>
|
|
|