def barycenter_graph(distance_matrix, X, reg=<NUM_LIT>):
(N, d_in) = X.shape<EOL>(rows, cols) = distance_matrix.nonzero()<EOL>W = sparse.lil_matrix((N, N)) <EOL>for i in range(N):<EOL><INDENT>nbrs_i = cols[rows == i]<EOL>n_neighbors_i = len(nbrs_i)<EOL>v = np.ones(n_neighbors_i, dtype=X.dtype)<EOL>C = X[nbrs_i] - X[i]<EOL>G = np.dot(C, C.T)<EOL>trace = np.trace(G)<EOL>if trace > <NUM_LIT:0>:<EOL><INDENT>R = reg * trace<EOL><DEDENT>else:<EOL><INDENT>R = reg<EOL><DEDENT>G.flat[::n_neighbors_i + <NUM_LIT:1>] += R<EOL>w = solve(G, v, sym_pos = True)<EOL>W[i, nbrs_i] = w / np.sum(w)<EOL><DEDENT>return W<EOL>
Computes the barycenter weighted graph for points in X

Parameters
----------
distance_matrix: sparse Ndarray, (N_obs, N_obs) pairwise distance matrix.
X : Ndarray (N_obs, N_dim) observed data matrix.
reg : float, optional
    Amount of regularization when solving the least-squares
    problem. Only relevant if mode='barycenter'. If None, use the
    default.

Returns
-------
W : sparse matrix in CSR format, shape = [n_samples, n_samples]
    W[i, j] is assigned the weight of edge that connects i to j.