Example #1
0
 def test_make_matrix(self):
     self.assertEqual([[0, 2, 4], [1, 3, 5]],
                      linalg.make_matrix(2, 3,
                                         lambda row, col: row + col * 2))
     self.assertEqual([[0, 0], [0, 0]],
                      linalg.make_matrix(2, 2, lambda row, col: 0))
     self.assertEqual([[], [], []],
                      linalg.make_matrix(3, 0, lambda row, col: 8))
     self.assertEqual([], linalg.make_matrix(0, 0, lambda row, col: 5))
def matrix_product(A, B):
    rows_A, cols_A = shape(A)
    rows_B, cols_B = shape(B)
    if cols_A != rows_B:
        raise ArithmeticError("incompatible shapes")

    return make_matrix(rows_A, cols_B, partial(matrix_product_entry, A, B))
def rescale(matrix):
    """rescale the matrix so that each column has mean 0 and standard deviation 1"""
    means, stdevs = scale(matrix)

    def rescaled(i, j):
        if stdevs[j] > 0:
            return (matrix[i][j] - means[j]) / stdevs[j]
        else:
            return matrix[i][j]

    num_rows, num_columns = shape(matrix)
    return make_matrix(num_rows, num_columns, rescaled)
def rescale(matrix):
    """rescale the matrix so that each column has mean 0 and standard deviation 1"""
    means, stdevs = scale(matrix)

    def rescaled(i, j):
        if stdevs[j] > 0:
            return (matrix[i][j] - means[j]) / stdevs[j]
        else:
            return matrix[i][j]

    num_rows, num_columns = shape(matrix)
    return make_matrix(num_rows, num_columns, rescaled)
Example #5
0
def correlation_matrix(data: List[Vector]) -> Matrix:
    """
    Returns the len(data) * len(data)) correlation matrix whose (i, j)-th entry is 
    the correlation between data[i] and data[j]
    
    Note: in this example, the data is shaped (features * examples) vs. (examples * features): we provide 
    correlation across axis=0
    """
    def correlation_ij(i: int, j: int) -> float:
        # inner function which will serve as our data-generator for make_matrix 
        # (note: the function defines relationship to argument data)
        return correlation(data[i], data[j])
    return make_matrix(len(data), len(data), correlation_ij)
def correlation_matrix(matrix):
    _, num_columns = shape(matrix)
    return make_matrix(num_columns, num_columns,
                       lambda i, j: columns_correlation(matrix, i, j))
def de_mean_matrix(matrix):
    """make each column have mean 0 by centering each element to its former mean"""
    nr, nc = shape(matrix)
    means, _ = scale(matrix)
    return make_matrix(nr, nc, lambda i, j: matrix[i][j] - means[j])
 def test_make_matrix(self):
     self.assertEqual([[0, 2, 4], [1, 3, 5]], linalg.make_matrix(2, 3, lambda row, col: row + col*2))
     self.assertEqual([[0, 0], [0, 0]], linalg.make_matrix(2, 2, lambda row, col: 0))
     self.assertEqual([[], [], []], linalg.make_matrix(3, 0, lambda row, col: 8))
     self.assertEqual([], linalg.make_matrix(0, 0, lambda row, col: 5))
                            users[id]["betweenness_centrality"] += contrib

    print "Betweenness centrality"
    for user in users:
        print user["id"], ' : ', user["betweenness_centrality"]
    print

    print "Closeness centrality"
    for user in users:
        print user["id"], ' : ', user["closeness_centrality"]
    print

    # eigenvector centrality
    def adjacency_fn(i, j):
        return 1 if (i, j) in friendships or (j, i) in friendships else 0
    adjacency_matrix = make_matrix(len(users), len(users), adjacency_fn)

    eigenvector_centralities, _ = find_eigenvector(adjacency_matrix)

    pprint.pprint(adjacency_matrix)
    print eigenvector_centralities

    # directed graphs
    print

    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
def correlation_matrix(matrix):
    _, num_columns = shape(matrix)
    return make_matrix(num_columns, num_columns, lambda i, j: columns_correlation(matrix, i, j))
def de_mean_matrix(matrix):
    """make each column have mean 0 by centering each element to its former mean"""
    nr, nc = shape(matrix)
    means, _ = scale(matrix)
    return make_matrix(nr, nc, lambda i, j: matrix[i][j] - means[j])