Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
OLS
✓ from spreg import OLS
✗ from spreg.ols import OLS
Top-level import is the correct pattern
GM_Lag
✓ from spreg import GM_Lag
✗ from spreg.GM import GM_Lag
GM_Lag is a top-level class, not in a submodule
ML_Error
✓ from spreg import ML_Error
✗ from spreg.ML import ML_Error
ML classes are at top-level
spsearch
✓ from spreg import spsearch
Added in v1.8.0
Basic OLS with spatial diagnostics using random data and distance-based weights.
import numpy as np
import libpysal
from spreg import OLS
# Generate random data
np.random.seed(123)
n = 100
x = np.random.randn(n, 2)
y = 1 + 2*x[:,0] + 3*x[:,1] + np.random.randn(n)*0.5
# Create spatial weights (Queen contiguity from random points)
pts = np.random.rand(n, 2)
w = libpysal.weights.DistanceBand.from_array(pts, threshold=0.5, binary=True)
w.transform = 'r'
# Fit OLS
m1 = OLS(y, x, w=w, spat_diag=True)
print(m1.summary)
Errors
Common errors & fixes
ImportError: cannot import name 'OLS' from 'spreg.ols'
Incorrect import path for OLS class.
fixUse: from spreg import OLS
ValueError: The weights matrix must be a W object.
Passed a raw scipy sparse matrix or numpy array instead of a libpysal W object.
fixConvert your weights using libpysal.weights.W(weights_matrix) or use libpysal.weights to read shapefiles.
TypeError: regimes must be a pandas Categorical or a list of strings with length equal to the number of observations.
Regimes argument is not properly formatted.
fixEnsure regimes is either a pandas Categorical or a list of strings of length n. Example: regimes = pd.Categorical(['a','b']*50)
Upgrade
Version history
1.9.0latest on PyPI · released Mar 24, 2026
Audit
Dependencies
libpysalrequiredProvides spatial weights (W) and other spatial utilities
pandasrequiredData I/O and DataFrame support
numpyrequiredNumerical computation
scipyrequiredSparse matrix operations and optimization