diff options
Diffstat (limited to 'PVCM/cama/en/ma34 ACP -- Exercice.ipynb')
| -rw-r--r-- | PVCM/cama/en/ma34 ACP -- Exercice.ipynb | 295 |
1 files changed, 295 insertions, 0 deletions
diff --git a/PVCM/cama/en/ma34 ACP -- Exercice.ipynb b/PVCM/cama/en/ma34 ACP -- Exercice.ipynb new file mode 100644 index 0000000..7890e42 --- /dev/null +++ b/PVCM/cama/en/ma34 ACP -- Exercice.ipynb @@ -0,0 +1,295 @@ +{ + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "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.8.10" + } + }, + "nbformat": 4, + "nbformat_minor": 2, + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import numpy.linalg as lin\n", + "import matplotlib.pylab as plt\n", + "import plotly.graph_objects as go\n", + "np.set_printoptions(precision=3, linewidth=150, suppress=True)\n", + "\n", + "%matplotlib inline\n", + "%config InlineBackend.figure_format = 'retina'\n", + "\n", + "np.random.seed(0)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "## Exercise: 3D point cloud\n", + "\n", + "We have measurement results and we know that we must have a relationship\n", + "quadratic between x and z:\n", + "\n", + "$$ z = \\alpha \\, x^2 + \\beta \\, x + \\gamma $$\n", + "\n", + "How to find these 3 coefficients?\n", + "\n", + "Of course we will do a principal component analysis but beware, this\n", + "only works for linear relations (it gives us a vector). Therefore\n", + "we must introduce a new variable so that our equation is linear.\n", + "\n", + "How to write our equation so that it is linear according to 2 variables?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### réponse :\n", + "\n", + "..." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "### Experience Data\n", + "\n", + "Make a 2D point cloud with the equation\n", + "\n", + "$$ z = -1.3 \\, x^2 + 0.2 \\, x + 1.45 + U(-1,1)$$\n", + "\n", + "where U is the uniform law that simulates noise." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "N = 50\n", + "x = ... # x varie entre -3 et 3\n", + "z = ...\n", + "nuage = np.array([x,z])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.plot(nuage[0], nuage[1], 'x')\n", + "plt.title('Un nuage de points')\n", + "plt.axis('equal');" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "### Calculations to find the characteristics of our cloud\n", + "\n", + "Manufacture from our 2D point cloud a 3D point cloud by introducing the new\n", + "variable that we have chosen.\n", + "\n", + "The new cloud is called `nuage3D`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "fig = go.Figure(data=[go.Scatter3d(x=nuage3D[0], y=nuage3D[1], z=nuage3D[2], mode='markers')])\n", + "fig.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "Calculate the covariance matrix of our cloud and its eigenvectors (we will store the eigenvectors in the `vec` variable)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "fig = go.Figure(data=[go.Scatter3d(x=nuage3D[0], y=nuage3D[1], z=nuage3D[2], mode='markers'),\n", + " go.Scatter3d(x=[0,-5*vec[0,0]], y=[0,-5*vec[1,0]], z=[0,-5*vec[2,0]]),\n", + " go.Scatter3d(x=[0,vec[0,1]], y=[0,vec[1,1]], z=[0,vec[2,1]])])\n", + "fig.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "What can we deduce from our first eigenvector?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### réponse\n", + "\n", + "..." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "Create a 2D point cloud that no longer takes into account the impact of the coefficient\n", + "that we just found.\n", + "\n", + "We call this new cloud `nuage2D` (it is not the same as our initial `cloud`)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.plot(nuage2D[0], nuage2D[1], 'x')\n", + "plt.axis('equal');" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "What can we deduce from this?\n", + "\n", + "#### response\n", + "\n", + "...." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"Les coefficients de z fonction polynomiale de degré 2 en x sont :\\n\")\n", + "print(f\"alpha = {alpha}\")\n", + "print(f\"beta = {beta}\")\n", + "print(f\"gamma = {gamma}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "You can rerun everything with changing the value of the randomness seed. You will see that sometimes the\n", + "noise disturbs the results much more." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ] +}
\ No newline at end of file |
