Ejemplo n.º 1
0
def south(df=False):
    """
    Sets up the data for the US southern counties example.

    Returns
    -------
    dictionary or (dictionary, dataframe), where the dictionary is keyed on:

    X           : Data from southern counties, columns "GI89", "BLK90", "HR90"
    Y           : Outcome variate, "DNL90"
    Z           : upper-level variate, the state average "FH90"
    W           : queen weights matrix between counties
    M           : queen matrix between states
    membership  : membership vector relating counties to their states

    and the dataframe contains the raw dataset
    """
    import geopandas
    data = geopandas.read_file(pysal.examples.get_path('south.shp'))
    data = data[data.STATE_NAME != 'District of Columbia']
    X = data[['GI89', 'BLK90', 'HR90']].values
    N = X.shape[0]
    Z = data.groupby('STATE_NAME')['FH90'].mean()
    Z = Z.values.reshape(-1, 1)
    J = Z.shape[0]

    Y = data.DNL90.values.reshape(-1, 1)

    W2 = pysal.queen_from_shapefile(pysal.examples.get_path('us48.shp'),
                                    idVariable='STATE_NAME')
    W2 = pysal.w_subset(
        W2,
        ids=data.STATE_NAME.unique().tolist())  #only keep what's in the data
    W1 = pysal.queen_from_shapefile(pysal.examples.get_path('south.shp'),
                                    idVariable='FIPS')
    W1 = pysal.w_subset(
        W1, ids=data.FIPS.tolist())  #again, only keep what's in the data

    W1.transform = 'r'
    W2.transform = 'r'

    membership = data.STATE_NAME.apply(lambda x: W2.id_order.index(x)).values

    d = {'X': X, 'Y': Y, 'Z': Z, 'W': W1, 'M': W2, 'membership': membership}
    if df:
        return d, data
    else:
        return d
Ejemplo n.º 2
0
def w_local_cluster(w):
    """
    Local clustering coefficients for each unit as a node in a graph. [ws]_

    Parameters
    ----------

    w   : W
          spatial weights object

    Returns
    -------

    c     : array (w.n,1)
            local clustering coefficients

    Notes
    -----

    The local clustering coefficient :math:`c_i` quantifies how close the
    neighbors of observation :math:`i` are to being a clique:

            .. math::

               c_i = | \{w_{j,k}\} |/ (k_i(k_i - 1)): j,k \in N_i

    where :math:`N_i` is the set of neighbors to :math:`i`, :math:`k_i =
    |N_i|` and :math:`\{w_{j,k}\}` is the set of non-zero elements of the
    weights between pairs in :math:`N_i`.

    References
    ----------

    .. [ws] Watts, D.J. and S.H. Strogatz (1988) "Collective dynamics of 'small-world' networks". Nature, 393: 440-442.

    Examples
    --------

    >>> w = pysal.lat2W(3,3, rook=False)
    >>> w_local_cluster(w)
    array([[ 1.        ],
           [ 0.6       ],
           [ 1.        ],
           [ 0.6       ],
           [ 0.42857143],
           [ 0.6       ],
           [ 1.        ],
           [ 0.6       ],
           [ 1.        ]])

    """

    c = np.zeros((w.n, 1), float)
    w.transformation = 'b'
    for i, id in enumerate(w.id_order):
        ki = max(w.cardinalities[id], 1)  # deal with islands
        Ni = w.neighbors[id]
        wi = pysal.w_subset(w, Ni).full()[0]
        c[i] = wi.sum() / (ki * (ki - 1))
    return c
Ejemplo n.º 3
0
Archivo: util.py Proyecto: xrzhou/pysal
def w_local_cluster(w):
    """
    Local clustering coefficients for each unit as a node in a graph. [ws]_

    Parameters
    ----------

    w   : W
          spatial weights object

    Returns
    -------

    c     : array (w.n,1)
            local clustering coefficients

    Notes
    -----

    The local clustering coefficient :math:`c_i` quantifies how close the
    neighbors of observation :math:`i` are to being a clique:

            .. math::

               c_i = | \{w_{j,k}\} |/ (k_i(k_i - 1)): j,k \in N_i

    where :math:`N_i` is the set of neighbors to :math:`i`, :math:`k_i =
    |N_i|` and :math:`\{w_{j,k}\}` is the set of non-zero elements of the
    weights between pairs in :math:`N_i`.

    References
    ----------

    .. [ws] Watts, D.J. and S.H. Strogatz (1988) "Collective dynamics of 'small-world' networks". Nature, 393: 440-442.

    Examples
    --------

    >>> w = pysal.lat2W(3,3, rook=False)
    >>> w_local_cluster(w)
    array([[ 1.        ],
           [ 0.6       ],
           [ 1.        ],
           [ 0.6       ],
           [ 0.42857143],
           [ 0.6       ],
           [ 1.        ],
           [ 0.6       ],
           [ 1.        ]])

    """

    c = np.zeros((w.n, 1), float)
    w.transformation = 'b'
    for i, id in enumerate(w.id_order):
        ki = max(w.cardinalities[id], 1)  # deal with islands
        Ni = w.neighbors[id]
        wi = pysal.w_subset(w, Ni).full()[0]
        c[i] = wi.sum() / (ki * (ki - 1))
    return c
Ejemplo n.º 4
0
 def _update_topo(self):
     atopo = ps.w_subset(self.w, self.agent_xyids, \
             silent_island_warning=True).neighbors
     xyid2agent_id = {xyid: agent_id for agent_id, xyid in \
             enumerate(self.agent_xyids)}
     self.atopo = {
         xyid2agent_id[i]: [xyid2agent_id[j] for j in atopo[i]]
         for i in atopo
     }  # Values are agent order in self.agents!!!
Ejemplo n.º 5
0
 def _update_topo(self):
     atopo = ps.w_subset(self.w, self.agent_xyids, \
             silent_island_warning=True).neighbors
     xyid2agent_id = {xyid: agent_id for agent_id, xyid in \
             enumerate(self.agent_xyids)}
     self.atopo = {xyid2agent_id[i]: [xyid2agent_id[j] for j in atopo[i]] for i in atopo}# Values are agent order in self.agents!!!