• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python dist_metrics.DistanceMetric类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python graph.kneighbors_graph函数代码示例发布时间:2022-05-27
下一篇:
Python classification.KNeighborsClassifier类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap