def main(): # Test print("Testing MaxHeap...") test_times = 100 run_time_1 = run_time_2 = 0 for _ in range(test_times): # Generate dataset randomly low = 0 high = 1000 n_rows = 10000 k = 100 nums = gen_data(low, high, n_rows) # Build Max Heap heap = MaxHeap(k, lambda x: x) start = time() for num in nums: heap.add(num) ret1 = copy(heap._items) run_time_1 += time() - start # Exhausted search start = time() ret2 = exhausted_search(nums, k) run_time_2 += time() - start # Compare result ret1.sort() assert ret1 == ret2, "target:%s\nk:%d\nrestult1:%s\nrestult2:%s\n" % ( str(nums), k, str(ret1), str(ret2)) print("%d tests passed!" % test_times) print("Max Heap Search %.2f s" % run_time_1) print("Exhausted search %.2f s" % run_time_2)
def main(): print("Testing K nearest search...") test_times = 100 run_time_1 = run_time_2 = 0 for _ in range(test_times): # Generate dataset randomly low = 0 high = 100 n_rows = 1000 n_cols = 2 X = gen_data(low, high, n_rows, n_cols) y = gen_data(low, high, n_rows) Xi = gen_data(low, high, n_cols) # Build KNN k = 2 model = KNeighborsBase() model.fit(X, y, k_neighbors=k) # KD Tree Search start = time() heap = model._knn_search(Xi) run_time_1 += time() - start ret1 = [get_euclidean_distance(Xi, nd.split[0]) for nd in heap.items] ret1.sort() # Exhausted search start = time() ret2 = exhausted_search(X, Xi, k) run_time_2 += time() - start ret2 = [get_euclidean_distance(Xi, row) for row in ret2] ret2.sort() # Compare result assert ret1 == ret2, "target:%s\nrestult1:%s\nrestult2:%s\ntree:\n%s" \ % (Xi, ret1, ret2, model.tree) print("%d tests passed!" % test_times) print("KNN Search %.2f s" % run_time_1) print("Exhausted search %.2f s" % run_time_2)
def main(): print("Testing KD Tree...") test_times = 100 run_time_1 = run_time_2 = 0 for _ in range(test_times): # Generate dataset randomly low = 0 high = 100 n_rows = 1000 n_cols = 2 X = gen_data(low, high, n_rows, n_cols) y = gen_data(low, high, n_rows) Xi = gen_data(low, high, n_cols) # Build KD Tree tree = KDTree() tree.build_tree(X, y) # KD Tree Search start = time() nd = tree.nearest_neighbour_search(Xi) run_time_1 += time() - start ret1 = get_eu_dist(Xi, nd.split[0]) # Exhausted search start = time() row = exhausted_search(X, Xi) run_time_2 += time() - start ret2 = get_eu_dist(Xi, row) # Compare result assert ret1 == ret2, "target:%s\nrestult1:%s\nrestult2:%s\ntree:\n%s" \ % (Xi, nd, row, tree) print("%d tests passed!" % test_times) print("KD Tree Search %.2f s" % run_time_1) print("Exhausted search %.2f s" % run_time_2)