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 PCA_augmentation(img): print('PCA color : Red augmentation begin ...') res = np.zeros(shape=(1, 3)) for i in range(img.shape[0]): # re-shape to make list of RGB vectors. arr = img[i].reshape((400 * 400), 3) # consolidate RGB vectors of all images res = np.concatenate((res, arr), axis=0) res = np.delete(res, (0), axis=0) mean = res.mean(axis=0) res = res - mean # allow the pca to work well R = np.cov(res, rowvar=False) # covariance matrix evals, evecs = linalg.eigh(R) idx = np.argsort(evals)[::-1] evecs = evecs[:, idx] # sort eigenvectors according to same index evals = evals[idx] # select the best 3 eigenvectors (3 is desired dimension # of rescaled data array) evecs = evecs[:, :3] # make a matrix with the three eigenvectors as its columns. evecs_mat = np.column_stack((evecs)) # PCA # carry out the transformation on the data using eigenvectors # and return the re-scaled data, eigenvalues, and eigenvectors m = np.dot(evecs.T, res.T).T # we need to add multiples of the found principal components with magnitudes proportional to # the corresponding eigenvalues times a random variable drawn from a Gaussian distribution # with mean zero and standard deviation 0.1. for i in range(img.shape[0]): img[i] = img[i] / 255.0 mu = 0 sigma = 0.1 feature_vec = np.matrix(evecs_mat) # 3 x 1 scaled eigenvalue matrix se = np.zeros((3, 1)) se[0][0] = np.random.normal(mu, sigma) * evals[0] se[1][0] = np.random.normal(mu, sigma) * evals[1] se[2][0] = np.random.normal(mu, sigma) * evals[2] se = np.matrix(se) val = feature_vec * se # Parse through every pixel value. for l in range(img[i].shape[0]): for j in range(img[i].shape[1]): # Parse through every dimension. for k in range(img[i].shape[2]): img[i][l, j, k] = float(img[i][l, j, k]) + float(val[k]) print('PCA color : Red augmentation has been done') return img
def plotGaussian(m, covar): """ plot a 2d gaussian """ t = arange(-pi, pi, 0.01) k = len(t) x = sin(t)[:, newaxis] y = cos(t)[:, newaxis] D, V = eigh(covar) A = real(dot(V, diag(sqrt(D))).T) z = dot(hstack([x, y]), A) hold('on') plot(z[:, 0] + m[0], z[:, 1] + m[1]) plot(array([m[0]]), array([m[1]]))
def plotGaussian(m, covar): """ plot a 2d gaussian """ t = arange(-pi,pi,0.01) k = len(t) x = sin(t)[:, newaxis] y = cos(t)[:, newaxis] D, V = eigh(covar) A = real(dot(V,diag(sqrt(D))).T) z = dot(hstack([x, y]), A) hold('on') plot(z[:,0]+m[0], z[:,1]+m[1]) plot(array([m[0]]), array([m[1]]))
def stationary(self, # GUN instance Nf, # Number of intervals in range of f(x) short = False # Skip ACF ): """Calculate transition matrix A and stationary distribution for discrete approximation of P(g). Characterize in terms of mean and covariance. Note: g = \log( \frac{f}{\tilde f} ) y = \log(x) """ from scipy.sparse.linalg import LinearOperator import scipy.sparse as ssm Q = self.peron(Nf) # Create sparse adjacency matrix n, N_states = Q.shape assert n == Nf,'n=%d, Nf=%d, N_states=%d'%(n,Nf,N_states) self.Pf = Q*self.pi if short: return self.mean = np.dot(self.Pf,self.f) # Expected value of f(0) n_s = len(self.s) ACF = np.zeros(n_s) diff = self.f-self.mean vn = Q.rmatvec(diff)*self.pi diff /= self.s[0] for n in range(n_s): # acf(n) \equiv EV (f(0)-mean)(f(n)-mean) ACF[n] = np.dot(Q*vn,diff*self.s[n]) vn = self.P_tran.rmatvec(vn) tilde = acf2d(ACF,self.dx*self.s/self.s[0]) _vals,_vecs = LA.eigh(tilde) i = np.argsort(_vals)[-1::-1] self.cov_vals = _vals[i].flatten() self.cov_vecs = _vecs.T[i,:] self.ACF = ACF self.Cov = acf2d(ACF, self.s/self.s[0]) # self.inner(self.inner(a,Cov),b) gives covariance of a and b ### 6 lines to verify that I understand the spectral decomposition vecT2 = self.cov_vecs[2] a = np.dot(vecT2, np.dot(tilde, vecT2)) b = self.cov_vals[2] assert np.abs((a-b)/b) < 1e-10,'a=%e b=%e'%(a, b) a = self.inner(vecT2, self.inner(self.Cov, vecT2)) assert np.abs((a-b)/b) < 1e-10,'a=%e b=%e'%(a, b) ### End of verification return
def fit_zca(X): filter_bias = 0.1 n_samples = X.shape[0] X = X.copy() # Center data mean_ = numpy.mean(X, axis=0) X -= mean_ eigs, eigv = linalg.eigh(numpy.dot(X.T, X) / X.shape[0] + filter_bias * numpy.identity(X.shape[1])) assert not numpy.any(numpy.isnan(eigs)) assert not numpy.any(numpy.isnan(eigv)) assert eigs.min() > 0 P = numpy.dot(eigv * numpy.sqrt(1.0 / eigs), eigv.T) assert not numpy.any(numpy.isnan(P)) return P, mean_
def get_gaussian_ellipse_artist(gaussian, nstd=1.96, linewidth=1): """ Returns an allipse artist for nstd times the standard deviation of this Gaussian """ assert (isinstance(gaussian, Gaussian)) assert (gaussian.dimension == 2) # compute eigenvalues (ordered) vals, vecs = eigh(gaussian.L.dot(gaussian.L.T)) order = vals.argsort()[::-1] vals, vecs = vals[order], vecs[:, order] theta = numpy.degrees(arctan2(*vecs[:, 0][::-1])) # width and height are "full" widths, not radius width, height = 2 * nstd * sqrt(vals) e = Ellipse(xy=gaussian.mu, width=width, height=height, angle=theta, \ edgecolor="red", fill=False, linewidth=linewidth) return e
def get_gaussian_ellipse_artist(gaussian, nstd=1.96, linewidth=1): """ Returns an allipse artist for nstd times the standard deviation of this Gaussian """ assert(isinstance(gaussian, Gaussian)) assert(gaussian.dimension == 2) # compute eigenvalues (ordered) vals, vecs = eigh(gaussian.L.dot(gaussian.L.T)) order = vals.argsort()[::-1] vals, vecs = vals[order], vecs[:, order] theta = numpy.degrees(arctan2(*vecs[:, 0][::-1])) # width and height are "full" widths, not radius width, height = 2 * nstd * sqrt(vals) e = Ellipse(xy=gaussian.mu, width=width, height=height, angle=theta, \ edgecolor="red", fill=False, linewidth=linewidth) return e
def run(self): """ Run the calculation. """ if self.done == True: raise AssertionError('Run LR calculation only once.') print('\nLR for %s (charge %.2f). ' % (self.el.get_name(), self.calc.get_charge()), end=' ', file=self.txt) # # select electron-hole excitations (i occupied, j not occupied) # de = excitation energy ej-ei (ej>ei) # df = occupation difference fi-fj (ej>ei so that fi>fj) # de = [] df = [] particle_holes = [] self.timer.start('setup ph pairs') for i in range(self.norb): for j in range(i + 1, self.norb): energy = self.e[j] - self.e[i] occup = ( self.f[i] - self.f[j] ) / 2 #normalize the double occupations (...is this rigorously right?) if energy < self.energy_cut and occup > 1E-6: assert energy > 0 and occup > 0 particle_holes.append([i, j]) de.append(energy) df.append(occup) self.timer.stop('setup ph pairs') de = np.array(de) df = np.array(df) # # setup the matrix (gamma-approximation) and diagonalize # self.timer.start('setup matrix') dim = len(de) print('Dimension %i. ' % dim, end=' ', file=self.txt) if not 0 < dim < 100000: raise RuntimeError('Coupling matrix too large or small (%i)' % dim) r = self.el.get_positions() transfer_q = np.array( [self.mulliken_transfer(ph[0], ph[1]) for ph in particle_holes]) rv = np.array([dot(tq, r) for tq in transfer_q]) matrix = np.zeros((dim, dim)) if self.SCC: gamma = self.es.get_gamma().copy() gamma_tq = np.zeros((dim, self.N)) for k in range(dim): gamma_tq[k, :] = dot(gamma, transfer_q[k, :]) for k1, ph1 in enumerate(particle_holes): matrix[k1, k1] = de[k1]**2 for k2, ph2 in enumerate(particle_holes): coupling = dot(transfer_q[k1, :], gamma_tq[k2, :]) matrix[k1, k2] += 2 * sqrt( df[k1] * de[k1] * de[k2] * df[k2]) * coupling else: for k1, ph1 in enumerate(particle_holes): matrix[k1, k1] = de[k1]**2 self.timer.stop('setup matrix') print('coupling matrix constructed. ', end=' ', file=self.txt) self.txt.flush() self.timer.start('diagonalize') omega2, eigv = eigh(matrix) self.timer.stop('diagonalize') print('Matrix diagonalized.', end=' ', file=self.txt) self.txt.flush() # assert np.all(omega2>1E-16) print(omega2) omega = sqrt(omega2) # calculate oscillator strengths F = [] collectivity = [] self.timer.start('oscillator strengths') for ex in range(dim): v = [] for i in range(3): v.append( sum(rv[:, i] * sqrt(df[:] * de[:]) * eigv[:, ex]) / sqrt(omega[ex]) * 2) F.append(omega[ex] * dot(v, v) * 2.0 / 3) collectivity.append(1 / sum(eigv[:, ex]**4)) self.omega = omega self.F = F self.eigv = eigv self.collectivity = collectivity self.dim = dim self.particle_holes = particle_holes self.timer.stop('oscillator strengths') if self.timing: self.timer.summary() self.done = True self.emax = max(omega) self.particle_holes = particle_holes
i = i + 1 [x, y] = [xpath[:, (xpath.shape[1]) - 1], ypath[:, (ypath.shape[1]) - 1]] for k in range(N): plt.plot(xpath[k], ypath[k], 'b-') plt.xlim(-3, 130) plt.ylim(-20, 130) plt.show() plt.plot(xpath[-1], ypath[-1], 'b-', x, y, 'r.') plt.xlim(-3, 130) plt.ylim(-20, 130) plt.show() s = 2.447651936039926 mat = np.array([x, y]) cmat = np.cov(mat) evals, evec = linalg.eigh(cmat) r1 = 2 * s * sqrt(evals[0]) r2 = 2 * s * sqrt(evals[1]) angle = 180 * atan2(evec[0, 1], evec[0, 0]) / np.pi ell = Ellipse((np.mean(x), np.mean(y)), r1, r2, angle) a = plt.subplot(111, aspect='equal') ell.set_alpha(0.2) a.add_artist(ell) plt.plot(xpath[-1], ypath[-1], 'b-', x, y, 'r.') plt.xlim(-3, 130) plt.ylim(-20, 130) plt.show()
""" Find two clusters in graph with given Laplacian matrix using spectral clustering. """ # Initialize Laplacian matrix. laplacian = np.array([[ 2, -1, -1, 0, 0, 0], [-1, 3, 0, -1, 0, -1], [-1, 0, 2, -1, 0, 0], [ 0, -1, -1, 3, -1, 0], [ 0, 0, 0, -1, 2, -1], [ 0, -1, 0, 0, -1, 2]]) # Find eigenvalues and eigenvectors. e_values, e_vectors = linalg.eigh(laplacian) # Find the second smallest eigenvalue and corresponding eigenvector. idx = e_values.argsort() second_e_val = e_values[idx[2]] second_e_vec = e_vectors[idx[2]] # Cluster graph into two parts by splitting second eigenvector at the mean value. threshold = np.mean(second_e_vec) first_cluster = [] second_cluster = [] for i in range(0, len(second_e_vec)): v = second_e_vec[i] # Choose cluster at random if point is a tie. if np.allclose(v, threshold, rtol=0): if random.randint(0, 1):
def _followxSingleDirection( self, x, direction = Direction.FORWARD, forward_curve = None, last_eigenvector = None, weights = 1.): '''Generates a partial lpc curve dictionary from the start point, x. Arguments --------- x : 1-dim, length m, numpy.array of floats, start point for the algorithm when m is dimension of feature space direction : bool, proceeds in Direction.FORWARD or Direction.BACKWARD from this point (just sets sign for first eigenvalue) forward_curve : dictionary as returned by this function, is used to detect crossing of the curve under construction with a previously constructed curve last_eigenvector : 1-dim, length m, numpy.array of floats, a unit vector that defines the initial direction, relative to which the first eigenvector is biased and initial cos_neu_neu is calculated weights : 1-dim, length n numpy.array of observation weights (can also be used to exclude individual observations from the computation by setting their weight to zero.), where n is the number of feature points ''' x0 = copy(x) N = self.Xi.shape[0] d = self.Xi.shape[1] it = self._lpcParameters['it'] h = array(self._lpcParameters['h']) t0 = self._lpcParameters['t0'] rho0 = self._lpcParameters['rho0'] save_xd = empty((it,d)) eigen_vecd = empty((it,d)) c0 = ones(it) cos_alt_neu = ones(it) cos_neu_neu = ones(it) lamb = empty(it) #NOTE this is named 'lambda' in the original R code rho = zeros(it) high_rho_points = empty((0,d)) count_points = 0 for i in range(it): kernel_weights = self._kernd(self.Xi, x0, c0[i]*h) * weights mu_x = average(self.Xi, axis = 0, weights = kernel_weights) sum_weights = sum(kernel_weights) mean_sub = self.Xi - mu_x cov_x = dot( dot(transpose(mean_sub), numpy.diag(kernel_weights)), mean_sub) / sum_weights #assert (abs(cov_x.transpose() - cov_x)/abs(cov_x.transpose() + cov_x) < 1e-6).all(), 'Covariance matrix not symmetric, \n cov_x = {0}, mean_sub = {1}'.format(cov_x, mean_sub) save_xd[i] = mu_x #save first point of the branch count_points += 1 #calculate path length if i==0: lamb[0] = 0 else: lamb[i] = lamb[i-1] + sqrt(sum((mu_x - save_xd[i-1])**2)) #calculate eigenvalues/vectors #(sorted_eigen_cov is a list of tuples containing eigenvalue and associated eigenvector, sorted descending by eigenvalue) 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) eigen_norm = sqrt(sum(sorted_eigen_cov[0][1]**2)) eigen_vecd[i] = direction * sorted_eigen_cov[0][1] / eigen_norm #Unit eigenvector corresponding to largest eigenvalue #rho parameters rho[i] = sorted_eigen_cov[1][0] / sorted_eigen_cov[0][0] #Ratio of two largest eigenvalues if i != 0 and rho[i] > rho0 and rho[i-1] <= rho0: high_rho_points = vstack((high_rho_points, x0)) #angle between successive eigenvectors if i==0 and last_eigenvector is not None: cos_alt_neu[i] = direction * dot(last_eigenvector, eigen_vecd[i]) if i > 0: cos_alt_neu[i] = dot(eigen_vecd[i], eigen_vecd[i-1]) #signum flipping if cos_alt_neu[i] < 0: eigen_vecd[i] = -eigen_vecd[i] cos_neu_neu[i] = -cos_alt_neu[i] else: cos_neu_neu[i] = cos_alt_neu[i] #angle penalization pen = self._lpcParameters['pen'] if pen > 0: if i == 0 and last_eigenvector is not None: a = abs(cos_alt_neu[i])**pen eigen_vecd[i] = a * eigen_vecd[i] + (1-a) * last_eigenvector if i > 0: a = abs(cos_alt_neu[i])**pen eigen_vecd[i] = a * eigen_vecd[i] + (1-a) * eigen_vecd[i-1] #check curve termination criteria if i not in (0, it-1): #crossing cross = self._lpcParameters['cross'] if forward_curve is None: full_curve_points = save_xd[0:i+1] else: full_curve_points = vstack((forward_curve['save_xd'],save_xd[0:i+1])) #inefficient, initialize then append? if not cross: prox = where(ravel(cdist(full_curve_points,[mu_x])) <= mean(h))[0] if len(prox) != max(prox) - min(prox) + 1: break #convergence convergence_at = self._lpcParameters['convergence_at'] conv_ratio = abs(lamb[i] - lamb[i-1]) / (2 * (lamb[i] + lamb[i-1])) if conv_ratio < convergence_at: break #boundary boundary = self._lpcParameters['boundary'] if conv_ratio < boundary: c0[i+1] = 0.995 * c0[i] else: c0[i+1] = min(1.01*c0[i], 1) #step along in direction eigen_vecd[i] x0 = mu_x + t0 * eigen_vecd[i] #trim output in the case where convergence occurs before 'it' iterations curve = { 'save_xd': save_xd[0:count_points], 'eigen_vecd': eigen_vecd[0:count_points], 'cos_neu_neu': cos_neu_neu[0:count_points], 'rho': rho[0:count_points], 'high_rho_points': high_rho_points, 'lamb': lamb[0:count_points], 'c0': c0[0:count_points] } return curve
def run(self): """ Run the calculation. """ if self.done == True: raise AssertionError("Run LR calculation only once.") print >> self.txt, "\nLR for %s (charge %.2f). " % (self.el.get_name(), self.calc.get_charge()), # # select electron-hole excitations (i occupied, j not occupied) # de = excitation energy ej-ei (ej>ei) # df = occupation difference fi-fj (ej>ei so that fi>fj) # de = [] df = [] particle_holes = [] self.timer.start("setup ph pairs") for i in range(self.norb): for j in range(i + 1, self.norb): energy = self.e[j] - self.e[i] occup = (self.f[i] - self.f[j]) / 2 # normalize the double occupations (...is this rigorously right?) if energy < self.energy_cut and occup > 1e-6: assert energy > 0 and occup > 0 particle_holes.append([i, j]) de.append(energy) df.append(occup) self.timer.stop("setup ph pairs") de = np.array(de) df = np.array(df) # # setup the matrix (gamma-approximation) and diagonalize # self.timer.start("setup matrix") dim = len(de) print >> self.txt, "Dimension %i. " % dim, if not 0 < dim < 100000: raise RuntimeError("Coupling matrix too large or small (%i)" % dim) r = self.el.get_positions() transfer_q = np.array([self.mulliken_transfer(ph[0], ph[1]) for ph in particle_holes]) rv = np.array([dot(tq, r) for tq in transfer_q]) matrix = np.zeros((dim, dim)) if self.SCC: gamma = self.es.get_gamma().copy() gamma_tq = np.zeros((dim, self.N)) for k in range(dim): gamma_tq[k, :] = dot(gamma, transfer_q[k, :]) for k1, ph1 in enumerate(particle_holes): matrix[k1, k1] = de[k1] ** 2 for k2, ph2 in enumerate(particle_holes): coupling = dot(transfer_q[k1, :], gamma_tq[k2, :]) matrix[k1, k2] += 2 * sqrt(df[k1] * de[k1] * de[k2] * df[k2]) * coupling else: for k1, ph1 in enumerate(particle_holes): matrix[k1, k1] = de[k1] ** 2 self.timer.stop("setup matrix") print >> self.txt, "coupling matrix constructed. ", self.txt.flush() self.timer.start("diagonalize") omega2, eigv = eigh(matrix) self.timer.stop("diagonalize") print >> self.txt, "Matrix diagonalized.", self.txt.flush() # assert np.all(omega2>1E-16) print omega2 omega = sqrt(omega2) # calculate oscillator strengths F = [] collectivity = [] self.timer.start("oscillator strengths") for ex in range(dim): v = [] for i in range(3): v.append(sum(rv[:, i] * sqrt(df[:] * de[:]) * eigv[:, ex]) / sqrt(omega[ex]) * 2) F.append(omega[ex] * dot(v, v) * 2.0 / 3) collectivity.append(1 / sum(eigv[:, ex] ** 4)) self.omega = omega self.F = F self.eigv = eigv self.collectivity = collectivity self.dim = dim self.particle_holes = particle_holes self.timer.stop("oscillator strengths") if self.timing: self.timer.summary() self.done = True self.emax = max(omega) self.particle_holes = particle_holes