Exemplo n.º 1
0
def test_graphs():
    """Smoke tests for graph methods."""
    n_samples_sizes = [5, 10, 20]
    n_features = 3
    rng = np.random.RandomState(42)

    for n_samples in n_samples_sizes:
        X = rng.rand(n_samples, n_features)
        lshf = LSHForest(min_hash_match=0)
        lshf.fit(X)

        kneighbors_graph = lshf.kneighbors_graph(X)
        radius_neighbors_graph = lshf.radius_neighbors_graph(X)

        assert_equal(kneighbors_graph.shape[0], n_samples)
        assert_equal(kneighbors_graph.shape[1], n_samples)
        assert_equal(radius_neighbors_graph.shape[0], n_samples)
        assert_equal(radius_neighbors_graph.shape[1], n_samples)
def test_graphs():
    # Smoke tests for graph methods.
    n_samples_sizes = [5, 10, 20]
    n_features = 3
    rng = np.random.RandomState(42)

    for n_samples in n_samples_sizes:
        X = rng.rand(n_samples, n_features)
        lshf = LSHForest(min_hash_match=0)
        ignore_warnings(lshf.fit)(X)

        kneighbors_graph = lshf.kneighbors_graph(X)
        radius_neighbors_graph = lshf.radius_neighbors_graph(X)

        assert_equal(kneighbors_graph.shape[0], n_samples)
        assert_equal(kneighbors_graph.shape[1], n_samples)
        assert_equal(radius_neighbors_graph.shape[0], n_samples)
        assert_equal(radius_neighbors_graph.shape[1], n_samples)
X = np.array(X_tmp)

from sklearn.neighbors import NearestNeighbors
from sklearn.neighbors import LSHForest

for n in range(2, 10):
    print "[[[[[" + str(n) + "]]]]]"
    start = time.time()
    # nbrs = NearestNeighbors(n_neighbors=n, algorithm='ball_tree').fit(X)
    # print nbrs.kneighbors_graph(X).toarray()
    neigh = NearestNeighbors(n_neighbors=n)
    neigh.fit(X)
    a = neigh.radius_neighbors_graph(X).toarray()
    print a
    # a = neigh.kneighbors_graph(X).toarray()
    pc.dump(a, open("knn" + str(n) + ".txt", "w"))
    end = time.time()
    print "NearestNeighbors", end - start

    start = time.time()
    lshf = LSHForest(n_neighbors=n, random_state=10000)
    lshf.fit(X)
    # distances, indices = lshf.kneighbors(X, n_neighbors=n)
    # print lshf.kneighbors_graph(X).toarray()
    a = lshf.radius_neighbors_graph(X).toarray()
    print a
    pc.dump(a, open("lsh" + str(n) + ".txt", "w"))
    end = time.time()
    print "LSHForest", end - start