In [1]:
# This line configures matplotlib to show figures embedded in the notebook, 
# instead of poping up a new window. More about that later. 
%pylab inline

import matplotlib.pyplot as plt
Populating the interactive namespace from numpy and matplotlib

Initialize points

Create 7 points in 2D Plot points

In [2]:
x = [2,3,3,3,3,6,8]
y = [3,2,3,6,8,3,3]


plt.scatter(x, y, s=100)
plt.axis([0, 10, 0, 10])
plt.xticks(np.arange(0, 11, 1.0))
plt.yticks(np.arange(0, 11, 1.0))
plt.title('7 points in 2D', fontsize=10)
Out[2]:
Text(0.5, 1.0, '7 points in 2D')

Center data

Center data by removing the mean of all 7 samples Plot points in new reference axis

In [3]:
import numpy as np

x0 = x - np.mean(x)
y0 = y - np.mean(y)

print(x0)
print(y0)

plt.scatter(x0, y0, s=100)
plt.axis([-5, 5, -5, 5])
plt.xticks(np.arange(-5, 6, 1.0))
plt.yticks(np.arange(-5, 6, 1.0))
plt.title('2D points', fontsize=10)
[-2. -1. -1. -1. -1.  2.  4.]
[-1. -2. -1.  2.  4. -1. -1.]
Out[3]:
Text(0.5, 1.0, '2D points')

Covariance matrix

Compute the 2*2 covariance matrix by computing variance / covariances over all 7 sammples

In [4]:
#samples = [x0,y0]
glo = np.array(x0)
up = np.array(y0)
samples = np.column_stack( (glo,up) ).T
print(samples)

cov = np.dot( samples, samples.T ) / (len(glo)-1)
print(3*cov)

# (24/7-X)² - Pi²

# [1,1] et [1,-1] assez évidemment

# (24+7*PI)/24
[[-2. -1. -1. -1. -1.  2.  4.]
 [-1. -2. -1.  2.  4. -1. -1.]]
[[14.  -3.5]
 [-3.5 14. ]]

Eigen-vectors and values

Compute the transform matrix for diagonalizing the covariance matrix, and the corresponding eigen values

In [5]:
from numpy import linalg as LA

w, v = LA.eig(cov)
print(v)
print(w)
[[ 0.70710678  0.70710678]
 [-0.70710678  0.70710678]]
[5.83333333 3.5       ]