本文整理汇总了Python中sklearn.neighbors.dist_metrics.DistanceMetric类的典型用法代码示例。如果您正苦于以下问题:Python DistanceMetric类的具体用法?Python DistanceMetric怎么用?Python DistanceMetric使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DistanceMetric类的17个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_input_data_size
def test_input_data_size():
# Regression test for #6288
# Previoulsly, a metric requiring a particular input dimension would fail
def custom_metric(x, y):
assert x.shape[0] == 3
return np.sum((x - y) ** 2)
rng = check_random_state(0)
X = rng.rand(10, 3)
pyfunc = DistanceMetric.get_metric("pyfunc", func=dist_func, p=2)
eucl = DistanceMetric.get_metric("euclidean")
assert_array_almost_equal(pyfunc.pairwise(X), eucl.pairwise(X))
开发者ID:amueller,项目名称:scikit-learn,代码行数:13,代码来源:test_dist_metrics.py
示例2: test_pyfunc_metric
def test_pyfunc_metric():
def dist_func(x1, x2, p):
return np.sum((x1 - x2) ** p) ** (1. / p)
X = np.random.random((10, 3))
euclidean = DistanceMetric.get_metric("euclidean")
pyfunc = DistanceMetric.get_metric("pyfunc", func=dist_func, p=2)
D1 = euclidean.pairwise(X)
D2 = pyfunc.pairwise(X)
assert_allclose(D1, D2)
开发者ID:Hydroinformatics-UNESCO-IHE,项目名称:scikit-learn,代码行数:13,代码来源:test_dist_metrics.py
示例3: check_pdist_bool
def check_pdist_bool(metric, D_true):
dm = DistanceMetric.get_metric(metric)
D12 = dm.pairwise(X1_bool)
# Based on https://github.com/scipy/scipy/pull/7373
# When comparing two all-zero vectors, scipy>=1.2.0 jaccard metric
# was changed to return 0, instead of nan.
if metric == 'jaccard' and LooseVersion(scipy_version) < '1.2.0':
D_true[np.isnan(D_true)] = 0
assert_array_almost_equal(D12, D_true)
开发者ID:amueller,项目名称:scikit-learn,代码行数:9,代码来源:test_dist_metrics.py
示例4: test_pyfunc_metric
def test_pyfunc_metric():
X = np.random.random((10, 3))
euclidean = DistanceMetric.get_metric("euclidean")
pyfunc = DistanceMetric.get_metric("pyfunc", func=dist_func, p=2)
# Check if both callable metric and predefined metric initialized
# DistanceMetric object is picklable
euclidean_pkl = pickle.loads(pickle.dumps(euclidean))
pyfunc_pkl = pickle.loads(pickle.dumps(pyfunc))
D1 = euclidean.pairwise(X)
D2 = pyfunc.pairwise(X)
D1_pkl = euclidean_pkl.pairwise(X)
D2_pkl = pyfunc_pkl.pairwise(X)
assert_array_almost_equal(D1, D2)
assert_array_almost_equal(D1_pkl, D2_pkl)
开发者ID:amueller,项目名称:scikit-learn,代码行数:19,代码来源:test_dist_metrics.py
示例5: test_kd_tree_two_point
def test_kd_tree_two_point(dualtree):
n_samples, n_features = (100, 3)
rng = check_random_state(0)
X = rng.random_sample((n_samples, n_features))
Y = rng.random_sample((n_samples, n_features))
r = np.linspace(0, 1, 10)
kdt = KDTree(X, leaf_size=10)
D = DistanceMetric.get_metric("euclidean").pairwise(Y, X)
counts_true = [(D <= ri).sum() for ri in r]
counts = kdt.two_point_correlation(Y, r=r, dualtree=dualtree)
assert_array_almost_equal(counts, counts_true)
开发者ID:MartinThoma,项目名称:scikit-learn,代码行数:13,代码来源:test_kd_tree.py
示例6: test_ball_tree_two_point
def test_ball_tree_two_point(n_samples=100, n_features=3):
np.random.seed(0)
X = np.random.random((n_samples, n_features))
Y = np.random.random((n_samples, n_features))
r = np.linspace(0, 1, 10)
bt = BallTree(X, leaf_size=10)
D = DistanceMetric.get_metric("euclidean").pairwise(Y, X)
counts_true = [(D <= ri).sum() for ri in r]
def check_two_point(r, dualtree):
counts = bt.two_point_correlation(Y, r=r, dualtree=dualtree)
assert_array_almost_equal(counts, counts_true)
for dualtree in (True, False):
yield check_two_point, r, dualtree
开发者ID:Afey,项目名称:scikit-learn,代码行数:16,代码来源:test_ball_tree.py
示例7: test_haversine_metric
def test_haversine_metric():
def haversine_slow(x1, x2):
return 2 * np.arcsin(np.sqrt(np.sin(0.5 * (x1[0] - x2[0])) ** 2
+ np.cos(x1[0]) * np.cos(x2[0]) *
np.sin(0.5 * (x1[1] - x2[1])) ** 2))
X = np.random.random((10, 2))
haversine = DistanceMetric.get_metric("haversine")
D1 = haversine.pairwise(X)
D2 = np.zeros_like(D1)
for i, x1 in enumerate(X):
for j, x2 in enumerate(X):
D2[i, j] = haversine_slow(x1, x2)
assert_array_almost_equal(D1, D2)
assert_array_almost_equal(haversine.dist_to_rdist(D1),
np.sin(0.5 * D2) ** 2)
开发者ID:amueller,项目名称:scikit-learn,代码行数:19,代码来源:test_dist_metrics.py
示例8: check_pdist
def check_pdist(metric, kwargs, D_true):
dm = DistanceMetric.get_metric(metric, **kwargs)
D12 = dm.pairwise(X1)
assert_array_almost_equal(D12, D_true)
开发者ID:amueller,项目名称:scikit-learn,代码行数:4,代码来源:test_dist_metrics.py
示例9: check_cdist_bool
def check_cdist_bool(metric, D_true):
dm = DistanceMetric.get_metric(metric)
D12 = dm.pairwise(X1_bool, X2_bool)
assert_array_almost_equal(D12, D_true)
开发者ID:amueller,项目名称:scikit-learn,代码行数:4,代码来源:test_dist_metrics.py
示例10: check_pdist
def check_pdist(self, metric, kwargs, D_true):
if metric == 'canberra' and cmp_version(scipy.__version__, '0.9') <= 0:
raise SkipTest("Canberra distance incorrect in scipy < 0.9")
dm = DistanceMetric.get_metric(metric, **kwargs)
D12 = dm.pairwise(self.X1)
assert_allclose(D12, D_true)
开发者ID:Hydroinformatics-UNESCO-IHE,项目名称:scikit-learn,代码行数:6,代码来源:test_dist_metrics.py
示例11: test_pickle_bool_metrics
def test_pickle_bool_metrics(metric):
dm = DistanceMetric.get_metric(metric)
D1 = dm.pairwise(X1_bool)
dm2 = pickle.loads(pickle.dumps(dm))
D2 = dm2.pairwise(X1_bool)
assert_array_almost_equal(D1, D2)
开发者ID:amueller,项目名称:scikit-learn,代码行数:6,代码来源:test_dist_metrics.py
示例12: check_cdist
def check_cdist(self, metric, kwargs, D_true):
dm = DistanceMetric.get_metric(metric, **kwargs)
D12 = dm.pairwise(self.X1, self.X2)
assert_array_almost_equal(D12, D_true)
开发者ID:Lavanya-Basavaraju,项目名称:scikit-learn,代码行数:4,代码来源:test_dist_metrics.py
示例13: check_pickle_bool
def check_pickle_bool(self, metric):
dm = DistanceMetric.get_metric(metric)
D1 = dm.pairwise(self.X1_bool)
dm2 = pickle.loads(pickle.dumps(dm))
D2 = dm2.pairwise(self.X1_bool)
assert_array_almost_equal(D1, D2)
开发者ID:Lavanya-Basavaraju,项目名称:scikit-learn,代码行数:6,代码来源:test_dist_metrics.py
示例14: check_cdist
def check_cdist(self, metric, kwargs, D_true):
if metric == "canberra" and cmp_version(scipy.__version__, "0.9") <= 0:
raise SkipTest("Canberra distance incorrect in scipy < 0.9")
dm = DistanceMetric.get_metric(metric, **kwargs)
D12 = dm.pairwise(self.X1, self.X2)
assert_array_almost_equal(D12, D_true)
开发者ID:cheral,项目名称:scikit-learn,代码行数:6,代码来源:test_dist_metrics.py
示例15: check_pdist_bool
def check_pdist_bool(self, metric, D_true):
dm = DistanceMetric.get_metric(metric)
D12 = dm.pairwise(self.X1_bool)
assert_allclose(D12, D_true)
开发者ID:Hydroinformatics-UNESCO-IHE,项目名称:scikit-learn,代码行数:4,代码来源:test_dist_metrics.py
示例16: brute_force_neighbors
def brute_force_neighbors(X, Y, k, metric, **kwargs):
D = DistanceMetric.get_metric(metric, **kwargs).pairwise(Y, X)
ind = np.argsort(D, axis=1)[:, :k]
dist = D[np.arange(Y.shape[0])[:, None], ind]
return dist, ind
开发者ID:MartinThoma,项目名称:scikit-learn,代码行数:5,代码来源:test_kd_tree.py
示例17: check_pickle
def check_pickle(metric, kwargs):
dm = DistanceMetric.get_metric(metric, **kwargs)
D1 = dm.pairwise(X1)
dm2 = pickle.loads(pickle.dumps(dm))
D2 = dm2.pairwise(X1)
assert_array_almost_equal(D1, D2)
开发者ID:amueller,项目名称:scikit-learn,代码行数:6,代码来源:test_dist_metrics.py
注:本文中的sklearn.neighbors.dist_metrics.DistanceMetric类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论