124 lines
5.3 KiB
HTML
124 lines
5.3 KiB
HTML
<!--
|
|
Automatically generated HTML file from DocOnce source
|
|
(https://github.com/hplgit/doconce/)
|
|
-->
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
|
<meta name="description" content="Data Analysis and Machine Learning: Support Vector Machines">
|
|
|
|
<title>Data Analysis and Machine Learning: Support Vector Machines</title>
|
|
|
|
|
|
<link href="https://cdn.rawgit.com/hplgit/doconce/master/bundled/html_styles/style_solarized_box/css/solarized_light_code.css" rel="stylesheet" type="text/css" title="light"/>
|
|
<script src="https://cdn.rawgit.com/hplgit/doconce/master/bundled/html_styles/style_solarized_box/js/highlight.pack.js"></script>
|
|
<script>hljs.initHighlightingOnLoad();</script>
|
|
|
|
<link href="https://thomasf.github.io/solarized-css/solarized-light.min.css" rel="stylesheet">
|
|
<style type="text/css">
|
|
h1 {color: #b58900;} /* yellow */
|
|
/* h1 {color: #cb4b16;} orange */
|
|
/* h1 {color: #d33682;} magenta, the original choice of thomasf */
|
|
code { padding: 0px; background-color: inherit; }
|
|
pre {
|
|
border: 0pt solid #93a1a1;
|
|
box-shadow: none;
|
|
}
|
|
|
|
div { text-align: justify; text-justify: inter-word; }
|
|
</style>
|
|
|
|
|
|
</head>
|
|
|
|
<!-- tocinfo
|
|
{'highest level': 2,
|
|
'sections': [('Support Vector Machines, overarching aims',
|
|
2,
|
|
None,
|
|
'___sec0')]}
|
|
end of tocinfo -->
|
|
|
|
<body>
|
|
|
|
|
|
<!-- ------------------- main content ---------------------- -->
|
|
|
|
|
|
|
|
<center><h1>Data Analysis and Machine Learning: Support Vector Machines</h1></center> <!-- document title -->
|
|
|
|
<p>
|
|
<!-- author(s): Morten Hjorth-Jensen -->
|
|
|
|
<center>
|
|
<b>Morten Hjorth-Jensen</b> [1, 2]
|
|
</center>
|
|
|
|
<p>
|
|
<!-- institution(s) -->
|
|
|
|
<center>[1] <b>Department of Physics, University of Oslo</b></center>
|
|
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
|
<br>
|
|
<p>
|
|
<center><h4>May 30, 2018</h4></center> <!-- date -->
|
|
<br>
|
|
<p>
|
|
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
<h2 id="___sec0">Support Vector Machines, overarching aims </h2>
|
|
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
|
|
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.svm</span> <span style="color: #8B008B; font-weight: bold">import</span> SVR
|
|
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
|
|
|
|
<span style="color: #228B22"># Generate sample data</span>
|
|
X = np.sort(<span style="color: #B452CD">5</span>*np.random.rand(<span style="color: #B452CD">40</span>,<span style="color: #B452CD">1</span>), axis=<span style="color: #B452CD">0</span>)
|
|
y = X**<span style="color: #B452CD">3</span>
|
|
y=y.ravel()
|
|
|
|
<span style="color: #228B22"># Add noise to targets</span>
|
|
X[::<span style="color: #B452CD">4</span>] +=<span style="color: #B452CD">3</span>*(<span style="color: #B452CD">0.5</span> - np.random.rand(<span style="color: #B452CD">1</span>))
|
|
y[::<span style="color: #B452CD">5</span>] += <span style="color: #B452CD">50</span> * (<span style="color: #B452CD">0.5</span> - np.random.rand(<span style="color: #B452CD">8</span>))
|
|
|
|
plt.plot(X,y, <span style="color: #CD5555">'g^'</span>)
|
|
|
|
<span style="color: #228B22">#SVR Fit</span>
|
|
svr_poly = SVR(kernel=<span style="color: #CD5555">'poly'</span>, C=<span style="color: #B452CD">1e3</span>, degree=<span style="color: #B452CD">3</span>)
|
|
y_poly = svr_poly.fit(X, y).predict(X)
|
|
|
|
<span style="color: #228B22"># Plots</span>
|
|
z = np.arange(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">5</span>, <span style="color: #B452CD">0.1</span>)
|
|
t = z**<span style="color: #B452CD">3</span>
|
|
fig = plt.figure()
|
|
ax = fig.add_subplot(<span style="color: #B452CD">111</span>)
|
|
plt.plot(z,z**<span style="color: #B452CD">3</span>, <span style="color: #CD5555">'r--'</span>, label=<span style="color: #CD5555">'Cubic Function with No Noise'</span>)
|
|
lw = <span style="color: #B452CD">2</span>
|
|
plt.scatter(X, y, color=<span style="color: #CD5555">'darkorange'</span>, label=<span style="color: #CD5555">'Gaussian Cubic Noise'</span>)
|
|
plt.plot(X, y_poly, color=<span style="color: #CD5555">'green'</span>, lw=lw, label=<span style="color: #CD5555">'Polynomial model'</span>)
|
|
plt.xlabel(<span style="color: #CD5555">'data'</span>)
|
|
plt.ylabel(<span style="color: #CD5555">'target'</span>)
|
|
plt.title(<span style="color: #CD5555">'Cubic Gaussian Distribution'</span>)
|
|
plt.legend()
|
|
plt.show()
|
|
</pre></div>
|
|
<p>
|
|
|
|
<!-- ------------------- end of main content --------------- -->
|
|
|
|
|
|
<center style="font-size:80%">
|
|
<!-- copyright --> © 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
|
|
</center>
|
|
|
|
|
|
</body>
|
|
</html>
|
|
|
|
|