Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
>>> w = np.array([0.5, 0.1, 0.3, 0.8])
We specify the number of groups for which the weighted mean is computed.
>>> n = 2
Applying sum_by_n function
>>> sum_by_n(d, w, n)
array([ 5.9, 30. ])
"""
t = len(d)
h = t // n #must be floor!
d = d * w
return np.array([sum(d[i: i + h]) for i in range(0, t, h)])
def internal_or_external(polys,filament, vertices):
#Modification of Serge's code to find poly in poly for line in poly
#pl = ps.cg.PolygonLocator(polys) #Trying to use this for polyline in polygon
#Spatial Index of Filaments and minimal cycles (polygons)
polyline = ps.cg.Chain([ps.cg.Point(vertices[pnt]) for pnt in filament])
polyline_mbr = polyline.bounding_box
pl = ps.cg.PolygonLocator(polys)
overlaps = pl.overlapping(polyline_mbr)
#For the overlapping MBRs check to see if the polyline is internal or external to the min cycle
for k in range(len(overlaps)):
s = sum(overlaps[k].contains_point(v) for v in polyline.vertices)
if s == len(polyline.vertices):
#Internal
return True
else:
return False
def setUp(self):
db = pysal.open(pysal.examples.get_path('columbus.dbf'),'r')
y = np.array(db.by_col("HOVAL"))
y = np.reshape(y, (49,1))
self.y = np.round(y).astype(int)
X = []
X.append(db.by_col("INC"))
X.append(db.by_col("CRIME"))
self.X = np.array(X).T
def _test():
import doctest
start_suppress = np.get_printoptions()['suppress']
np.set_printoptions(suppress=True)
doctest.testmod()
np.set_printoptions(suppress=start_suppress)
if __name__ == '__main__':
_test()
import numpy as np
import pysal
db = pysal.open(pysal.examples.get_path("columbus.dbf"), 'r')
y_var = 'CRIME'
y = np.array([db.by_col(y_var)]).reshape(49, 1)
x_var = ['INC']
x = np.array([db.by_col(name) for name in x_var]).T
yd_var = ['HOVAL']
yd = np.array([db.by_col(name) for name in yd_var]).T
q_var = ['DISCBD']
q = np.array([db.by_col(name) for name in q_var]).T
w = pysal.rook_from_shapefile(pysal.examples.get_path("columbus.shp"))
w.transform = 'r'
model = GM_Lag(
y, x, yd, q, w=w, spat_diag=True, name_y=y_var, name_x=x_var,
name_yend=yd_var, name_q=q_var, name_ds='columbus', name_w='columbus.gal')
print model.summary
def _test():
import doctest
doctest.testmod()
if __name__ == '__main__':
_test()
import numpy as np
import pysal
db=pysal.open("examples/columbus.dbf","r")
y = np.array(db.by_col("CRIME"))
y = np.reshape(y, (49,1))
X = []
X.append(db.by_col("INC"))
X = np.array(X).T
yd = []
yd.append(db.by_col("HOVAL"))
yd = np.array(yd).T
# instrument for HOVAL with DISCBD
q = []
q.append(db.by_col("DISCBD"))
q = np.array(q).T
reg = BaseTSLS(y, X, yd, q=q, robust='white')
print reg.betas
print reg.vm
model.w = w_r
return model
def _test():
import doctest
start_suppress = np.get_printoptions()['suppress']
np.set_printoptions(suppress=True)
doctest.testmod()
np.set_printoptions(suppress=start_suppress)
if __name__ == '__main__':
_test()
import numpy as np
import pysal
db = pysal.open(pysal.examples.get_path('columbus.dbf'), 'r')
y_var = 'CRIME'
y = np.array([db.by_col(y_var)]).reshape(49, 1)
x_var = ['INC', 'HOVAL']
x = np.array([db.by_col(name) for name in x_var]).T
r_var = 'NSA'
regimes = db.by_col(r_var)
w = pysal.rook_from_shapefile(pysal.examples.get_path("columbus.shp"))
w.transform = 'r'
olsr = OLS_Regimes(y, x, regimes, w=w, constant_regi='many', nonspat_diag=False, spat_diag=False, name_y=y_var, name_x=['INC', 'HOVAL'],
name_ds='columbus', name_regimes=r_var, name_w='columbus.gal', regime_err_sep=True, cols2regi=[True, True], sig2n_k=True, robust='white')
print olsr.summary
def _test():
import doctest
start_suppress = np.get_printoptions()['suppress']
np.set_printoptions(suppress=True)
doctest.testmod()
np.set_printoptions(suppress=start_suppress)
if __name__ == '__main__':
_test()
import numpy as np
import pysal
dbf = pysal.open(pysal.examples.get_path('columbus.dbf'), 'r')
y = np.array([dbf.by_col('CRIME')]).T
var_x = ['INC', 'HOVAL']
x = np.array([dbf.by_col(name) for name in var_x]).T
w = pysal.open(pysal.examples.get_path("columbus.gal"), 'r').read()
w.transform = 'r'
probit1 = Probit(
(y > 40).astype(float), x, w=w, name_x=var_x, name_y="CRIME",
name_ds="Columbus", name_w="columbus.dbf")
print probit1.summary
model.schwarz = DIAG.schwarz(reg=model)
return model
def _test():
import doctest
start_suppress = np.get_printoptions()['suppress']
np.set_printoptions(suppress=True)
doctest.testmod()
np.set_printoptions(suppress=start_suppress)
if __name__ == "__main__":
_test()
import numpy as np
import pysal as ps
db = ps.open(ps.examples.get_path("baltim.dbf"), 'r')
ds_name = "baltim.dbf"
y_name = "PRICE"
y = np.array(db.by_col(y_name)).T
y.shape = (len(y), 1)
x_names = ["NROOM", "NBATH", "PATIO", "FIREPL",
"AC", "GAR", "AGE", "LOTSZ", "SQFT"]
x = np.array([db.by_col(var) for var in x_names]).T
ww = ps.open(ps.examples.get_path("baltim_q.gal"))
w = ww.read()
ww.close()
w_name = "baltim_q.gal"
w.transform = 'r'
regimes = db.by_col("CITCOU")
mllag = ML_Lag_Regimes(y, x, regimes, w=w, method='full', name_y=y_name, name_x=x_names,
name_w=w_name, name_ds=ds_name, regime_lag_sep=True, constant_regi='many',
np.set_printoptions(suppress=start_suppress)
if __name__ == "__main__":
_test()
import numpy as np
import pysal as ps
db = ps.open(ps.examples.get_path("baltim.dbf"), 'r')
ds_name = "baltim.dbf"
y_name = "PRICE"
y = np.array(db.by_col(y_name)).T
y.shape = (len(y), 1)
x_names = ["NROOM", "NBATH", "PATIO", "FIREPL",
"AC", "GAR", "AGE", "LOTSZ", "SQFT"]
x = np.array([db.by_col(var) for var in x_names]).T
ww = ps.open(ps.examples.get_path("baltim_q.gal"))
w = ww.read()
ww.close()
w_name = "baltim_q.gal"
w.transform = 'r'
regimes = []
y_coord = np.array(db.by_col("Y"))
for i in y_coord:
if i > 544.5:
regimes.append("North")
else:
regimes.append("South")
mlerror = ML_Error_Regimes(y, x, regimes, w=w, method='full', name_y=y_name,
name_x=x_names, name_w=w_name, name_ds=ds_name, regime_err_sep=False,
name_regimes="North")
start_suppress = np.get_printoptions()['suppress']
np.set_printoptions(suppress=True)
doctest.testmod()
np.set_printoptions(suppress=start_suppress)
if __name__ == '__main__':
_test()
import numpy as np
import pysal
db = pysal.open(pysal.examples.get_path("columbus.dbf"), 'r')
y_var = 'CRIME'
y = np.array([db.by_col(y_var)]).reshape(49, 1)
x_var = ['INC', 'HOVAL']
x = np.array([db.by_col(name) for name in x_var]).T
w = pysal.rook_from_shapefile(pysal.examples.get_path("columbus.shp"))
w.transform = 'r'
ols = OLS(
y, x, w=w, nonspat_diag=True, spat_diag=True, name_y=y_var, name_x=x_var,
name_ds='columbus', name_w='columbus.gal', robust='white', sig2n_k=True, moran=True)
print ols.summary