Skip to content

Instantly share code, notes, and snippets.

@brendancol
Last active December 19, 2024 13:29
Show Gist options
  • Select an option

  • Save brendancol/30abae78ce8006ca8937 to your computer and use it in GitHub Desktop.

Select an option

Save brendancol/30abae78ce8006ca8937 to your computer and use it in GitHub Desktop.
Running Getis Ord using PySAL
from __future__ import print_function
# std lib
import os
import sys
# 3rd party
import pandas
import numpy
numpy.random.seed(10)
from pysal.weights.Distance import DistanceBand
from getisord import G_Local
def main(input_points, headers, output_points):
df = pandas.read_csv(input_points)
df.columns = headers
points = df[['x','y']].values.tolist()
y = numpy.array(df['pop'].values.tolist())
w = DistanceBand(points, threshold=100000)
w.transform = "B"
g = G_Local(y, w, star=True)
results = zip(points, g.Zs)
flat_results = map(lambda x: (x[0][0], x[0][1], x[1]), results)
output_df = pandas.DataFrame(flat_results)
output_df.columns = ('x', 'y', 'z_score')
output_df.to_csv(output_points, index=False)
if __name__ == '__main__':
input_points_csv = 'small_results.csv'
headers = ('year', 'x', 'y', 'pop')
output_points_csv = 'gedis_ord_result.csv'
main(input_points_csv, headers, output_points_csv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment