# Changeset 10194:1d3b705df05a in orange

Ignore:
Timestamp:
02/13/12 15:35:21 (2 years ago)
Branch:
default
Message:

Replaced from numpy import * with import numpy

File:
1 edited

Unmodified
Added
Removed
• ## Orange/projection/mds.py

 r9994 """ from math import * from numpy import * import numpy from numpy.linalg import svd class PivotMDS(object): def __init__(self, distances=None, pivots=50, dim=2, **kwargs): self.dst = array([m for m in distances]) self.dst = numpy.array([m for m in distances]) self.n = len(self.dst) if type(pivots) == type(1): self.k = pivots self.pivots = random.permutation(len(self.dst))[:pivots] self.pivots = numpy.random.permutation(len(self.dst))[:pivots] #self.pivots.sort() elif type(pivots) == type([]): C = d**2 # double-center d cavg = sum(d, axis=0)/(self.k+0.0)      # column sum ravg = sum(d, axis=1)/(self.n+0.0)    # row sum tavg = sum(cavg)/(self.n+0.0)   # total sum cavg = numpy.sum(d, axis=0)/(self.k+0.0)      # column sum ravg = numpy.sum(d, axis=1)/(self.n+0.0)    # row sum tavg = numpy.sum(cavg)/(self.n+0.0)   # total sum # TODO: optimize for i in xrange(self.n): C = -0.5 * (C + tavg) w,v = linalg.eig(dot(C.T, C)) w,v = numpy.linalg.eig(numpy.dot(C.T, C)) tmp = zip([float(val) for val in w], range(self.n)) tmp.sort() w1, w2 = tmp[-1][0], tmp[-2][0] v1, v2 = v[:, tmp[-1][1]], v[:, tmp[-2][1]] x = dot(C, v1) y = dot(C, v2) x = numpy.dot(C, v1) y = numpy.dot(C, v2) return x, y """ # Torgerson's initial approximation O = array([m for m in self.distances]) O = numpy.array([m for m in self.distances]) ##        #B = matrixmultiply(O,O) # B = double-center O**2 !!! J = identity(self.n) - (1/float(self.n)) B = -0.5 * dot(dot(J, O**2), J) J = numpy.identity(self.n) - (1/numpy.float(self.n)) B = -0.5 * numpy.dot(numpy.dot(J, O**2), J) # SVD-solve B = ULU' # # self.X = matrixmultiply(U,identity(self.n)*sqrt(L)) # X is n-dimensional, we take the two dimensions with the largest singular values idx = argsort(L)[-self.dim:].tolist() idx = numpy.argsort(L)[-self.dim:].tolist() idx.reverse() Lt = take(L,idx)   # take those singular values Ut = take(U,idx,axis=1) # take those columns that are enabled Dt = identity(self.dim)*sqrt(Lt)  # make a diagonal matrix, with squarooted values self.points = Orange.core.FloatListList(dot(Ut,Dt)) Lt = numpy.take(L,idx)   # take those singular values Ut = numpy.take(U,idx,axis=1) # take those columns that are enabled Dt = numpy.identity(self.dim)*numpy.sqrt(Lt)  # make a diagonal matrix, with squarooted values self.points = Orange.core.FloatListList(numpy.dot(Ut,Dt)) self.freshD = 0 for i in d: sum += i[2]*i[2]*i[1] f = sqrt(distnorm/max(sum,1e-6)) f = numpy.sqrt(distnorm/numpy.max(sum,1e-6)) # transform O k = 0
Note: See TracChangeset for help on using the changeset viewer.