'spatial regression in Python - read matrix from list

I have a following problem. I am following this example about spatial regression in Python:

import numpy
import libpysal
import spreg
import pickle



# Read spatial data
ww = libpysal.io.open(libpysal.examples.get_path("baltim_q.gal"))
w = ww.read()
ww.close()
w_name = "baltim_q.gal"
w.transform = "r"

Example above works. But I would like to read my own spatial matrix which I have now as a list of lists. See my approach:

ww = libpysal.io.open(matrix)

But I got this error message:

Traceback (most recent call last):
  File "/usr/lib/python3.8/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "/home/vojta/Desktop/INTERNET_HANDEL/ZASILKOVNA/optimal-delivery-branches/venv/lib/python3.8/site-packages/libpysal/io/fileio.py", line 90, in __new__
    cls.__registry[cls.getType(dataPath, mode, dataFormat)][mode][0]
  File "/home/vojta/Desktop/INTERNET_HANDEL/ZASILKOVNA/optimal-delivery-branches/venv/lib/python3.8/site-packages/libpysal/io/fileio.py", line 105, in getType
    ext = os.path.splitext(dataPath)[1]
  File "/usr/lib/python3.8/posixpath.py", line 118, in splitext
    p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not list

this is how matrix looks like:

[[0, 2, 1], [2, 0, 4], [1, 4, 0]]

EDIT: If I try to insert my matrix into the GM_Lag like this:

model = spreg.GM_Lag(
    y,
    X,
    w=matrix,
)

I got following error:

warn("w must be API-compatible pysal weights object")
Traceback (most recent call last):
  File "/usr/lib/python3.8/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 2, in <module>
  File "/home/vojta/Desktop/INTERNET_HANDEL/ZASILKOVNA/optimal-delivery-branches/venv/lib/python3.8/site-packages/spreg/twosls_sp.py", line 469, in __init__
    USER.check_weights(w, y, w_required=True)
  File "/home/vojta/Desktop/INTERNET_HANDEL/ZASILKOVNA/optimal-delivery-branches/venv/lib/python3.8/site-packages/spreg/user_output.py", line 444, in check_weights
    if w.n != y.shape[0] and time == False:
AttributeError: 'list' object has no attribute 'n'

EDIT 2:

This is how I read the list of lists:

import pickle

with open("weighted_matrix.pkl", "rb") as f:
    matrix = pickle.load(f)

How can I insert list of lists into spreg.GM_Lag ? Thanks



Solution 1:[1]

Why do you want to pass it to the libpysal.io.open method? If I understand correctly this code, you first open a file, then read it (and the read method seems to be returning a List). So in your case, where you already have the matrix, you don't need to neither open nor read any file.

What will be needed though is what w is supposed to look like here: w = ww.read(). If it is a simple matrix, then you can initialize w = matrix. If the read method also format the data a certain way, you'll need to do it another way. If you could describe the expected behavior of the read method (e.g. what does the input file contain, and what is returned), it would be useful.

As mentioned, as the data is formatted into a libpysal.weights object, you must build one yourself. This can supposedly be done with this method libpysal.weights.W. (Read the doc too fast).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1