Skip to content

Commit d52d684

Browse files
committed
exponential kernel
1 parent 98d4cee commit d52d684

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

libpysal/graph/_kernel.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ def _cosine(distances, bandwidth):
4747
return (numpy.pi / 4) * numpy.cos(numpy.pi / 2 * u)
4848

4949

50+
def _exponential(distances, bandwidth):
51+
u = distances / bandwidth
52+
return numpy.exp(-u)
53+
54+
5055
def _boxcar(distances, bandwidth):
5156
r = (distances < bandwidth).astype(int)
5257
return r
@@ -64,6 +69,7 @@ def _identity(distances, _):
6469
"cosine": _cosine,
6570
"boxcar": _boxcar,
6671
"discrete": _boxcar,
72+
"exponential": _exponential,
6773
"identity": _identity,
6874
None: _identity,
6975
}
@@ -106,6 +112,7 @@ def _kernel(
106112
- gaussian:
107113
- bisquare:
108114
- cosine:
115+
- exponential:
109116
- boxcar/discrete: all distances less than `bandwidth` are 1, and all
110117
other distances are 0
111118
- identity/None : do nothing, weight similarity based on raw distance

libpysal/graph/tests/test_kernel.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ def test_kernels(kernel, grocs):
224224
elif kernel in ["identity", None]:
225225
assert weight.mean() == pytest.approx(39758.007361814016)
226226
assert weight.max() == pytest.approx(127937.75271993055)
227+
elif kernel in ["exponential", None]:
228+
assert weight.mean() == pytest.approx(0.25104208195691335)
229+
assert weight.max() == pytest.approx(0.9875261386315732)
227230
else: # function
228231
assert weight.mean() == pytest.approx(0.6880384553732511)
229232
assert weight.max() == pytest.approx(0.9855481738848647)

0 commit comments

Comments
 (0)