Ejemplo n.º 1
0
def test():
    anp = np.random.randn(4, 5)
    b = random.randint(1, 13)
    a = lg.array(anp)

    np.not_equal(anp, b, out=anp)
    lg.not_equal(a, b, out=a)

    assert np.array_equal(a, anp)

    return
Ejemplo n.º 2
0
def test():
    anp = np.random.randn(4, 5)
    b = random.randint(1, 13)
    a = lg.array(anp)

    # test with scalar on the rhs
    assert np.array_equal(lg.not_equal(a, b), np.not_equal(anp, b))

    # test with scalar on the lhs
    assert np.array_equal(lg.not_equal(b, a), np.not_equal(b, anp))

    return
Ejemplo n.º 3
0
def test():
    anp = np.random.randn(4, 5)
    bnp = np.random.randn(4, 5)
    a = lg.array(anp)
    b = lg.array(bnp)

    assert np.array_equal(lg.not_equal(a, b), np.not_equal(anp, bnp))

    return
Ejemplo n.º 4
0
def run_kmeans(C, D, T, I, N, S, benchmarking):  # noqa: E741
    print("Running kmeans...")
    print("Number of data points: " + str(N))
    print("Number of dimensions: " + str(D))
    print("Number of centroids: " + str(C))
    print("Max iterations: " + str(I))
    start = datetime.datetime.now()
    data, centroids = initialize(N, D, C, T)

    data_dots = np.square(np.linalg.norm(data, ord=2, axis=1))
    data_index = np.linspace(0, N - 1, N, dtype=np.int)

    labels = None
    iteration = 0
    prior_distance_sum = None
    # We run for max iterations or until we converge
    # We only test convergence every S iterations
    while iteration < I:
        pairwise_distances = calculate_distances(data, centroids, data_dots)

        new_labels, distances = relabel(pairwise_distances, data_index)
        distance_sum = np.sum(distances)

        centroids = find_centroids(data, new_labels, C, D)

        if iteration > 0 and iteration % S == 0:
            changes = np.not_equal(labels, new_labels)
            total_changes = np.sum(changes)
            delta = distance_sum / prior_distance_sum
            print("Iteration " + str(iteration) + " produced " +
                  str(total_changes) + " changes, and total distance is " +
                  str(distance_sum))
            # We ignore the result of the threshold test in the case that we
            # are running performance benchmarks to measure performance for a
            # certain number of iterations
            if delta > 1 - 0.000001 and not benchmarking:
                print("Threshold triggered, terminating iterations early")
                break
        prior_distance_sum = distance_sum
        labels = new_labels
        iteration += 1
    # This final distance sum also synchronizes the results
    print("Final distance sum at iteration " + str(iteration) + ": " +
          str(prior_distance_sum))
    stop = datetime.datetime.now()
    delta = stop - start
    total = delta.total_seconds() * 1000.0
    print("Elapsed Time: " + str(total) + " ms")
    return total
Ejemplo n.º 5
0
def test():
    a = random.randint(1, 13)
    b = random.randint(1, 13)
    assert np.array_equal(lg.not_equal(a, b), np.not_equal(a, b))

    return