pysal / spreg

Spatial econometric regression in Python
https://pysal.org/spreg/
Other
67 stars 23 forks source link

Pysal/numpy incompatibility? No loop matching the specified signature and casting was found for ufunc add #72

Open jooglyp opened 3 years ago

jooglyp commented 3 years ago

Quoting: https://stackoverflow.com/questions/64727148/pysal-in-python-error-no-loop-matching-the-specified-signature-and-casting-was

I am using the pysal package to do some spatial regressions. I am adopting the example here where the dataset is here:

This is what I am doing:

abb_link = '/Users/emassaro/Downloads/listings.csv.gz'
lst = pd.read_csv(abb_link)
x = ['host_listings_count', 'bathrooms', 'bedrooms', 'beds', 'guests_included']
def has_pool(a):
    if 'Pool' in a:
        return 1
    else:
        return 0

lst['pool'] = lst['amenities'].apply(has_pool)
yxs = lst.loc[:, x + ['pool', 'price']].dropna()
y = np.log( yxs['price'].apply(lambda x: float(x.strip('$').replace(',', '')))+ 0.000001)
w = ps.knnW_from_array(lst.loc[yxs.index, ['longitude', 'latitude']].values)
w.transform = 'R'

m1 = ps.spreg.OLS(y.values[:, None], yxs.drop('price', axis=1).values, \
                  w=w, spat_diag=True, \
                  name_x=yxs.drop('price', axis=1).columns.tolist(), name_y='ln(price)')

However if I want to use another model, such as GM_Lag

m2 = ps.spreg.GM_Lag(y.values[:, None], yxs.drop('price', axis=1), w=w, spat_diag=True)

I get the following error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-36-2a848adc0fbe> in <module>
----> 1 m2 = ps.spreg.GM_Lag(yy, y2, w=w, spat_diag=True)

/usr/local/lib/python3.8/site-packages/pysal/spreg/twosls_sp.py in __init__(self, y, x, yend, q, w, w_lags, lag_q, robust, gwk, sig2n_k, spat_diag, vm, name_y, name_x, name_yend, name_q, name_w, name_gwk, name_ds)
    495         self.name_w = USER.set_name_w(name_w, w)
    496         self.name_gwk = USER.set_name_w(name_gwk, gwk)
--> 497         SUMMARY.GM_Lag(reg=self, w=w, vm=vm, spat_diag=spat_diag)
    498 
    499 

/usr/local/lib/python3.8/site-packages/pysal/spreg/summary_output.py in GM_Lag(reg, vm, w, spat_diag, regimes)
    152     reg.__summary = {}
    153     # compute diagnostics and organize summary output
--> 154     beta_diag_lag(reg, reg.robust, error=False)
    155     if spat_diag:
    156         # compute diagnostics and organize summary output

/usr/local/lib/python3.8/site-packages/pysal/spreg/summary_output.py in beta_diag_lag(reg, robust, error)
    719     reg.std_err = diagnostics.se_betas(reg)
    720     reg.z_stat = diagnostics.t_stat(reg, z_stat=True)
--> 721     reg.pr2 = diagnostics_tsls.pr2_aspatial(reg)
    722     # organize summary output
    723     reg.__summary['summary_std_err'] = robust

/usr/local/lib/python3.8/site-packages/pysal/spreg/diagnostics_tsls.py in pr2_aspatial(tslsreg)
    213     y = tslsreg.y
    214     predy = tslsreg.predy
--> 215     pr = pearsonr(y, predy)[0]
    216     pr2_result = float(pr ** 2)
    217     return pr2_result

/usr/local/lib/python3.8/site-packages/scipy/stats/stats.py in pearsonr(x, y)
   3854         return dtype(np.sign(x[1] - x[0])*np.sign(y[1] - y[0])), 1.0
   3855 
-> 3856     xmean = x.mean(dtype=dtype)
   3857     ymean = y.mean(dtype=dtype)
   3858 

/usr/local/lib/python3.8/site-packages/numpy/core/_methods.py in _mean(a, axis, dtype, out, keepdims)
    158             is_float16_result = True
    159 
--> 160     ret = umr_sum(arr, axis, dtype, out, keepdims)
    161     if isinstance(ret, mu.ndarray):
    162         ret = um.true_divide(

TypeError: No loop matching the specified signature and casting was found for ufunc add
jGaboardi commented 3 years ago

@jooglyp This happening in specifically in the spreg module so I'm going to transfer it over there.