def test_user_metric(m1=2, m2=3): X1 = np.random.random((m1, DTEST)) X2 = np.random.random((m2, DTEST)) f = lambda x, y: np.dot(x[::-1], y) dist_metric = DistanceMetric(f) res1 = dist_metric.cdist(X1, X2) res2 = cdist(X1, X2, f) assert np.allclose(res1, res2)
def test_user_metric(m1 = 2, m2 = 3): X1 = np.random.random((m1, DTEST)) X2 = np.random.random((m2, DTEST)) f = lambda x, y: np.dot(x[::-1], y) dist_metric = DistanceMetric(f) res1 = dist_metric.cdist(X1, X2) res2 = cdist(X1, X2, f) assert np.allclose(res1, res2)
def bench_float(m1=200, m2=200, rseed=0): print 79 * '_' print " real valued distance metrics" print np.random.seed(rseed) X1 = np.random.random((m1, DTEST)) X2 = np.random.random((m2, DTEST)) for (metric, argdict) in METRIC_DICT.iteritems(): keys = argdict.keys() for vals in itertools.product(*argdict.values()): kwargs = dict(zip(keys, vals)) print metric, param_info(kwargs) t0 = time() try: dist_metric = DistanceMetric(metric, **kwargs) Yc1 = dist_metric.cdist(X1, X2) except Exception as inst: print " >>>>>>>>>> error in pyDistances cdist:" print " ", inst t1 = time() try: Yc2 = cdist(X1, X2, metric, **kwargs) except Exception as inst: print " >>>>>>>>>> error in scipy cdist:" print " ", inst t2 = time() try: dist_metric = DistanceMetric(metric, **kwargs) Yp1 = dist_metric.pdist(X1) except Exception as inst: print " >>>>>>>>>> error in pyDistances pdist:" print " ", inst t3 = time() try: Yp2 = pdist(X1, metric, **kwargs) except Exception as inst: print " >>>>>>>>>> error in scipy pdist:" print " ", inst t4 = time() if not np.allclose(Yc1, Yc2): print " >>>>>>>>>> FAIL: cdist results don't match" if not np.allclose(Yp1, Yp2): print " >>>>>>>>>> FAIL: pdist results don't match" print " - pyDistances: c: %.4f sec p: %.4f sec" % (t1 - t0, t3 - t2) print " - scipy: c: %.4f sec p: %.4f sec" % (t2 - t1, t4 - t3) print ''
def bench_float(m1=100, m2=100, rseed=0): print 79 * '_' print " real valued distance metrics" print np.random.seed(rseed) X1 = np.random.random((m1, DTEST)) X2 = np.random.random((m2, DTEST)) for (metric, argdict) in METRIC_DICT.iteritems(): keys = argdict.keys() for vals in itertools.product(*argdict.values()): kwargs = dict(zip(keys, vals)) print metric, param_info(kwargs) t0 = time() try: dist_metric = DistanceMetric(metric, **kwargs) Yc1 = dist_metric.cdist(X1, X2) except Exception as inst: print " >>>>>>>>>> error in pyDistances cdist:" print " ", inst t1 = time() try: Yc2 = cdist(X1, X2, metric, **kwargs) except Exception as inst: print " >>>>>>>>>> error in scipy cdist:" print " ", inst t2 = time() try: dist_metric = DistanceMetric(metric, **kwargs) Yp1 = dist_metric.pdist(X1) except Exception as inst: print " >>>>>>>>>> error in pyDistances pdist:" print " ", inst t3 = time() try: Yp2 = pdist(X1, metric, **kwargs) except Exception as inst: print " >>>>>>>>>> error in scipy pdist:" print " ", inst t4 = time() if not np.allclose(Yc1, Yc2): print " >>>>>>>>>> FAIL: cdist results don't match" if not np.allclose(Yp1, Yp2): print " >>>>>>>>>> FAIL: pdist results don't match" print " - pyDistances: c: %.2g sec p: %.2g sec" % (t1 - t0, t3 - t2) print " - scipy: c: %.2g sec p: %.2g sec" % (t2 - t1, t4 - t3) print ''
def _check_metrics_bool(self, k, metric, kwargs): bt = BallTree(self.Xbool, metric=metric, **kwargs) dist_bt, ind_bt = bt.query(self.Ybool, k=k) dm = DistanceMetric(metric=metric, **kwargs) D = dm.cdist(self.Ybool, self.Xbool) ind_dm = np.argsort(D, 1)[:, :k] dist_dm = D[np.arange(self.Ybool.shape[0])[:, None], ind_dm] # we don't check the indices here because there are very often # ties for nearest neighbors, which cause the test to fail. # Distances will be correct in either case. assert_array_almost_equal(dist_bt, dist_dm)
def test_query_radius_indices(self, n_queries=20): # center the data X = 2 * self.X - 1 dm = DistanceMetric() D = dm.cdist(X[:n_queries], X) r = np.mean(D) bt = BallTree(X) ind = bt.query_radius(X[:n_queries], r, return_distance=False) ind2 = np.zeros(D.shape) + np.arange(D.shape[1]) ind = np.concatenate(map(np.sort, ind)) ind2 = ind2[D <= r] assert_array_almost_equal(ind, ind2)
def test_query_radius_distance(self): # center the data X = 2 * self.X - 1 # choose a query point near the origin query_pt = 0.01 * X[:1] eps = 1E-15 # roundoff error can cause test to fail bt = BallTree(X, leaf_size=5) # compute reference distances dm = DistanceMetric() dist_true = dm.cdist(query_pt, X)[0] dist_true.sort() for r in np.linspace(dist_true[0], dist_true[-1], 10): yield (self._check_query_radius_distance, X, bt, query_pt, dist_true, r, eps)
def test_ball_tree_query_radius_indices(n_samples=100, n_features=10): X = 2 * np.random.random(size=(n_samples, n_features)) - 1 dm = DistanceMetric() D = dm.cdist(X[:10], X) r = np.mean(D) bt = BallTree(X) ind = bt.query_radius(X[:10], r, return_distance=False) for i in range(10): ind1 = ind[i] ind2 = np.where(D[i] <= r)[0] ind1.sort() ind2.sort() assert_array_almost_equal(ind1, ind2)
def test_cdist(m1=15, m2=20, rseed=0): """Compare DistanceMetric.cdist to scipy.spatial.distance.cdist""" np.random.seed(rseed) X1 = np.random.random((m1, DTEST)) X2 = np.random.random((m2, DTEST)) for (metric, argdict) in METRIC_DICT.iteritems(): keys = argdict.keys() for vals in itertools.product(*argdict.values()): kwargs = dict(zip(keys, vals)) dist_metric = DistanceMetric(metric, **kwargs) Y1 = dist_metric.cdist(X1, X2) Y2 = cdist(X1, X2, metric, **kwargs) if not np.allclose(Y1, Y2): print metric, keys, vals print Y1[:5, :5] print Y2[:5, :5] assert np.allclose(Y1, Y2)
def test_cdist_bool(m1=15, m2=20, rseed=0): """Compare DistanceMetric.cdist to scipy.spatial.distance.cdist""" np.random.seed(rseed) X1 = (np.random.random((m1, DTEST)) > 0.5).astype(float) X2 = (np.random.random((m2, DTEST)) > 0.5).astype(float) for (metric, argdict) in BOOL_METRIC_DICT.iteritems(): keys = argdict.keys() for vals in itertools.product(*argdict.values()): kwargs = dict(zip(keys, vals)) dist_metric = DistanceMetric(metric, **kwargs) Y1 = dist_metric.cdist(X1, X2) Y2 = cdist(X1, X2, metric, **kwargs) if not np.allclose(Y1, Y2): print metric, keys, vals print Y1[:5, :5] print Y2[:5, :5] assert np.allclose(Y1, Y2)
def bench_cdist_bool(m1=100, m2=100, rseed=0): np.random.seed(rseed) X1 = (np.random.random((m1, DTEST)) > 0.5).astype(float) X2 = (np.random.random((m2, DTEST)) > 0.5).astype(float) for (metric, argdict) in BOOL_METRIC_DICT.iteritems(): keys = argdict.keys() for vals in itertools.product(*argdict.values()): kwargs = dict(zip(keys, vals)) t0 = time() dist_metric = DistanceMetric(metric, **kwargs) Y1 = dist_metric.cdist(X1, X2) t1 = time() Y2 = cdist(X1, X2, metric, **kwargs) t2 = time() print metric, print_params(kwargs) if not np.allclose(Y1, Y2): print " >>>>>>>>>>>>>>>>>>>> FAIL: results don't match" print " - pyDistances: %.2g sec" % (t1 - t0) print " - scipy: %.2g sec" % (t2 - t1)