Commit 0affb9fb authored by Rémy Huet's avatar Rémy Huet 💻
Browse files

Début TP2

parent d006d330
......@@ -9,6 +9,9 @@ numpy = "*"
scipy = "*"
matplotlib = "*"
ipywidgets = "*"
pandas = "*"
seaborn = "*"
scikit-learn = "*"
[dev-packages]
......
This diff is collapsed.
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Bayesian linear regression\n",
"\n",
"## Synthetic data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Defined model for the exercise\n",
"def model(x):\n",
" f = 0.2 + x**2\n",
" y = f + np.random.normal(loc=0.0, scale=1.0, size=x.shape)\n",
" return y\n",
"\n",
"# training data\n",
"Xtr = np.array([[-1,0,1,2,3]]).T\n",
"ytr = model(Xtr)\n",
"\n",
"# test data\n",
"Xte = np.array([[-2, 5]]).T\n",
"Yte = model(Xte)\n",
"\n",
"# data for plotting\n",
"Xpl = np.atleast_2d(np.linspace(-5,5,100)).T\n",
"plt.figure(figsize=(5,5))\n",
"plt.plot(Xtr, ytr, 'bo', label='train data')\n",
"plt.plot(Xte, Yte, 'g+', label='test data')\n",
"plt.xlabel('x')\n",
"plt.ylabel('y')\n",
"plt.legend()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Bayesian (Gaussian) linear regression\n",
"\n",
"### Programming\n",
"\n",
"We want to program a function to make predictions given: \n",
"- A set Xtr of training instances (X matrix)\n",
"- The associated values ytr (y vector)\n",
"- A noise covariance matrix $\\Sigma n$\n",
"- A prior matrix $\\Sigma p$\n",
"- A set of instances Xpr for wich predictions must be made.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def predGLR(Xpr, Xtr, ytr, Sign, Sigp):\n",
" Xtr = np.concatenate([np.ones(Xtr.shape), Xtr], axis=1)\n",
" Xpr = np.concatenate([np.ones(Xpr.shape), Xpr], axis=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Gaussian process regression"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.gaussian_process import GaussianProcessRegressor as GPR\n",
"from sklearn.gaussian_process.kernels import DotProduct, WhiteKernel"
]
}
],
"metadata": {
"interpreter": {
"hash": "3abb0a1ef4892304d86bb3a3dfd052bcca35057beadba016173999c775e8d3ba"
},
"kernelspec": {
"display_name": "Python 3.9.7 64-bit ('AOS1-QteoCFsS': pipenv)",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment