Esempio n. 1
0
def pca2d(pointList):
  '''sets up the pca for a list of points in 2d. solves eig problem'''
  matrixList = [[0, 0], [0, 0]]  # init to 0
  avgPt = geometry.getAverageArbitraryDimension(pointList, 2)
  diffs = [0, 0]
  for point in pointList:
    for index in range(2):
      diffs[index] = point[index] - avgPt[index]
    #matrix is old plus a2 ab
    #                   ba b2
    #          only compute upper diagonal now
    matrixList[0][0] += diffs[0] * diffs[0]  # just hardcode it
    matrixList[0][1] += diffs[0] * diffs[1]
    matrixList[1][1] += diffs[1] * diffs[1]
  #make symmetric
  matrixList[1][0] = matrixList[0][1]
  actualMatrix = Matrix(matrixList)
  val, vec = eigenvectors(actualMatrix)
  return val, vec
Esempio n. 2
0
def pcaN3d(pointListList):
  '''sets up the pca for a list of list of points in 3d. solves eig problem.
  the pointListList is a list of sets of points of the same length.'''
  length = len(pointListList[0]) * 3  # know 3d points times length(list of pts)
  #print 'making a square matrix of size', length
  oneList = [0. for count in range(length)]  # bunch of 0.0s
  matrixList = [oneList[:] for count in range(length)]  # copy list of 0.0s
  #let's go ahead and flatten the input list!
  #print 'makin flattened list of size', length
  flattenedList = flatten(pointListList)
  avgPt = geometry.getAverageArbitraryDimension(
      flattenedList, dimension=length)
  diffs = oneList[:]
  #print 'filling a square matrix of size', length
  for point in flattenedList:  # this point has length dimensions
    for index in xrange(length):
      diffs[index] = point[index] - avgPt[index]
    #matrix is old plus a2 ab ac ad etc
    #                   ba b2 bc bd etc
    #                   ca cb c2 cd etc
    #                   da db dc d2 etc
    #                   etc etc etc etc etc (etc)
    #only compute upper diagonal now
    #can't get away with hardcoding anymore
    for row in xrange(length):
      for col in xrange(row, length):
        matrixList[row][col] += diffs[row] * diffs[col]
  #make symmetric
  for row in xrange(length):
    for col in xrange(0, row):
      matrixList[row][col] = matrixList[col][row]
  #print "mat" #debugging madness
  #for row in xrange(length):
  #  for col in xrange(length):
  #    print matrixList[row][col],
  #  print " "
  actualMatrix = Matrix(matrixList)
  #print 'running eigenvector calculation', type(actualMatrix)
  val, vec = eigenvectors(actualMatrix)
  return val, vec