当前位置: 首页>>代码示例>>Python>>正文


Python KDTree.sparse_distance_matrix方法代码示例

本文整理汇总了Python中pysal.common.KDTree.sparse_distance_matrix方法的典型用法代码示例。如果您正苦于以下问题:Python KDTree.sparse_distance_matrix方法的具体用法?Python KDTree.sparse_distance_matrix怎么用?Python KDTree.sparse_distance_matrix使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pysal.common.KDTree的用法示例。


在下文中一共展示了KDTree.sparse_distance_matrix方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: DistanceBand

# 需要导入模块: from pysal.common import KDTree [as 别名]
# 或者: from pysal.common.KDTree import sparse_distance_matrix [as 别名]

#.........这里部分代码省略.........
    >>> w.weights
    {0: [1, 1], 1: [1, 1, 1], 2: [1], 3: [1, 1], 4: [1, 1, 1], 5: [1]}
    >>> w.neighbors
    {0: [1, 3], 1: [0, 3, 4], 2: [4], 3: [0, 1], 4: [1, 2, 5], 5: [4]}

    inverse distance weights

    >>> w=DistanceBand(points,threshold=11.2,binary=False)
    WARNING: there is one disconnected observation (no neighbors)
    Island id:  [2]
    >>> w.weights[0]
    [0.10000000000000001, 0.089442719099991588]
    >>> w.neighbors[0]
    [1, 3]
    >>>

    gravity weights

    >>> w=DistanceBand(points,threshold=11.2,binary=False,alpha=-2.)
    WARNING: there is one disconnected observation (no neighbors)
    Island id:  [2]
    >>> w.weights[0]
    [0.01, 0.0079999999999999984]

    Notes
    -----

    this was initially implemented running scipy 0.8.0dev (in epd 6.1).
    earlier versions of scipy (0.7.0) have a logic bug in scipy/sparse/dok.py
    so serge changed line 221 of that file on sal-dev to fix the logic bug

    """
    def __init__(self, data, threshold, p=2, alpha=-1.0, binary=True, ids=None):
        """
        Casting to floats is a work around for a bug in scipy.spatial.  See detail in pysal issue #126
        """
        if issubclass(type(data), scipy.spatial.KDTree):
            self.kd = data
            self.data = self.kd.data
        else:
            try:
                data = np.asarray(data)
                if data.dtype.kind != 'f':
                    data = data.astype(float)
                self.data = data
                self.kd = KDTree(self.data)
            except:
                raise ValueError("Could not make array from data")

        self.p = p
        self.threshold = threshold
        self.binary = binary
        self.alpha = alpha
        self._band()
        neighbors, weights = self._distance_to_W(ids)
        W.__init__(self, neighbors, weights, ids)

    def _band(self):
        """
        find all pairs within threshold
        """
        kd = self.kd
        #ns=[kd.query_ball_point(point,self.threshold) for point in self.data]
        ns = kd.query_ball_tree(kd, self.threshold)
        self._nmat = ns

    def _distance_to_W(self, ids=None):
        allneighbors = {}
        weights = {}
        if ids:
            ids = np.array(ids)
        else:
            ids = np.arange(len(self._nmat))
        if self.binary:
            for i, neighbors in enumerate(self._nmat):
                ns = [ni for ni in neighbors if ni != i]
                neigh = list(ids[ns])
                if len(neigh) == 0:
                    allneighbors[ids[i]] = []
                    weights[ids[i]] = []
                else:
                    allneighbors[ids[i]] = neigh
                    weights[ids[i]] = [1] * len(ns)
        else:
            self.dmat = self.kd.sparse_distance_matrix(
                self.kd, max_distance=self.threshold)
            for i, neighbors in enumerate(self._nmat):
                ns = [ni for ni in neighbors if ni != i]
                neigh = list(ids[ns])
                if len(neigh) == 0:
                    allneighbors[ids[i]] = []
                    weights[ids[i]] = []
                else:
                    try:
                        allneighbors[ids[i]] = neigh
                        weights[ids[i]] = [self.dmat[(
                            i, j)] ** self.alpha for j in ns]
                    except ZeroDivisionError, e:
                        print(e, "Cannot compute inverse distance for elements at same location (distance=0).")
        return allneighbors, weights
开发者ID:cheneason,项目名称:pysal,代码行数:104,代码来源:Distance.py

示例2: DistanceBand

# 需要导入模块: from pysal.common import KDTree [as 别名]
# 或者: from pysal.common.KDTree import sparse_distance_matrix [as 别名]

#.........这里部分代码省略.........
        Returns
        --------
        Kernel Weights Object

        See Also
        ---------
        :class: `pysal.weights.DistanceBand`
        :class: `pysal.weights.W`
        """
        points = get_points_array_from_shapefile(filepath)
        if idVariable is not None:
            ids = get_ids(filepath, idVariable)
        else:
            ids = None
        return cls.from_array(points, threshold, ids=ids, **kwargs)
    
    @classmethod
    def from_array(cls, array, threshold, **kwargs):
        """
        Construct a DistanceBand weights from an array. Supports all the same options
        as :class:`pysal.weights.DistanceBand`

        See Also
        --------
        :class:`pysal.weights.DistanceBand`
        :class:`pysal.weights.W`
        """
        return cls(array, threshold, **kwargs)
    
    @classmethod
    def from_dataframe(cls, df, threshold, geom_col='geometry', ids=None, **kwargs):
        """
        Make DistanceBand weights from a dataframe.

        Parameters
        ----------
        df      :   pandas.dataframe
                    a dataframe with a geometry column that can be used to
                    construct a W object
        geom_col :   string
                    column name of the geometry stored in df
        ids     :   string or iterable
                    if string, the column name of the indices from the dataframe
                    if iterable, a list of ids to use for the W
                    if None, df.index is used.

        See Also
        --------
        :class:`pysal.weights.DistanceBand`
        :class:`pysal.weights.W`
        """
        pts = get_points_array(df[geom_col])
        if ids is None:
            ids = df.index.tolist()
        elif isinstance(ids, str):
            ids = df[ids].tolist()
        return cls(pts, threshold, ids=ids, **kwargs)

    def _band(self):
        """Find all pairs within threshold.

        """
        if self.build_sp:    
            self.dmat = self.kd.sparse_distance_matrix(
                    self.kd, max_distance=self.threshold).tocsr()
        else:
            if str(self.kd).split('.')[-1][0:10] == 'Arc_KDTree':
            	raise TypeError('Unable to calculate dense arc distance matrix;'
            	        ' parameter "build_sp" must be set to True for arc'
            	        ' distance type weight')
            self.dmat = self._spdistance_matrix(self.data, self.data, self.threshold)


    def _distance_to_W(self, ids=None):
        if self.binary:
            self.dmat[self.dmat>0] = 1
            self.dmat.eliminate_zeros()
            tempW = WSP2W(WSP(self.dmat), silent_island_warning=self.silent)
            neighbors = tempW.neighbors
            weight_keys = tempW.weights.keys()
            weight_vals = tempW.weights.values()
            weights = dict(zip(weight_keys, map(list, weight_vals)))
            return neighbors, weights
        else:
            weighted = self.dmat.power(self.alpha)
            weighted[weighted==np.inf] = 0
            weighted.eliminate_zeros()
            tempW = WSP2W(WSP(weighted), silent_island_warning=self.silent)
            neighbors = tempW.neighbors
            weight_keys = tempW.weights.keys()
            weight_vals = tempW.weights.values()
            weights = dict(zip(weight_keys, map(list, weight_vals)))
            return neighbors, weights

    def _spdistance_matrix(self, x,y, threshold=None):
        dist = distance_matrix(x,y)
        if threshold is not None:
            zeros = dist > threshold
            dist[zeros] = 0
        return sp.csr_matrix(dist)
开发者ID:lanselin,项目名称:pysal,代码行数:104,代码来源:Distance.py

示例3: DistanceBand

# 需要导入模块: from pysal.common import KDTree [as 别名]
# 或者: from pysal.common.KDTree import sparse_distance_matrix [as 别名]

#.........这里部分代码省略.........
    Island id:  [2]
    >>> w.weights
    {0: [1, 1], 1: [1, 1], 2: [], 3: [1, 1], 4: [1], 5: [1]}
    >>> w.neighbors
    {0: [1, 3], 1: [0, 3], 2: [], 3: [1, 0], 4: [5], 5: [4]}
    >>> w=DistanceBand(points,threshold=14.2)
    >>> w.weights
    {0: [1, 1], 1: [1, 1, 1], 2: [1], 3: [1, 1], 4: [1, 1, 1], 5: [1]}
    >>> w.neighbors
    {0: [1, 3], 1: [0, 3, 4], 2: [4], 3: [1, 0], 4: [5, 1, 2], 5: [4]}

    inverse distance weights

    >>> w=DistanceBand(points,threshold=11.2,binary=False)
    WARNING: there is one disconnected observation (no neighbors)
    Island id:  [2]
    >>> w.weights[0]
    [0.10000000000000001, 0.089442719099991588]
    >>> w.neighbors[0]
    [1, 3]
    >>>

    gravity weights

    >>> w=DistanceBand(points,threshold=11.2,binary=False,alpha=-2.)
    WARNING: there is one disconnected observation (no neighbors)
    Island id:  [2]
    >>> w.weights[0]
    [0.01, 0.0079999999999999984]

    Notes
    -----

    This was initially implemented running scipy 0.8.0dev (in epd 6.1).
    earlier versions of scipy (0.7.0) have a logic bug in scipy/sparse/dok.py
    so serge changed line 221 of that file on sal-dev to fix the logic bug.

    """

    def __init__(self, data, threshold, p=2, alpha=-1.0, binary=True, ids=None):
        """Casting to floats is a work around for a bug in scipy.spatial.
        See detail in pysal issue #126.

        """
        if issubclass(type(data), scipy.spatial.KDTree):
            self.kd = data
            self.data = self.kd.data
        else:
            try:
                data = np.asarray(data)
                if data.dtype.kind != 'f':
                    data = data.astype(float)
                self.data = data
                self.kd = KDTree(self.data)
            except:
                raise ValueError("Could not make array from data")

        self.p = p
        self.threshold = threshold
        self.binary = binary
        self.alpha = alpha
        self._band()
        neighbors, weights = self._distance_to_W(ids)
        W.__init__(self, neighbors, weights, ids)

    def _band(self):
        """Find all pairs within threshold.

        """
        self.dmat = self.kd.sparse_distance_matrix(
                self.kd, max_distance=self.threshold)

    def _distance_to_W(self, ids=None):
        if ids:
            ids = np.array(ids)
        else:
            ids = np.arange(self.dmat.shape[0])
        neighbors = dict([(i,[]) for i in ids])
        weights = dict([(i,[]) for i in ids])
        if self.binary:
            for key,weight in self.dmat.items():
                i,j = key
                if j not in neighbors[i]:
                    weights[i].append(1)
                    neighbors[i].append(j)
                if i not in neighbors[j]:
                    weights[j].append(1)
                    neighbors[j].append(i)

        else:
            for key,weight in self.dmat.items():
                i,j = key
                if j not in neighbors[i]:
                    weights[i].append(weight**self.alpha)
                    neighbors[i].append(j)
                if i not in neighbors[j]:
                    weights[j].append(weight**self.alpha)
                    neighbors[j].append(i)

        return neighbors, weights
开发者ID:umutturk,项目名称:pysal,代码行数:104,代码来源:Distance.py


注:本文中的pysal.common.KDTree.sparse_distance_matrix方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。