当前位置: 首页>>代码示例>>Python>>正文


Python KDTree.build方法代码示例

本文整理汇总了Python中sklearn.neighbors.KDTree.build方法的典型用法代码示例。如果您正苦于以下问题:Python KDTree.build方法的具体用法?Python KDTree.build怎么用?Python KDTree.build使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.neighbors.KDTree的用法示例。


在下文中一共展示了KDTree.build方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: make_test

# 需要导入模块: from sklearn.neighbors import KDTree [as 别名]
# 或者: from sklearn.neighbors.KDTree import build [as 别名]
def make_test(test_start=1000, test_end=1050):
    f1 = open('states.pkl', 'r')
    f2 = open('states_for_test.pkl', 'r')
    data_states = cPickle.load(f1)
    test_states = cPickle.load(f2)
    f1.close()
    f2.close()

    time_brute = []
    time_sk_kd = []
    time_sk_ball = []
    time_kdtree = []
    time_annoy = []
    time_flann = []
    time_brute_tot = time_sk_kd_tot = time_sk_ball_tot = time_kdtree_tot = time_annoy_tot = time_flann_tot = 0

    kdtree_tree = None
    for items in xrange(test_start, test_end):
        print "item:", items

        ground_truth = np.zeros((test_num_for_each, K), dtype=np.int32)
        time_brute_start = time.time()
        for no_test in xrange(test_num_for_each):
            distance_list = []
            current_state = test_states[items, no_test]
            for target in xrange(items):
                target_state = data_states[target]
                distance_list.append(DistanceNode(np.sum(np.absolute(current_state - target_state)**2), target))
            smallest = heapq.nsmallest(K, distance_list, key=lambda x: x.distance)
            ground_truth[no_test] = [x.index for x in smallest]
        time_brute_end = time.time()
        time_brute.append(time_brute_end - time_brute_start)
        time_brute_tot += time_brute[-1]
        # print ground_truth

        time_sk_kd_start = time.time()
        tree = KDTree(data_states[:items, :])
        dist, indices = tree.query(test_states[items], K)
        time_sk_kd_end = time.time()
        time_sk_kd.append(time_sk_kd_end - time_sk_kd_start)
        time_sk_kd_tot += time_sk_kd[-1]
        # print indices

        time_sk_ball_start = time.time()
        tree = BallTree(data_states[:items, :], 10000)
        dist, indices = tree.query(test_states[items], K)
        time_sk_ball_end = time.time()
        time_sk_ball.append(time_sk_ball_end - time_sk_ball_start)
        time_sk_ball_tot += time_sk_ball[-1]
        # print indices

        """
        annoy is absolutely disappointing for its low speed and poor accuracy.
        """
        time_annoy_start = time.time()
        annoy_result = np.zeros((test_num_for_each, K), dtype=np.int32)
        tree = AnnoyIndex(dimension_result)
        for i in xrange(items):
            tree.add_item(i, data_states[i, :])
        tree.build(10)
        for no_test in xrange(test_num_for_each):
            current_state = test_states[items, no_test]
            annoy_result[no_test] = tree.get_nns_by_vector(current_state, K)
        time_annoy_end = time.time()
        time_annoy.append(time_annoy_end - time_annoy_start)
        time_annoy_tot += time_annoy[-1]
        # print annoy_result
        # print annoy_result - indices

        """
        flann is still not very ideal
        """

        time_flann_start = time.time()
        flann = FLANN()
        result, dist = flann.nn(data_states[:items, :], test_states[items], K, algorithm='kdtree', trees=10, checks=16)
        time_flann_end = time.time()
        time_flann.append(time_flann_end - time_flann_start)
        time_flann_tot += time_flann[-1]
        # print result-indices

        """
        This kdtree module is so disappointing!!!! It is 100 times slower than Sklearn and even slower than brute force,
        more over it even makes mistakes.

        This kdtree module supports online insertion and deletion. I thought it would be much faster than Sklearn
         KdTree which rebuilds the tree every time. But the truth is the opposite.
        """

        # time_kdtree_start = time.time()
        # if kdtree_tree is None:
        #     point_list = [MyTuple(data_states[i, :], i) for i in xrange(items)]
        #     kdtree_tree = kdtree.create(point_list)
        # else:
        #     point = MyTuple(data_states[items, :], items)
        #     kdtree_tree.add(point)
        # kdtree_result = np.zeros((test_num_for_each, K), dtype=np.int32)
        # for no_test in xrange(test_num_for_each):
        #     current_state = test_states[items, no_test]
        #     smallest = kdtree_tree.search_knn(MyTuple(current_state, -1), K)
#.........这里部分代码省略.........
开发者ID:ShibiHe,项目名称:Model-Free-Episodic-Control,代码行数:103,代码来源:KNN_runtime_test.py


注:本文中的sklearn.neighbors.KDTree.build方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。