libpysal.weights.Kernel¶
-
class
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.
- Parameters
- dataarray
(n,k) or KDTree where KDtree.data is array (n,k) n observations on k characteristics used to measure distances between the n objects
- bandwidthfloat
or array-like (optional) the bandwidth \(h_i\) for the kernel.
- fixedbinary
If true then \(h_i=h \forall i\). If false then bandwidth is adaptive across observations.
- kint
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\)
- diagonalboolean
If true, set diagonal weights = 1.0, if false (default), diagonals weights are set to value according to kernel function.
- function{‘triangular’,’uniform’,’quadratic’,’quartic’,’gaussian’}
kernel function defined as follows with
\[z_{i,j} = d_{i,j}/h_i\]triangular
\[K(z) = (1 - |z|) \ if |z| \le 1\]uniform
\[K(z) = 1/2 \ if |z| \le 1\]quadratic
\[K(z) = (3/4)(1-z^2) \ if |z| \le 1\]quartic
\[K(z) = (15/16)(1-z^2)^2 \ if |z| \le 1\]gaussian
\[K(z) = (2\pi)^{(-1/2)} exp(-z^2 / 2)\]- epsfloat
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]}
- Attributes
- weightsdict
Dictionary keyed by id with a list of weights for each neighbor
- neighborsdict
of lists of neighbors keyed by observation id
- bandwidtharray
array of bandwidths
-
__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
__init__(self, data[, bandwidth, fixed, k, …])Initialize self.
asymmetry(self[, intrinsic])Asymmetry check.
from_WSP(WSP[, silence_warnings])from_adjlist(adjlist[, focal_col, …])Return an adjacency list representation of a weights object.
from_array(array, \*\*kwargs)Construct a Kernel weights from an array.
from_dataframe(df[, geom_col, ids])Make Kernel weights from a dataframe.
from_file([path, format])from_networkx(graph[, weight_col])Convert a networkx graph to a PySAL W object.
from_shapefile(filepath[, idVariable])Kernel based weights from shapefile
full(self)Generate a full numpy array.
get_transform(self)Getter for transform property.
plot(self, gdf[, indexed_on, ax, color, …])Plot spatial weights objects.
remap_ids(self, new_ids)In place modification throughout W of id values from w.id_order to new_ids in all
set_shapefile(self, shapefile[, idVariable, …])Adding meta data for writing headers of gal and gwt files.
set_transform(self[, value])Transformations of weights.
symmetrize(self[, inplace])Construct a symmetric KNN weight.
to_WSP(self)Generate a WSP object.
to_adjlist(self[, remove_symmetric, …])Compute an adjacency list representation of a weights object.
to_networkx(self)Convert a weights object to a networkx graph
Attributes
asymmetriesList of id pairs with asymmetric weights.
cardinalitiesNumber of neighbors for each observation.
component_labelsStore the graph component in which each observation falls.
diagW2Diagonal of \(WW\).
diagWtWDiagonal of \(W^{'}W\).
diagWtW_WWDiagonal of \(W^{'}W + WW\).
histogramCardinality histogram as a dictionary where key is the id and value is the number of neighbors for that unit.
id2iDictionary where the key is an ID and the value is that ID’s index in W.id_order.
id_orderReturns the ids for the observations in the order in which they would be encountered if iterating over the weights.
id_order_setReturns True if user has set id_order, False if not.
islandsList of ids without any neighbors.
max_neighborsLargest number of neighbors.
mean_neighborsAverage number of neighbors.
min_neighborsMinimum number of neighbors.
nNumber of units.
n_componentsStore whether the adjacency matrix is fully connected.
neighbor_offsetsGiven the current id_order, neighbor_offsets[id] is the offsets of the id’s neighbors in id_order.
nonzeroNumber of nonzero weights.
pct_nonzeroPercentage of nonzero weights.
s0s0 is defined as
s1s1 is defined as
s2s2 is defined as
s2arrayIndividual elements comprising s2.
sdStandard deviation of number of neighbors.
sparseSparse matrix object.
transformGetter for transform property.
trcW2Trace of \(WW\).
trcWtWTrace of \(W^{'}W\).
trcWtW_WWTrace of \(W^{'}W + WW\).
-
classmethod
from_array(array, **kwargs)[source]¶ Construct a Kernel weights from an array. Supports all the same options as
libpysal.weights.KernelSee also
libpysal.weights.weights.W
-
classmethod
from_dataframe(df, geom_col='geometry', ids=None, **kwargs)[source]¶ Make Kernel weights from a dataframe.
- Parameters
- dfpandas.dataframe
a dataframe with a geometry column that can be used to construct a W object
- geom_colstring
column name of the geometry stored in df
- idsstring 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
libpysal.weights.weights.W