Skip to content

Instantly share code, notes, and snippets.

@yogabonito
Created August 28, 2017 18:51
Show Gist options
  • Save yogabonito/5e5adba8d7a44fb6e3e75ce3b09828ea to your computer and use it in GitHub Desktop.
Save yogabonito/5e5adba8d7a44fb6e3e75ce3b09828ea to your computer and use it in GitHub Desktop.
a short demonstration of the Python-3-port of clusterpy
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# clusterpy with Python 3"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ClusterPy: Library of spatially constrained clustering algorithms\n",
"Some functions are not available, reason: No module named 'Polygon'\n",
"Some functions are not available, reason: No module named 'Polygon'\n"
]
}
],
"source": [
"import clusterpy as cp\n",
"import libpysal as ps"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"layer = cp.importArcData(ps.examples.get_path(\"columbus\"))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Getting variables\n",
"Variables successfully extracted\n",
"Running original Arisel algorithm\n",
"Number of areas: 49\n",
"Number of regions: 4\n",
"INITIAL SOLUTION: [1, 1, 1, 1, 1, 2, 3, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 0, 1, 2, 0, 2, 2, 0, 1, 1, 1, 1, 1, 1, 0, 2, 1, 0, 1, 0, 1, 1, 0, 2, 2, 0, 1, 1, 1, 0, 2, 1, 1] \n",
"INITIAL OF: 7908.486978277309\n",
"FINAL SOLUTION: [1, 1, 1, 1, 1, 2, 3, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 0, 2, 1, 0, 1, 0, 1, 1, 0, 2, 2, 0, 1, 1, 1, 0, 2, 1, 1] \n",
"FINAL OF: 6612.971225320095\n",
"Adding variables\n",
"Done\n"
]
}
],
"source": [
"layer.cluster('arisel', ['CRIME'], 4, wType='queen')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Getting variables\n",
"Variables successfully extracted\n",
"Running original AZP algorithm (Openshaw and Rao, 1995)\n",
"Number of areas: 49\n",
"Number of regions: 4\n",
"Constructing regions\n",
"initial Solution: [2, 2, 2, 2, 2, 3, 2, 2, 3, 3, 2, 2, 2, 2, 2, 2, 3, 2, 2, 3, 2, 2, 3, 2, 2, 2, 0, 0, 2, 2, 2, 3, 1, 2, 1, 2, 2, 0, 2, 3, 3, 2, 1, 1, 1, 2, 3, 1, 1]\n",
"initial O.F: 10495.717665446204\n",
"Performing local search\n",
"FINAL SOLUTION: [2, 2, 2, 2, 2, 3, 2, 2, 3, 3, 2, 2, 2, 2, 2, 2, 3, 2, 2, 3, 2, 2, 3, 2, 2, 2, 0, 0, 0, 2, 2, 3, 1, 2, 1, 2, 2, 0, 2, 3, 3, 2, 2, 1, 1, 2, 3, 1, 1]\n",
"FINAL O.F.: 9967.595996594946\n",
"Done\n",
"Adding variables\n",
"Done\n"
]
}
],
"source": [
"layer.cluster('azp', ['CRIME'], 4)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Getting variables\n",
"Variables successfully extracted\n",
"Running max-p-regions model (Duque, Anselin and Rey, 2010)\n",
"Local search method: Tabu Search\n",
"Number of areas: 49\n",
"threshold value: 100\n",
"FINAL SOLUTION: [0, 0, 0, 0, 0, 6, 3, 0, 6, 6, 4, 4, 3, 3, 10, 10, 2, 11, 3, 2, 7, 1, 2, 11, 11, 6, 1, 6, 11, 8, 7, 2, 1, 7, 9, 7, 8, 5, 7, 2, 2, 7, 5, 9, 9, 7, 2, 5, 9]\n",
"FINAL OF: 0.0\n",
"Done\n",
"Adding variables\n",
"Done\n"
]
}
],
"source": [
"layer.cluster('maxpTabu', ['CRIME'], threshold=100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment