def test___init__(self): import numpy as np f = ps.io.open(ps.examples.get_path('usjoin.csv')) pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]) q5 = np.array([mc.Quantiles(y).yb for y in pci]).transpose() m = Markov(q5) np.testing.assert_array_almost_equal(markov_mobility(m.p, measure="P"), 0.19758992000997844)
def test___init__(self): f = ps.io.open(ps.examples.get_path('usjoin.csv')) pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]) q5 = np.array([mc.Quantiles(y).yb for y in pci]).transpose() m = Markov(q5) res = np.array( [0.08988764, 0.21468144, 0.21125, 0.20194986, 0.07259074]) np.testing.assert_array_almost_equal(prais(m.p), res)
def G_display(G): # make new graph H = nx.Graph() for v in G: # print(v) H.add_node(v) weightValue = list(nx.get_edge_attributes(G, 'weight').values()) #提取权重 # weightsForWidth=[G[u][v]['weight'] for u,v in G.edges()] #another way # print(weightValue) import pysal.viz.mapclassify as mc q = mc.Quantiles(weightValue, k=30).bins #计算分位数,用于显示值的提取 # print(q) for (u, v, d) in tqdm(G.edges(data=True)): # print(u,v,d) # print() # print(d['weight']) if d['weight'] > q[28]: H.add_edge(u, v) print("H_digraph has %d nodes with %d edges" % (nx.number_of_nodes(H), nx.number_of_edges(H))) # draw with matplotlib/pylab plt.figure(figsize=(18, 18)) # m=2 # fig = figure(figsize=(9*m,9*m) # with nodes colored by degree sized by value elected node_color = [float(H.degree(v)) for v in H] # print(node_color) # nx.draw(H, G.position,node_size=[G.perimeter[v] for v in H],node_color=node_color, with_labels=True) weightsForWidthScale = np.interp(weightValue, (min(weightValue), max(weightValue)), (1, 3000)) #setting the edge width scaleNode = 1 # sklearn.preprocessing.minmax_scale(X, feature_range=(0, 1), axis=0, copy=True) nx.draw(H, G.position, node_size=minmax_scale([G.shape_area[v] * scaleNode for v in H], feature_range=(10, 2200)), node_color=node_color, with_labels=True, edge_cmap=plt.cm.Blues, width=weightsForWidthScale) #edge_cmap=plt.cm.Blues # scale the axes equally # plt.xlim(-5000, 500) # plt.ylim(-2000, 3500) plt.show()
def test___init__(self): pi = np.array([0.1, 0.2, 0.2, 0.4, 0.1]) f = ps.io.open(ps.examples.get_path('usjoin.csv')) pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]) q5 = np.array([mc.Quantiles(y).yb for y in pci]).transpose() m = Markov(q5) np.testing.assert_array_almost_equal(markov_mobility(m.p, measure="P"), 0.19758992000997844) np.testing.assert_array_almost_equal(markov_mobility(m.p, measure="D"), 0.60684854623695594) np.testing.assert_array_almost_equal( markov_mobility(m.p, measure="L2"), 0.039782002308159647) np.testing.assert_array_almost_equal( markov_mobility(m.p, measure="B1", ini=pi), 0.2277675878319787) np.testing.assert_array_almost_equal( markov_mobility(m.p, measure="B2", ini=pi), 0.046366601194789261)
def test___init__(self): # markov = Markov(class_ids, classes) f = ps.io.open(ps.examples.get_path('usjoin.csv')) pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]) q5 = np.array([mc.Quantiles(y).yb for y in pci]).transpose() m = Markov(q5) expected = np.array([[729., 71., 1., 0., 0.], [72., 567., 80., 3., 0.], [0., 81., 631., 86., 2.], [0., 3., 86., 573., 56.], [0., 0., 1., 57., 741.]]) np.testing.assert_array_equal(m.transitions, expected) expected = np.array( [[0.91011236, 0.0886392, 0.00124844, 0., 0.], [0.09972299, 0.78531856, 0.11080332, 0.00415512, 0.], [0., 0.10125, 0.78875, 0.1075, 0.0025], [0., 0.00417827, 0.11977716, 0.79805014, 0.07799443], [0., 0., 0.00125156, 0.07133917, 0.92740926]]) np.testing.assert_array_almost_equal(m.p, expected) expected = np.array( [0.20774716, 0.18725774, 0.20740537, 0.18821787, 0.20937187]) np.testing.assert_array_almost_equal(m.steady_state, expected)
alpha=1, legend=True, edgecolor='w', linewidth=0.1) plt.axis('equal') plt.show() # ### Quantiles # One solution to obtain a more balanced classification scheme is using quantiles. This, by definition, assigns the same amount of values to each bin: the entire series is laid out in order and break points are assigned in a way that leaves exactly the same amount of observations between each of them. This "observation-based" approach contrasts with the "value-based" method of equal intervals and, although it can obscure the magnitude of extreme values, it can be more informative in cases with skewed distributions. # # Calculating a quantiles classification with `PySAL` can be done with the following line of code: # In[38]: classi = mapclassify.Quantiles(imd['imd_rank'], k=7) classi # And, similarly, the bins can also be inspected: # In[39]: classi.bins # The visualization of the distribution can be generated in a similar way as well: # In[40]: # Set up the figure f, ax = plt.subplots(1) # Plot the kernel density estimation (KDE)
def networkAnalysis_B(G): #本次实验中未使用该部分,但保留在该代码文件中,已备查看 try: import pygraphviz # conda install -c alubbock pygraphviz 用此方法安装pygraphviz库,能正常安装 from networkx.drawing.nx_agraph import graphviz_layout layout = graphviz_layout except ImportError: try: import pydot from networkx.drawing.nx_pydot import graphviz_layout layout = graphviz_layout except ImportError: print("PyGraphviz and pydot not found;\n" "drawing with spring layout;\n" "will be slow.") layout = nx.spring_layout # 建立新网络图,复制G,make new graph H = nx.Graph() for v in G: # print(v) H.add_node(v) weightValue = list(nx.get_edge_attributes(G, 'weight').values()) #提取权重值 # weightsForWidth=[G[u][v]['weight'] for u,v in G.edges()] #another way # print(weightValue) import pysal.viz.mapclassify as mc q = mc.Quantiles(weightValue, k=50).bins #计算分位数,用于显示值的提取 # print(q) #复制G到H for (u, v, d) in G.edges(data=True): # print(u,v,d) # print() # print(d['weight']) if d['weight'] > q[48]: #根据权重选择较为紧密的部分提取 H.add_edge(u, v) print("H_digraph has %d nodes with %d edges" % (nx.number_of_nodes(H), nx.number_of_edges(H))) weightsForWidthScale = np.interp( weightValue, (min(weightValue), max(weightValue)), (1, 3000)) #缩放值到新区间,用于强化显示数据,setting the edge width # print(weightsForWidthScale) node_color = [float(H.degree(v)) for v in H] scaleNode = 1 # the following range of p values should be close to the threshold plt.figure(figsize=(100, 100)) plt.subplots_adjust(left=0, right=1, bottom=0, top=0.95, wspace=0.01, hspace=0.01) # nx.draw(H, G.position, with_labels=False, node_size=800) #打印图_node部分 nx.draw(H, G.position, node_size=minmax_scale([G.shape_area[v] * scaleNode for v in H], feature_range=(100, 8200)), node_color=node_color, with_labels=True, font_size=60, edge_cmap=plt.cm.Blues, width=weightsForWidthScale) #edge_cmap=plt.cm.Blues #根据edge长度排序node identify largest connected component Gcc = sorted(nx.connected_components(H), key=len, reverse=True) # print(Gcc) G0 = H.subgraph(Gcc[0]) #为最长的边 nx.draw_networkx_edges(G0, G.position, with_labels=False, edge_color='r', width=9.0) #设置条件,显示其它长度的边 show other connected components for Gi in Gcc[1:]: if len(Gi) > 10: nx.draw_networkx_edges(H.subgraph(Gi), G.position, with_labels=False, edge_color='r', alpha=0.3, width=7.0) plt.show()