merge in pg work

This commit is contained in:
Taylor Oshan 2016-11-28 12:36:37 -07:00
commit 1c0e4fae47
13 changed files with 68 additions and 1252 deletions

11
src/pg/sql/21_gwr.sql Normal file
View File

@ -0,0 +1,11 @@
CREATE OR REPLACE FUNCTION
CDB_GWR(subquery text, dep_var text, ind_vars text[],
fixed boolean default False, kernel text default 'bisquare')
RETURNS table(v1 numeric, v2 numeric, v3 numeric, v4 numeric, v5 numeric, v6 numeric, rowid bigint)
AS $$
from crankshaft.regression import gwr
return gwr(subquery, dep_var, ind_vars, fixed, kernel)
$$ LANGUAGE plpythonu;

View File

@ -3,3 +3,4 @@ import crankshaft.random_seeds
import crankshaft.clustering import crankshaft.clustering
import crankshaft.space_time_dynamics import crankshaft.space_time_dynamics
import crankshaft.segmentation import crankshaft.segmentation
import crankshaft.regression

View File

@ -42,7 +42,7 @@ def get_weight(query_res, w_type='knn', num_ngbrs=5):
return built_weight return built_weight
def query_attr_select(params): def query_attr_select(params, table_ref=True):
""" """
Create portion of SELECT statement for attributes inolved in query. Create portion of SELECT statement for attributes inolved in query.
@param params: dict of information used in query (column names, @param params: dict of information used in query (column names,
@ -50,11 +50,15 @@ def query_attr_select(params):
""" """
attr_string = "" attr_string = ""
template = "i.\"%(col)s\"::numeric As attr%(alias_num)s, " template = "\"%(col)s\"::numeric As attr%(alias_num)s, "
if 'time_cols' in params: if table_ref:
# if markov analysis template = "i." + template
attrs = params['time_cols']
if ('time_cols' in params) or ('ind_vars' in params):
# if markov or gwr analysis
attrs = (params['time_cols'] if 'time_cols' in params
else params['ind_vars'])
for idx, val in enumerate(attrs): for idx, val in enumerate(attrs):
attr_string += template % {"col": val, "alias_num": idx + 1} attr_string += template % {"col": val, "alias_num": idx + 1}
@ -71,7 +75,7 @@ def query_attr_select(params):
return attr_string return attr_string
def query_attr_where(params): def query_attr_where(params, table_ref=True):
""" """
Construct where conditions when building neighbors query Construct where conditions when building neighbors query
Create portion of WHERE clauses for weeding out NULL-valued geometries Create portion of WHERE clauses for weeding out NULL-valued geometries
@ -90,11 +94,14 @@ def query_attr_where(params):
NULL AND idx_replace."time3" IS NOT NULL' NULL AND idx_replace."time3" IS NOT NULL'
""" """
attr_string = [] attr_string = []
template = "idx_replace.\"%s\" IS NOT NULL" template = "\"%s\" IS NOT NULL"
if table_ref:
template = "idx_replace." + template
if 'time_cols' in params: if ('time_cols' in params) or ('ind_vars' in params):
# markov where clauses # markov or gwr where clauses
attrs = params['time_cols'] attrs = (params['time_cols'] if 'time_cols' in params
else params['ind_vars'])
# add values to template # add values to template
for attr in attrs: for attr in attrs:
attr_string.append(template % attr) attr_string.append(template % attr)
@ -104,13 +111,16 @@ def query_attr_where(params):
# get keys # get keys
attrs = sorted([k for k in params attrs = sorted([k for k in params
if k not in ('id_col', 'geom_col', 'subquery', if k not in ('id_col', 'geom_col', 'subquery',
'num_ngbrs', 'subquery')]) 'num_ngbrs', 'ind_vars')])
# add values to template # add values to template
for attr in attrs: for attr in attrs:
attr_string.append(template % params[attr]) attr_string.append(template % params[attr])
if len(attrs) == 2: if len(attrs) == 2:
attr_string.append("idx_replace.\"%s\" <> 0" % params[attrs[1]]) check_zero = "\"%s\" <> 0" % params[attrs[1]]
if table_ref is not None:
check_zero = "idx_replace." + check_zero
attr_string.append(check_zero)
out = " AND ".join(attr_string) out = " AND ".join(attr_string)
@ -122,8 +132,8 @@ def knn(params):
@param vars: dict of values to fill template @param vars: dict of values to fill template
""" """
attr_select = query_attr_select(params) attr_select = query_attr_select(params, table_ref=True)
attr_where = query_attr_where(params) attr_where = query_attr_where(params, table_ref=True)
replacements = {"attr_select": attr_select, replacements = {"attr_select": attr_select,
"attr_where_i": attr_where.replace("idx_replace", "i"), "attr_where_i": attr_where.replace("idx_replace", "i"),
@ -177,6 +187,31 @@ def queen(params):
return query.format(**params) return query.format(**params)
def gwr_query(params):
"""
GWR query
"""
replacements = {"ind_vars_select": query_attr_select(params,
table_ref=None),
"ind_vars_where": query_attr_where(params,
table_ref=None)}
query = '''
SELECT
array_agg(ST_X({geom_col})) As x,
array_agg(ST_Y({geom_col})) As y,
array_agg({dep_var}) As dep_var,
%(ind_vars_select)s,
array_agg({id_col}) As rowid
FROM ({subquery}) As q
WHERE
{dep_var} IS NOT NULL AND
%(ind_vars_where)s
''' % replacements
return query.format(**params).strip()
# to add more weight methods open a ticket or pull request # to add more weight methods open a ticket or pull request

View File

@ -0,0 +1,2 @@
from crankshaft.regression.gwr import *
from crankshaft.regression.glm import *

View File

@ -16,14 +16,14 @@ def gwr(subquery, dep_var, ind_vars, fixed=False, kernel='bisquare'):
x = np.array(query_result[0]['x']) x = np.array(query_result[0]['x'])
y = np.array(query_result[0]['y']) y = np.array(query_result[0]['y'])
coords = zip(x,y) coords = zip(x, y)
Y = query_result[0]['dep'].reshape((-1,1)) Y = query_result[0]['dep'].reshape((-1, 1))
n = Y.shape[0] n = Y.shape[0]
k = len(ind_vars) k = len(ind_vars)
X = np.zeros((n, k)) X = np.zeros((n, k))
for attr in range(0,k): for attr in range(0, k):
attr_name = 'attr' + str(attr+1) attr_name = 'attr' + str(attr+1)
X[:, attr] = np.array(query_result[0][attr_name]).flatten() X[:, attr] = np.array(query_result[0][attr_name]).flatten()

View File

@ -1,169 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import sys\n",
"import numpy as np\n",
"import pandas as pd\n",
"import pysal as ps\n",
"sys.path.append('/Users/toshan/Dropbox/GWR/PyGWRJing/PyGWR/')\n",
"from M_FBGWR_May2016 import FBGWR\n",
"from M_GWGLM import GWGLM\n",
"from M_selection import Band_Sel\n",
"import scipy"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"path = ps.examples.get_path('GData_utm.csv')\n",
"shp = pd.read_csv(path)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Prep data into design matrix and coordinates\n",
"\n",
"#Dependent variable\n",
"y = shp.PctBach.reshape((-1,1))\n",
"\n",
"#Design matrix - covariates - intercept added automatically\n",
"pov = shp.PctPov.reshape((-1,1))\n",
"rural = shp.PctRural.reshape((-1,1))\n",
"blk = shp.PctBlack.reshape((-1,1))\n",
"X = np.hstack([pov, rural, blk])\n",
"labels = ['Intercept', 'PctPov', 'PctRural', 'PctBlack']\n",
"\n",
"#Coordinates for calibration points\n",
"u = shp.X\n",
"v = shp.Y\n",
"coords = zip(u,v)\n",
"\n",
"coords_dict = {}\n",
"for i, x in enumerate(coords):\n",
" coords_dict[i] = x"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(60.0, <Kernel.GWR_W object at 0x10b73a450>, [(89.0, 1088.4132720309442), (73.0, 1082.7676645259439), (62.0, 1079.8414440181946), (62.0, 1079.8414440181946), (62.0, 1079.8414440181946), (60.0, 1079.6668177916372), (60.0, 1079.6668177916372), (60.0, 1079.6668177916372)])\n",
"CPU times: user 1.75 s, sys: 80.7 ms, total: 1.83 s\n",
"Wall time: 1.75 s\n"
]
}
],
"source": [
"%%time\n",
"print Band_Sel(y, None, X, coords_dict)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from pysal.contrib.gwr.sel_bw import Sel_BW\n",
"from pysal.contrib.gwr.gwr import GWR"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"60.0\n",
"CPU times: user 1.1 s, sys: 3.5 ms, total: 1.11 s\n",
"Wall time: 1.1 s\n"
]
}
],
"source": [
"%%time\n",
"print Sel_BW(coords, y, X, [], kernel='bisquare', constant=False).search()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%time\n",
"results = FBGWR(y, X, coords_dict, tolFB=1e-03)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [],
"source": [
"%%time\n",
"sel = Sel_BW(coords, y, X, [], kernel='bisquare', fb=True, constant=False)\n",
"results = sel.search(tol_fb=1e-03)"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [fbgwr]",
"language": "python",
"name": "Python [fbgwr]"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@ -1,6 +0,0 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@ -1,6 +0,0 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 1
}

View File

@ -1,6 +0,0 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@ -1,571 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"import pysal as ps\n",
"from pysal.weights.Distance import Kernel\n",
"import sys\n",
"sys.path.append('/Users/toshan/dev/pysal/pysal/weights')\n",
"from Distance import Kernel as kn\n",
"from util import full2W as f2w"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"n = 5 #number of observations\n",
"m = 3 #number of calibration points"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x = np.random.randint(1,1000, n)\n",
"y = np.random.randint(1,1000, n)\n",
"D1 = zip(x,y)\n",
"W1 = kn(D1)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x = np.random.randint(1,1000, m)\n",
"y = np.random.randint(1,1000, m)\n",
"D2 = zip(x,y)\n",
"W2 = kn(D2)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.39894228, 0.1009935 , 0.05619733, 0.04896355, 0.32650695,\n",
" 0.37879836, 0.31411809, 0.1073076 ],\n",
" [ 0.1009935 , 0.39894228, 0.36195557, 0.36626279, 0.07720723,\n",
" 0.11754135, 0.03669346, 0.33721825],\n",
" [ 0.05619733, 0.36195557, 0.39894228, 0.39108403, 0.0345001 ,\n",
" 0.06151089, 0.02103471, 0.24420603],\n",
" [ 0.04896355, 0.36626279, 0.39108403, 0.39894228, 0.0335092 ,\n",
" 0.05713611, 0.01604658, 0.26976648],\n",
" [ 0.32650695, 0.07720723, 0.0345001 , 0.0335092 , 0.39894228,\n",
" 0.37224696, 0.18985479, 0.11774827],\n",
" [ 0.37879836, 0.11754135, 0.06151089, 0.05713611, 0.37224696,\n",
" 0.39894228, 0.24197075, 0.14519262],\n",
" [ 0.31411809, 0.03669346, 0.02103471, 0.01604658, 0.18985479,\n",
" 0.24197075, 0.39894228, 0.03119528],\n",
" [ 0.1073076 , 0.33721825, 0.24420603, 0.26976648, 0.11774827,\n",
" 0.14519262, 0.03119528, 0.39894228]])"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"D3 = np.vstack([D1,D2])\n",
"W3 = Kernel(D3, function='gaussian')\n",
"W3 = kn(D3, function='gaussian', truncate=False)\n",
"\n",
"W3.full()[0]"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a\n",
"a\n",
"a\n",
"a\n",
"a\n",
"a\n",
"a\n",
"a\n",
"a\n",
"a\n",
"a\n",
"a\n",
"a\n",
"a\n",
"a\n"
]
}
],
"source": [
"\n",
"coord_ids = np.arange(n)\n",
"points_ids = np.arange(n, n+m)\n",
"all_ids = np.arange(n+m)\n",
"dists = np.zeros((m,n))\n",
"\n",
"\n",
"for i in all_ids:\n",
" if i in points_ids:\n",
" for j in coord_ids:\n",
" if j in W3[j].keys():\n",
" if i >= n:\n",
" print 'a'\n",
" dists[i-n][j] = W3.full()[0][i][j]\n",
" elif j >= m:\n",
" print 'b'\n",
" dists[i][j-m] = W3.full()[0][i][j]\n",
" elif (i >= n) & (j >= m):\n",
" print 'c'\n",
" dists[i-n][j-m] = W3.full()[0][i][j]\n",
" else:\n",
" print 'd'\n",
" dists[i][j] = W3.full()[0][i][j]\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.37879836, 0.11754135, 0.06151089, 0.05713611, 0.37224696],\n",
" [ 0.31411809, 0.03669346, 0.02103471, 0.01604658, 0.18985479],\n",
" [ 0.1073076 , 0.33721825, 0.24420603, 0.26976648, 0.11774827]])"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dists"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "ValueError",
"evalue": "3 is not in list",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-22-3036666a721b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mw\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mf2w\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdists\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mw\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfull\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m//anaconda/lib/python2.7/site-packages/pysal/weights/weights.pyc\u001b[0m in \u001b[0;36mfull\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 952\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 953\u001b[0m \"\"\"\n\u001b[0;32m--> 954\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mutil\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfull\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 955\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 956\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mtowsp\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m//anaconda/lib/python2.7/site-packages/pysal/weights/util.pyc\u001b[0m in \u001b[0;36mfull\u001b[0;34m(w)\u001b[0m\n\u001b[1;32m 711\u001b[0m \u001b[0mw_i\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mw\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mweights\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 712\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwij\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn_i\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mw_i\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 713\u001b[0;31m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mkeys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 714\u001b[0m \u001b[0mwfull\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mwij\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 715\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mwfull\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkeys\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: 3 is not in list"
]
}
],
"source": [
"w = f2w(dists)\n",
"w.full()[0]"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"new_neighbs = {}\n",
"new_weights = {}\n",
"for each in W2.neighbors:\n",
" three = set(W3.neighbors[each])\n",
" two = set(W2.neighbors[each])\n",
" new_neighbs[each] = list(three.difference(two))\n",
" new_weights[each] = {}\n",
" for weight in new_neighbs[each]:\n",
" new_weights[each][weight] = W3[each][weight]"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'\\nfor ids in all_ids:\\n if ids in points_ids:\\n _all = set(W3.neighbors[ids])\\n points = set(W2.neighbors[ids])\\n new_neighbs[ids] = list(_all.difference(points))\\n new_weights[ids] = {}\\n for weight in new_neighbs[ids]:\\n new_weights[ids][weight] = W3[ids][weight]\\n\\n else:\\n new_weights[ids] = {}\\n'"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"coord_ids = W1.id_order\n",
"points_ids = W2.id_order\n",
"all_ids = W3.id_order\n",
"dists = np.zeros((2,3))\n",
"\n",
"'''\n",
"for ids in all_ids:\n",
" if ids in points_ids:\n",
" _all = set(W3.neighbors[ids])\n",
" points = set(W2.neighbors[ids])\n",
" new_neighbs[ids] = list(_all.difference(points))\n",
" new_weights[ids] = {}\n",
" for weight in new_neighbs[ids]:\n",
" new_weights[ids][weight] = W3[ids][weight]\n",
"\n",
" else:\n",
" new_weights[ids] = {}\n",
"''' \n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3 0\n",
"a\n",
"3 1\n",
"a\n",
"3 2\n",
"a\n",
"4 0\n",
"a\n",
"4 1\n",
"a\n",
"4 2\n",
"a\n",
"5 0\n",
"a\n",
"5 1\n",
"a\n",
"5 2\n",
"a\n",
"6 0\n",
"a\n",
"6 1\n",
"a\n",
"6 2\n",
"a\n",
"7 0\n",
"a\n",
"7 1\n",
"a\n",
"7 2\n",
"a\n"
]
}
],
"source": [
"n = 3 #number of observations\n",
"m = 2 #number of \n",
"for i in all_ids:\n",
" if i in points_ids:\n",
" for j in coord_ids:\n",
" if j in W3[j].keys():\n",
" print i,j\n",
" if i >= n:\n",
" print 'a'\n",
" dists[i-n][j] = W3.full()[0][i][j]\n",
" elif j >= m:\n",
" print 'b'\n",
" dists[i][j-m] = W3.full()[0][i][j]\n",
" elif (i >= n) & (j >= m):\n",
" print 'c'\n",
" dists[i-n][j-m] = W3.full()[0][i][j]\n",
" else:\n",
" print 'd'\n",
" dists[i][j] = W3.full()[0][i][j]\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0. , 0.45118883, 0.85404129],\n",
" [ 0.27754126, 0.40233088, 0.20596816],\n",
" [ 0. , 0.37941649, 0.6032733 ],\n",
" [ 0. , 0. , 0. ],\n",
" [ 0.35446728, 0.73305802, 0.50022967]])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dists"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"points_ids"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"W4 = ps.W(new_neighbs, new_weights)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{0: [3, 4, 5, 6, 7], 1: [3, 4, 5, 6, 7], 2: [3, 4, 5, 6, 7]}"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"W4.neighbors"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{0: {3: -1.0482874068379293,\n",
" 4: 0.36696681575222334,\n",
" 5: 0.678090925231672,\n",
" 6: 0.308555499289517,\n",
" 7: -0.620566108018012},\n",
" 1: {3: 0.5865615659374835,\n",
" 4: -0.8123595700046831,\n",
" 5: -0.5633467635255329,\n",
" 6: -1.1845906645769202,\n",
" 7: 0.42019589403802593},\n",
" 2: {3: 0.8005291815217084,\n",
" 4: -1.212624893235362,\n",
" 5: -0.9337024333901489,\n",
" 6: -1.4259607135415542,\n",
" 7: 0.009238162970930053}}"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"W4.weights"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 2]"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"W4.id_order"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "ValueError",
"evalue": "3 is not in list",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-29-bea43da3bca2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mW4\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfull\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m//anaconda/lib/python2.7/site-packages/pysal/weights/weights.pyc\u001b[0m in \u001b[0;36mfull\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 952\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 953\u001b[0m \"\"\"\n\u001b[0;32m--> 954\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mutil\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfull\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 955\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 956\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mtowsp\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m//anaconda/lib/python2.7/site-packages/pysal/weights/util.pyc\u001b[0m in \u001b[0;36mfull\u001b[0;34m(w)\u001b[0m\n\u001b[1;32m 711\u001b[0m \u001b[0mw_i\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mw\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mweights\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 712\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwij\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn_i\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mw_i\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 713\u001b[0;31m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mkeys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 714\u001b[0m \u001b[0mwfull\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mwij\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 715\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mwfull\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkeys\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: 3 is not in list"
]
}
],
"source": [
"W4.full()[0]"
]
},
{
"cell_type": "code",
"execution_count": 244,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{1: 1}"
]
},
"execution_count": 244,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"W4."
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([5])"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.arange(5,6)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [Root]",
"language": "python",
"name": "Python [Root]"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@ -1,6 +0,0 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@ -0,0 +1 @@
from base import *