Last active
December 19, 2024 13:29
-
-
Save brendancol/30abae78ce8006ca8937 to your computer and use it in GitHub Desktop.
Running Getis Ord using PySAL
This file contains hidden or 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
| 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