コード例 #1
0
 def buildWeights(self):
     """Performs Distance-based Weights Creation"""
     ARCPY.SetProgressor("default", "Constructing spatial weights object...")
     
     #### Shorthand Attributes ####
     distanceType = self.distanceType
     threshold = self.threshold
     knnNum = self.knnNum
     idField = self.idField
     outputExt = self.outputExt
     ssdo = self.ssdo
     
     #### Create Distance-based WeightObj (0-based IDs) ####
     dataArray = ssdo.xyCoords
     if distanceType.upper() == DISTTYPE[0]:
         weightObj = PYSAL.threshold_binaryW_from_array(dataArray, threshold)
     elif distanceType.upper() == DISTTYPE[1]:
         weightObj = PYSAL.knnW_from_array(dataArray, knnNum)
     elif distanceType.upper() == DISTTYPE[2]:
         alpha = -1 * self.inverseDist
         weightObj = PYSAL.threshold_continuousW_from_array(\
             dataArray, threshold, alpha=alpha)
       
     #### Re-Create WeightObj for NOT 0-based idField #### 
     if idField:
         if ssdo.master2Order.keys() != ssdo.master2Order.values(): 
             o2M = ssdo.order2Master
             neighborDict = {o2M[oid] : [o2M[nid] for nid in nbrs] \
                             for oid,nbrs in weightObj.neighbors.items()}
             weightDict = {o2M[oid] : weights \
                           for oid, weights in weightObj.weights.items()}
             weightObj = W(neighborDict, weightDict)
         
     #### Save weightObj Class Object for Writing Result #### 
     self.weightObj = weightObj
コード例 #2
0
 def test_knnW_from_array(self):
     import numpy as np
     x, y = np.indices((5, 5))
     x.shape = (25, 1)
     y.shape = (25, 1)
     data = np.hstack([x, y])
     wnn2 = pysal.knnW_from_array(data, k=2)
     wnn4 = pysal.knnW_from_array(data, k=4)
     self.assertEquals(set(wnn4.neighbors[0]), set([1, 5, 6, 2]))
     self.assertEquals(set(wnn4.neighbors[5]), set([0, 6, 10, 1]))
     self.assertEquals(set(wnn2.neighbors[0]), set([1, 5]))
     self.assertEquals(set(wnn2.neighbors[5]), set([0, 6]))
     self.assertAlmostEquals(wnn2.pct_nonzero, 8.0)
     self.assertAlmostEquals(wnn4.pct_nonzero, 16.0)
     wnn4 = pysal.knnW_from_array(data, k=4)
     self.assertEquals(set(wnn4.neighbors[0]), set([1, 5, 6, 2]))
     '''
コード例 #3
0
ファイル: test_user.py プロジェクト: CartoDB/pysal
 def test_knnW_from_array(self):
     import numpy as np
     x, y = np.indices((5, 5))
     x.shape = (25, 1)
     y.shape = (25, 1)
     data = np.hstack([x, y])
     wnn2 = pysal.knnW_from_array(data, k=2)
     wnn4 = pysal.knnW_from_array(data, k=4)
     self.assertEquals(set(wnn4.neighbors[0]), set([1, 5, 6, 2]))
     self.assertEquals(set(wnn4.neighbors[5]), set([0, 6, 10, 1]))
     self.assertEquals(set(wnn2.neighbors[0]), set([1, 5]))
     self.assertEquals(set(wnn2.neighbors[5]), set([0, 6]))
     self.assertAlmostEquals(wnn2.pct_nonzero, 8.0)
     self.assertAlmostEquals(wnn4.pct_nonzero, 16.0)
     wnn4 = pysal.knnW_from_array(data, k=4)
     self.assertEquals(set(wnn4.neighbors[0]), set([1, 5, 6, 2]))
     '''
コード例 #4
0
ファイル: test_smoothing.py プロジェクト: sunpoet/pysal
 def setUp(self):
     sids = pysal.open(pysal.examples.get_path("sids2.shp"), "r")
     self.sids = sids
     self.d = np.array([i.centroid for i in sids])
     self.w = knnW_from_array(self.d, k=5)
     if not self.w.id_order_set:
         self.w.id_order = self.w.id_order
     sids_db = pysal.open(pysal.examples.get_path("sids2.dbf"), "r")
     self.b, self.e = np.array(sids_db[:, 8]), np.array(sids_db[:, 9])
コード例 #5
0
 def setUp(self):
     sids = pysal.open(pysal.examples.get_path('sids2.shp'), 'r')
     self.sids = sids
     self.d = np.array([i.centroid for i in sids])
     self.w = knnW_from_array(self.d, k=5)
     if not self.w.id_order_set:
         self.w.id_order = self.w.id_order
     sids_db = pysal.open(pysal.examples.get_path('sids2.dbf'), 'r')
     self.b, self.e = np.array(sids_db[:, 8]), np.array(sids_db[:, 9])
コード例 #6
0
ファイル: test_user.py プロジェクト: Alwnikrotikz/pysal
 def test_knnW_from_array(self):
     import numpy as np
     x, y = np.indices((5, 5))
     x.shape = (25, 1)
     y.shape = (25, 1)
     data = np.hstack([x, y])
     wnn2 = pysal.knnW_from_array(data, k=2)
     wnn4 = pysal.knnW_from_array(data, k=4)
     self.assertEquals(wnn4.neighbors[0], [1, 5, 6, 2])
     self.assertEquals(wnn4.neighbors[5], [0, 6, 10, 1])
     self.assertEquals(wnn2.neighbors[0], [1, 5])
     self.assertEquals(wnn2.neighbors[5], [0, 6])
     self.assertAlmostEquals(wnn2.pct_nonzero, 0.080000000000000002)
     self.assertAlmostEquals(wnn4.pct_nonzero, 0.16)
     wnn4 = pysal.knnW_from_array(data, k=4)
     self.assertEquals(wnn4.neighbors[0], [1, 5, 6, 2])
     wnn3e = pysal.knnW(data, p=2, k=3)
     self.assertEquals(wnn3e.neighbors[0], [1, 5, 6])
     wnn3m = pysal.knnW(data, p=1, k=3)
     self.assertEquals(wnn3m.neighbors[0], [1, 5, 2])
コード例 #7
0
ファイル: test_smoothing.py プロジェクト: sunpoet/pysal
 def test_Headbanging_Median_Rate(self):
     sids_d = np.array([i.centroid for i in self.sids])
     sids_w = pysal.knnW_from_array(sids_d, k=5)
     if not sids_w.id_order_set:
         sids_w.id_order = sids_w.id_order
     s_ht = sm.Headbanging_Triples(sids_d, sids_w, k=5)
     sids_db = pysal.open(pysal.examples.get_path("sids2.dbf"), "r")
     s_e, s_b = np.array(sids_db[:, 9]), np.array(sids_db[:, 8])
     sids_hb_r = sm.Headbanging_Median_Rate(s_e, s_b, s_ht)
     sids_hb_rr5 = np.array([0.00075586, 0.0, 0.0008285, 0.0018315, 0.00498891])
     np.testing.assert_array_almost_equal(sids_hb_rr5, sids_hb_r.r[:5])
     sids_hb_r2 = sm.Headbanging_Median_Rate(s_e, s_b, s_ht, iteration=5)
     sids_hb_r2r5 = np.array([0.0008285, 0.00084331, 0.00086896, 0.0018315, 0.00498891])
     np.testing.assert_array_almost_equal(sids_hb_r2r5, sids_hb_r2.r[:5])
     sids_hb_r3 = sm.Headbanging_Median_Rate(s_e, s_b, s_ht, aw=s_b)
     sids_hb_r3r5 = np.array([0.00091659, 0.0, 0.00156838, 0.0018315, 0.00498891])
     np.testing.assert_array_almost_equal(sids_hb_r3r5, sids_hb_r3.r[:5])
コード例 #8
0
ファイル: WghUtilities.py プロジェクト: rajeshwarn/GeoCloud
    def calcWeightMatrix(self, get_wm=0):

        wm = None
        if self.geotype == 'Polygon':
            parser = WKTParser()
            shape_l = MyList([parser.fromWKT(s) for s in self.listWKT])
            neighbour_data = ContiguityWeights(shape_l).w
            wm = WeightMatrix(neighbour_data, id_order=self.ids)
        elif self.geotype == 'Point':
            parser = WKTParser()
            shape_l = MyList([parser.fromWKT(s) for s in self.listWKT])
            #wm = pysal.knnW_from_array(np.array(self.listWKT), k=5, ids=self.ids)
            wm = pysal.knnW_from_array(np.array(shape_l), k=5, ids=self.ids)
        if get_wm == 1:
            return wm
        warray = self.dict2array(wm, self.N)
        return warray
コード例 #9
0
 def test_Headbanging_Median_Rate(self):
     sids_d = np.array([i.centroid for i in self.sids])
     sids_w = pysal.knnW_from_array(sids_d, k=5)
     if not sids_w.id_order_set:
         sids_w.id_order = sids_w.id_order
     s_ht = sm.Headbanging_Triples(sids_d, sids_w, k=5)
     sids_db = pysal.open(pysal.examples.get_path('sids2.dbf'), 'r')
     s_e, s_b = np.array(sids_db[:, 9]), np.array(sids_db[:, 8])
     sids_hb_r = sm.Headbanging_Median_Rate(s_e, s_b, s_ht)
     sids_hb_rr5 = np.array([0.00075586, 0.,
                             0.0008285, 0.0018315, 0.00498891])
     np.testing.assert_array_almost_equal(sids_hb_rr5, sids_hb_r.r[:5])
     sids_hb_r2 = sm.Headbanging_Median_Rate(s_e, s_b, s_ht, iteration=5)
     sids_hb_r2r5 = np.array([0.0008285, 0.00084331,
                              0.00086896, 0.0018315, 0.00498891])
     np.testing.assert_array_almost_equal(sids_hb_r2r5, sids_hb_r2.r[:5])
     sids_hb_r3 = sm.Headbanging_Median_Rate(s_e, s_b, s_ht, aw=s_b)
     sids_hb_r3r5 = np.array([0.00091659, 0.,
                              0.00156838, 0.0018315, 0.00498891])
     np.testing.assert_array_almost_equal(sids_hb_r3r5, sids_hb_r3.r[:5])
コード例 #10
0
def randomKnnW(n, k, p=2.):
    '''
    Builds a W object from random points based on knn
    ...

    Arguments
    ---------
    n       : int
              Number of observations for the W object
    k       : int
              Number of neighbors per observation in the W object
    p       : float
              Minkowski p-norm distance metric parameter

    Returns
    -------
    w       : W
              PySAL weights object

    '''
    xy = np.random.random(n * 2)
    xy = xy.reshape((n, 2))
    return pysal.knnW_from_array(xy, k, p)
コード例 #11
0
ファイル: test_smoothing.py プロジェクト: lanselin/pysal
 def setUp(self):
     sids = pysal.open(pysal.examples.get_path('sids2.shp'), 'r')
     self.sids = sids
     self.d = np.array([i.centroid for i in sids])
     self.w = knnW_from_array(self.d, k=5)
     if not self.w.id_order_set:
         self.w.id_order = self.w.id_order
     sids_db = pysal.open(pysal.examples.get_path('sids2.dbf'), 'r')
     self.b, self.e = np.array(sids_db[:, 8]), np.array(sids_db[:, 9])
     self.sids_hb_rr5 = np.array([0.00075586, 0.,
                                  0.0008285, 0.0018315, 0.00498891])
     self.sids_hb_r2r5 = np.array([0.0008285, 0.00084331,
                                   0.00086896, 0.0018315, 0.00498891])
     self.sids_hb_r3r5 = np.array([0.00091659, 0.,
                                   0.00156838, 0.0018315, 0.00498891])
     if not PANDAS_EXTINCT:
         self.df = sids_db.to_df()
         self.ename = 'SID74'
         self.bname = 'BIR74'
         self.enames = [self.ename, 'SID79']
         self.bnames = [self.bname, 'BIR79']
         self.sids79r = np.array([.000563, .001659, .001879, 
                                  .002410, .002720])
コード例 #12
0
 def setUp(self):
     sids = pysal.open(pysal.examples.get_path('sids2.shp'), 'r')
     self.sids = sids
     self.d = np.array([i.centroid for i in sids])
     self.w = knnW_from_array(self.d, k=5)
     if not self.w.id_order_set:
         self.w.id_order = self.w.id_order
     sids_db = pysal.open(pysal.examples.get_path('sids2.dbf'), 'r')
     self.b, self.e = np.array(sids_db[:, 8]), np.array(sids_db[:, 9])
     self.sids_hb_rr5 = np.array(
         [0.00075586, 0., 0.0008285, 0.0018315, 0.00498891])
     self.sids_hb_r2r5 = np.array(
         [0.0008285, 0.00084331, 0.00086896, 0.0018315, 0.00498891])
     self.sids_hb_r3r5 = np.array(
         [0.00091659, 0., 0.00156838, 0.0018315, 0.00498891])
     if not PANDAS_EXTINCT:
         self.df = sids_db.to_df()
         self.ename = 'SID74'
         self.bname = 'BIR74'
         self.enames = [self.ename, 'SID79']
         self.bnames = [self.bname, 'BIR79']
         self.sids79r = np.array(
             [.000563, .001659, .001879, .002410, .002720])
コード例 #13
0
def randomKnnW(n, k, p=2.):
    '''
    Builds a W object from random points based on knn
    ...

    Arguments
    ---------
    n       : int
              Number of observations for the W object
    k       : int
              Number of neighbors per observation in the W object
    p       : float
              Minkowski p-norm distance metric parameter

    Returns
    -------
    w       : W
              PySAL weights object

    '''
    xy = np.random.random(n * 2)
    xy = xy.reshape((n, 2))
    return pysal.knnW_from_array(xy, k, p)
コード例 #14
0
    def buildWeights(self):
        """Performs Distance-based Weights Creation"""
        ARCPY.SetProgressor("default",
                            "Constructing spatial weights object...")

        #### Shorthand Attributes ####
        distanceType = self.distanceType
        threshold = self.threshold
        knnNum = self.knnNum
        idField = self.idField
        outputExt = self.outputExt
        ssdo = self.ssdo

        #### Create Distance-based WeightObj (0-based IDs) ####
        dataArray = ssdo.xyCoords
        if distanceType.upper() == DISTTYPE[0]:
            weightObj = PYSAL.threshold_binaryW_from_array(
                dataArray, threshold)
        elif distanceType.upper() == DISTTYPE[1]:
            weightObj = PYSAL.knnW_from_array(dataArray, knnNum)
        elif distanceType.upper() == DISTTYPE[2]:
            alpha = -1 * self.inverseDist
            weightObj = PYSAL.threshold_continuousW_from_array(\
                dataArray, threshold, alpha=alpha)

        #### Re-Create WeightObj for NOT 0-based idField ####
        if idField:
            if ssdo.master2Order.keys() != ssdo.master2Order.values():
                o2M = ssdo.order2Master
                neighborDict = {o2M[oid] : [o2M[nid] for nid in nbrs] \
                                for oid,nbrs in weightObj.neighbors.items()}
                weightDict = {o2M[oid] : weights \
                              for oid, weights in weightObj.weights.items()}
                weightObj = W(neighborDict, weightDict)

        #### Save weightObj Class Object for Writing Result ####
        self.weightObj = weightObj
コード例 #15
0
YVar = 'StructurePrice'
XVarsdummy = ['LivingArea']
num = 0

for s in finalSet:
    XVarsdummy.append(s)
    lst[s] = lst[s].astype(float)

XVars = ['median_income', 'LivingArea', 'Age', 'num_trees']
yxs = lst.loc[:, XVars + [YVar]].dropna()
yxs_dummy = lst.loc[:, XVarsdummy + [YVar]].dropna()
y = lst[YVar]

w = pysal.knnW_from_array(lst.loc[\
                               yxs.index, \
                              ['centroid_long', 'centroid_lat']\
                              ].values, k=30)
w.transform = 'R'

yxs = yxs.assign(w_res=pysal.lag_spatial(w, yxs_dummy['residential'].values))
"""
yxs = yxs.assign(w_mixed=pysal.lag_spatial(w, yxs_dummy['mixed'].values))
yxs = yxs.assign(w_retail=pysal.lag_spatial(w, yxs_dummy['retail'].values))
yxs = yxs.assign(w_apt=pysal.lag_spatial(w, yxs_dummy['apt'].values))
yxs = yxs.assign(w_industrial=pysal.lag_spatial(w, yxs_dummy['industrial'].values))
yxs = yxs.assign(w_office=pysal.lag_spatial(w, yxs_dummy['office'].values))
yxs = yxs.assign(w_school=pysal.lag_spatial(w, yxs_dummy['school'].values))
yxs = yxs.assign(w_auto_shop=pysal.lag_spatial(w, yxs_dummy['auto_shop'].values))
yxs = yxs.assign(w_religious=pysal.lag_spatial(w, yxs_dummy['religious'].values))
yxs = yxs.assign(w_food=pysal.lag_spatial(w, yxs_dummy['food'].values))
yxs = yxs.assign(w_charitable=pysal.lag_spatial(w, yxs_dummy['charitable'].values))