The weights.Distance module provides for spatial weights defined on distance relationships.
New in version 1.0.
Distance based spatial weights
Spatial weights based on distance band
| Parameters: | data : array (n,m)
threshold : float
p : float
binary : binary
alpha : float
|
|---|
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
Examples
>>> points=[(10, 10), (20, 10), (40, 10), (15, 20), (30, 20), (30, 30)]
>>> w=DistanceBand(points,threshold=11.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: [0, 1], 4: [5], 5: [4]}
inverse distance weights
>>> w=DistanceBand(points,threshold=11.2,binary=False)
>>> w.weights[0]
[0.10000000000000001, 0.089442719099991588]
>>> w.neighbors[0]
[1, 3]
>>>
gravity weights
>>> w=DistanceBand(points,threshold=11.2,binary=False,alpha=-2.)
>>> w.weights[0]
[0.01, 0.0079999999999999984]
Methods
| asymmetry | |
| full | |
| get_transform | |
| higher_order | |
| order | |
| set_transform | |
| shimbel |
Spatial weights based on kernel functions
| Parameters: | data : array (n,k)
bandwidth : float or array-like (optional)
fixed : binary
k : int
function : string {‘triangular’,’uniform’,’quadratic’,’quartic’,’gaussian’}
eps : float
|
|---|
Examples
>>> points=[(10, 10), (20, 10), (40, 10), (15, 20), (30, 20), (30, 30)]
>>> kw=Kernel(points)
>>> kw.weights[0]
[1.0, 0.50000004999999503, 0.44098306152674649]
>>> kw.neighbors[0]
[0, 1, 3]
>>> kw.bandwidth
array([[ 20.000002],
[ 20.000002],
[ 20.000002],
[ 20.000002],
[ 20.000002],
[ 20.000002]])
>>> kw15=Kernel(points,bandwidth=15.0)
>>> kw15[0]
{0: 1.0, 1: 0.33333333333333337, 3: 0.2546440075000701}
>>> kw15.neighbors[0]
[0, 1, 3]
>>> kw15.bandwidth
array([[ 15.],
[ 15.],
[ 15.],
[ 15.],
[ 15.],
[ 15.]])
Adaptive bandwidths user specified
>>> bw=[25.0,15.0,25.0,16.0,14.5,25.0]
>>> kwa=Kernel(points,bandwidth=bw)
>>> kwa.weights[0]
[1.0, 0.59999999999999998, 0.55278640450004202, 0.10557280900008403]
>>> kwa.neighbors[0]
[0, 1, 3, 4]
>>> kwa.bandwidth
array([[ 25. ],
[ 15. ],
[ 25. ],
[ 16. ],
[ 14.5],
[ 25. ]])
Endogenous adaptive bandwidths
>>> kwea=Kernel(points,fixed=False)
>>> kwea.weights[0]
[1.0, 0.10557289844279438, 9.9999990066379496e-08]
>>> kwea.neighbors[0]
[0, 1, 3]
>>> kwea.bandwidth
array([[ 11.18034101],
[ 11.18034101],
[ 20.000002 ],
[ 11.18034101],
[ 14.14213704],
[ 18.02775818]])
Endogenous adaptive bandwidths with Gaussian kernel
>>> kweag=Kernel(points,fixed=False,function='gaussian')
>>> kweag.weights[0]
[0.3989422804014327, 0.26741902915776961, 0.24197074871621341]
>>> kweag.bandwidth
array([[ 11.18034101],
[ 11.18034101],
[ 20.000002 ],
[ 11.18034101],
[ 14.14213704],
[ 18.02775818]])
Methods
| asymmetry | |
| full | |
| get_transform | |
| higher_order | |
| order | |
| set_transform | |
| shimbel |
Creates contiguity matrix based on k nearest neighbors
| Parameters: | point_array : multitype
k : int
p : float
ids : list
Returns : ——- : w : W instance
|
|---|
See also
Notes
Ties between neighbors of equal distance are arbitrarily broken.
Examples
>>> x,y=np.indices((5,5))
>>> x.shape=(25,1)
>>> y.shape=(25,1)
>>> data=np.hstack([x,y])
>>> wnn2=knnW(data,k=2)
>>> wnn4=knnW(data,k=4)
>>> wnn4.neighbors[0]
[1, 5, 6, 2]
>>> wnn4.neighbors[5]
[0, 6, 10, 1]
>>> wnn2.neighbors[0]
[1, 5]
>>> wnn2.neighbors[5]
[0, 6]
>>> wnn2.pct_nonzero
0.080000000000000002
>>> wnn4.pct_nonzero
0.16
>>> wnn3e=knnW(data,p=2,k=3)
>>> wnn3e.neighbors[0]
[1, 5, 6]
>>> wnn3m=knnW(data,p=1,k=3)
>>> wnn3m.neighbors[0]
[1, 5, 2]