def subsample(self, fcs, *args, **kwargs): x = fcs[:] neg_py = mixnormpdf(x, self.neg.pis, self.neg.mus, self.neg.sigmas, logged=True, *args, **kwargs) pos_py = mixnormpdf(x, self.pos.pis, self.pos.mus, self.pos.sigmas, logged=True, *args, **kwargs) diff = pos_py - neg_py probs = np.exp(diff) probs = probs / np.sum(probs) samp = npr.choice(np.arange(x.shape[0]), size=self.n, replace=False, p=probs) if isinstance(fcs, fcm.FCMdata): node = SubsampleNode("", fcs.get_cur_node(), samp) fcs.add_view(node) return fcs else: return x[samp]
def testThatItWorks(self): self.assertTrue(self._initialized) ## use kmeans to fit the data and get labels kmeanResults = run_kmeans_with_sv(case1) kmeanCentroids = kmeanResults['centroids'] kmeanLabels = kmeanResults['labels'] k = kmeanResults['k'] avgSilVal = kmeanResults['avgSilVal'] ## ensure good kmean results self.assertEqual(int(k),2) self.assertEqual(len(kmeanCentroids),int(k)) self.assertEqual(len(kmeanLabels),int(case1.shape[0])) ## use sample stats to parameterize the mixnormpdf mu = np.array([case1[np.where(kmeanLabels == i)[0],:].mean(axis=0) for i in [0,1]]) sigma = np.array([np.cov(case1[np.where(kmeanLabels == i)[0],:].T) for i in [0,1]]) pi = np.array([float(len(np.where(kmeanLabels == i)[0])) / float(len(kmeanLabels)) for i in [0,1]]) x = np.array([[8,20],[10,12]]) mnpdf = mixnormpdf(x,pi,mu,sigma) ## make sure the point the is far away has a smaller probability self.assertTrue(mnpdf[1] > mnpdf[0]) ## ensure we have proper probabilities self.assertEqual(len(np.where(mnpdf < 1.0)[0]),2) self.assertEqual(len(np.where(mnpdf > 0.0)[0]),2)
def subsample(self, fcs,*args, **kwargs): x = fcs[:] neg_py = mixnormpdf(x, self.neg.pis, self.neg.mus, self.neg.sigmas, logged=True, *args, **kwargs) pos_py = mixnormpdf(x, self.pos.pis, self.pos.mus, self.pos.sigmas, logged=True,*args, **kwargs) diff = pos_py - neg_py probs = np.exp(diff) probs = probs / np.sum(probs) samp = npr.choice(np.arange(x.shape[0]), size=self.n, replace=False, p=probs) if isinstance(fcs, fcm.FCMdata): node = SubsampleNode("", fcs.get_cur_node(), samp) fcs.add_view(node) return fcs else: return x[samp]
def subsample(self, fcs, *args, **kwargs): x = fcs[:] p = mixnormpdf(x, self.neg.pis, self.neg.mus, self.neg.sigmas) p = 1/p p = p/np.sum(p) samp = npr.choice(np.arange(x.shape[0]), size=self.n, replace=False, p=p) if isinstance(fcs, fcm.FCMdata): node = SubsampleNode("", fcs.get_cur_node(), samp) fcs.add_view(node) return fcs else: return x[samp]
def subsample(self, fcs, *args, **kwargs): x = fcs[:] p = mixnormpdf(x, self.neg.pis, self.neg.mus, self.neg.sigmas) p = 1 / p p = p / np.sum(p) samp = npr.choice(np.arange(x.shape[0]), size=self.n, replace=False, p=p) if isinstance(fcs, fcm.FCMdata): node = SubsampleNode("", fcs.get_cur_node(), samp) fcs.add_view(node) return fcs else: return x[samp]
img = make_image(zip(x1, y1),view_xmin,view_xmax,view_ymin,view_ymax, 1024,1024) xi = np.linspace(0,1024,1024) yi = np.linspace(0,1024,1024) plt.figure(figsize=(8,8)) # Pseudocolor using bilinear interpolation plt.subplot(2,2,1) try: z = np.load('density_10072101_01.npy') except: m = stats.DPMixtureModel(nclusts=32, niter=100, burnin=1000) m.ident = True r = m.fit(data[:, cols], verbose=True).average() z = stats.mixnormpdf(data[:, cols], r.pis(), r.mus(), r.sigmas()) np.save('density_10072101_02.npy', z) idx = np.argsort(z) plt.scatter(x[idx], y[idx], s=1, edgecolors='none', c=z[idx]) plt.xticks([]) plt.yticks([]) plt.imshow(z0, origin='lower', extent=[xmin, xmax, ymin, ymax], cmap=plt.cm.binary) plt.title('Pseudocolor (DPGMM)') # Pseudocolor using kernel density estimate plt.subplot(2,2,2) kde = gaussian_kde([x1, y1]) z = kde.evaluate([x1, y1]) plt.scatter(x, y, s=1, edgecolors='none', c=z)
img = make_image(zip(x1, y1), view_xmin, view_xmax, view_ymin, view_ymax, 1024, 1024) xi = np.linspace(0, 1024, 1024) yi = np.linspace(0, 1024, 1024) plt.figure(figsize=(8, 8)) # Pseudocolor using bilinear interpolation plt.subplot(2, 2, 1) try: z = np.load('density_10072101_01.npy') except: m = stats.DPMixtureModel(nclusts=32, niter=100, burnin=1000) m.ident = True r = m.fit(data[:, cols], verbose=True).average() z = stats.mixnormpdf(data[:, cols], r.pis(), r.mus(), r.sigmas()) np.save('density_10072101_02.npy', z) idx = np.argsort(z) plt.scatter(x[idx], y[idx], s=1, edgecolors='none', c=z[idx]) plt.xticks([]) plt.yticks([]) plt.imshow(z0, origin='lower', extent=[xmin, xmax, ymin, ymax], cmap=plt.cm.binary) plt.title('Pseudocolor (DPGMM)') # Pseudocolor using kernel density estimate plt.subplot(2, 2, 2) kde = gaussian_kde([x1, y1])