def correlation_matrix(data): '''returns the num_cols x num_cols matrix whose (i, j)th entry is the correlation between cols i and j of data''' _, num_columns = lin_alg.shape(data) def matrix_entry(i, j): return stats.correlation(lin_alg.get_col(data, i), get_col(data, j)) return lin_alg.make_matrix(num_columns, num_columns, matrix_entry)
users[id]["betweeness_centrality"] += contrib for user in users: user["closeness_centrality"] = 1 / farness(user) '''for user in users: print("User " + str(user["id"]) + ": " + str(user["shortest_paths"])) print() print("User " + str(user["id"]) + " betweeness centrality: " + str(user["betweeness_centrality"])) print() print("User " + str(user["id"]) + " closeness centrality: " + str(user["closeness_centrality"])) print()''' #eigenvector analysis n = len(users) adjacency_matrix = lin_alg.make_matrix(n, n, entry_fn) eigenvector_centralities, _ = find_eigenvector(adjacency_matrix) for i in range(0, len(eigenvector_centralities)): print("User " + str(i) + ": " + str(eigenvector_centralities[i])) #PageRank and directed graphs (endorsements) endorsements = [(0, 1), (1, 0), (0, 2), (2, 0), (1, 2), (2, 1), (1, 3), (2, 3), (3, 4), (5, 4), (5, 6), (7, 5), (6, 8), (8, 7), (8, 9)] for user in users: user["endorses"] = [] # add one list to track outgoing endorsements user["endorsed_by"] = [] # and another to track endorsements for source_id, target_id in endorsements: users[source_id]["endorses"].append(users[target_id]) users[target_id]["endorsed_by"].append(users[source_id])
def de_mean_matrix(A): '''returns results of subtracting from every value in A the value of the mean in that value's column. resulting matrix has mean 0 in every col''' nr, nc = lin_alg.shape(A) column_means, _ = scale(A) return lin_alg.make_matrix(nr, nc, key=lambda i, j: A[i][j] - column_means[j])