%matplotlib inline
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
def plot_3D_axis():
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax._axis3don = False
ax.plot(np.zeros(3),np.array(range(-1,2)),np.zeros(3),"k-")
ax.plot(np.array(range(-1,2)),np.zeros(3),np.zeros(3),"k-")
ax.plot(np.zeros(3),np.zeros(3),np.array(range(-1,2)),"k-")
ax.set_xlim([-0.5,0.5])
ax.set_ylim([-0.5,0.5])
ax.set_zlim([-0.5,0.5])
ax.set_xticks([-1,-0.5,0,0.5,1])
ax.set_xticklabels([],fontsize=20)
ax.set_yticks([-1,-0.5,0,0.5,1])
ax.set_yticklabels([],fontsize=20)
ax.set_zticks([-1,-0.5,0,0.5,1])
ax.set_zticklabels([],fontsize=20)
ax.text(1.05,0,0,"$D_1$",fontsize=17)
ax.text(0,1.05,0,"$D_2$",fontsize=17)
ax.text(0,0,1.05,"$D_3$",fontsize=17)
ax.text(-0.1,-0.01,0.01,"$\mathcal{O}$",fontsize=20)
return(ax)
m = 200
n = 3
T = np.array([[1,1,1],
[0,1,3],
[2,0,2]]) # matrice quelconque 3x3
sigma = 0.05
mean = np.asarray([0.4,0.2,0.4])
X = np.random.randn(m,n) * sigma
A = np.dot(X,T) + mean
ax = plot_3D_axis()
for a in A:
ax.plot([a[0]],[a[1]],[a[2]],"o",alpha=0.2,color="b")
P_x_y = np.asarray([[1,0,0],
[0,1,0],
[0,0,0]])
P_y_z = np.asarray([[0,0,0],
[0,1,0],
[0,0,1]])
P_x_z = np.asarray([[1,0,0],
[0,0,0],
[0,0,1]])
A_x_y = np.dot(A,P_x_y)
A_y_z = np.dot(A,P_y_z)
A_x_z = np.dot(A,P_x_z)
ax = plot_3D_axis()
for a in A:
ax.plot([a[0]],[a[1]],[a[2]],"o",alpha=0.,color="b")
for a in A_x_y:
ax.plot([a[0]],[a[1]],[a[2]],"o",alpha=0.2,color="r")
for a in A_y_z:
ax.plot([a[0]],[a[1]],[a[2]],"o",alpha=0.2,color="g")
for a in A_x_z:
ax.plot([a[0]],[a[1]],[a[2]],"o",alpha=0.2,color="y")
Q = np.asarray([[4.,2.,0],
[-4.,2.,0],
[0,0,0]])
plt.imshow(Q.T.dot(Q))
def get_norm(M,axis=0):
return(np.sqrt((M**2).sum(axis=axis)))
def to_normed(M,axis=0):
norms = get_norm(M,axis)
for i,norm in enumerate(norms):
if norm:
M[:,i] *= 1./norm
return(M)
norms = get_norm(Q)
Q = to_normed(Q)
norms
plt.imshow(Q.T.dot(Q))
P = Q.dot(Q.T)
plt.imshow(P)
Q
Q = np.asarray([[2.,1.,0],
[1.,-3.,0],
[1,1,0]])
plt.imshow(Q.T.dot(Q))
norms = get_norm(Q)
Q = to_normed(Q)
norms
plt.imshow(Q.T.dot(Q))
P = Q.dot(Q.T)
plt.imshow(P)
A_p = np.dot(A,P)
P
P.dot(P)
np.sum((A_p - A_p.dot(P))**2)
Solve the system:
$(x=-4, y =1, z = 7)$: is a solution
normal = np.asarray([-4,1,7])
normal = normal/get_norm(normal)
Q.T.dot(normal)
xx, yy = np.meshgrid(np.arange(-0.5,1.5,0.1), np.arange(-0.5,1.5,0.1))
z = (-normal[0] * xx - normal[1] * yy ) * 1. /normal[2]
ax = plot_3D_axis()
ax.plot_surface(xx, yy, z,alpha=0.3)
for a in A_p:
ax.plot([a[0]],[a[1]],[a[2]],"o",alpha=0.2,color="r")