def get_pca_transform(blocks, numpcs=2, feature='axes_correlation'): # Put all blocks together into one big array vectors = concatenate( [b[feature] for b in blocks if b[feature].shape != (0, )]).T # coeff, score, latent = princomp(vectors, numpcs) Pcomponents, Trans, fracVar = prepca(vectors) return Trans
def get_pca_transform(blocks, numpcs=2, feature='axes_correlation'): # Put all blocks together into one big array vectors = concatenate([b[feature] for b in blocks if b[feature].shape!=(0,)]).T # coeff, score, latent = princomp(vectors, numpcs) Pcomponents, Trans, fracVar = prepca(vectors) return Trans
'running', 'walking'] all_f = numpy.empty((0, 45), 'float32') for action in action_names: flow_dir = path.join(output_dir, action) flow_names = os.listdir(flow_dir) i = 0; for flow_name in flow_names: if i < 500: flow_path = path.join(flow_dir, flow_name) f = numpy.load(flow_path) all_f = numpy.vstack((all_f, f.reshape(-1, 45))) i += 1 else: break all_f = all_f.transpose() [pcomponent, trans, fracVar] = mlab.prepca(all_f) # take first 3 component pca = pcomponent[0:3, :] fig = pyplot.figure() c = ['red', 'blue', 'purple', 'green', 'black', 'pink'] ax = p3.Axes3D(fig) for i in xrange(6): ax.scatter(pca[0, i*500:(i+1)*500], pca[1, i*500:(i+1)*500], pca[2, i*500:(i+1)*500], marker='x', color=c[i]) pyplot.show()
random.seed(options.randSeed) # Load up the data f = open(options.inFile) try: dataset = LoadDescriptors(f, prob=options.fractionUse) finally: f.close() print 'Opened up %i datapoints' % dataset.shape[0] # Perform PCA #pcaObj = mlab.PCA(dataset) dataset = dataset.transpose() (pcaMat, pcaWeights, fracVar) = mlab.prepca(dataset) print 'The top fractions are: %s' % str(fracVar[0:10]) if options.ndims == 2: figure(1) scatter(np.dot(pcaMat[:, 0], dataset), np.dot(pcaMat[:, 1], dataset)) elif options.ndims == 3: fig = figure(1) ax = Axes3D(fig) ax.scatter(np.dot(pcaMat[:, 0], dataset), np.dot(pcaMat[:, 1], dataset), np.dot(pcaMat[:, 2], dataset)) show()
def computeConsistency(config): '''Computes the consistency, assuming that the deviation has already been computed''' try: devFile = DeviationReader(deviationFile(config)) except: raise IOError( "Unable to load the deviation file for computing consistency") T = int(config['consistencyWindow']) if (T % 2 != 1 and T <= 0): raise ValueError( "The window size for consistency calculation must be positive and odd. Given %d." % T) if (T > devFile.frameCount): raise ValueError( "The consistency window size is larger than the number of frames: T = %d, frame count = %d" % (T, devFile.frameCount)) conFile = ConsistencyWriter(consistencyFile(config)) conFile.setWindowSize(T) conFile.setAgentCount(devFile.agtCount) print "\nCONSISTENCY" print "\tWindow:", T print "\tTotal frames:", devFile.frameCount print "\tTotal agents:", devFile.agtCount deviations = np.zeros((devFile.agtCount, 2, T), dtype=np.float32) print "\tLoading %d deviations:" % (T) for t in xrange(T): # no need to put this in a try block, because I know that I will be able to read all of these. deviations[:, :, t] = devFile.next() print "\t\t", deviations[:, :, t] replaceID = 0 consistency = np.zeros((devFile.agtCount, 6), dtype=np.float32) print "\tComputing consistency" count = 0 while (True): # The PCA class performs the mean for me -- in fact, I can't stop it. # So, although I'm sure it would be cheaper to compute the mean myself in one huge # block, I can't stop PCA from doing so. # NOTE: Not currently using PCA because I can't figure out what the damn AXES are mean = deviations.mean(axis=2) print "\t\tMean:", mean mean.shape = (-1, 2, 1) centered = deviations - mean for a in xrange(devFile.agtCount): ## p = mlab.PCA( centered[ a, :, : ].T ) ## print p.Wt with warnings.catch_warnings(): warnings.simplefilter("ignore") components, trace, fracVar = mlab.prepca(centered[a, :, :].T) sig2 = np.sqrt(np.sum(components**2, axis=1)) sig = np.sqrt(sig2) a0 = components[0] / sig[0] a1 = components[1] / sig[1] consistency[a, :2] = a0 consistency[a, 2:4] = a1 consistency[a, 4:] = fracVar conFile.write(consistency) count += 1 try: deviations[:, :, replaceID] = devFile.next() except StopIteration: break else: replaceID = (replaceID + 1) % T conFile.setFrameCount(count) conFile.close()