def compress_rowcols(x, axis=None): """Suppresses the rows and/or columns of a 2D array that contains masked values. The suppression behavior is selected with the `axis`parameter. - If axis is None, rows and columns are suppressed. - If axis is 0, only rows are suppressed. - If axis is 1 or -1, only columns are suppressed. Returns a *pure* ndarray. """ x = asarray(x) if x.ndim <> 2: raise NotImplementedError, "compress2d works for 2D arrays only." m = getmask(x) # Nothing is masked: return x if m is nomask or not m.any(): return x._data # All is masked: return empty if m.all(): return nxarray([]) # Builds a list of rows/columns indices (idxr, idxc) = (range(len(x)), range(x.shape[1])) masked = m.nonzero() if not axis: for i in function_base.unique(masked[0]): idxr.remove(i) if axis in [None, 1, -1]: for j in function_base.unique(masked[1]): idxc.remove(j) return x._data[idxr][:,idxc]
def _removeNonTracklikeClusterCenters(self): '''NOTE : Much of this code is copied from LPCMImpl.followXSingleDirection (factor out?) ''' labels = self._meanShift.labels_ labels_unique = unique(labels) cluster_centers = self._meanShift.cluster_centers_ rsp = lpcRandomStartPoints() cluster_representatives = [] for k in range(len(labels_unique)): cluster_members = labels == k cluster_center = cluster_centers[k] cluster = self._Xi[cluster_members,:] mean_sub = cluster - cluster_center cov_x = dot(transpose(mean_sub), mean_sub) eigen_cov = eigh(cov_x) sorted_eigen_cov = zip(eigen_cov[0],map(ravel,vsplit(eigen_cov[1].transpose(),len(eigen_cov[1])))) sorted_eigen_cov.sort(key = lambda elt: elt[0], reverse = True) rho = sorted_eigen_cov[1][0] / sorted_eigen_cov[0][0] #Ratio of two largest eigenvalues if rho < self._lpcParameters['rho_threshold']: cluster_representatives.append(cluster_center) else: #append a random element of the cluster random_cluster_element = rsp(cluster, 1)[0] cluster_representatives.append(random_cluster_element) return array(cluster_representatives)
def twoDisjointLinesWithMSClustering(): t = arange(-1,1,0.002) x = map(lambda x: x + gauss(0,0.02)*(1-x*x), t) y = map(lambda x: x + gauss(0,0.02)*(1-x*x), t) z = map(lambda x: x + gauss(0,0.02)*(1-x*x), t) line1 = array(zip(x,y,z)) line = vstack((line1, line1 + 3)) lpc = LPCImpl(start_points_generator = lpcMeanShift(ms_h = 1), h = 0.05, mult = None, it = 200, cross = False, scaled = False, convergence_at = 0.001) lpc_curve = lpc.lpc(X=line) #Plot results fig = plt.figure() ax = Axes3D(fig) labels = lpc._startPointsGenerator._meanShift.labels_ labels_unique = unique(labels) cluster_centers = lpc._startPointsGenerator._meanShift.cluster_centers_ n_clusters = len(labels_unique) colors = cycle('bgrcmyk') for k, col in zip(range(n_clusters), colors): cluster_members = labels == k cluster_center = cluster_centers[k] ax.scatter(line[cluster_members, 0], line[cluster_members, 1], line[cluster_members, 2], c = col, alpha = 0.1) ax.scatter([cluster_center[0]], [cluster_center[1]], [cluster_center[2]], c = 'b', marker= '^') curve = lpc_curve[k]['save_xd'] ax.plot(curve[:,0],curve[:,1],curve[:,2], c = col, linewidth = 3) plt.show()
def mask_rowcols(a, axis=None): """Masks whole rows and/or columns of a 2D array that contain masked values. The masking behavior is selected with the `axis`parameter. - If axis is None, rows and columns are suppressed. - If axis is 0, only rows are suppressed. - If axis is 1 or -1, only columns are suppressed. Returns a *pure* ndarray. """ a = asarray(a) if a.ndim != 2: raise NotImplementedError, "compress2d works for 2D arrays only." m = getmask(a) # Nothing is masked: return a if m is nomask or not m.any(): return a maskedval = m.nonzero() a._mask = a._mask.copy() if not axis: a[function_base.unique(maskedval[0])] = masked if axis in [None, 1, -1]: a[:,function_base.unique(maskedval[1])] = masked return a