- Timestamp:
- 09/04/15 08:00:51 (9 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/_math/distance.py
r822 r824 20 20 # weights is a numpy array 21 21 # p is an int 22 from numpy import asarray, seterr, inf 23 weights = asarray(weights).flatten() 22 24 if not p: 23 25 w = float(len(weights[weights != 0.0])) # total number of nonzero elements 26 elif p == inf: 27 w = float(max(abs(weights))) 24 28 else: 25 w = float(sum(abs(weights**p)))**(1./p) 29 orig = seterr(over='raise', invalid='raise') 30 try: 31 w = float(sum(abs(weights**p)))**(1./p) 32 except FloatingPointError: # use the infinity norm 33 w = float(max(abs(weights))) 34 seterr(**orig) 26 35 return w 27 36 28 def absolute_distance(x, xp , up=False, dmin=0):37 def absolute_distance(x, xp=None, up=False, dmin=0): 29 38 """distance = |x - x'|; (see euclidean_distance for notes)""" 30 from numpy import abs, asarray, newaxis as nwxs 39 from numpy import abs, asarray, newaxis as nwxs, zeros_like 31 40 from __builtin__ import max 32 41 # cast as arrays of the same dimension 33 x = asarray(x); xp = asarray(xp) 42 x = asarray(x) 43 xp = zeros_like(x) if xp is None else asarray(xp) 34 44 xsize = max(len(x.shape), len(xp.shape), dmin) 35 45 while len(x.shape) < xsize: x = x[nwxs] … … 39 49 return abs(x - xp) 40 50 41 def euclidean_distance(x, xp , up=True, dmin=0):51 def euclidean_distance(x, xp=None, up=True, dmin=0): 42 52 """1-D euclidean distance between points 43 53 … … 54 64 return absolute_distance(x,xp,up=up,dmin=dmin).swapaxes(0,1).T 55 65 56 def manhattan_distance(x, xp , **kwds):66 def manhattan_distance(x, xp=None, **kwds): 57 67 """1-D manhattan distance between points 58 68 … … 65 75 # dmin: force upconvert to x,x' to dimension >= dmin 66 76 dmin = kwds['dmin'] if 'dmin' in kwds else 0 # default dimension 67 from numpy import abs, asarray, newaxis as nwxs 77 from numpy import abs, asarray, newaxis as nwxs, zeros_like 68 78 from __builtin__ import max 69 79 # cast as arrays of the same dimension 70 x = asarray(x); xp = asarray(xp) 80 x = asarray(x) 81 xp = zeros_like(x) if xp is None else asarray(xp) 71 82 xsize = max(len(x.shape), len(xp.shape), dmin) 72 83 while len(x.shape) < xsize: x = x[nwxs] … … 86 97 ############################################################################### 87 98 88 def lipschitz_metric(L, x, xp ):99 def lipschitz_metric(L, x, xp=None): 89 100 """sum of lipschitz-weighted distance between points 90 101 … … 119 130 ########################################################################### 120 131 121 def chebyshev(x,xp , up=True, dmin=0, axis=None):132 def chebyshev(x,xp=None, up=True, dmin=0, axis=None): 122 133 """infinity norm distance between points in euclidean space 123 134 … … 138 149 """ 139 150 d = euclidean_distance(x,xp,up=up,dmin=dmin) 140 from numpy import max 141 return max(d, axis=axis) 142 143 144 def minkowski(x,xp, up=True, dmin=0, p=3, axis=None): 151 return d.max(axis=axis).astype(float) 152 153 154 def hamming(x,xp=None, up=True, dmin=0, axis=None): 155 """zero 'norm' distance between points in euclidean space 156 157 d(0) = sum( x[0] != x[0]', x[1] != x[1]', ..., x[n] != x[n]' ) 158 159 Input: 160 x = array of points, x 161 xp = array pf points, x' 162 163 Additional Input: 164 up = True if upconvert x with x[:,newaxis] 165 dmin = upconvert to x,x' to dimension >= dmin 166 axis = if not None, reduce across the selected axis 167 168 Notes: 169 for standard array behavior, use up=False 170 for element-wise across all elements, use up=True 171 """ 172 d = euclidean_distance(x,xp,up=up,dmin=dmin) 173 return d.astype(bool).sum(axis=axis).astype(float) 174 175 176 def minkowski(x,xp=None, up=True, dmin=0, p=3, axis=None): 145 177 """p-norm distance between points in euclidean space 146 178 … … 161 193 for element-wise across all elements, use up=True 162 194 """ 195 from numpy import seterr, inf 196 if p == inf: return chebyshev(x,xp,up=up,dmin=dmin,axis=axis) 163 197 d = euclidean_distance(x,xp,up=up,dmin=dmin) 164 from numpy import sum 165 return sum(d**p, axis=axis)**(1./p) 166 167 168 def euclidean(x,xp, up=True, dmin=0, axis=None): 198 orig = seterr(over='raise', invalid='raise') 199 try: 200 d = (d**p).sum(axis=axis)**(1./p) 201 except FloatingPointError: # use the infinity norm 202 d = d.max(axis=axis).astype(float) 203 seterr(**orig) 204 return d 205 206 207 def euclidean(x,xp=None, up=True, dmin=0, axis=None): 169 208 """L-2 norm distance between points in euclidean space 170 209 … … 187 226 188 227 189 def manhattan(x,xp , up=True, dmin=0, axis=None):228 def manhattan(x,xp=None, up=True, dmin=0, axis=None): 190 229 """L-1 norm distance between points in euclidean space 191 230
Note: See TracChangeset
for help on using the changeset viewer.