geosnap.analyze.dynamics module¶
Transition and sequence analysis of neighborhood change.
-
geosnap.analyze.dynamics.
sequence
(gdf, cluster_col, seq_clusters=5, subs_mat=None, dist_type=None, indel=None, time_var='year', id_var='geoid')[source]¶ Pairwise sequence analysis and sequence clustering.
Dynamic programming if optimal matching.
- Parameters
- gdf(geo)DataFrame
Long-form (geo)DataFrame containing neighborhood attributes with a column defining neighborhood clusters.
- cluster_colstring or int
Column name for the neighborhood segmentation, such as “ward”, “kmeans”, etc.
- seq_clustersint, optional
Number of neighborhood sequence clusters. Agglomerative Clustering with Ward linkage is now used for clustering the sequences. Default is 5.
- dist_typestring
“hamming”: hamming distance (substitution only and its cost is constant 1) from sklearn.metrics; “markov”: utilize empirical transition probabilities to define substitution costs; “interval”: differences between states are used to define substitution costs, and indel=k-1; “arbitrary”: arbitrary distance if there is not a strong theory guidance: substitution=0.5, indel=1. “tran”: transition-oriented optimal matching. Sequence of transitions. Based on [Bie11].
- subs_matarray
(k,k), substitution cost matrix. Should be hollow ( 0 cost between the same type), symmetric and non-negative.
- indelfloat, optional
insertion/deletion cost.
- time_varstring, optional
Column defining time and or sequencing of the long-form data. Default is “year”.
- id_varstring, optional
Column identifying the unique id of spatial units. Default is “geoid”.
Examples
>>> from geosnap.data import Community >>> columbus = Community.from_ltdb(msa_fips=columbusfips) >>> columbus1 = columbus.cluster(columns=['median_household_income', ... 'p_poverty_rate', 'p_edu_college_greater', 'p_unemployment_rate'], ... method='ward', n_clusters=6) >>> gdf = columbus1.gdf >>> gdf_new, df_wide, seq_hamming = Sequence(gdf, dist_type="hamming") >>> seq_hamming.seq_dis_mat[:5, :5] array([[0., 3., 4., 5., 5.], [3., 0., 3., 3., 3.], [4., 3., 0., 2., 2.], [5., 3., 2., 0., 0.], [5., 3., 2., 0., 0.]])
-
geosnap.analyze.dynamics.
transition
(gdf, cluster_col, time_var='year', id_var='geoid', w_type=None, permutations=0)[source]¶ (Spatial) Markov approach to transitional dynamics of neighborhoods.
- Parameters
- gdf(geo)DataFrame
Long-form (geo)DataFrame containing neighborhood attributes with a column defining neighborhood clusters.
- cluster_colstring or int
Column name for the neighborhood segmentation, such as “ward”, “kmeans”, etc.
- time_varstring, optional
Column defining time and or sequencing of the long-form data. Default is “year”.
- id_varstring, optional
Column identifying the unique id of spatial units. Default is “geoid”.
- w_typestring, optional
Type of spatial weights type (“rook”, “queen”, “knn” or “kernel”) to be used for spatial structure. Default is None, if non-spatial Markov transition rates are desired.
- permutationsint, optional
number of permutations for use in randomization based inference (the default is 0).
Examples
>>> from geosnap.data import Community >>> columbus = Community.from_ltdb(msa_fips=columbusfips) >>> columbus1 = columbus.cluster(columns=['median_household_income', ... 'p_poverty_rate', 'p_edu_college_greater', 'p_unemployment_rate'], ... method='ward', n_clusters=6) >>> gdf = columbus1.gdf >>> a = transition(gdf, "ward", w_type="rook") >>> a.p array([[0.79189189, 0.00540541, 0.0027027 , 0.13243243, 0.06216216, 0.00540541], [0.0203252 , 0.75609756, 0.10569106, 0.11382114, 0. , 0.00406504], [0.00917431, 0.20183486, 0.75229358, 0.01834862, 0. , 0.01834862], [0.1959799 , 0.18341709, 0.00251256, 0.61809045, 0. , 0. ], [0.32307692, 0. , 0. , 0. , 0.66153846, 0.01538462], [0.09375 , 0.0625 , 0. , 0. , 0. , 0.84375 ]]) >>> a.P[0] array([[0.82119205, 0. , 0. , 0.10927152, 0.06622517, 0.00331126], [0.14285714, 0.57142857, 0.14285714, 0.14285714, 0. , 0. ], [0.5 , 0. , 0.5 , 0. , 0. , 0. ], [0.21428571, 0.14285714, 0. , 0.64285714, 0. , 0. ], [0.18918919, 0. , 0. , 0. , 0.78378378, 0.02702703], [0.28571429, 0. , 0. , 0. , 0. , 0.71428571]])