Search code examples
pythonpysal

Suddenly, Probit doesn't work on PySAL (1.14.4)


Up until last week, I had a piece of code for calculating the odds of something occurring for a series of events, but starting this week the code doesn't work.

The code is the following:

import numpy as np
import pysal
import os

dbf = pysal.open('file.csv','r')

y = np.array([dbf.by_col('result')]).T

names_to_extract = ['dist', 'angle']
x = np.array([dbf.by_col(name) for name in names_to_extract]).T

id = np.array([dbf.by_col('incidenceId')]).T

model = pysal.spreg.probit.Probit(y, x, w=None, name_y='result', name_x=['dist','angle'], name_w=None, name_ds=None)

fin = np.column_stack((id, model.y, model.predy))

os.chdir("/destinyfolder")

np.savetxt('xg.csv', fin, delimiter=',', fmt='%d, %d, %f')

And I get this error:

/usr/local/lib/python3.7/site-packages/pysal/__init__.py:65: VisibleDeprecationWarning: PySAL's API will be changed on 2018-12-31. The last release made with this API is version 1.14.4. A preview of the next API version is provided in the `pysalnext` package. The API changes and a guide on how to change imports is provided at https://migrating.pysal.org
  ), VisibleDeprecationWarning)
Traceback (most recent call last):
  File "xg.py", line 14, in <module>
    model = pysal.spreg.probit.Probit(y, x, w=None, name_y='result', name_x=['dist','angle'], name_w=None, name_ds=None)
  File "/usr/local/lib/python3.7/site-packages/pysal/spreg/probit.py", line 807, in __init__
    n = USER.check_arrays(y, x)
  File "/usr/local/lib/python3.7/site-packages/pysal/spreg/user_output.py", line 359, in check_arrays
    if not spu.spisfinite(i):
  File "/usr/local/lib/python3.7/site-packages/pysal/spreg/sputils.py", line 267, in spisfinite
    return np.isfinite(a.sum())
  File "/usr/local/lib/python3.7/site-packages/numpy/core/_methods.py", line 36, in _sum
    return umr_sum(a, axis, dtype, out, keepdims, initial)
TypeError: cannot perform reduce with flexible type

Usually, I only got the DeprecationWarning, but the code worked. Since today, the code doesn't work. Consider that I am currently using pysal 1.14.4, numpy 1.16.2 and scipy 1.2.1. I have not updated my code to pysal 2.0 because I couldn't figure out how to port this code to the new version (and that's why I got the DeprecationWarning in the first place).

Here's the file: file.csv

Can you help me to make this work?


Solution

  • Entries in your x are strings, convert them to float:

    x = np.array(...).T.astype(np.float)
    

    This will fail at first because of "NULL" values in file.csv, you need to either filter these out or specify what float value they should be converted to.

    Before posting a question, try first a simple google search for an immediate answer. If you google this error along with "pysal 1.14.4", you will immediately find this solution:

    TypeError: cannot perform reduce with flexible type