ECSP




Pay Notebook Creator: Haige Cui0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0
In [2]:
# CrossCompute
address_table_path = 'address.csv'
search_radius_in_miles = 0.5
industry_select = """
	Industry 1

	Industry 1
	Industry 2
	Industry 3
"""
program_select = """
	Program 2

	Program 1
	Program 2
"""
target_folder = '/tmp'


# 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]



# 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()


# 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]


# Load your model
In [1]:
# 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]

## 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)
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-1-6ea66c6de420> in <module>
     52 # Load your model
     53 from pickle import load
---> 54 model = load(open('dummy-model.pkl', 'rb'))
     55 model
     56 

FileNotFoundError: [Errno 2] No such file or directory: 'dummy-model.pkl'
In [3]:
target_path = target_folder + '/savings-map.csv'
map_table = prediction_table.copy()
map_table['RadiusInPixelsRange3-30'] = map_table['Monthly Savings']
map_table['FillColor'] = ['b' if index in address_table else 'y' for index in map_table.index]
map_table.to_csv(target_path, index=False)
print(f'prediction_geotable_path = {target_path}')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/.virtualenvs/crosscompute/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2655             try:
-> 2656                 return self._engine.get_loc(key)
   2657             except KeyError:

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

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: 'Monthly Savings'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-3-0b31b35890bb> in <module>
      1 target_path = target_folder + '/savings-map.csv'
      2 map_table = prediction_table.copy()
----> 3 map_table['RadiusInPixelsRange3-30'] = map_table['Monthly Savings']
      4 map_table['FillColor'] = ['b' if index in address_table else 'y' for index in map_table.index]
      5 map_table.to_csv(target_path, index=False)

~/.virtualenvs/crosscompute/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2925             if self.columns.nlevels > 1:
   2926                 return self._getitem_multilevel(key)
-> 2927             indexer = self.columns.get_loc(key)
   2928             if is_integer(indexer):
   2929                 indexer = [indexer]

~/.virtualenvs/crosscompute/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2656                 return self._engine.get_loc(key)
   2657             except KeyError:
-> 2658                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2659         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2660         if indexer.ndim > 1 or indexer.size > 1:

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

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: 'Monthly Savings'
In [4]:
%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}')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-4-3bbf13842cb8> in <module>
      1 get_ipython().run_line_magic('matplotlib', 'inline')
      2 axes = prediction_table[[
----> 3     'Monthly Savings',
      4 ]].plot(kind='bar')
      5 

~/.virtualenvs/crosscompute/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2932                 key = list(key)
   2933             indexer = self.loc._convert_to_indexer(key, axis=1,
-> 2934                                                    raise_missing=True)
   2935 
   2936         # take() does not accept boolean indexers

~/.virtualenvs/crosscompute/lib/python3.6/site-packages/pandas/core/indexing.py in _convert_to_indexer(self, obj, axis, is_setter, raise_missing)
   1352                 kwargs = {'raise_missing': True if is_setter else
   1353                           raise_missing}
-> 1354                 return self._get_listlike_indexer(obj, axis, **kwargs)[1]
   1355         else:
   1356             try:

~/.virtualenvs/crosscompute/lib/python3.6/site-packages/pandas/core/indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
   1159         self._validate_read_indexer(keyarr, indexer,
   1160                                     o._get_axis_number(axis),
-> 1161                                     raise_missing=raise_missing)
   1162         return keyarr, indexer
   1163 

~/.virtualenvs/crosscompute/lib/python3.6/site-packages/pandas/core/indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
   1244                 raise KeyError(
   1245                     u"None of [{key}] are in the [{axis}]".format(
-> 1246                         key=key, axis=self.obj._get_axis_name(axis)))
   1247 
   1248             # We (temporarily) allow for some missing keys with .loc, except in

KeyError: "None of [Index(['Monthly Savings'], dtype='object')] are in the [columns]"
In [ ]: