Skip to content

Commit f4a9e6a

Browse files
committed
distances: Compatibility with numpy 1.13
1 parent bfc3714 commit f4a9e6a

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

Orange/distance/__init__.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
'SpearmanRAbsolute', 'PearsonR', 'PearsonRAbsolute', 'Mahalanobis',
1414
'MahalanobisDistance']
1515

16+
# TODO: When we upgrade to numpy 1.13, change use argument copy=False in
17+
# nan_to_num instead of assignment
1618

1719
# TODO this *private* function is called from several widgets to prepare
1820
# data for calling the below classes. After we (mostly) stopped relying
@@ -255,11 +257,6 @@ def compute_distances(self, x1, x2):
255257
call directly."""
256258
pass
257259

258-
@staticmethod
259-
def check_no_two_tables(x2):
260-
if x2 is not None:
261-
raise ValueError("columns of two tables cannot be compared")
262-
263260

264261
class FittedDistanceModel(DistanceModel):
265262
"""
@@ -504,7 +501,16 @@ def __init__(self, attributes, impute, normalize, means, vars):
504501
self.vars = vars
505502

506503
def compute_distances(self, x1, x2=None):
507-
self.check_no_two_tables(x2)
504+
"""
505+
Compute distances between columns of x1.
506+
507+
The method
508+
- extracts normalized continuous attributes and then uses `row_norms`
509+
and `safe_sparse_do`t to compute the distance as x^2 - 2xy - y^2
510+
(the trick from sklearn);
511+
- calls a function in Cython that adds the contributions of discrete
512+
columns
513+
"""
508514
if self.normalize:
509515
x1 = x1 - self.means
510516
x1 /= np.sqrt(2 * self.vars)
@@ -620,7 +626,6 @@ def __init__(self, attributes, impute, normalize, medians, mads):
620626
self.mads = mads
621627

622628
def compute_distances(self, x1, x2=None):
623-
self.check_no_two_tables(x2)
624629
if self.normalize:
625630
x1 = x1 - self.medians
626631
x1 /= 2
@@ -691,7 +696,7 @@ def fit_rows(self, attributes, x, n_vals):
691696
discrete = n_vals > 0
692697
x = self.discrete_to_indicators(x, discrete)
693698
means = util.nanmean(x, axis=0)
694-
np.nan_to_num(means, copy=False)
699+
means = np.nan_to_num(means)
695700
return self.CosineModel(attributes, self.axis, self.impute,
696701
discrete, means)
697702

@@ -748,7 +753,6 @@ def compute_distances(self, x1, x2):
748753
x2 is not None)
749754
else:
750755
nans1 = _distance.any_nan_row(x1.T)
751-
self.check_no_two_tables(x2)
752756
return _distance.jaccard_cols(
753757
nonzeros1, x1, nans1, self.ps)
754758

0 commit comments

Comments
 (0)