Esempio n. 1
0
 def draw(self,data):
     if self.dim == 2:
         fig = pylab.figure()
         ass = gm_assign_to_cluster(data,self.mean,self.covm,self.c)
         clrs = 'brgy'
         for i in range(self.N):
             pts = data[ass == i]
             #pts = pts[::100]
             pylab.plot(pts[:,0],pts[:,1],'.',c=clrs[i])
             x1, x2 = gauss_ellipse_2d(self.mean[i], self.covm[i])
             pylab.plot(x1, x2, 'k', linewidth=2)
         pylab.show()
     elif self.dim == 3:
         fig = pylab.figure()
         ax = Axes3D(fig)
         ass = gm_assign_to_cluster(data,self.mean,self.covm,self.c)
         clrs = 'brgy'
         for i in range(self.N):
             pts = data[ass == i].astype(float64)
             pts = pts[::100]
             ax.scatter(pts[:,0],pts[:,1],pts[:,2],c=clrs[i])
         pylab.show()
Esempio n. 2
0
def iter_plot(cen_lst, cov_lst, itr):
    # For plotting EM progress
    if itr % 2 == 0:
        for i in range(len(cen_lst)):
            x, y = gmm.gauss_ellipse_2d(cen_lst[i], cov_lst[i])
            plt.plot(x, y, 'k', linewidth=0.5)
Esempio n. 3
0
    np.array([335, 741]),
    np.array([693, 741])
]

cen_lst, cov_lst, p_k, logL = gmm.em_gm(dataset,
                                        K=9,
                                        max_iter=400,
                                        verbose=True,
                                        init_kw={
                                            'cluster_init': 'custom',
                                            'cntr_lst': self_centroids
                                        })
print "Log likelihood (how well the data fits the model) = ", logL

for i in range(len(cen_lst)):
    x1, x2 = gmm.gauss_ellipse_2d(cen_lst[i], cov_lst[i])
    plt.plot(x1, x2, 'k', linewidth=2)
plt.title("")
plt.xlabel(r'$x_1$')
plt.ylabel(r'$x_2$')

plt.figure()
CS = plt.contour(X, Y, Z, levels=np.logspace(0, 100, 10))
CB = plt.colorbar(CS, shrink=0.8, extend='both')
plt.scatter(dataset[:, 0], dataset[:, 1], .8)
plt.title('Negative log-likelihood predicted by a GMM')
plt.axis('tight')
plt.show()


###############################################################################
Esempio n. 4
0
def iter_plot(cen_lst, cov_lst, itr):
    # For plotting EM progress
    if itr % 2 == 0:
        for i in range(len(cen_lst)):
            x,y = gmm.gauss_ellipse_2d(cen_lst[i], cov_lst[i])
            plt.plot(x, y, 'k', linewidth=0.5)
Esempio n. 5
0
self_centroids = [np.array([77, 75]),
                  np.array([335, 75]),
                  np.array([693, 75]),  
                  np.array([77, 350]),
                  np.array([335, 350]),
                  np.array([693, 350]),
                  np.array([77, 741]),
                  np.array([335,741]),
                  np.array([693, 741])]

cen_lst, cov_lst, p_k, logL = gmm.em_gm(dataset, K = 9, max_iter = 400,verbose = True, init_kw={'cluster_init':'custom', 'cntr_lst':self_centroids})
print "Log likelihood (how well the data fits the model) = ", logL                

for i in range(len(cen_lst)):
    x1,x2 = gmm.gauss_ellipse_2d(cen_lst[i], cov_lst[i])
    plt.plot(x1, x2, 'k', linewidth=2)
plt.title(""); plt.xlabel(r'$x_1$'); plt.ylabel(r'$x_2$')
                
plt.figure()
CS = plt.contour(X, Y, Z, levels=np.logspace(0, 100, 10))
CB = plt.colorbar(CS, shrink=0.8, extend='both')
plt.scatter(dataset[:, 0], dataset[:, 1], .8)
plt.title('Negative log-likelihood predicted by a GMM')
plt.axis('tight')
plt.show()

###############################################################################
def conditional_distribution(df, xs, xe):
    edf1 = df.ix[xs <= df[df.columns[0]].values]
    edf = edf1.ix[edf1[edf1.columns[0]].values < xe]
Esempio n. 6
0
# Drawing samples from a Gaussian Mixture Model
from numpy import *
from matplotlib.pylab import *
import pypr.clustering.gmm as gmm

mc = [0.4, 0.4, 0.2] # Mixing coefficients
centroids = [ array([0,0]), array([3,3]), array([0,4]) ]
ccov = [ array([[1,0.4],[0.4,1]]), diag((1,2)), diag((0.4,0.1)) ]
    # Covariance matrices

X = gmm.sample_gaussian_mixture(centroids, ccov, mc, samples=1000)
plot(X[:,0], X[:,1], '.')

for i in range(len(mc)):
    x1, x2 = gmm.gauss_ellipse_2d(centroids[i], ccov[i])
    plot(x1, x2, 'k', linewidth=2)
xlabel('$x_1$'); ylabel('$x_2$')
Esempio n. 7
0
# Drawing samples from a Gaussian Mixture Model
from numpy import *
from matplotlib.pylab import *
import pypr.clustering.gmm as gmm

mc = [0.4, 0.4, 0.2]  # Mixing coefficients
centroids = [array([0, 0]), array([3, 3]), array([0, 4])]
ccov = [array([[1, 0.4], [0.4, 1]]), diag((1, 2)), diag((0.4, 0.1))]
# Covariance matrices

X = gmm.sample_gaussian_mixture(centroids, ccov, mc, samples=1000)
plot(X[:, 0], X[:, 1], '.')

for i in range(len(mc)):
    x1, x2 = gmm.gauss_ellipse_2d(centroids[i], ccov[i])
    plot(x1, x2, 'k', linewidth=2)
xlabel('$x_1$')
ylabel('$x_2$')
Esempio n. 8
0
def plotCenters(cen_lst, cov_lst, c, offset):
    for i in range(len(cen_lst)):
        text(cen_lst[i][0], cen_lst[i][1], str(i+1 + offset), horizontalalignment='center',
            verticalalignment='center', size=32, color=(0.2,0,0))
        ex,ey = gmm.gauss_ellipse_2d(cen_lst[i], cov_lst[i])
        plot(ex, ey, c, linewidth=0.5)
Esempio n. 9
0
x = -1.0
(con_cen, con_cov, new_p_k) = gmm.cond_dist(np.array([x, np.nan, np.nan]), \
        cen_lst, cov_lst, p_k)
samples = gmm.sample_gaussian_mixture(con_cen, con_cov, new_p_k, samples=50)

# add fixed values
samples = np.hstack((np.ones((samples.shape[0], 1)) * x, samples))

#new_p_k += np.max(new_p_k) / 5
#new_p_k /= np.max(new_p_k)

for i in range(len(samples)):
    ax.scatter(samples[i][0],
               samples[i][1],
               samples[i][2],
               c='r',
               alpha=0.6,
               edgecolors='none')

for i in range(len(con_cen)):
    ex, ey = gmm.gauss_ellipse_2d(con_cen[i], con_cov[i])
    ax.plot(np.ones_like(ex) * x, ex, ey, 'k', linewidth=1, alpha=new_p_k[i])

#x2plt = gmm.gmm_pdf(c_[x1plt], con_cen, con_cov, new_p_k)
#ax.plot(x1plt, x2plt,'r', linewidth=2)

ax.legend()

show()
plot(X[:, 0], X[:, 1], '.', alpha=0.2)

cen_lst, cov_lst, p_k, logL = gmm.em_gm(X, K = 3, max_iter = 400, \
verbose = True, iter_call = None)

x1plt = np.linspace(axis()[0], axis()[1], 200)

for i in range(len(cen_lst)):
    text(cen_lst[i][0],
         cen_lst[i][1],
         str(i + 1),
         horizontalalignment='center',
         verticalalignment='center',
         size=32,
         color=(0.2, 0, 0))
    ex, ey = gmm.gauss_ellipse_2d(cen_lst[i], cov_lst[i])
    plot(ex, ey, 'k', linewidth=0.5)

# get conditional distribution
y = 0.3
(con_cen, con_cov, new_p_k) = gmm.cond_dist(np.array([y,np.nan]), \
        cen_lst, cov_lst, p_k)

x2plt = gmm.gmm_pdf(c_[x1plt], con_cen, con_cov, new_p_k)

x2plt /= np.sum(x2plt)

newSamples = np.random.choice(x1plt.tolist(), 20, p=x2plt.tolist())

plot(np.ones_like(newSamples) * y, newSamples, '.r')