Skip to content

IndexError: index out of bounds in predict() when prediction points differ from calibration points #160

@ppriyanshu015

Description

@ppriyanshu015

Describe the bug
When using mgwr.gwr.GWR.predict() to predict values on a set of coordinates (prediction_points) that is different from the coordinates used to fit the model (calibration_points), an IndexError occurs. The error message typically looks like IndexError: index [X] is out of bounds for axis 0 with size [Y], where Y is the number of calibration points and X is an index equal to or greater than Y.

This occurs even after ensuring the predictor matrix for the prediction points (X_pred) does not contain NaNs (e.g., by using np.nan_to_num).

To Reproduce
Steps to reproduce the behavior:

  1. Fit a GWR model using N calibration points: gwr_model = GWR(calibration_coords, calibration_y, calibration_X, bw).fit() (where calibration_X has shape (N, k))
  2. Prepare prediction coordinates (prediction_coords) and corresponding predictors (X_pred, shape (M, k)), where M > N (e.g., predicting on a full grid).
  3. Call the predict method: gwr_model.predict(prediction_coords, X_pred)
  4. Observe the IndexError.

Traceback Analysis
The full traceback (see below) indicates the error originates within the _local_fit method called by joblib during parallel processing. More specifically, the traceback suggests that the predict method (at mgwr/gwr.py:412 in the traceback provided earlier) incorrectly calls self.fit() internally, which likely leads to the index mismatch when _local_fit tries to access self.X[i] (calibration data) using an index i derived from the prediction point loop.

Expected behavior
The predict() method should successfully compute predictions and local parameters for the prediction_coords without raising an IndexError, using the state of the already-fitted model. It should not be calling fit() internally during prediction.

Traceback (most recent call last):
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\joblib\externals\loky\process_executor.py", line 463, in _process_worker
r = call_item()
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\joblib\externals\loky\process_executor.py", line 291, in call
return self.fn(*self.args, **self.kwargs)
~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\joblib\parallel.py", line 598, in call
return [func(*args, **kwargs)
~~~~^^^^^^^^^^^^^^^^^
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\mgwr\gwr.py", line 268, in _local_fit
predy = np.dot(self.X[i], betas)[0]
~~~~~~^^^
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\numpy_core\memmap.py", line 358, in getitem
res = super().getitem(index)
IndexError: index 58757 is out of bounds for axis 0 with size 58757
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "e:\Pankaj_2023\Visual_Studio\Python\aa", line 291, in
pred_results = gwr_model.predict(coords, X_coarse_full_imputed)
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\mgwr\gwr.py", line 412, in predict
gwr = self.fit(**fit_params)
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\mgwr\gwr.py", line 348, in fit
rslt = Parallel(n_jobs=self.n_jobs)(delayed(self._local_fit)(i) for i in range(m))
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\joblib\parallel.py", line 2007, in call
return output if self.return_generator else list(output)
~~~~^^^^^^^^
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\joblib\parallel.py", line 1650, in _get_outputs
yield from self._retrieve()
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\joblib\parallel.py", line 1754, in _retrieve
self._raise_error_fast()
~~~~~~~~~~~~~~~~~~~~~~^^
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\joblib\parallel.py", line 1789, in _raise_error_fast
error_job.get_result(self.timeout)
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\joblib\parallel.py", line 745, in get_result
return self._return_or_raise()
~~~~~~~~~~~~~~~~~~~~~^^
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\joblib\parallel.py", line 763, in _return_or_raise
raise self._result
IndexError: index 58757 is out of bounds for axis 0 with size 58757

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions