Skip to content
Snippets Groups Projects
Commit 4b879c58 authored by yager2's avatar yager2
Browse files

Changed factorize to return error. The error value was being lost and could...

Changed factorize to return error.  The error value was being lost and could not be recalculated.  Global problem?
parent c6be74e2
No related branches found
No related tags found
No related merge requests found
......@@ -27,6 +27,7 @@ def sparsity_metric(H):
def calculate_error(D, W, H):
norm_D = np.linalg.norm(D)
return np.linalg.norm(D - N(W@H))/norm_D*100
......@@ -39,7 +40,7 @@ def impose_L1_constraint(W, H):
def axe_H(H, tail_cutoff = 0.25):
H2 = H.copy()
for i, column in enumerate(H2.T):
for i, column in enumerate(H.T):
sorted_column = sorted(column)
cumulative_sum = it.accumulate(sorted_column)
kill_count = sum(list(cumulative_sum) < tail_cutoff*sum(column))
......@@ -51,14 +52,18 @@ def sort_WH(W, H):
H2 = axe_H(H)
weights = H2/sum(H2)
signature_popularities = sum(weights.T)
signature_popularities = sorted(enumerate(signature_popularities), key = lambda x : x[1])
signature_popularities.reverse()
signature_popularities
signature_popularities = sorted(enumerate(signature_popularities), key = lambda x : x[1], reverse=True)
W2 = W.copy()
H3 = H2.copy()
for i in range(len(signature_popularities)):
W2[:, i] = W[:, signature_popularities[i][0]]
H3[i] = H2[signature_popularities[i][0]]
try:
assert ( np.linalg.norm(W@H2 - W2@H3) / np.linalg.norm(W@H2) ) < 0.01
except AssertionError:
log.critical('Sorting is not working correctly')
return W2, H3
return W2, H3
......@@ -130,7 +135,7 @@ def factorize(data_array,
diff_H = 100
global norm_D
norm_D = np.linalg.norm(D)
norm_D = np.linalg.norm(N(D))
while abs(diff_W) + abs(diff_H) > threshold or iterations < 100:
if iterations > max_iter:
......@@ -177,4 +182,4 @@ def factorize(data_array,
sparsity = sparsity_metric(H)
log.info('Error= %s, Sparsity= %s', error, sparsity)
return W, H, results[column_names]
return W, H, results[column_names], error
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment