import numpy as np
import numpy.linalg as lin
import matplotlib.pylab as plt
import plotly.graph_objects as go
np.set_printoptions(precision=3, linewidth=150, suppress=True)
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
np.random.seed(0)
Exercise: 3D point cloud¶
We have measurement results and we know that we must have a relationship quadratic between x and z:
$$ z = \alpha \, x^2 + \beta \, x + \gamma $$
How to find these 3 coefficients?
Of course we will do a principal component analysis but beware, this only works for linear relations (it gives us a vector). Therefore we must introduce a new variable so that our equation is linear.
How to write our equation so that it is linear according to 2 variables?
réponse :¶
...
Experience Data¶
Make a 2D point cloud with the equation
$$ z = -1.3 \, x^2 + 0.2 \, x + 1.45 + U(-1,1)$$
where U is the uniform law that simulates noise.
N = 50
x = ... # x varie entre -3 et 3
z = ...
nuage = np.array([x,z])
plt.plot(nuage[0], nuage[1], 'x')
plt.title('Un nuage de points')
plt.axis('equal');
Calculations to find the characteristics of our cloud¶
Manufacture from our 2D point cloud a 3D point cloud by introducing the new variable that we have chosen.
The new cloud is called nuage3D
.
fig = go.Figure(data=[go.Scatter3d(x=nuage3D[0], y=nuage3D[1], z=nuage3D[2], mode='markers')])
fig.show()
Calculate the covariance matrix of our cloud and its eigenvectors (we will store the eigenvectors in the vec
variable).
fig = go.Figure(data=[go.Scatter3d(x=nuage3D[0], y=nuage3D[1], z=nuage3D[2], mode='markers'),
go.Scatter3d(x=[0,-5*vec[0,0]], y=[0,-5*vec[1,0]], z=[0,-5*vec[2,0]]),
go.Scatter3d(x=[0,vec[0,1]], y=[0,vec[1,1]], z=[0,vec[2,1]])])
fig.show()
What can we deduce from our first eigenvector?
réponse¶
...
Create a 2D point cloud that no longer takes into account the impact of the coefficient that we just found.
We call this new cloud nuage2D
(it is not the same as our initial cloud
).
plt.plot(nuage2D[0], nuage2D[1], 'x')
plt.axis('equal');
print("Les coefficients de z fonction polynomiale de degré 2 en x sont :\n")
print(f"alpha = {alpha}")
print(f"beta = {beta}")
print(f"gamma = {gamma}")
You can rerun everything with changing the value of the randomness seed. You will see that sometimes the noise disturbs the results much more.