In [2]:
from scipy import misc
import matplotlib.pyplot as plt
In [3]:
plt.imshow(misc.face())
Out[3]:
In [4]:
plt.show()
In [5]:
plt.imsave('face.png', misc.face())
In [6]:
img = misc.imread('face.png')
In [7]:
import numpy
In [8]:
type(img)
Out[8]:
In [9]:
img.shape
Out[9]:
In [10]:
img
Out[10]:
In [11]:
img[img < 128] = 0
img[img >= 128] = 255
In [12]:
plt.imshow(img)
Out[12]:
In [13]:
plt.show()
In [14]:
img
Out[14]:
In [15]:
imgL = misc.imread('face.png', mode='L')
In [16]:
imgL.shape
Out[16]:
In [17]:
imgL[0]
Out[17]:
In [18]:
misc.imsave('faceL.png', imgL)
In [19]:
imgL[imgL < 128] = 0
imgL[imgL >= 128] = 255
misc.imsave('faceL_bw.png', imgL)
In [21]:
fl = misc.imread('faceL_bw.png')
plt.imshow(fl)
plt.show()
In [22]:
del fl
In [23]:
fl
In [24]:
import to_bw
In [25]:
to_bw.to_bw
Out[25]:
In [30]:
hanabi = misc.imread('hanabi_bw.png', mode='L')
In [31]:
plt.imshow(hanabi)
plt.show()
In [32]:
hanabi.shape
Out[32]:
In [33]:
hanabi
Out[33]:
In [36]:
hanabi == hanabi
Out[36]:
In [40]:
hanabi[0][hanabi[0] == 255]
Out[40]:
In [44]:
numpy.nonzero(hanabi)
Out[44]:
In [46]:
# this is equivalent to the data plot!
X, y = numpy.nonzero(hanabi)
In [47]:
X
Out[47]:
In [48]:
y
Out[48]:
In [49]:
data = numpy.transpose(numpy.nonzero(hanabi))
In [50]:
from sklearn.mixture import GaussianMixture
In [51]:
est = GaussianMixture(n_components=20, covariance_type='full')
In [52]:
est.fit(data)
Out[52]:
In [53]:
def make_ellipses(gmm, ax):
for n, color in enumerate(colors):
if gmm.covariance_type == 'full':
covariances = gmm.covariances_[n][:2, :2]
elif gmm.covariance_type == 'tied':
covariances = gmm.covariances_[:2, :2]
elif gmm.covariance_type == 'diag':
covariances = np.diag(gmm.covariances_[n][:2])
elif gmm.covariance_type == 'spherical':
covariances = np.eye(gmm.means_.shape[1]) * gmm.covariances_[n]
v, w = np.linalg.eigh(covariances)
u = w[0] / np.linalg.norm(w[0])
angle = np.arctan2(u[1], u[0])
angle = 180 * angle / np.pi # convert to degrees
v = 2. * np.sqrt(2.) * np.sqrt(v)
ell = mpl.patches.Ellipse(gmm.means_[n, :2], v[0], v[1],
180 + angle, color=color)
ell.set_clip_box(ax.bbox)
ell.set_alpha(0.5)
ax.add_artist(ell)
In [56]:
from matplotlib import cm
colors=iter(cm.rainbow(numpy.linspace(0,1,20)))
In [59]:
data.shape
Out[59]:
In [68]:
plt.scatter(data[:,0], data[:,1], s=0.001)
plt.show()
In [71]:
import numpy as np
import matplotlib as mpl
make_ellipses(est, plt.plot())
In [72]:
est.covariances_
Out[72]:
In [74]:
covariances = est.covariances_
In [76]:
from matplotlib.patches import Ellipse
def plot_point_cov(points, nstd=2, ax=None, **kwargs):
"""
Plots an `nstd` sigma ellipse based on the mean and covariance of a point
"cloud" (points, an Nx2 array).
Parameters
----------
points : An Nx2 array of the data points.
nstd : The radius of the ellipse in numbers of standard deviations.
Defaults to 2 standard deviations.
ax : The axis that the ellipse will be plotted on. Defaults to the
current axis.
Additional keyword arguments are pass on to the ellipse patch.
Returns
-------
A matplotlib ellipse artist
"""
pos = points.mean(axis=0)
cov = np.cov(points, rowvar=False)
return plot_cov_ellipse(cov, pos, nstd, ax, **kwargs)
def plot_cov_ellipse(cov, pos, nstd=2, ax=None, **kwargs):
"""
Plots an `nstd` sigma error ellipse based on the specified covariance
matrix (`cov`). Additional keyword arguments are passed on to the
ellipse patch artist.
Parameters
----------
cov : The 2x2 covariance matrix to base the ellipse on
pos : The location of the center of the ellipse. Expects a 2-element
sequence of [x0, y0].
nstd : The radius of the ellipse in numbers of standard deviations.
Defaults to 2 standard deviations.
ax : The axis that the ellipse will be plotted on. Defaults to the
current axis.
Additional keyword arguments are pass on to the ellipse patch.
Returns
-------
A matplotlib ellipse artist
"""
def eigsorted(cov):
vals, vecs = np.linalg.eigh(cov)
order = vals.argsort()[::-1]
return vals[order], vecs[:,order]
if ax is None:
ax = plt.gca()
vals, vecs = eigsorted(cov)
theta = np.degrees(np.arctan2(*vecs[:,0][::-1]))
# Width and height are "full" widths, not radius
width, height = 2 * nstd * np.sqrt(vals)
ellip = Ellipse(xy=pos, width=width, height=height, angle=theta, **kwargs)
ax.add_artist(ellip)
return ellip
In [ ]: