Exemple #1
0
def test_dense_svd():
    dense = DenseTensor(np.random.random(size=(10, 15)))
    sparse = dense.to_sparse()

    print repr(data(dense.svd().u))
    dense_svd_u = np.abs(data(dense.svd().u))
    sparse_svd_u = np.abs(data(sparse.svd().u))

    assert np.all(np.abs(dense_svd_u - sparse_svd_u) < 0.0001)
Exemple #2
0
def aspace_mds():
    from csc.conceptnet.analogyspace import conceptnet_2d_from_db
    cnet = conceptnet_2d_from_db('en')
    aspace = cnet.normalized().svd(k=100)
    labels = cnet.label_list(0)

    ptmatrix = data(aspace.u)
    ptmatrix *= data(aspace.svals)

    proj = mds(ptmatrix)

    result = proj.project(data(aspace.u))
    return LabeledView(DenseTensor(result), [labels, None])
Exemple #3
0
def aspace_mds():
    from csc.conceptnet.analogyspace import conceptnet_2d_from_db
    cnet = conceptnet_2d_from_db('en')
    aspace = cnet.normalized().svd(k=100)
    labels = cnet.label_list(0)

    ptmatrix = data(aspace.u)
    ptmatrix *= data(aspace.svals)

    proj = mds(ptmatrix)

    result = proj.project(data(aspace.u))
    return LabeledView(DenseTensor(result), [labels, None])
Exemple #4
0
def mds(matrix, k=2):
    if isinstance(matrix, Tensor):
        matrix = data(matrix.to_dense())

    # Find Landmark points
    N = matrix.shape[0]
    landmarks = _getLandmarkPoints(N)
    num_landmarks = len(landmarks)

    landmark_matrix = matrix[landmarks]
    sqdistances = compute_distances(landmark_matrix, landmark_matrix)

    # Do normal MDS on landmark points
    means = np.mean(sqdistances, axis=1)  # this is called mu_n in the paper
    global_mean = np.mean(means)  # this is called mu in the paper

    # this is called B in the paper
    distances_balanced = -(sqdistances - means[np.newaxis, :] -
                           means[:, np.newaxis] + global_mean) / 2

    # find the eigenvectors and eigenvalues with our all-purpose hammer
    # for the paper, Lambda = lambda, Q = V
    Q, Lambda, Qt = np.linalg.svd(distances_balanced)
    k = min(k, len(Lambda))
    mdsarray_sharp = Q[:, :k]  # called L^sharp transpose in the paper
    mdsarray = mdsarray_sharp * np.sqrt(Lambda)[
        np.newaxis, :k]  # called L transpose in the paper

    # Make Triangulation Object
    return MDSProjection(landmark_matrix, mdsarray_sharp, means)
Exemple #5
0
def mds(matrix, k=2):
    if isinstance(matrix, Tensor):
        matrix = data(matrix.to_dense())

    # Find Landmark points
    N = matrix.shape[0]
    landmarks = _getLandmarkPoints(N)
    num_landmarks = len(landmarks)
    
    landmark_matrix = matrix[landmarks]
    sqdistances = compute_distances(landmark_matrix, landmark_matrix)

    # Do normal MDS on landmark points
    means = np.mean(sqdistances, axis=1)      # this is called mu_n in the paper
    global_mean = np.mean(means)              # this is called mu in the paper

    # this is called B in the paper
    distances_balanced = -(sqdistances - means[np.newaxis,:] - means[:,np.newaxis] + global_mean)/2

    # find the eigenvectors and eigenvalues with our all-purpose hammer
    # for the paper, Lambda = lambda, Q = V
    Q, Lambda, Qt = np.linalg.svd(distances_balanced)
    k = min(k, len(Lambda))
    mdsarray_sharp = Q[:,:k]                       # called L^sharp transpose in the paper
    mdsarray = mdsarray_sharp * np.sqrt(Lambda)[np.newaxis,:k]  # called L transpose in the paper

    # Make Triangulation Object
    return MDSProjection(landmark_matrix, mdsarray_sharp, means)
Exemple #6
0
def test_dense_data():
    t1 = LabeledView(DenseTensor(zeros((2, 2))), [['a', 'b'], ['c', 'd']])
    assert isinstance(data(t1), ndarray)
Exemple #7
0
def test_dense_data():
    t1 = LabeledView(DenseTensor(zeros((2,2))), [['a', 'b'], ['c', 'd']])
    assert isinstance(data(t1), ndarray)