def analyze_last_to_move_all(graphs, nodes, weight='weight'): last = graphs[-1] observed = _crosstab(last, nodes, weight) ca = CA() ca.fit(observed) _project(last, ca, observed) for g in graphs[:-1]: if sorted(g.nodes) != sorted(last.nodes): raise ValueError('all graphs must have the same nodes') observed = _crosstab(g, nodes, weight) _project(g, ca, observed)
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 setup_class(cls): # Load a dataframe cls.initial_dataframe = pd.read_csv('tests/data/presidentielles07.csv', index_col=0) # Detemine the shape of the initial dataframe (cls.n, cls.p) = cls.initial_dataframe.shape # Determine the total number of observations cls.N = np.sum(cls.initial_dataframe.values) # Calculate a full CA cls.n_components = cls.p cls.ca = CA(cls.initial_dataframe, n_components=cls.n_components)
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 analyze_to_move(g, nodes, weight='weight'): observed = _crosstab(g, nodes, weight) ca = CA() ca.fit(observed) _project(g, ca, observed)
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 ca(df, k): """The executed CA.""" return CA(df, n_components=k)
def corplot(df, x, y): observed = pd.crosstab(df[y], df[x]) ca = CA() ca.fit(observed) ca.plot_coordinates(observed)