Prepare and Fit Spatial Regression Models 20190222




Pay Notebook Creator: Roy Hyunjin Han0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0

Predict Metrics by Address

Here is an dummy tool template that you can use to prototype your tool. This tool template assumes that each row of your training dataset corresponds to an address.

Note that this tool uses a dummy model. Please modify the inputs, outputs and model to fit your chosen hypothesis and training dataset.

Thanks to the following groups for making this work possible:

{address_table : Addresses ? Specify the addresses for which you would like to predict metrics}

In [ ]:
# CrossCompute
address_table_path = 'address.csv'
target_folder = '/tmp'

Load Arguments

In [ ]:
import pandas as pd
address_table = pd.read_csv(address_table_path)
address_table[:3]

Run Model to Estimate Target Variable

In [ ]:
# Load model
from pickle import load
model = load(open('dummy-model.pkl', 'rb'))  # !!! Replace dummy model with your model
model
In [ ]:
# Run model
X = address_table[['Tree Count Within 100 Meters', 'Sum of Distances to Each Tree Within 100 Meters']].values
y = model.predict(X)
y
In [ ]:
# Add column
address_table['Predicted Graduation Rate'] = y
address_table

Render Table

In [ ]:
# Save file to target folder to include it in the result download
target_path = target_folder + '/a.csv'
address_table.to_csv(target_path, index=False)
print(f'a_table_path = {target_path}')  # Print table_path to render table

Render Map

In [ ]:
address_geotable = address_table.copy()  # Prevent SettingwithCopyWarning
In [ ]:
# Geocode address locations
from geopy import GoogleV3
geocode = GoogleV3('AIzaSyDNqc0tWzXHx_wIp1w75-XTcCk4BSphB5w').geocode

def get_longitude_latitude(row):
    location = geocode(row['Address'])
    row['Longitude'] = location.longitude
    row['Latitude'] = location.latitude
    return row

address_geotable = address_geotable.apply(get_longitude_latitude, axis=1)
address_geotable[:3]
In [ ]:
# Set radius for each point
address_geotable['RadiusInPixelsRange10-20'] = address_geotable['Tree Count Within 100 Meters']
In [ ]:
# Set color for each point using a gradient
# address_geotable['FillReds'] = address_geotable['Predicted Graduation Rate']
In [ ]:
# Set color for each point using a rule
address_geotable['FillColor'] = address_geotable.apply(
    lambda row: 'r' if row['Predicted Graduation Rate'] < 50 else 'g',
    axis=1)
In [ ]:
# See what we did
address_geotable[:3]
In [ ]:
# Save file to target folder to include it in the result download
target_path = target_folder + '/b.csv'
address_geotable.to_csv(target_path, index=False)
print(f'b_geotable_path = {target_path}')  # Print geotable_path to render map

Render Plot

In [ ]:
%matplotlib inline
axes = address_table[[
    'Tree Count Within 100 Meters',
    'Predicted Graduation Rate',
]].plot(kind='bar')
In [ ]:
# Save file to target folder to include it in the result download
target_path = target_folder + '/c.png'
figure = axes.get_figure()
figure.savefig(target_path)
print(f'c_image_path = {target_path}')

Predicted Metrics by Address

YOUR INTERPRETATION OF THE RESULTS

{a_table : YOUR TABLE NAME ? YOUR TABLE DESCRIPTION}

{b_geotable : YOUR MAP NAME ? YOUR MAP DESCRIPTION}

{c_image : YOUR PLOT NAME ? YOUR PLOT DESCRIPTION}