Files
FYS-STK4155/doc/pub/svm/ipynb/svm.ipynb
T
2018-05-30 23:00:43 -04:00

73 lines
2.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- dom:TITLE: Data Analysis and Machine Learning: Support Vector Machines -->\n",
"# Data Analysis and Machine Learning: Support Vector Machines\n",
"<!-- dom:AUTHOR: Morten Hjorth-Jensen at Department of Physics, University of Oslo & Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University -->\n",
"<!-- Author: --> \n",
"**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n",
"\n",
"Date: **May 30, 2018**\n",
"\n",
"Copyright 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
"\n",
"\n",
"\n",
"\n",
"## Support Vector Machines, overarching aims"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"\n",
"import numpy as np\n",
"from sklearn.svm import SVR\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Generate sample data\n",
"X = np.sort(5*np.random.rand(40,1), axis=0)\n",
"y = X**3\n",
"y=y.ravel()\n",
"\n",
"# Add noise to targets\n",
"X[::4] +=3*(0.5 - np.random.rand(1))\n",
"y[::5] += 50 * (0.5 - np.random.rand(8))\n",
"\n",
"plt.plot(X,y, 'g^')\n",
"\n",
"#SVR Fit\n",
"svr_poly = SVR(kernel='poly', C=1e3, degree=3)\n",
"y_poly = svr_poly.fit(X, y).predict(X)\n",
"\n",
"# Plots\n",
"z = np.arange(0, 5, 0.1)\n",
"t = z**3\n",
"fig = plt.figure()\n",
"ax = fig.add_subplot(111)\n",
"plt.plot(z,z**3, 'r--', label='Cubic Function with No Noise')\n",
"lw = 2\n",
"plt.scatter(X, y, color='darkorange', label='Gaussian Cubic Noise')\n",
"plt.plot(X, y_poly, color='green', lw=lw, label='Polynomial model')\n",
"plt.xlabel('data')\n",
"plt.ylabel('target')\n",
"plt.title('Cubic Gaussian Distribution')\n",
"plt.legend()\n",
"plt.show()"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}