def corplot_graph(g, nodes, weight='weight', plot=True): other = [m for m in g.nodes if m not in nodes] for n, m in g.edges: if (n in nodes and m in nodes) or (n in other and m in other): raise ValueError('nodes do not define a bipartition') if isinstance(g, nx.DiGraph) and (n in other and m in nodes): raise ValueError('nodes do not define a directed bipartition') nodes = [n for n in nodes if g.degree(n) > 0] other = [m for m in other if g.degree(m) > 0] sparse = nx.bipartite.biadjacency_matrix(g, nodes, other, weight=weight) matrix = sparse.toarray() observed = pd.DataFrame(matrix, nodes, other) ca = CA() ca.fit(observed) if plot: ca.plot_coordinates(observed) h = g.subgraph(nodes + other).copy() pos = ca.row_coordinates(observed) for n, x, y in zip(nodes, pos[0], pos[1]): h.nodes[n]['pos'] = (x, y) h.nodes[n]['color'] = (76, 116, 172) pos = ca.column_coordinates(observed) for m, x, y in zip(other, pos[0], pos[1]): h.nodes[m]['pos'] = (x, y) h.nodes[m]['color'] = (219, 130, 87) normalize(h) return h
def plot_ca(self, groupby, len_set=None, labels_in=None): ca = CA(n_components=2, n_iter=3, random_state=101) df = self.get_union(groupby=groupby, len_set=len_set, labels_in=labels_in) ca.fit(df) ax = ca.plot_coordinates(X=df, figsize=(20, 8)) ax.get_legend().remove() plt.savefig( "%s%s%s对应分析图.png" % (self.savepath, self.name, groupby), format="png", bbox_inches="tight", transparent=True, dpi=600, )
def corplot_twomode(g, nodes, weight='weight'): observed = _crosstab(g, nodes, weight) ca = CA() ca.fit(observed) ca.plot_coordinates(observed)
def corplot_loose(x, y): observed = pd.crosstab(_series(x), _series(y)) ca = CA() ca.fit(observed) ca.plot_coordinates(observed)
def corplot(df, x, y): observed = pd.crosstab(df[y], df[x]) ca = CA() ca.fit(observed) ca.plot_coordinates(observed)