Changeset 9752:cbd6f6f10f06 in orange for Orange/distance/__init__.py
 Timestamp:
 02/06/12 16:33:23 (2 years ago)
 Branch:
 default
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

Orange/distance/__init__.py
r9723 r9752 1 1 import Orange 2 3 #%s/ExamplesDistanceConstructor/DistanceConstructor/gc4 #%s/ExamplesDistance_Normalized/DistanceNormalized/gc5 #ExampleDistance > Distance6 #Hamming > HammingDistance7 #DTW > DTWDistance8 #Euclidean > EuclideanDistance9 #Manhattan > ...10 #Maximal > ...11 #Relief > ..12 #DTWConstructor13 #EuclideanConstructor14 #HammingConstructor15 #ManhattanConstructor16 #MaximalConstructor17 #ReliefConstructor18 #PearsonRConstructor > PearsonR19 #PearsonR > PearsonRDistance20 #SpearmanRConstructor > SpearmanR21 #SpearmanR > SpearmanRDistance22 #MahalanobisConstructor > Mahalanobis23 #Mahalanobis > MahalanobisDistance24 2 25 3 from Orange.core import \ … … 42 20 ExamplesDistanceConstructor_Relief as Relief 43 21 22 from Orange.misc import progress_bar_milestones 23 44 24 import statc 45 25 import numpy … … 260 240 return 1.0 261 241 262 263 def distance_matrix(data, distance_constructor, progress_callback=None): 264 """ A helper function that computes an obj:`Orange.core.SymMatrix` of all 242 def _pairs(seq, same = False): 243 """ Return all pairs from elements of `seq`. 244 """ 245 seq = list(seq) 246 same = 0 if same else 1 247 for i in range(len(seq)): 248 for j in range(i + same, len(seq)): 249 yield seq[i], seq[j] 250 251 def distance_matrix(data, distance_constructor=Euclidean, progress_callback=None): 252 """ A helper function that computes an :obj:`Orange.data.SymMatrix` of all 265 253 pairwise distances between instances in `data`. 266 254 … … 268 256 :type data: :obj:`Orange.data.Table` 269 257 270 :param distance_constructor: An DistanceConstructor instance .258 :param distance_constructor: An DistanceConstructor instance (defaults to :obj:`Euclidean`). 271 259 :type distance_constructor: :obj:`Orange.distances.DistanceConstructor` 272 273 """ 274 from Orange.misc import progressBarMilestones as progress_milestones 275 matrix = Orange.core.SymMatrix(len(data)) 260 261 :param progress_callback: A function (taking one argument) to use for 262 reporting the on the progress. 263 :type progress_callback: function 264 265 :rtype: :class:`Orange.data.SymMatrix` 266 267 """ 268 matrix = Orange.data.SymMatrix(len(data)) 276 269 dist = distance_constructor(data) 277 278 msize = len(data)*(len(data)  1)/2 279 milestones = progress_milestones(msize, 100) 280 count = 0 281 for i in range(len(data)): 282 for j in range(i + 1, len(data)): 283 matrix[i, j] = dist(data[i], data[j]) 270 271 iter_count = matrix.dim * (matrix.dim  1) / 2 272 milestones = progress_bar_milestones(iter_count, 100) 273 274 for count, ((i, ex1), (j, ex2)) in enumerate(_pairs(enumerate(data))): 275 matrix[i, j] = dist(ex1, ex2) 276 if progress_callback and count in milestones: 277 progress_callback(100.0 * count / iter_count) 284 278 285 if progress_callback and count in milestones: 286 progress_callback(100.0 * count / msize) 287 count += 1 288 289 return matrix 279 return matrix
Note: See TracChangeset
for help on using the changeset viewer.