ECSP




Pay Notebook Creator: Haige Cui0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0
In [1]:
# CrossCompute
address_table_path = 'Table with average monthly savings(within 0.5 Mile) and tree count(within 0.5 Mile).csv'
search_radius_in_miles = 0.5
industry_select = """
Manufacturing

Manufacturing
Wholesale/Warehouse/Distribution
Commercial
Landlord
Public Benefit Corp
Other

"""
program_select = """
ICIP

City/State
ICAP
ICIP
IDA
Relocator
Tenant

"""
# Average Monthly Savings
# set bins for range search

target_folder = '/tmp'
In [2]:
# Load inputs
import pandas as pd
address_table = pd.read_csv(address_table_path)
industry = industry_select.strip().splitlines()[0]
program = program_select.strip().splitlines()[0]
In [3]:
# Get longitude and latitude for each address
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_table = address_table.apply(get_longitude_latitude, axis=1)
address_table[:3]

# Prepare prediction dataset
prediction_table = address_table.copy()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/.virtualenvs/crosscompute/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   3123             try:
-> 3124                 return libindex.get_value_box(s, key)
   3125             except IndexError:

pandas/_libs/index.pyx in pandas._libs.index.get_value_box()

pandas/_libs/index.pyx in pandas._libs.index.get_value_box()

TypeError: 'str' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-3-d38627b05264> in <module>
      9     return row
     10 
---> 11 address_table = address_table.apply(get_longitude_latitude, axis=1)
     12 address_table[:3]
     13 

~/.virtualenvs/crosscompute/lib/python3.6/site-packages/pandas/core/frame.py in apply(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds)
   6012                          args=args,
   6013                          kwds=kwds)
-> 6014         return op.get_result()
   6015 
   6016     def applymap(self, func):

~/.virtualenvs/crosscompute/lib/python3.6/site-packages/pandas/core/apply.py in get_result(self)
    140             return self.apply_raw()
    141 
--> 142         return self.apply_standard()
    143 
    144     def apply_empty_result(self):

~/.virtualenvs/crosscompute/lib/python3.6/site-packages/pandas/core/apply.py in apply_standard(self)
    246 
    247         # compute the result using the series generator
--> 248         self.apply_series_generator()
    249 
    250         # wrap results

~/.virtualenvs/crosscompute/lib/python3.6/site-packages/pandas/core/apply.py in apply_series_generator(self)
    275             try:
    276                 for i, v in enumerate(series_gen):
--> 277                     results[i] = self.f(v)
    278                     keys.append(v.name)
    279             except Exception as e:

<ipython-input-3-d38627b05264> in get_longitude_latitude(row)
      4 
      5 def get_longitude_latitude(row):
----> 6     location = geocode(row['Address'])
      7     row['Longitude'] = location.longitude
      8     row['Latitude'] = location.latitude

~/.virtualenvs/crosscompute/lib/python3.6/site-packages/pandas/core/series.py in __getitem__(self, key)
    765         key = com._apply_if_callable(key, self)
    766         try:
--> 767             result = self.index.get_value(self, key)
    768 
    769             if not is_scalar(result):

~/.virtualenvs/crosscompute/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   3130                     raise InvalidIndexError(key)
   3131                 else:
-> 3132                     raise e1
   3133             except Exception:  # pragma: no cover
   3134                 raise e1

~/.virtualenvs/crosscompute/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   3116         try:
   3117             return self._engine.get_value(s, k,
-> 3118                                           tz=getattr(series.dtype, 'tz', None))
   3119         except KeyError as e1:
   3120             if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ('Address', 'occurred at index 0')
In [ ]:
# Prepare prediction dataset
prediction_table = address_table.copy()
# Add "Average Monthly Savings of Buildings Within 0.5 Mile Radius" column
prediction_table['Average Monthly Savings of Buildings Within 0.5 Mile Radius'] = [100, 200, 300]
# Add "Tree Count Within 0.5 Mile Radius"
prediction_table['Tree Count Within 0.5 Mile Radius'] = [10, 20, 30]
In [ ]:
# # Load your model
# from pickle import load
# model = load(open('dummy-model.pkl', 'rb'))
# model

# # Run your model to add column "Monthly Savings"
prediction_table['Monthly Savings'] = [1, 2, 3]
In [ ]:
 X = prediction_table[['Average Monthly Savings of Buildings Within 0.5 Mile Radius', 'Tree Count Within 0.5 Mile Radius']].values
## prediction_table['Monthly Savings'] = model.predict(X)
In [ ]:
target_path = target_folder + '/savings.csv'
prediction_table.to_csv(target_path, index=False)
print(f'prediction_table_path = {target_path}')
In [ ]:
target_path = target_folder + '/savings-map.csv'
map_table = prediction_table.copy()
map_table['RadiusInPixelsRange3-30'] = map_table['Monthly Savings']
In [ ]:
map_table['FillColor'] = ['y' if index in address_table else 'b' for index in map_table.index]
map_table.to_csv(target_path, index=False)
print(f'prediction_geotable_path = {target_path}')
In [ ]:
%matplotlib inline
axes = prediction_table[[
    'Monthly Savings',
]].plot(kind='bar')

# 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}')
In [ ]: