Skip to content

Instantly share code, notes, and snippets.

@aleszu
Created November 23, 2020 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aleszu/910da0ba8efbfb6cdbc35ef785907431 to your computer and use it in GitHub Desktop.
Save aleszu/910da0ba8efbfb6cdbc35ef785907431 to your computer and use it in GitHub Desktop.
def score_calculation(weights, school, district):
'''
district is a list of census blocks which we are including on the district
school is a specific school id
the weights normalize the scores taking into account their relative importance
'''
# population of district and minority population from global DataFrame block_data
pop = block_data['HS_POP'].loc[district].sum()
minority_pop = block_data['MINORITY'].loc[district].sum()
#squared error between overall demographic ratio and local demographic ratio
demographic_score = (abs((minority_pop/pop) - target_ratio))**2*pop*100
# global distance_matrix holds travel times between each pair of blocks and schools
# this sum gives the total travel time
travel_score = sum[distance_matrix[x][school] *
block_data['HS_POP'].loc[x] for x in district]
# use graph library NetworkX to find the number of census blocks which the district borders
outgoing_edges = list(nx.edge_boundary(G,district))
# the compactnes score is the ratio between the number of bordering districts and its overall size
compactness_score= len(outgoing_edges)/len(district)
#weighted sum of scores
aggregate_score = weights[demographic]*demographic_score
+ weights[compactness]*compactness_score + weights[travel]*travel_score
return (demographic_score, travel_score, compactness_score, aggregate_score)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment