Skip to content

The function truncated_svd() should not return negative singular values. #1059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,15 @@ def remove_component(X):

projected_X = matrix_multiplication(A, [[x] for x in X])
projected_X = [x[0] for x in projected_X]
eivals.append(norm(projected_X, 1)/norm(X, 1))
eivec_m.append(X[:m])
eivec_n.append(X[m:])
new_eigenvalue = norm(projected_X, 1)/norm(X, 1)
ev_m = X[:m]
ev_n = X[m:]
if new_eigenvalue < 0:
new_eigenvalue = -new_eigenvalue
ev_m = [-ev_m_i for ev_m_i in ev_m]
eivals.append(new_eigenvalue)
eivec_m.append(ev_m)
eivec_n.append(ev_n)
return (eivec_m, eivec_n, eivals)

# ______________________________________________________________________________
Expand Down
16 changes: 8 additions & 8 deletions tests/test_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,28 +143,28 @@ def test_truncated_svd():
test_mat = [[17, 0],
[0, 11]]
_, _, eival = truncated_svd(test_mat)
assert isclose(abs(eival[0]), 17)
assert isclose(abs(eival[1]), 11)
assert isclose(eival[0], 17)
assert isclose(eival[1], 11)

test_mat = [[17, 0],
[0, -34]]
_, _, eival = truncated_svd(test_mat)
assert isclose(abs(eival[0]), 34)
assert isclose(abs(eival[1]), 17)
assert isclose(eival[0], 34)
assert isclose(eival[1], 17)

test_mat = [[1, 0, 0, 0, 2],
[0, 0, 3, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, 0, 0, 0]]
_, _, eival = truncated_svd(test_mat)
assert isclose(abs(eival[0]), 3)
assert isclose(abs(eival[1]), 5**0.5)
assert isclose(eival[0], 3)
assert isclose(eival[1], 5**0.5)

test_mat = [[3, 2, 2],
[2, 3, -2]]
_, _, eival = truncated_svd(test_mat)
assert isclose(abs(eival[0]), 5)
assert isclose(abs(eival[1]), 3)
assert isclose(eival[0], 5)
assert isclose(eival[1], 3)


def test_decision_tree_learner():
Expand Down