libpysal.weights.
Kernel
(data, bandwidth=None, fixed=True, k=2, function='triangular', eps=1.0000001, ids=None, diagonal=False, distance_metric='euclidean', radius=None, **kwargs)[source]¶Spatial weights based on kernel functions.
(n,k) or KDTree where KDtree.data is array (n,k) n observations on k characteristics used to measure distances between the n objects
or array-like (optional) the bandwidth \(h_i\) for the kernel.
If true then \(h_i=h \forall i\). If false then bandwidth is adaptive across observations.
the number of nearest neighbors to use for determining bandwidth. For fixed bandwidth, \(h_i=max(dknn) \forall i\) where \(dknn\) is a vector of k-nearest neighbor distances (the distance to the kth nearest neighbor for each observation). For adaptive bandwidths, \(h_i=dknn_i\)
If true, set diagonal weights = 1.0, if false (default), diagonals weights are set to value according to kernel function.
kernel function defined as follows with
triangular
uniform
quadratic
quartic
gaussian
adjustment to ensure knn distance range is closed on the knnth observations
Examples
>>> from libpysal.weights import Kernel
>>> points=[(10, 10), (20, 10), (40, 10), (15, 20), (30, 20), (30, 30)]
>>> kw=Kernel(points)
>>> kw.weights[0]
[1.0, 0.500000049999995, 0.4409830615267465]
>>> 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.6, 0.552786404500042, 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.99999900663795e-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.2674190291577696, 0.2419707487162134]
>>> kweag.bandwidth
array([[11.18034101],
[11.18034101],
[20.000002 ],
[11.18034101],
[14.14213704],
[18.02775818]])
Diagonals to 1.0
>>> kq = Kernel(points,function='gaussian')
>>> kq.weights
{0: [0.3989422804014327, 0.35206533556593145, 0.3412334260702758], 1: [0.35206533556593145, 0.3989422804014327, 0.2419707487162134, 0.3412334260702758, 0.31069657591175387], 2: [0.2419707487162134, 0.3989422804014327, 0.31069657591175387], 3: [0.3412334260702758, 0.3412334260702758, 0.3989422804014327, 0.3011374490937829, 0.26575287272131043], 4: [0.31069657591175387, 0.31069657591175387, 0.3011374490937829, 0.3989422804014327, 0.35206533556593145], 5: [0.26575287272131043, 0.35206533556593145, 0.3989422804014327]}
>>> kqd = Kernel(points, function='gaussian', diagonal=True)
>>> kqd.weights
{0: [1.0, 0.35206533556593145, 0.3412334260702758], 1: [0.35206533556593145, 1.0, 0.2419707487162134, 0.3412334260702758, 0.31069657591175387], 2: [0.2419707487162134, 1.0, 0.31069657591175387], 3: [0.3412334260702758, 0.3412334260702758, 1.0, 0.3011374490937829, 0.26575287272131043], 4: [0.31069657591175387, 0.31069657591175387, 0.3011374490937829, 1.0, 0.35206533556593145], 5: [0.26575287272131043, 0.35206533556593145, 1.0]}
Dictionary keyed by id with a list of weights for each neighbor
of lists of neighbors keyed by observation id
array of bandwidths
Methods
|
Asymmetry check. |
|
Return an adjacency list representation of a weights object. |
|
Construct a Kernel weights from an array. |
|
Make Kernel weights from a dataframe. |
|
Convert a networkx graph to a PySAL W object. |
|
Kernel based weights from shapefile |
|
Generate a full numpy array. |
|
Getter for transform property. |
|
Plot spatial weights objects. |
|
In place modification throughout W of id values from w.id_order to new_ids in all |
|
Adding meta data for writing headers of gal and gwt files. |
|
Transformations of weights. |
|
Construct a symmetric KNN weight. |
|
Generate a WSP object. |
|
Compute an adjacency list representation of a weights object. |
|
Convert a weights object to a networkx graph |
from_WSP |
|
from_file |
__init__
(self, data, bandwidth=None, fixed=True, k=2, function='triangular', eps=1.0000001, ids=None, diagonal=False, distance_metric='euclidean', radius=None, **kwargs)[source]¶Initialize self. See help(type(self)) for accurate signature.
Methods
|
Initialize self. |
|
Asymmetry check. |
|
|
|
Return an adjacency list representation of a weights object. |
|
Construct a Kernel weights from an array. |
|
Make Kernel weights from a dataframe. |
|
|
|
Convert a networkx graph to a PySAL W object. |
|
Kernel based weights from shapefile |
|
Generate a full numpy array. |
|
Getter for transform property. |
|
Plot spatial weights objects. |
|
In place modification throughout W of id values from w.id_order to new_ids in all |
|
Adding meta data for writing headers of gal and gwt files. |
|
Transformations of weights. |
|
Construct a symmetric KNN weight. |
|
Generate a WSP object. |
|
Compute an adjacency list representation of a weights object. |
|
Convert a weights object to a networkx graph |
Attributes
|
List of id pairs with asymmetric weights. |
|
Number of neighbors for each observation. |
|
Store the graph component in which each observation falls. |
|
Diagonal of \(WW\). |
|
Diagonal of \(W^{'}W\). |
|
Diagonal of \(W^{'}W + WW\). |
|
Cardinality histogram as a dictionary where key is the id and value is the number of neighbors for that unit. |
|
Dictionary where the key is an ID and the value is that ID’s index in W.id_order. |
|
Returns the ids for the observations in the order in which they would be encountered if iterating over the weights. |
|
Returns True if user has set id_order, False if not. |
|
List of ids without any neighbors. |
|
Largest number of neighbors. |
|
Average number of neighbors. |
|
Minimum number of neighbors. |
|
Number of units. |
|
Store whether the adjacency matrix is fully connected. |
|
Given the current id_order, neighbor_offsets[id] is the offsets of the id’s neighbors in id_order. |
|
Number of nonzero weights. |
|
Percentage of nonzero weights. |
|
s0 is defined as |
|
s1 is defined as |
|
s2 is defined as |
|
Individual elements comprising s2. |
|
Standard deviation of number of neighbors. |
|
Sparse matrix object. |
|
Getter for transform property. |
|
Trace of \(WW\). |
|
Trace of \(W^{'}W\). |
|
Trace of \(W^{'}W + WW\). |