def norm_matrix(data_matrix):
    """ rescales the input columns to have zero mean and stdev 1. Ignores
        cols with no deviation """
    means, stds = scale(data_matrix)

    def norm(i, j):
        if stds[j] > 0:
            return (data_matrix[i][j] - means[j]) / stds[j]
        else:
            return data_matrix[i][j]

    num_rows, num_cols = shape(data_matrix)
    return make_matrix(num_rows, num_cols, norm)
Esempio n. 2
0
def norm_matrix(data_matrix):
    """ rescales the input columns to have zero mean and stdev 1. Ignores
        cols with no deviation """
    means, stds = scale(data_matrix)

    def norm(i, j):
        if stds[j] > 0:
            return (data_matrix[i][j] - means[j]) / stds[j]
        else:
            return data_matrix[i][j]

    num_rows, num_cols = shape(data_matrix)
    return make_matrix(num_rows, num_cols, norm)
# Eigenvector-Centrality #
##########################
# We will use the friendships to establish an adjacency matrix which
# represents whether there is a friendship (edge) between every pair of
# users. Thus the matrix will be square. The eigenvectors from a basis set
# for this matrix one per node in the network. The eigenvalues represent how
# many connections are arriving at that node and how well connected that
# node is to other nodes with lots of connections.

def entry_fn(i, j):
    """ returns a 1 if a friendship exist b/w i,j or j,i """
    return 1 if (i, j) in friendships or (j,i) in friendships else 0

n = len(users)
# make the adjacency matrix using entry_fn
adjacency_matrix = make_matrix(n, n, entry_fn)

# now compute the eigenvalues
eigenvalues, _ = eva.find_eigenvector(adjacency_matrix)
# scale up the eigenvalues (default node size in networkx is 300)
sizes = [1000*eigenvalue for eigenvalue in eigenvalues]

# Make a plot using the eigenvalues as the node sizes
fig3 = plt.figure(3)
fig3.suptitle("Eigenvalue-Centrality of User-Friends Network",fontsize=14, 
              fontweight='bold')
# get the nodes and attributes dictionary
positions = nx.get_node_attributes(G,'pos')
# add the friendships as edges to the graph
G.add_edges_from(friendships)
# draw the graph placing the nodes at the positions