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 isKDTree(data): 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 __init__(self, data, threshold, p=2, alpha=-1.0, binary=True, ids=None, build_sp=True, silent=False): """Casting to floats is a work around for a bug in scipy.spatial. See detail in pysal issue #126. """ self.p = p self.threshold = threshold self.binary = binary self.alpha = alpha self.build_sp = build_sp self.silent = silent if isKDTree(data): self.kd = data self.data = self.kd.data else: if self.build_sp: 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") else: self.data = data self.kd = None self._band() neighbors, weights = self._distance_to_W(ids) W.__init__(self, neighbors, weights, ids, silent_island_warning=self.silent)
def __init__(self, data, bandwidth=None, fixed=True, k=2, function='triangular', eps=1.0000001, ids=None, diagonal=False): if isKDTree(data): self.kdt = data self.data = self.kdt.data data = self.data else: self.data = data self.kdt = KDTree(self.data) self.k = k + 1 self.function = function.lower() self.fixed = fixed self.eps = eps if bandwidth: try: bandwidth = np.array(bandwidth) bandwidth.shape = (len(bandwidth), 1) except: bandwidth = np.ones((len(data), 1), 'float') * bandwidth self.bandwidth = bandwidth else: self._set_bw() self._eval_kernel() neighbors, weights = self._k_to_W(ids) if diagonal: for i in neighbors: weights[i][neighbors[i].index(i)] = 1.0 W.__init__(self, neighbors, weights, ids)
def __init__(self, data, k=2, p=2, ids=None, radius=None, distance_metric='euclidean'): if isKDTree(data): self.kdtree = data self.data = data.data else: self.data = data self.kdtree = KDTree(data, radius=radius, distance_metric=distance_metric) self.k = k self.p = p this_nnq = self.kdtree.query(self.data, k=k + 1, p=p) to_weight = this_nnq[1] if ids is None: ids = list(range(to_weight.shape[0])) neighbors = {} for i, row in enumerate(to_weight): row = row.tolist() row.remove(i) row = [ids[j] for j in row] focal = ids[i] neighbors[focal] = row W.__init__(self, neighbors, id_order=ids)
def __init__(self, data, k=2, p=2, ids=None, radius=None, distance_metric='euclidean'): if isKDTree(data): self.kdtree = data self.data = data.data else: self.data = data self.kdtree = KDTree(data, radius=radius, distance_metric=distance_metric) self.k = k self.p = p this_nnq = self.kdtree.query(self.data, k=k+1, p=p) to_weight = this_nnq[1] if ids is None: ids = list(range(to_weight.shape[0])) neighbors = {} for i,row in enumerate(to_weight): row = row.tolist() row.remove(i) row = [ids[j] for j in row] focal = ids[i] neighbors[focal] = row W.__init__(self, neighbors, id_order=ids)
def knnW(data, k=2, p=2, ids=None): """ Creates nearest neighbor weights matrix based on k nearest neighbors. Parameters ---------- kdtree : object PySAL KDTree or ArcKDTree where KDtree.data is array (n,k) n observations on k characteristics used to measure distances between the n objects k : int number of nearest neighbors p : float Minkowski p-norm distance metric parameter: 1<=p<=infinity 2: Euclidean distance 1: Manhattan distance Ignored if the KDTree is an ArcKDTree ids : list identifiers to attach to each observation Returns ------- w : W instance Weights object with binary weights Examples -------- >>> points = [(10, 10), (20, 10), (40, 10), (15, 20), (30, 20), (30, 30)] >>> kd = pysal.cg.kdtree.KDTree(np.array(points)) >>> wnn2 = pysal.knnW(kd, 2) >>> [1,3] == wnn2.neighbors[0] True ids >>> wnn2 = knnW(kd,2) >>> wnn2[0] {1: 1.0, 3: 1.0} >>> wnn2[1] {0: 1.0, 3: 1.0} now with 1 rather than 0 offset >>> wnn2 = knnW(kd, 2, ids=range(1,7)) >>> wnn2[1] {2: 1.0, 4: 1.0} >>> wnn2[2] {1: 1.0, 4: 1.0} >>> 0 in wnn2.neighbors False Notes ----- Ties between neighbors of equal distance are arbitrarily broken. See Also -------- pysal.weights.W """ if isKDTree(data): kdt = data data = kdt.data else: kdt = KDTree(data) nnq = kdt.query(data, k=k + 1, p=p) info = nnq[1] neighbors = {} for i, row in enumerate(info): row = row.tolist() if i in row: row.remove(i) focal = i if ids: row = [ids[j] for j in row] focal = ids[i] neighbors[focal] = row return pysal.weights.W(neighbors, id_order=ids)
def knnW(data, k=2, p=2, ids=None): """ Creates nearest neighbor weights matrix based on k nearest neighbors. Parameters ---------- kdtree : object PySAL KDTree or ArcKDTree where KDtree.data is array (n,k) n observations on k characteristics used to measure distances between the n objects k : int number of nearest neighbors p : float Minkowski p-norm distance metric parameter: 1<=p<=infinity 2: Euclidean distance 1: Manhattan distance Ignored if the KDTree is an ArcKDTree ids : list identifiers to attach to each observation Returns ------- w : W instance Weights object with binary weights Examples -------- >>> points = [(10, 10), (20, 10), (40, 10), (15, 20), (30, 20), (30, 30)] >>> kd = pysal.cg.kdtree.KDTree(np.array(points)) >>> wnn2 = pysal.knnW(kd, 2) >>> [1,3] == wnn2.neighbors[0] True ids >>> wnn2 = knnW(kd,2) >>> wnn2[0] {1: 1.0, 3: 1.0} >>> wnn2[1] {0: 1.0, 3: 1.0} now with 1 rather than 0 offset >>> wnn2 = knnW(kd, 2, ids=range(1,7)) >>> wnn2[1] {2: 1.0, 4: 1.0} >>> wnn2[2] {1: 1.0, 4: 1.0} >>> 0 in wnn2.neighbors False Notes ----- Ties between neighbors of equal distance are arbitrarily broken. See Also -------- pysal.weights.W """ if isKDTree(data): kdt = data data = kdt.data else: kdt = KDTree(data) nnq = kdt.query(data, k=k+1, p=p) info = nnq[1] neighbors = {} for i, row in enumerate(info): row = row.tolist() if i in row: row.remove(i) focal = i if ids: row = [ ids[j] for j in row] focal = ids[i] neighbors[focal] = row return pysal.weights.W(neighbors, id_order=ids)