Changeset 10194:1d3b705df05a in orange
 Timestamp:
 02/13/12 15:35:21 (2 years ago)
 Branch:
 default
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

Orange/projection/mds.py
r9994 r10194 96 96 """ 97 97 98 99 from math import * 100 from numpy import * 98 import numpy 101 99 from numpy.linalg import svd 102 100 … … 124 122 class PivotMDS(object): 125 123 def __init__(self, distances=None, pivots=50, dim=2, **kwargs): 126 self.dst = array([m for m in distances])124 self.dst = numpy.array([m for m in distances]) 127 125 self.n = len(self.dst) 128 126 129 127 if type(pivots) == type(1): 130 128 self.k = pivots 131 self.pivots = random.permutation(len(self.dst))[:pivots]129 self.pivots = numpy.random.permutation(len(self.dst))[:pivots] 132 130 #self.pivots.sort() 133 131 elif type(pivots) == type([]): … … 153 151 C = d**2 154 152 # doublecenter d 155 cavg = sum(d, axis=0)/(self.k+0.0) # column sum156 ravg = sum(d, axis=1)/(self.n+0.0) # row sum157 tavg = sum(cavg)/(self.n+0.0) # total sum153 cavg = numpy.sum(d, axis=0)/(self.k+0.0) # column sum 154 ravg = numpy.sum(d, axis=1)/(self.n+0.0) # row sum 155 tavg = numpy.sum(cavg)/(self.n+0.0) # total sum 158 156 # TODO: optimize 159 157 for i in xrange(self.n): … … 162 160 163 161 C = 0.5 * (C + tavg) 164 w,v = linalg.eig(dot(C.T, C))162 w,v = numpy.linalg.eig(numpy.dot(C.T, C)) 165 163 tmp = zip([float(val) for val in w], range(self.n)) 166 164 tmp.sort() 167 165 w1, w2 = tmp[1][0], tmp[2][0] 168 166 v1, v2 = v[:, tmp[1][1]], v[:, tmp[2][1]] 169 x = dot(C, v1)170 y = dot(C, v2)167 x = numpy.dot(C, v1) 168 y = numpy.dot(C, v2) 171 169 return x, y 172 170 … … 316 314 """ 317 315 # Torgerson's initial approximation 318 O = array([m for m in self.distances])316 O = numpy.array([m for m in self.distances]) 319 317 320 318 ## #B = matrixmultiply(O,O) … … 332 330 333 331 # B = doublecenter O**2 !!! 334 J = identity(self.n)  (1/float(self.n))335 B = 0.5 * dot(dot(J, O**2), J)332 J = numpy.identity(self.n)  (1/numpy.float(self.n)) 333 B = 0.5 * numpy.dot(numpy.dot(J, O**2), J) 336 334 337 335 # SVDsolve B = ULU' … … 341 339 # # self.X = matrixmultiply(U,identity(self.n)*sqrt(L)) 342 340 # X is ndimensional, we take the two dimensions with the largest singular values 343 idx = argsort(L)[self.dim:].tolist()341 idx = numpy.argsort(L)[self.dim:].tolist() 344 342 idx.reverse() 345 343 346 Lt = take(L,idx) # take those singular values347 Ut = take(U,idx,axis=1) # take those columns that are enabled348 Dt = identity(self.dim)*sqrt(Lt) # make a diagonal matrix, with squarooted values349 self.points = Orange.core.FloatListList( dot(Ut,Dt))344 Lt = numpy.take(L,idx) # take those singular values 345 Ut = numpy.take(U,idx,axis=1) # take those columns that are enabled 346 Dt = numpy.identity(self.dim)*numpy.sqrt(Lt) # make a diagonal matrix, with squarooted values 347 self.points = Orange.core.FloatListList(numpy.dot(Ut,Dt)) 350 348 self.freshD = 0 351 349 … … 417 415 for i in d: 418 416 sum += i[2]*i[2]*i[1] 419 f = sqrt(distnorm/max(sum,1e6))417 f = numpy.sqrt(distnorm/numpy.max(sum,1e6)) 420 418 # transform O 421 419 k = 0
Note: See TracChangeset
for help on using the changeset viewer.