In [1]:
import pysal.contrib.clusterpy as cp
%pylab inline
In [2]:
import numpy as np
import pysal as ps
from collections import Counter
In [3]:
columbus = cp.loadArcData(ps.examples.get_path('columbus.shp'))
columbus.fieldNames
n = len(columbus.Wqueen)
#columbus.generateData('Uniform', 'rook', 1, 1, 10)
columbus.dataOperation("CONSTANT = 1")
columbus.cluster('maxpTabu', ['CRIME', 'CONSTANT'], threshold=4, dissolve=0, std=0)
In [4]:
Counter(columbus.region2areas)
Out[4]:
In [5]:
columbus.cluster('arisel', ['CRIME'], 5, wType='rook', inits=10, dissolve=0)
#calif.cluster('arisel', ['PCR2002'], 9, wType='rook', inits=10, dissolve=1)
In [6]:
# regionalization solutions are added as a list of region ids at the end
columbus.fieldNames
Out[6]:
In [7]:
warisel = ps.block_weights(columbus.region2areas)
In [8]:
warisel.neighbors
Out[8]:
In [9]:
wregimes = ps.block_weights(columbus.region2areas)
In [10]:
columbus.region2areas[5]
Out[10]:
In [11]:
wregimes.n
Out[11]:
In [12]:
wregimes.neighbors
Out[12]:
In [13]:
mexico = cp.importCsvData(ps.examples.get_path('mexico.csv'))
In [14]:
mexico.fieldNames
Out[14]:
In [15]:
w = ps.open(ps.examples.get_path('mexico.gal')).read()
In [16]:
w.n
Out[16]:
In [17]:
cp.addRook2Layer(ps.examples.get_path('mexico.gal'), mexico)
In [18]:
mexico.Wrook
Out[18]:
In [19]:
mexico.cluster('arisel', ['pcgdp1940'], 5, wType='rook', inits=10, dissolve=0)
In [20]:
mexico.fieldNames
Out[20]:
In [21]:
mexico.getVars('pcgdp1940')
Out[21]:
In [22]:
# mexico example all together
csvfile = ps.examples.get_path('mexico.csv')
galfile = ps.examples.get_path('mexico.gal')
mexico = cp.importCsvData(csvfile)
cp.addRook2Layer(galfile, mexico)
mexico.cluster('arisel', ['pcgdp1940'], 5, wType='rook', inits=10, dissolve=0)
In [23]:
mexico.region2areas.index(2)
Out[23]:
In [24]:
mexico.Wrook[0]
Out[24]:
In [25]:
mexico.getVars('State')
Out[25]:
In [26]:
regions = np.array(mexico.region2areas)
In [27]:
regions
Out[27]:
In [28]:
Counter(regions)
Out[28]:
In [29]:
usf = ps.examples.get_path('us48.shp')
In [30]:
us = cp.loadArcData(usf.split(".")[0])
In [31]:
us.Wqueen
Out[31]:
In [32]:
us.fieldNames
Out[32]:
In [33]:
uscsv = ps.examples.get_path("usjoin.csv")
In [34]:
f = ps.open(uscsv)
pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]).T
In [35]:
pci
Out[35]:
In [36]:
usy = cp.Layer()
In [37]:
cp.addQueen2Layer(ps.examples.get_path('states48.gal'), usy)
In [38]:
names = ["Y_%d"%v for v in range(1929,2010)]
cp.addArray2Layer(pci, usy, names)
In [39]:
names
Out[39]:
In [40]:
usy.fieldNames
Out[40]:
In [41]:
usy.getVars('Y_1929')
Out[41]:
In [42]:
usy.Wrook
Out[42]:
In [43]:
usy.cluster('arisel', ['Y_1980'], 8, wType='queen', inits=10, dissolve=0)
#mexico.cluster('arisel', ['pcgdp1940'], 5, wType='rook', inits=10, dissolve=0)
In [43]:
In [44]:
us = cp.Layer()
In [45]:
cp.addQueen2Layer(ps.examples.get_path('states48.gal'), us)
In [46]:
uscsv = ps.examples.get_path("usjoin.csv")
In [47]:
f = ps.open(uscsv)
pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]).T
names = ["Y_%d"%v for v in range(1929,2010)]
cp.addArray2Layer(pci, us, names)
In [48]:
usy.cluster('arisel', ['Y_1980'], 8, wType='queen', inits=10, dissolve=0)
In [49]:
us_alpha = cp.importCsvData(ps.examples.get_path('usjoin.csv'))
In [50]:
alpha_fips = us_alpha.getVars('STATE_FIPS')
In [51]:
alpha_fips
Out[51]:
In [52]:
dbf = ps.open(ps.examples.get_path('us48.dbf'))
In [53]:
dbf.header
Out[53]:
In [54]:
state_fips = dbf.by_col('STATE_FIPS')
names = dbf.by_col('STATE_NAME')
In [55]:
names
Out[55]:
In [56]:
state_fips = map(int, state_fips)
In [57]:
state_fips
Out[57]:
In [58]:
# the csv file has the states ordered alphabetically, but this isn't the case for the order in the shapefile so we have to reorder before any choropleths are drawn
alpha_fips = [i[0] for i in alpha_fips.values()]
reorder = [ alpha_fips.index(s) for s in state_fips]
In [59]:
regions = usy.region2areas
In [60]:
regions
Out[60]:
In [61]:
from pysal.contrib.viz import mapping as maps
In [62]:
shp = ps.examples.get_path('us48.shp')
regions = np.array(regions)
In [63]:
maps.plot_choropleth(shp, regions[reorder], 'unique_values')
In [64]:
usy.cluster('arisel', ['Y_1929'], 8, wType='queen', inits=10, dissolve=0)
In [65]:
regions = usy.region2areas
In [66]:
regions = np.array(regions)
In [67]:
maps.plot_choropleth(shp, regions[reorder], 'unique_values')
In [ ]:
names = ["Y_%d"%i for i in range(1929, 2010)]
#usy.cluster('arisel', ['Y_1929'], 8, wType='queen', inits=10, dissolve=0)
usy.cluster('arisel', names, 8, wType='queen', inits=10, dissolve=0)
In [ ]:
regions = usy.region2areas
regions = np.array(regions)
maps.plot_choropleth(shp, regions[reorder], 'unique_values', title='All Years')
In [ ]:
ps.version
In [ ]:
usy.cluster('arisel', names[:40], 8, wType='queen', inits=10, dissolve=0)
regions = usy.region2areas
regions = np.array(regions)
maps.plot_choropleth(shp, regions[reorder], 'unique_values', title='1929-68')
In [ ]:
usy.cluster('arisel', names[40:], 8, wType='queen', inits=10, dissolve=0)
regions = usy.region2areas
regions = np.array(regions)
maps.plot_choropleth(shp, regions[reorder], 'unique_values', title='1969-2009')
In [ ]:
usy.cluster('arisel', names[40:], 8, wType='queen', inits=10, dissolve=0)
In [ ]:
usy.dataOperation("CONSTANT = 1")
usy.Wrook = usy.Wqueen
usy.cluster('maxpTabu', ['Y_1929', 'Y_1929'], threshold=1000, dissolve=0)
regions = usy.region2areas
regions = np.array(regions)
maps.plot_choropleth(shp, regions[reorder], 'unique_values', title='maxp 1929')
In [ ]:
Counter(regions)
In [ ]:
usy.getVars('Y_1929')
In [ ]:
usy.Wrook
In [ ]:
usy.cluster('maxpTabu', ['Y_1929', 'CONSTANT'], threshold=8, dissolve=0)
regions = usy.region2areas
regions = np.array(regions)
maps.plot_choropleth(shp, regions[reorder], 'unique_values', title='maxp 1929')
In [ ]:
regions
In [ ]:
Counter(regions)
In [ ]:
vars = names
In [ ]:
vars.append('CONSTANT')
In [ ]:
vars
In [ ]:
usy.cluster('maxpTabu', vars, threshold=8, dissolve=0)
regions = usy.region2areas
regions = np.array(regions)
maps.plot_choropleth(shp, regions[reorder], 'unique_values', title='maxp 1929-2009')
In [ ]:
Counter(regions)
In [ ]:
south = cp.loadArcData(ps.examples.get_path("south.shp"))
In [ ]:
south.fieldNames
In [ ]:
# uncomment if you have some time ;->
#south.cluster('arisel', ['HR70'], 20, wType='queen', inits=10, dissolve=0)
In [ ]:
#regions = south.region2areas
In [ ]:
shp = ps.examples.get_path('south.shp')
#maps.plot_choropleth(shp, np.array(regions), 'unique_values')
In [ ]:
south.dataOperation("CONSTANT = 1")
south.cluster('maxpTabu', ['HR70', 'CONSTANT'], threshold=70, dissolve=0)
regions = south.region2areas
regions = np.array(regions)
maps.plot_choropleth(shp, regions, 'unique_values', title='maxp HR70 threshold=70')
In [ ]:
Counter(regions)
In [ ]: