Created
July 8, 2016 21:48
-
-
Save TaylorOshan/ca2bdee790620d4d1983991a01620555 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import pysal as ps\n", | |
"from pysal import weights as w\n", | |
"import numpy as np\n", | |
"import scipy.sparse as sp" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"#check transformation\n", | |
"\n", | |
"def OD(Wo, Wd):\n", | |
" Wo = Wo.sparse\n", | |
" Wd = Wd.sparse\n", | |
" Ww = sp.kron(Wo, Wd)\n", | |
" return w.WSP2W(w.WSP(Ww))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(16, 16)\n" | |
] | |
} | |
], | |
"source": [ | |
"origins = ps.weights.lat2W(2,2)\n", | |
"dests = ps.weights.lat2W(2,2)\n", | |
"Ww = OD(origins, dests)\n", | |
"Ww.transform = 'r'\n", | |
"print Ww.full()[0].shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 0]\n", | |
" [38]\n", | |
" [36]\n", | |
" [86]\n", | |
" [30]\n", | |
" [ 0]\n", | |
" [69]\n", | |
" [19]\n", | |
" [84]\n", | |
" [43]\n", | |
" [ 0]\n", | |
" [80]\n", | |
" [58]\n", | |
" [ 3]\n", | |
" [35]\n", | |
" [ 0]]\n", | |
"[[ 28. ]\n", | |
" [ 53.25]\n", | |
" [ 53.25]\n", | |
" [ 28. ]\n", | |
" [ 28. ]\n", | |
" [ 36. ]\n", | |
" [ 36. ]\n", | |
" [ 28. ]\n", | |
" [ 28. ]\n", | |
" [ 36. ]\n", | |
" [ 36. ]\n", | |
" [ 28. ]\n", | |
" [ 28. ]\n", | |
" [ 53.25]\n", | |
" [ 53.25]\n", | |
" [ 28. ]]\n" | |
] | |
} | |
], | |
"source": [ | |
"flows = np.random.randint(0,100, (4,4))\n", | |
"np.fill_diagonal(flows, 0)\n", | |
"flows = flows.reshape((16,1))\n", | |
"print flows\n", | |
"slag = ps.lag_spatial(Ww, flows)\n", | |
"print slag" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{0: [1.0, 1.0], 1: [1.0, 1.0], 2: [1.0, 1.0], 3: [1.0, 1.0]}" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"origins.weights" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import os\n", | |
"os.chdir('/Users/toshan/dev/pysal/pysal/weights')\n", | |
"from spintW import ODW" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"origins = ps.weights.lat2W(2,2)\n", | |
"dests = ps.weights.lat2W(2,2)\n", | |
"Ww = ODW(origins, dests)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[ 0. , 0. , 0. , 0. , 0. , 0.25, 0.25, 0. , 0. ,\n", | |
" 0.25, 0.25, 0. , 0. , 0. , 0. , 0. ],\n", | |
" [ 0. , 0. , 0. , 0. , 0.25, 0. , 0. , 0.25, 0.25,\n", | |
" 0. , 0. , 0.25, 0. , 0. , 0. , 0. ],\n", | |
" [ 0. , 0. , 0. , 0. , 0.25, 0. , 0. , 0.25, 0.25,\n", | |
" 0. , 0. , 0.25, 0. , 0. , 0. , 0. ],\n", | |
" [ 0. , 0. , 0. , 0. , 0. , 0.25, 0.25, 0. , 0. ,\n", | |
" 0.25, 0.25, 0. , 0. , 0. , 0. , 0. ],\n", | |
" [ 0. , 0.25, 0.25, 0. , 0. , 0. , 0. , 0. , 0. ,\n", | |
" 0. , 0. , 0. , 0. , 0.25, 0.25, 0. ],\n", | |
" [ 0.25, 0. , 0. , 0.25, 0. , 0. , 0. , 0. , 0. ,\n", | |
" 0. , 0. , 0. , 0.25, 0. , 0. , 0.25],\n", | |
" [ 0.25, 0. , 0. , 0.25, 0. , 0. , 0. , 0. , 0. ,\n", | |
" 0. , 0. , 0. , 0.25, 0. , 0. , 0.25],\n", | |
" [ 0. , 0.25, 0.25, 0. , 0. , 0. , 0. , 0. , 0. ,\n", | |
" 0. , 0. , 0. , 0. , 0.25, 0.25, 0. ],\n", | |
" [ 0. , 0.25, 0.25, 0. , 0. , 0. , 0. , 0. , 0. ,\n", | |
" 0. , 0. , 0. , 0. , 0.25, 0.25, 0. ],\n", | |
" [ 0.25, 0. , 0. , 0.25, 0. , 0. , 0. , 0. , 0. ,\n", | |
" 0. , 0. , 0. , 0.25, 0. , 0. , 0.25],\n", | |
" [ 0.25, 0. , 0. , 0.25, 0. , 0. , 0. , 0. , 0. ,\n", | |
" 0. , 0. , 0. , 0.25, 0. , 0. , 0.25],\n", | |
" [ 0. , 0.25, 0.25, 0. , 0. , 0. , 0. , 0. , 0. ,\n", | |
" 0. , 0. , 0. , 0. , 0.25, 0.25, 0. ],\n", | |
" [ 0. , 0. , 0. , 0. , 0. , 0.25, 0.25, 0. , 0. ,\n", | |
" 0.25, 0.25, 0. , 0. , 0. , 0. , 0. ],\n", | |
" [ 0. , 0. , 0. , 0. , 0.25, 0. , 0. , 0.25, 0.25,\n", | |
" 0. , 0. , 0.25, 0. , 0. , 0. , 0. ],\n", | |
" [ 0. , 0. , 0. , 0. , 0.25, 0. , 0. , 0.25, 0.25,\n", | |
" 0. , 0. , 0.25, 0. , 0. , 0. , 0. ],\n", | |
" [ 0. , 0. , 0. , 0. , 0. , 0.25, 0.25, 0. , 0. ,\n", | |
" 0.25, 0.25, 0. , 0. , 0. , 0. , 0. ]])" | |
] | |
}, | |
"execution_count": 17, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"Ww.full()[0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"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.9" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment